balihoo / phockito-unit
此软件包已被放弃,不再维护。未建议替代软件包。
简化Phockito与PHP Unit结合的库
1.0.0
2013-10-16 23:26 UTC
Requires
- php: >=5.3.0
- hafriedlander/phockito: dev-master
- mnapoli/phpdocreader: 1.*
Requires (Dev)
- satooshi/php-coveralls: dev-master
This package is not auto-updated.
Last update: 2023-04-24 07:57:55 UTC
README
PhockitoUnit
PhockitoUnit存在是为了将PHP Unit与Phockito模拟框架结合在一起,这种结合得到了全球PHP开发者的赞誉。它的功能相当简单
- 自动生成测试所需的模拟对象
- 自动生成测试所需的间谍对象
- 自动启用Hamcrest匹配
就这么多!
PhockitoUnit的实际应用
这是一个非常经典的PHP Unit测试,使用Phockito模拟依赖项
class SomeTest extends PHPUnit_Framework_TestCase { public function setUp(){ Phockito::include_hamcrest(); } function testSomeMethod(){ /** @var SomeDependency $mockDependency **/ $mockDependency = Phockito::mock('SomeDependency'); Phockito::when($mockDependency->dependentMethod(anything()))->return("value"); $instance = new ThingThatNeedsDependency($mockDependency); $this->assertEquals("value", $instance->methodThatUsesDependency()); } function testSomeMethodWhenSomeDependencyThrows(){ /** @var SomeDependency $mockDependency **/ $mockDependency = Phockito::mock('SomeDependency'); Phockito::when($mockDependency->dependentMethod(anything()))->throw(new Exception("Some error")); $instance = new ThingThatNeedsDependency($mockDependency); try{ $instance->methodThatUsesDependency()); $this->fail("Expected exception not thrown"); } catch(Exception $ex) { $this->assertEquals("Some error", $ex->getMessage()); } } }
你肯定遇到过或编写过至少与此结构类似的单元测试。PhockitoUnit通过删除一些常见的模板代码来简化这种结构,如下所示
class SomeTest extends \PhockitoUnit\PhockitoUnitTestCase { /** @var SomeDependency **/ protected $mockDependency; function testSomeMethod(){ Phockito::when($this->mockDependency->dependentMethod(anything()))->return("value"); $instance = new ThingThatNeedsDependency($mockDependency); $this->assertEquals("value", $instance->methodThatUsesDependency()); } function testSomeMethodWhenSomeDependencyThrows(){ Phockito::when($this->mockDependency->dependentMethod(anything()))->throw(new Exception("Some error")); $instance = new ThingThatNeedsDependency($mockDependency); try{ $instance->methodThatUsesDependency()); $this->fail("Expected exception not thrown"); } catch(Exception $ex) { $this->assertEquals("Some error", $ex->getMessage()); } } }
这并不是一个巨大的改变,但它确实很有帮助,消除了类名拼写错误、类重命名重构等可能性。在更复杂的场景中,当你模拟领域对象图时,它可以让你更容易地编写更多的测试。更多的测试意味着更广泛的意图覆盖。以下是一个设置图并使用间谍对象的示例
class FamilyTest extends \PhockitoUnit\PhockitoUnitTestCase { /** @var Child **/ protected $mockChild1; /** @var Child **/ protected $spyChild2; /** @var Parent **/ protected $mockParent; public function setUp(){ parent::setUp(); Phockito::when($this->mockParent->getEledestChild())->return($this->mockChild1); Phockito::when($this->mockParent->getYoungestChild())->return($this->spyChild1); } function testGetEldestChildNickName(){ Phockito::when($this->mockChild1->getNickName())->return("Oldie"); $family = new Family(array($this->mockParent)); $this->assertEquals("Oldie", $family->getElestChildNickName()); } function testGetYoungestchildFullName(){ Phockito::when($this->spyChild2->getFirstName())->return("Youngy"); Phockito::when($this->spyChild2->getLastName())->return("McYoung"); $family = new Family(array($this->mockParent)); $this->assertEquals("Youngy McYoung", $family->testGetYoungestchildFullName()); } }
你使用依赖注入(DI)框架吗?
如果你使用Phockito来模拟某些内容,那么你很可能使用依赖注入框架。如果你使用,我们建议在此基础上构建一个包来自动注册你的模拟到DI容器中。我们使用PHP-DI,因此我们构建了PhockitoUnit-PHP-DI,以使测试应用程序代码变得更加容易。