Mocks for the XP Framework.
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());
}
}
See XP RFC #0219 for a detailled introduction.