title: What happens to objects passed between dependent PHPUnit tests?
body: <p>This isn't so much a question as an attempt to save somebody else the hour I just wasted on PHPUnit.</p>
  
  <p>My problem was that my mock object, when used in a dependent test, was not returning the expected value.  It seems that PHPUnit does not preserve the same object between dependent tests, even though the syntax makes it look like it does.</p>
  
  <p>Does anyone know why PHPUnit does this?  Is this a bug?  Things like this in PHPUnit make it <strong>very</strong> frustrating to use.</p>
  
  <pre><code>&lt;?php 
  class PhpUnitTest
  extends PHPUnit_Framework_TestCase
  {
  private $mock;
  
  public function setUp()
  {
      $this-&gt;mock = $this-&gt;getMock('stdClass', array('getFoo'));
  
      $this-&gt;mock-&gt;expects( $this-&gt;any() )
          -&gt;method('getFoo')
          -&gt;will( $this-&gt;returnValue( 'foo' ) );
  }
  
  public function testMockReturnValueTwice()
  {
      $this-&gt;assertEquals('foo', $this-&gt;mock-&gt;getFoo());
      $this-&gt;assertEquals('foo', $this-&gt;mock-&gt;getFoo());
  
      return $this-&gt;mock;
  }
  
  /**
   * @depends testMockReturnValueTwice
   */
  public function testMockReturnValueInDependentTest($mock)
  {
      /* I would expect this next line to work, but it doesn't! */
      //$this-&gt;assertEquals('foo', $mock-&gt;getFoo());
  
      /* Instead, the $mock parameter is not the same object as
       * generated by the previous test! */
      $this-&gt;assertNull( $mock-&gt;getFoo() );
  }
  
  }
  </code></pre>