ormin / phpspec-ddd-project-extension
PHPSpec 辅助类,用于利用 DDD 及其相关常用模式
1.0.1
2016-02-26 14:36 UTC
Requires
- phpspec/phpspec: ^2.3
This package is not auto-updated.
Last update: 2024-10-02 18:30:17 UTC
README
扩展,帮助您对领域对象进行规格说明。
包括匹配器和令牌,用于值对象,这使得在测试中可以通过对象的值而不是内部标识符进行比较。
要安装它,请使用 Composer
$ composer require --dev ormin/phpspec-ddd-project-extension:dev-master
然后在您的项目中的 phpspec.yml
文件中添加以下内容
extensions: - Ormin\DDDProjectExtension\DDDProjectExtension
您可以使用匹配器如下(在示例中我使用了假想的 UserId 对象,但您可以使用任何想要的值对象)
function it_will_return_the_correct_user_id() {
$userId = new UserId(682);
// $domainObject->returnMeSomeCoolUserId() will return a UserId value object, which we want to match against the above. ReturnValue will fail due to non-matching identity.
$domainObject->returnMeSomeCoolUserId()->shouldReturnValue($userId);
}
您可以使用令牌如下
function it_will_call_funny_method() {
$userId = new UserId(682);
$domainObject->doSeriousStuff(new ExactValueObjectToken($userId))->shouldBeCalled();
// $domainObject->doSomeFunnyStuff() should call doSeriousStuff(UserId $userId) with an UserId value object which is created inside the method, which we want to match against the above value object. Normal ExactValueToken will fail due to non-matching identity.
$domainObject->doSomeFunnyStuff();
}
警告:不要将其用于不是显式值对象且通过标识符比较的对象!每次这样做,都会有一只小猫死去,所以请珍惜小猫并编写干净的代码 :)