I'm not sure if this is what you are looking for, so please leave a comment:
class StubTest extends PHPUnit_Framework_TestCase { public function testChainingStub() { // Creating the stub with the methods to be called $stub = $this->getMock('Zend_Db_Select', array( 'select', 'where', 'limit', 'execute' ), array(), '', FALSE); // telling the stub to return a certain result on execute $stub->expects($this->any()) ->method('execute') ->will($this->returnValue('expected result')); // telling the stub to return itself on any other calls $stub->expects($this->any()) ->method($this->anything()) ->will($this->returnValue($stub)); // testing that we can chain the stub $this->assertSame( 'expected result', $stub->select('my_table') ->where(array('my_field'=>'a_value')) ->limit(1) ->execute() ); } }
You can combine this with expectations:
class StubTest extends PHPUnit_Framework_TestCase { public function testChainingStub() { // Creating the stub with the methods to be called $stub = $this->getMock('Zend_Db_Select', array( 'select', 'where', 'limit', 'execute' ), array(), '', FALSE); // overwriting stub to return something when execute is called $stub->expects($this->exactly(1)) ->method('execute') ->will($this->returnValue('expected result')); $stub->expects($this->exactly(1)) ->method('limit') ->with($this->equalTo(1)) ->will($this->returnValue($stub)); $stub->expects($this->exactly(1)) ->method('where') ->with($this->equalTo(array('my_field'=>'a_value'))) ->will($this->returnValue($stub)); $stub->expects($this->exactly(1)) ->method('select') ->with($this->equalTo('my_table')) ->will($this->returnValue($stub)); // testing that we can chain the stub $this->assertSame( 'expected result', $stub->select('my_table') ->where(array('my_field'=>'a_value')) ->limit(1) ->execute() ); } }
Gordon
source share