Mock Objects in PHPUnit
While I am preparing the
Suppose you have the following class
The
The work on the Code Generator that generates the code for a Mock Object class is complete and I started to integrate the Mock Objects functionality into the PHPUnit framework.
PHPUNIT_2_3 CVS branch for a release of PHPUnit 2.3.0 alongside PHP 5.1.0, I recently started working on support for Mock Objects in PHPUnit.Suppose you have the following class
Foo:
class Foo {
public function bar() {
}
}
?>
You can now write a test case that checks whether or not the bar() method gets called on an object of the Foo class:
require_once 'PHPUnit2/Framework/TestCase.php';
class FooTest extends PHPUnit2_Framework_TestCase {
public function testBarGetsCalled() {
$mock = $this->getMockObject('Foo');
$mock->__expectAtLeastOnce('bar');
$mock->bar();
}
}
?>
The test above will succeed when the bar() method gets called at least once on the $mock object inside the scope of the testBarGetsCalled() method.The
getMockObject('Foo') call generates a class MockFoo (the Mock Object class for Foo) that extends from Foo and the signatures of all public methods are copied from Foo to MockFoo. In addition, a couple of MockObjects API methods are added to the MockFoo class:__expectArguments($method, $arguments, $message = "")__expectArgumentsAt($method, $arguments, $timing, $message = "")__expectCallCount($method, $count, $message = "")__expectMaximumCallCount($method, $count, $message = "")__expectMinimumCallCount($method, $count, $message = "")__expectNever($method, $message = "")__expectOnce($method, $arguments = FALSE, $message = "")__expectAtLeastOnce($method, $arguments = FALSE, $message = "")__getCallCount($method)__setReturn($method, $value, $arguments = FALSE)__setReturnAt($method, $value, $timing, $arguments = FALSE)
The work on the Code Generator that generates the code for a Mock Object class is complete and I started to integrate the Mock Objects functionality into the PHPUnit framework.
06/07/2005 at 01:29 Permalink
yours, Marcus
Reply
13/07/2005 at 00:38 Permalink
maybe you could do a post explaining why 5.1?
Reply
13/07/2005 at 16:59 Permalink
In the previous versions of PHPUnit2 I used a "backport" of PHP 5.1's AppendIterator written in PHP. This is no longer necessary with PHP 5.1.
I also wanted to use other feature additions that are in PHP 5.1, like the Array typehint and the new exception classes of the SPL.
Reply
25/07/2005 at 22:52 Permalink
Reply
26/07/2005 at 06:57 Permalink
Reply
24/09/2007 at 16:13 Permalink
Kind Regards
Keith
Reply
25/10/2007 at 18:37 Permalink
Function '__setReturn' does not exist or is not acce
ssable! (oxBase)
And I can't say what version of phpUnit I am using, as for current version it displays:
PHPUnit @package_version@ by Sebastian Bergmann.
Greetings from Lithuania
Tomas Liubinas
Reply
25/10/2007 at 19:06 Permalink
Reply
08/09/2008 at 15:04 Permalink
Thanks
Reply