Mocks for the XP Framework

v8.0.1 2022-02-27 10:42 UTC

This package is auto-updated.

Last update: 2024-03-27 15:16:35 UTC


README

Build status on GitHub XP Framework Module BSD Licence Requires PHP 7.0+ Supports PHP 8.0+ Latest Stable Version

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.