title: PHPUnit Mock that returns the same array every time
body: <p>I'm building a session management class and associated unit test.  In order to separate the class from the global state of $_SESSION I'm using an extremely simple class to manage the binding between the class and the session data.  </p>
  
  <p>The entire source of the Binding class is as follows: </p>
  
  <pre><code>class Binding implements iface\Binding
  {
      public function &amp;getNamespace ($namespace)
      {
          return $_SESSION [$namespace];
      }
  }
  </code></pre>
  
  <p>In the consuming Session class I have the following: </p>
  
  <pre><code>protected function initStorage ()
  {
      // Check that storage hasn't already been bound to the session
      if ($this -&gt; storage === NULL)
      {
          // Attempt to start the session if it hasn't already been started
          if (($this -&gt; sessionId () !== '')
          || ((!$this -&gt; headersSent ())
          &amp;&amp; ($this -&gt; startSession ())))
          {
              // Bind the storage to the session
              $this -&gt; storage    =&amp; $this -&gt; binding -&gt; getNamespace ($this -&gt; namespace);
              // Make sure the session is in a usable state
              if (!$this -&gt; hasData ())
              {
                  $this -&gt; reset ();
              }
          }
          else
          {
              // We couldn't start the session
              throw new \RuntimeException (__METHOD__ . ': Unable to initiate session storage at this time');
          }
      }
  
      return $this;
  }
  </code></pre>
  
  <p>sessionId, headersSent and startSession are simple one-line functions that serve as "test seams", which I can easily replace with mocks in PHPUnit.  </p>
  
  <p>I realised whilst writing the test that I could do more with a mock binding class than just detach the session from the class, I could also use it as a way of observing a non-public property of the class without having to actually expose any internal state and thus making the class fragile.  As the class operates on a reference to an array rather than directly on an array I can observe the array being referenced.  </p>
  
  <p>I was hoping to do this with PHPUnit's mocking API, but I can't figure out how to do it.  </p>
  
  <p>I know I can create a mock that returns an array like this: </p>
  
  <pre><code>$mock = $this -&gt; getMock ('iface\Binding');
  
  $mock -&gt; expects ($this -&gt; any ())
        -&gt; method ('getNamespace')
        -&gt; will ($this -&gt; returnValue (array ()));
  </code></pre>
  
  <p>It's not useful for observing the state changes though, as it returns a different array every time.  What I need is a mock that returns a reference to the same array every time.  </p>
  
  <p>In the end I wrote a class to take the place of the real Binding class and use that instead:  </p>
  
  <pre><code>class BindingMock implements iface\Binding
  {
      protected $storage = array ();
  
      public function &amp;getNamespace ($namespace)
      {
          return $this -&gt; storage [$namespace];
      }
  }
  </code></pre>
  
  <p>Using this class lets me examine the contents of $storage before and after calling something in the Session API because I can look at what's in the storage array without having to expose non-public state in the Session class.  Here's an example test that uses the technique: </p>
  
  <pre><code>public function testCreateItem ()
  {
      $storage    =&amp; $this -&gt; binding -&gt; getNamespace ('unittest');
      $this -&gt; assertEmpty ($storage);
      $this -&gt; object -&gt;  createItem ('This is a test', 'test');
      $this -&gt; assertNotEmpty ($storage);
      $this -&gt; assertEquals ('This is a test', $storage ['test']);
  }
  </code></pre>
  
  <p>I'd rather be able to generate the substitute class using PHPUnit, though, having the additional class just for the unit test seems like the wrong way to do it unless there's no way of achieving the same thing in PHPUnit.  </p>