summaryrefslogtreecommitdiff
path: root/tests/Unit/Middleware
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-11-14 02:17:27 +0100
committermsquare <msquare@notrademark.de>2018-12-02 12:53:31 +0100
commitc9d7e88cc70b6ad124c28be479a6e64c4b174ea6 (patch)
treeee4a37c27b05c1ef4ccc6a01cc088b510f5084ef /tests/Unit/Middleware
parent55beca95cd8d6ecfe610daea304c8374a10485af (diff)
Implemented controller permissions
Diffstat (limited to 'tests/Unit/Middleware')
-rw-r--r--tests/Unit/Middleware/CallableHandlerTest.php8
-rw-r--r--tests/Unit/Middleware/RequestHandlerTest.php77
-rw-r--r--tests/Unit/Middleware/Stub/ControllerImplementation.php24
3 files changed, 103 insertions, 6 deletions
diff --git a/tests/Unit/Middleware/CallableHandlerTest.php b/tests/Unit/Middleware/CallableHandlerTest.php
index 29424480..a0dbfce8 100644
--- a/tests/Unit/Middleware/CallableHandlerTest.php
+++ b/tests/Unit/Middleware/CallableHandlerTest.php
@@ -11,7 +11,6 @@ use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
-use ReflectionClass as Reflection;
use stdClass;
class CallableHandlerTest extends TestCase
@@ -28,17 +27,14 @@ class CallableHandlerTest extends TestCase
/**
* @dataProvider provideCallable
* @covers \Engelsystem\Middleware\CallableHandler::__construct
+ * @covers \Engelsystem\Middleware\CallableHandler::getCallable
* @param callable $callable
*/
public function testInit($callable)
{
$handler = new CallableHandler($callable);
- $reflection = new Reflection(get_class($handler));
- $property = $reflection->getProperty('callable');
- $property->setAccessible(true);
-
- $this->assertEquals($callable, $property->getValue($handler));
+ $this->assertEquals($callable, $handler->getCallable());
}
/**
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
diff --git a/tests/Unit/Middleware/Stub/ControllerImplementation.php b/tests/Unit/Middleware/Stub/ControllerImplementation.php
new file mode 100644
index 00000000..939dde5b
--- /dev/null
+++ b/tests/Unit/Middleware/Stub/ControllerImplementation.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Middleware\Stub;
+
+use Engelsystem\Controllers\BaseController;
+
+class ControllerImplementation extends BaseController
+{
+ /**
+ * @param array $permissions
+ */
+ public function setPermissions(array $permissions)
+ {
+ $this->permissions = $permissions;
+ }
+
+ /**
+ * @return string
+ */
+ public function actionStub()
+ {
+ return '';
+ }
+}