summaryrefslogtreecommitdiff
path: root/tests/Unit/Middleware/RequestHandlerTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Unit/Middleware/RequestHandlerTest.php')
-rw-r--r--tests/Unit/Middleware/RequestHandlerTest.php77
1 files changed, 77 insertions, 0 deletions
diff --git a/tests/Unit/Middleware/RequestHandlerTest.php b/tests/Unit/Middleware/RequestHandlerTest.php
index cb5fc4a6..7ff803d0 100644
--- a/tests/Unit/Middleware/RequestHandlerTest.php
+++ b/tests/Unit/Middleware/RequestHandlerTest.php
@@ -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;
@@ -132,6 +136,79 @@ class RequestHandlerTest extends TestCase
}
/**
+ * @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
*/
protected function getMocks(): array