https://github.com/xp-framework/mocks
Mocks for the XP Framework
https://github.com/xp-framework/mocks
mock php unittest xp-framework
Last synced: 2 months ago
JSON representation
Mocks for the XP Framework
- Host: GitHub
- URL: https://github.com/xp-framework/mocks
- Owner: xp-framework
- Created: 2015-07-31T15:35:44.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2022-02-27T10:48:01.000Z (about 3 years ago)
- Last Synced: 2025-02-21T16:48:07.937Z (3 months ago)
- Topics: mock, php, unittest, xp-framework
- Language: PHP
- Size: 82 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog.md
Awesome Lists containing this project
README
Mocks
=====[](https://github.com/xp-framework/mocks/actions)
[](https://github.com/xp-framework/core)
[](https://github.com/xp-framework/core/blob/master/LICENCE.md)
[](http://php.net/)
[](http://php.net/)
[](https://packagist.org/packages/xp-framework/mocks)Mocks for the XP Framework.
Example
-------
Here's an example implementation:```php
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:
```php
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](https://github.com/xp-framework/rfc/issues/219) for a detailled introduction.