|
|
|
@ -3,7 +3,11 @@
|
|
|
|
|
namespace Engelsystem\Test\Unit\Middleware;
|
|
|
|
|
|
|
|
|
|
use Engelsystem\Application;
|
|
|
|
|
use Engelsystem\Helpers\Authenticator;
|
|
|
|
|
use Engelsystem\Http\Exceptions\HttpForbidden;
|
|
|
|
|
use Engelsystem\Middleware\CallableHandler;
|
|
|
|
|
use Engelsystem\Middleware\RequestHandler;
|
|
|
|
|
use Engelsystem\Test\Unit\Middleware\Stub\ControllerImplementation;
|
|
|
|
|
use InvalidArgumentException;
|
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
@ -131,6 +135,79 @@ class RequestHandlerTest extends TestCase
|
|
|
|
|
$this->assertEquals($return, $response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \Engelsystem\Middleware\RequestHandler::process
|
|
|
|
|
* @covers \Engelsystem\Middleware\RequestHandler::checkPermissions
|
|
|
|
|
*/
|
|
|
|
|
public function testCheckPermissions()
|
|
|
|
|
{
|
|
|
|
|
/** @var Application|MockObject $container */
|
|
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
|
|
|
|
/** @var RequestHandlerInterface|MockObject $handler */
|
|
|
|
|
/** @var ResponseInterface|MockObject $response */
|
|
|
|
|
list($container, $request, $handler, $response) = $this->getMocks();
|
|
|
|
|
|
|
|
|
|
/** @var Authenticator|MockObject $auth */
|
|
|
|
|
$auth = $this->createMock(Authenticator::class);
|
|
|
|
|
|
|
|
|
|
$class = new ControllerImplementation();
|
|
|
|
|
/** @var CallableHandler|MockObject $callable */
|
|
|
|
|
$callable = $this->getMockBuilder(CallableHandler::class)
|
|
|
|
|
->setConstructorArgs([[$class, 'actionStub']])
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$callable->expects($this->exactly(2))
|
|
|
|
|
->method('getCallable')
|
|
|
|
|
->willReturn([$class, 'actionStub']);
|
|
|
|
|
|
|
|
|
|
$callable->expects($this->exactly(1))
|
|
|
|
|
->method('process')
|
|
|
|
|
->with($request, $handler)
|
|
|
|
|
->willReturn($response);
|
|
|
|
|
|
|
|
|
|
$request->expects($this->exactly(2))
|
|
|
|
|
->method('getAttribute')
|
|
|
|
|
->with('route-request-handler')
|
|
|
|
|
->willReturn($callable);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @var RequestHandler|MockObject $middleware */
|
|
|
|
|
$middleware = $this->getMockBuilder(RequestHandler::class)
|
|
|
|
|
->setConstructorArgs([$container])
|
|
|
|
|
->setMethods(['resolveRequestHandler'])
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$middleware->expects($this->exactly(2))
|
|
|
|
|
->method('resolveRequestHandler')
|
|
|
|
|
->with($callable)
|
|
|
|
|
->willReturn($callable);
|
|
|
|
|
|
|
|
|
|
$container->expects($this->exactly(2))
|
|
|
|
|
->method('get')
|
|
|
|
|
->with('auth')
|
|
|
|
|
->willReturn($auth);
|
|
|
|
|
|
|
|
|
|
$hasPermissions = [];
|
|
|
|
|
$auth->expects($this->atLeastOnce())
|
|
|
|
|
->method('can')
|
|
|
|
|
->willReturnCallback(function ($permission) use (&$hasPermissions) {
|
|
|
|
|
return in_array($permission, $hasPermissions);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$hasPermissions = ['foo', 'test', 'user'];
|
|
|
|
|
$class->setPermissions([
|
|
|
|
|
'foo',
|
|
|
|
|
'loremIpsumAction' => ['dolor', 'sit'],
|
|
|
|
|
'actionStub' => ['test'],
|
|
|
|
|
'user',
|
|
|
|
|
]);
|
|
|
|
|
$middleware->process($request, $handler);
|
|
|
|
|
|
|
|
|
|
$class->setPermissions(array_merge(['not.existing.permission'], $hasPermissions));
|
|
|
|
|
$this->expectException(HttpForbidden::class);
|
|
|
|
|
$middleware->process($request, $handler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|