This isn't so much a question as an attempt to save somebody else the hour I just wasted on PHPUnit.
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.
Does anyone know why PHPUnit does this? Is this a bug? Things like this in PHPUnit make it very frustrating to use.
<?php
class PhpUnitTest
extends PHPUnit_Framework_TestCase
{
private $mock;
public function setUp()
{
$this->mock = $this->getMock('stdClass', array('getFoo'));
$this->mock->expects( $this->any() )
->method('getFoo')
->will( $this->returnValue( 'foo' ) );
}
public function testMockReturnValueTwice()
{
$this->assertEquals('foo', $this->mock->getFoo());
$this->assertEquals('foo', $this->mock->getFoo());
return $this->mock;
}
/**
* @depends testMockReturnValueTwice
*/
public function testMockReturnValueInDependentTest($mock)
{
/* I would expect this next line to work, but it doesn't! */
//$this->assertEquals('foo', $mock->getFoo());
/* Instead, the $mock parameter is not the same object as
* generated by the previous test! */
$this->assertNull( $mock->getFoo() );
}
}