xp-framework / mocks
Mocks for the XP Framework
Installs: 10 885
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
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
Mocks for the XP Framework.
Example
Here's an example implementation:
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:) } }
This is how we can mock the Context
interface:
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()); } }
Further reading
See XP RFC #0219 for a detailled introduction.