Shmock(SHorthadforMOCKig)是PHPUit创建mocks的平稳替代,使用EasyMock的mock/replay概念,但是又使用mockig定义的闭包范围。
示例代码:
<?php amespace Foo; /** * Here's a class we're tryig to test yay. */ class Foo { private $foo = 0; private $icremetig_service = ull; public fuctio __costruct(Icremetig_Service $icremetig_service) { $this->icremetig_service = $icremetig_service; } public fuctio ext_foo() { $this->foo = $this->icremetig_service->icremet($this->foo); retur $this->foo; } } /** * Our test case rus the same test case twice - oce with the origial PHPUit mockig * sytax ad a secod time with Shmock sytax. */ class Foo_Test exteds PHPUit_Framework_TestCase { use \Shmock\Shmockers; // This eables the use of the Shmock helper methods (replicated below) public fuctio test_phpuit_origial_mockig_sytax() { // this is the origial PHPUit mock sytax $icremetig_service_mock = $this->getMock('\Foo\Icremetig_Service', array('icremet')); $icremetig_service_mock->expects($this->oce()) ->method('icremet') ->with($this->equalTo(0)) ->will($this->returValue(1)); $foo = ew Foo($icremetig_service_mock); $this->assertEquals(1, $foo->ext_foo(0)); } /** * Create a shmock represetatio for $class_ame ad cofigure expected * mock iteractio with $cof_closure * @retur Shmock A fully cofigured mock object * @ote You do ot eed this protected method if you use the Shmockers trait, show above */ protected fuctio shmock($class_ame, $cof_closure) { retur \Shmock\Shmock::create_class($this, $class_ame, $cof_closure); } public fuctio test_shmock_sytax() { // here's shmock. Neat huh? $icremetig_service_mock = $this->shmock('\Foo\Icremetig_Service', fuctio($shmock) { $shmock->icremet(0)->retur_value(1); }); $foo = ew Foo($icremetig_service_mock); $this->assertEquals(1, $foo->ext_foo(0)); } }










评论