xp-framework / mocks
XP 框架的模拟
v8.0.1
2022-02-27 10:42 UTC
Requires
- php: >=7.0.0
- xp-framework/collections: ^10.0 | ^9.0 | ^8.0 | ^7.0
- xp-framework/core: ^11.0 | ^10.0 | ^9.0 | ^8.0 | ^7.0
- xp-framework/unittest: ^11.0 | ^10.0 | ^9.0 | ^8.0 |^7.0
README
XP 框架的模拟。
示例
以下是一个实现示例
use lang\IllegalAccessException; interface Context { public function hasPermission($name); } class UserService { private $context; public function __construct(Context $context) { $this->context= $context; } public function allUsers() { if (!$this->context->hasPermission('rt=all,rn=users')) { throw new IllegalAccessException('Permission denied!'); } return []; // TODO: Actually do something:) } }
这是如何模拟 Context
接口的
use unittest\TestCase; class UserServiceTest extends TestCase { #[@test] public function allUsers_works_when_hasPermission_returns_true() { $mocks= new MockRepository(); $context= $mocks->createMock('Context'); $context->hasPermission('rt=all,rn=users')->returns(true); $mocks->replayAll(); $fixture= new UserService($context); $this->assertEquals([], $fixture->allUsers()); } }
进一步阅读
详见 XP RFC #0219 以获取详细的介绍。