summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-09-04 18:35:13 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-09-04 21:13:28 +0200
commitb52444af8a289e089d1239657cdf6ff06b21b29d (patch)
treec2754b3049a50ad6743841a609ab0574f241720d /tests
parentb320fc779063ee80b8f0ba505cb323287ccccbf5 (diff)
parenta1bc763a16ee8be109de5c9053fbc5eded53824e (diff)
Merge remote-tracking branch 'MyIgel/routing'
Diffstat (limited to 'tests')
-rw-r--r--tests/Unit/HelpersTest.php7
-rw-r--r--tests/Unit/Http/LegacyUrlGeneratorTest.php (renamed from tests/Unit/Routing/LegacyUrlGeneratorTest.php)12
-rw-r--r--tests/Unit/Http/UrlGeneratorServiceProviderTest.php29
-rw-r--r--tests/Unit/Http/UrlGeneratorTest.php (renamed from tests/Unit/Routing/UrlGeneratorTest.php)16
-rw-r--r--tests/Unit/Middleware/CallableHandlerTest.php141
-rw-r--r--tests/Unit/Middleware/DispatcherTest.php58
-rw-r--r--tests/Unit/Middleware/LegacyMiddlewareTest.php8
-rw-r--r--tests/Unit/Middleware/NotFoundResponseTest.php39
-rw-r--r--tests/Unit/Middleware/RequestHandlerServiceProviderTest.php36
-rw-r--r--tests/Unit/Middleware/RequestHandlerTest.php89
-rw-r--r--tests/Unit/Middleware/ResolvesMiddlewareTraitTest.php67
-rw-r--r--tests/Unit/Middleware/RouteDispatcherServiceProviderTest.php65
-rw-r--r--tests/Unit/Middleware/RouteDispatcherTest.php148
-rw-r--r--tests/Unit/Middleware/Stub/HasStaticMethod.php8
-rw-r--r--tests/Unit/Middleware/Stub/ResolvesMiddlewareTraitImplementation.php35
-rw-r--r--tests/Unit/Routing/RoutingServiceProviderTest.php64
16 files changed, 649 insertions, 173 deletions
diff --git a/tests/Unit/HelpersTest.php b/tests/Unit/HelpersTest.php
index cd001df9..fb9e6f00 100644
--- a/tests/Unit/HelpersTest.php
+++ b/tests/Unit/HelpersTest.php
@@ -8,7 +8,8 @@ use Engelsystem\Container\Container;
use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
use Engelsystem\Renderer\Renderer;
-use Engelsystem\Routing\UrlGeneratorInterface;
+use Engelsystem\Http\UrlGenerator;
+use Engelsystem\Http\UrlGeneratorInterface;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
use Symfony\Component\HttpFoundation\Session\Session;
@@ -201,11 +202,11 @@ class HelpersTest extends TestCase
{
$urlGeneratorMock = $this->getMockForAbstractClass(UrlGeneratorInterface::class);
- $this->getAppMock('routing.urlGenerator', $urlGeneratorMock);
+ $this->getAppMock('http.urlGenerator', $urlGeneratorMock);
$this->assertEquals($urlGeneratorMock, url());
$urlGeneratorMock->expects($this->once())
- ->method('linkTo')
+ ->method('to')
->with('foo/bar', ['param' => 'value'])
->willReturn('http://lorem.ipsum/foo/bar?param=value');
diff --git a/tests/Unit/Routing/LegacyUrlGeneratorTest.php b/tests/Unit/Http/LegacyUrlGeneratorTest.php
index 3d42afbd..2c087f8f 100644
--- a/tests/Unit/Routing/LegacyUrlGeneratorTest.php
+++ b/tests/Unit/Http/LegacyUrlGeneratorTest.php
@@ -1,12 +1,12 @@
<?php
-namespace Engelsystem\Test\Unit\Routing;
+namespace Engelsystem\Test\Unit\Http;
use Engelsystem\Application;
use Engelsystem\Container\Container;
use Engelsystem\Http\Request;
-use Engelsystem\Routing\LegacyUrlGenerator;
-use Engelsystem\Routing\UrlGeneratorInterface;
+use Engelsystem\Http\LegacyUrlGenerator;
+use Engelsystem\Http\UrlGeneratorInterface;
use PHPUnit\Framework\TestCase;
class LegacyUrlGeneratorTest extends TestCase
@@ -23,14 +23,14 @@ class LegacyUrlGeneratorTest extends TestCase
/**
* @dataProvider provideLinksTo
- * @covers \Engelsystem\Routing\LegacyUrlGenerator::linkTo
+ * @covers \Engelsystem\Http\LegacyUrlGenerator::to
*
* @param string $urlToPath
* @param string $willReturn
* @param string[] $arguments
* @param string $expectedUrl
*/
- public function testLinkTo($urlToPath, $willReturn, $arguments, $expectedUrl)
+ public function testTo($urlToPath, $willReturn, $arguments, $expectedUrl)
{
$app = new Container();
Application::setInstance($app);
@@ -48,7 +48,7 @@ class LegacyUrlGeneratorTest extends TestCase
$urlGenerator = new LegacyUrlGenerator();
$this->assertInstanceOf(UrlGeneratorInterface::class, $urlGenerator);
- $url = $urlGenerator->linkTo($urlToPath, $arguments);
+ $url = $urlGenerator->to($urlToPath, $arguments);
$this->assertEquals($expectedUrl, $url);
}
}
diff --git a/tests/Unit/Http/UrlGeneratorServiceProviderTest.php b/tests/Unit/Http/UrlGeneratorServiceProviderTest.php
new file mode 100644
index 00000000..874268b0
--- /dev/null
+++ b/tests/Unit/Http/UrlGeneratorServiceProviderTest.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Http;
+
+use Engelsystem\Http\UrlGenerator;
+use Engelsystem\Http\UrlGeneratorServiceProvider;
+use Engelsystem\Test\Unit\ServiceProviderTest;
+use PHPUnit_Framework_MockObject_MockObject;
+
+class UrlGeneratorServiceProviderTest extends ServiceProviderTest
+{
+ /**
+ * @covers \Engelsystem\Http\UrlGeneratorServiceProvider::register()
+ */
+ public function testRegister()
+ {
+ /** @var PHPUnit_Framework_MockObject_MockObject|UrlGenerator $urlGenerator */
+ $urlGenerator = $this->getMockBuilder(UrlGenerator::class)
+ ->getMock();
+
+ $app = $this->getApp();
+
+ $this->setExpects($app, 'make', [UrlGenerator::class], $urlGenerator);
+ $this->setExpects($app, 'instance', ['http.urlGenerator', $urlGenerator]);
+
+ $serviceProvider = new UrlGeneratorServiceProvider($app);
+ $serviceProvider->register();
+ }
+}
diff --git a/tests/Unit/Routing/UrlGeneratorTest.php b/tests/Unit/Http/UrlGeneratorTest.php
index e128bfe7..fa2ec36e 100644
--- a/tests/Unit/Routing/UrlGeneratorTest.php
+++ b/tests/Unit/Http/UrlGeneratorTest.php
@@ -1,12 +1,11 @@
<?php
-namespace Engelsystem\Test\Unit\Routing;
+namespace Engelsystem\Test\Unit\Http;
use Engelsystem\Application;
use Engelsystem\Container\Container;
use Engelsystem\Http\Request;
-use Engelsystem\Routing\UrlGenerator;
-use Engelsystem\Routing\UrlGeneratorInterface;
+use Engelsystem\Http\UrlGenerator;
use PHPUnit\Framework\TestCase;
class UrlGeneratorTest extends TestCase
@@ -14,7 +13,6 @@ class UrlGeneratorTest extends TestCase
public function provideLinksTo()
{
return [
- ['/', '/', 'http://foo.bar/', [], 'http://foo.bar/'],
['/foo/path', '/foo/path', 'http://foo.bar/foo/path', [], 'http://foo.bar/foo/path'],
['foo', '/foo', 'https://foo.bar/foo', [], 'https://foo.bar/foo'],
['foo', '/foo', 'http://f.b/foo', ['test' => 'abc', 'bla' => 'foo'], 'http://f.b/foo?test=abc&bla=foo'],
@@ -23,7 +21,7 @@ class UrlGeneratorTest extends TestCase
/**
* @dataProvider provideLinksTo
- * @covers \Engelsystem\Routing\UrlGenerator::linkTo
+ * @covers \Engelsystem\Http\UrlGenerator::to
*
* @param string $path
* @param string $willReturn
@@ -31,9 +29,10 @@ class UrlGeneratorTest extends TestCase
* @param string[] $arguments
* @param string $expectedUrl
*/
- public function testLinkTo($urlToPath, $path, $willReturn, $arguments, $expectedUrl)
+ public function testTo($urlToPath, $path, $willReturn, $arguments, $expectedUrl)
{
$app = new Container();
+ $urlGenerator = new UrlGenerator();
Application::setInstance($app);
$request = $this->getMockBuilder(Request::class)
@@ -46,10 +45,7 @@ class UrlGeneratorTest extends TestCase
$app->instance('request', $request);
- $urlGenerator = new UrlGenerator();
- $this->assertInstanceOf(UrlGeneratorInterface::class, $urlGenerator);
-
- $url = $urlGenerator->linkTo($urlToPath, $arguments);
+ $url = $urlGenerator->to($urlToPath, $arguments);
$this->assertEquals($expectedUrl, $url);
}
}
diff --git a/tests/Unit/Middleware/CallableHandlerTest.php b/tests/Unit/Middleware/CallableHandlerTest.php
new file mode 100644
index 00000000..6e6dab58
--- /dev/null
+++ b/tests/Unit/Middleware/CallableHandlerTest.php
@@ -0,0 +1,141 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Middleware;
+
+use Engelsystem\Container\Container;
+use Engelsystem\Http\Response;
+use Engelsystem\Middleware\CallableHandler;
+use Engelsystem\Test\Unit\Middleware\Stub\HasStaticMethod;
+use PHPUnit\Framework\MockObject\MockObject;
+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
+{
+ public function provideCallable()
+ {
+ return [
+ [function () { }],
+ [[$this, 'provideCallable']],
+ [[HasStaticMethod::class, 'foo']],
+ ];
+ }
+
+ /**
+ * @dataProvider provideCallable
+ * @covers \Engelsystem\Middleware\CallableHandler::__construct
+ * @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));
+ }
+
+ /**
+ * @covers \Engelsystem\Middleware\CallableHandler::process
+ */
+ public function testProcess()
+ {
+ /** @var ServerRequestInterface|MockObject $request */
+ /** @var ResponseInterface|MockObject $response */
+ /** @var callable|MockObject $callable */
+ /** @var RequestHandlerInterface|MockObject $handler */
+ list($request, $response, $callable, $handler) = $this->getMocks();
+
+ $callable->expects($this->once())
+ ->method('__invoke')
+ ->with($request, $handler)
+ ->willReturn($response);
+
+ $middleware = new CallableHandler($callable);
+ $middleware->process($request, $handler);
+ }
+
+ /**
+ * @covers \Engelsystem\Middleware\CallableHandler::handle
+ */
+ public function testHandler()
+ {
+ /** @var ServerRequestInterface|MockObject $request */
+ /** @var ResponseInterface|MockObject $response */
+ /** @var callable|MockObject $callable */
+ list($request, $response, $callable) = $this->getMocks();
+
+ $callable->expects($this->once())
+ ->method('__invoke')
+ ->with($request)
+ ->willReturn($response);
+
+ $middleware = new CallableHandler($callable);
+ $middleware->handle($request);
+ }
+
+ /**
+ * @covers \Engelsystem\Middleware\CallableHandler::execute
+ */
+ public function testExecute()
+ {
+ /** @var ServerRequestInterface|MockObject $request */
+ /** @var Response|MockObject $response */
+ /** @var callable|MockObject $callable */
+ list($request, $response, $callable) = $this->getMocks();
+ /** @var Container|MockObject $container */
+ $container = $this->createMock(Container::class);
+
+ $callable->expects($this->exactly(3))
+ ->method('__invoke')
+ ->with($request)
+ ->willReturnOnConsecutiveCalls($response, 'Lorem ipsum?', 'I\'m not an exception!');
+
+ $container->expects($this->once())
+ ->method('get')
+ ->with('response')
+ ->willReturn($response);
+
+ $response->expects($this->once())
+ ->method('withContent')
+ ->with('Lorem ipsum?')
+ ->willReturn($response);
+
+ $middleware = new CallableHandler($callable, $container);
+ $return = $middleware->handle($request);
+ $this->assertInstanceOf(ResponseInterface::class, $return);
+ $this->assertEquals($response, $return);
+
+ $return = $middleware->handle($request);
+ $this->assertInstanceOf(ResponseInterface::class, $return);
+ $this->assertEquals($response, $return);
+
+ $middleware = new CallableHandler($callable);
+ $this->expectException(\InvalidArgumentException::class);
+ $middleware->handle($request);
+ }
+
+ /**
+ * @return array
+ */
+ protected function getMocks(): array
+ {
+ /** @var ServerRequestInterface|MockObject $request */
+ $request = $this->getMockForAbstractClass(ServerRequestInterface::class);
+ /** @var RequestHandlerInterface|MockObject $handler */
+ $handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
+ /** @var Response|MockObject $response */
+ $response = $this->createMock(Response::class);
+ /** @var callable|MockObject $callable */
+ $callable = $this->getMockBuilder(stdClass::class)
+ ->setMethods(['__invoke'])
+ ->getMock();
+ return array($request, $response, $callable, $handler);
+ }
+}
diff --git a/tests/Unit/Middleware/DispatcherTest.php b/tests/Unit/Middleware/DispatcherTest.php
index c01c5029..4e1c51a7 100644
--- a/tests/Unit/Middleware/DispatcherTest.php
+++ b/tests/Unit/Middleware/DispatcherTest.php
@@ -5,7 +5,6 @@ namespace Engelsystem\Test\Unit\Middleware;
use Engelsystem\Application;
use Engelsystem\Middleware\Dispatcher;
use Engelsystem\Test\Unit\Middleware\Stub\NotARealMiddleware;
-use Engelsystem\Test\Unit\Middleware\Stub\ReturnResponseMiddleware;
use InvalidArgumentException;
use LogicException;
use PHPUnit\Framework\TestCase;
@@ -158,14 +157,14 @@ class DispatcherTest extends TestCase
/** @var Dispatcher|MockObject $dispatcher */
$dispatcher = $this->getMockBuilder(Dispatcher::class)
- ->setConstructorArgs([[MiddlewareInterface::class]])
+ ->setConstructorArgs([[MiddlewareInterface::class, MiddlewareInterface::class]])
->setMethods(['resolveMiddleware'])
->getMock();
- $dispatcher->expects($this->once())
+ $dispatcher->expects($this->exactly(2))
->method('resolveMiddleware')
->with(MiddlewareInterface::class)
- ->willReturn($middleware);
+ ->willReturnOnConsecutiveCalls($middleware, null);
$middleware->expects($this->once())
->method('process')
@@ -174,57 +173,26 @@ class DispatcherTest extends TestCase
$return = $dispatcher->handle($request);
$this->assertEquals($response, $return);
+
+ $this->expectException(InvalidArgumentException::class);
+ $dispatcher->handle($request);
}
/**
- * @covers \Engelsystem\Middleware\Dispatcher::resolveMiddleware
* @covers \Engelsystem\Middleware\Dispatcher::setContainer
*/
- public function testResolveMiddleware()
+ public function testSetContainer()
{
/** @var Application|MockObject $container */
$container = $this->createMock(Application::class);
- /** @var ServerRequestInterface|MockObject $request */
- $request = $this->createMock(ServerRequestInterface::class);
- /** @var ResponseInterface|MockObject $response */
- $response = $this->createMock(ResponseInterface::class);
-
- $returnResponseMiddleware = new ReturnResponseMiddleware($response);
-
- $container->expects($this->exactly(2))
- ->method('has')
- ->withConsecutive([ReturnResponseMiddleware::class], ['middleware'])
- ->willReturnOnConsecutiveCalls(false, true);
-
- $container->expects($this->once())
- ->method('make')
- ->with(ReturnResponseMiddleware::class)
- ->willReturn($returnResponseMiddleware);
-
- $container->expects($this->once())
- ->method('get')
- ->with('middleware')
- ->willReturn($returnResponseMiddleware);
-
- $dispatcher = new Dispatcher([ReturnResponseMiddleware::class]);
- $dispatcher->setContainer($container);
- $dispatcher->handle($request);
- $dispatcher = new Dispatcher(['middleware'], $container);
- $dispatcher->handle($request);
- }
+ $middleware = new Dispatcher();
+ $middleware->setContainer($container);
- /**
- * @covers \Engelsystem\Middleware\Dispatcher::resolveMiddleware
- */
- public function testResolveMiddlewareNoContainer()
- {
- /** @var ServerRequestInterface|MockObject $request */
- $request = $this->createMock(ServerRequestInterface::class);
-
- $this->expectException(InvalidArgumentException::class);
+ $reflection = new Reflection(get_class($middleware));
+ $property = $reflection->getProperty('container');
+ $property->setAccessible(true);
- $dispatcher = new Dispatcher([ReturnResponseMiddleware::class]);
- $dispatcher->handle($request);
+ $this->assertEquals($container, $property->getValue($middleware));
}
}
diff --git a/tests/Unit/Middleware/LegacyMiddlewareTest.php b/tests/Unit/Middleware/LegacyMiddlewareTest.php
index 34e60b60..ed9a5a74 100644
--- a/tests/Unit/Middleware/LegacyMiddlewareTest.php
+++ b/tests/Unit/Middleware/LegacyMiddlewareTest.php
@@ -46,10 +46,11 @@ class LegacyMiddlewareTest extends TestCase
['title2', 'content2']
);
- $middleware->expects($this->exactly(2))
+ $middleware->expects($this->exactly(3))
->method('renderPage')
->withConsecutive(
['user_worklog', 'title', 'content'],
+ ['404', 'Page not found'],
['login', 'title2', 'content2']
)
->willReturn($response);
@@ -73,11 +74,6 @@ class LegacyMiddlewareTest extends TestCase
'/'
);
- $handler->expects($this->once())
- ->method('handle')
- ->with($request)
- ->willReturn($response);
-
$middleware->process($request, $handler);
$middleware->process($request, $handler);
$middleware->process($request, $handler);
diff --git a/tests/Unit/Middleware/NotFoundResponseTest.php b/tests/Unit/Middleware/NotFoundResponseTest.php
deleted file mode 100644
index 9279e81d..00000000
--- a/tests/Unit/Middleware/NotFoundResponseTest.php
+++ /dev/null
@@ -1,39 +0,0 @@
-<?php
-
-namespace Engelsystem\Test\Unit\Middleware;
-
-use Engelsystem\Middleware\NotFoundResponse;
-use PHPUnit\Framework\TestCase;
-use PHPUnit_Framework_MockObject_MockObject as MockObject;
-use Psr\Http\Message\ResponseInterface;
-use Psr\Http\Message\ServerRequestInterface;
-use Psr\Http\Server\RequestHandlerInterface;
-
-class NotFoundResponseTest extends TestCase
-{
- /**
- * @covers \Engelsystem\Middleware\NotFoundResponse::process
- */
- public function testRegister()
- {
- /** @var NotFoundResponse|MockObject $middleware */
- $middleware = $this->getMockBuilder(NotFoundResponse::class)
- ->setMethods(['renderPage'])
- ->getMock();
- /** @var ResponseInterface|MockObject $response */
- $response = $this->getMockForAbstractClass(ResponseInterface::class);
- /** @var RequestHandlerInterface|MockObject $handler */
- $handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
- /** @var ServerRequestInterface|MockObject $request */
- $request = $this->getMockForAbstractClass(ServerRequestInterface::class);
-
- $middleware->expects($this->once())
- ->method('renderPage')
- ->willReturn($response);
-
- $handler->expects($this->never())
- ->method('handle');
-
- $middleware->process($request, $handler);
- }
-}
diff --git a/tests/Unit/Middleware/RequestHandlerServiceProviderTest.php b/tests/Unit/Middleware/RequestHandlerServiceProviderTest.php
new file mode 100644
index 00000000..281016b5
--- /dev/null
+++ b/tests/Unit/Middleware/RequestHandlerServiceProviderTest.php
@@ -0,0 +1,36 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Middleware;
+
+use Engelsystem\Middleware\RequestHandler;
+use Engelsystem\Middleware\RequestHandlerServiceProvider;
+use Engelsystem\Test\Unit\ServiceProviderTest;
+use PHPUnit\Framework\MockObject\MockObject;
+
+class RequestHandlerServiceProviderTest extends ServiceProviderTest
+{
+ /**
+ * @covers \Engelsystem\Middleware\RequestHandlerServiceProvider::register()
+ */
+ public function testRegister()
+ {
+ /** @var RequestHandler|MockObject $requestHandler */
+ $requestHandler = $this->createMock(RequestHandler::class);
+
+ $app = $this->getApp(['make', 'instance', 'bind']);
+
+ $app->expects($this->once())
+ ->method('make')
+ ->with(RequestHandler::class)
+ ->willReturn($requestHandler);
+ $app->expects($this->once())
+ ->method('instance')
+ ->with('request.handler', $requestHandler);
+ $app->expects($this->once())
+ ->method('bind')
+ ->with(RequestHandler::class, 'request.handler');
+
+ $serviceProvider = new RequestHandlerServiceProvider($app);
+ $serviceProvider->register();
+ }
+}
diff --git a/tests/Unit/Middleware/RequestHandlerTest.php b/tests/Unit/Middleware/RequestHandlerTest.php
new file mode 100644
index 00000000..896b55c3
--- /dev/null
+++ b/tests/Unit/Middleware/RequestHandlerTest.php
@@ -0,0 +1,89 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Middleware;
+
+use Engelsystem\Application;
+use Engelsystem\Middleware\RequestHandler;
+use InvalidArgumentException;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\MiddlewareInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+use ReflectionClass as Reflection;
+
+class RequestHandlerTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Middleware\RequestHandler::__construct
+ */
+ public function testInit()
+ {
+ /** @var Application|MockObject $container */
+ $container = $this->createMock(Application::class);
+
+ $handler = new RequestHandler($container);
+
+ $reflection = new Reflection(get_class($handler));
+ $property = $reflection->getProperty('container');
+ $property->setAccessible(true);
+
+ $this->assertEquals($container, $property->getValue($handler));
+ }
+
+ /**
+ * @covers \Engelsystem\Middleware\RequestHandler::process
+ */
+ public function testProcess()
+ {
+ /** @var Application|MockObject $container */
+ $container = $this->createMock(Application::class);
+ /** @var ServerRequestInterface|MockObject $request */
+ $request = $this->getMockForAbstractClass(ServerRequestInterface::class);
+ /** @var RequestHandlerInterface|MockObject $handler */
+ $handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
+ /** @var ResponseInterface|MockObject $response */
+ $response = $this->getMockForAbstractClass(ResponseInterface::class);
+
+ $middlewareInterface = $this->getMockForAbstractClass(MiddlewareInterface::class);
+ $requestHandlerInterface = $this->getMockForAbstractClass(RequestHandlerInterface::class);
+
+ $request->expects($this->exactly(3))
+ ->method('getAttribute')
+ ->with('route-request-handler')
+ ->willReturn('FooBarClass');
+
+ /** @var RequestHandler|MockObject $middleware */
+ $middleware = $this->getMockBuilder(RequestHandler::class)
+ ->setConstructorArgs([$container])
+ ->setMethods(['resolveMiddleware'])
+ ->getMock();
+ $middleware->expects($this->exactly(3))
+ ->method('resolveMiddleware')
+ ->with('FooBarClass')
+ ->willReturnOnConsecutiveCalls(
+ $middlewareInterface,
+ $requestHandlerInterface,
+ null
+ );
+
+ $middlewareInterface->expects($this->once())
+ ->method('process')
+ ->with($request, $handler)
+ ->willReturn($response);
+ $requestHandlerInterface->expects($this->once())
+ ->method('handle')
+ ->with($request)
+ ->willReturn($response);
+
+ $return = $middleware->process($request, $handler);
+ $this->assertEquals($return, $response);
+
+ $middleware->process($request, $handler);
+ $this->assertEquals($return, $response);
+
+ $this->expectException(InvalidArgumentException::class);
+ $middleware->process($request, $handler);
+ }
+}
diff --git a/tests/Unit/Middleware/ResolvesMiddlewareTraitTest.php b/tests/Unit/Middleware/ResolvesMiddlewareTraitTest.php
new file mode 100644
index 00000000..320a6d6b
--- /dev/null
+++ b/tests/Unit/Middleware/ResolvesMiddlewareTraitTest.php
@@ -0,0 +1,67 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Middleware;
+
+use Engelsystem\Application;
+use Engelsystem\Middleware\CallableHandler;
+use Engelsystem\Test\Unit\Middleware\Stub\HasStaticMethod;
+use Engelsystem\Test\Unit\Middleware\Stub\ResolvesMiddlewareTraitImplementation;
+use InvalidArgumentException;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+use Psr\Http\Server\MiddlewareInterface;
+
+class ResolvesMiddlewareTraitTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Middleware\ResolvesMiddlewareTrait::resolveMiddleware
+ * @covers \Engelsystem\Middleware\ResolvesMiddlewareTrait::isMiddleware
+ */
+ public function testResolveMiddleware()
+ {
+ /** @var Application|MockObject $container */
+ $container = $this->createMock(Application::class);
+ $middlewareInterface = $this->getMockForAbstractClass(MiddlewareInterface::class);
+ $callable = [HasStaticMethod::class, 'foo'];
+
+ $container->expects($this->exactly(3))
+ ->method('make')
+ ->withConsecutive(
+ ['FooBarClass'],
+ [CallableHandler::class, ['callable' => $callable]],
+ ['UnresolvableClass']
+ )
+ ->willReturnOnConsecutiveCalls(
+ $middlewareInterface,
+ $middlewareInterface,
+ null
+ );
+
+ $middleware = new ResolvesMiddlewareTraitImplementation($container);
+
+ $return = $middleware->callResolveMiddleware('FooBarClass');
+ $this->assertEquals($middlewareInterface, $return);
+
+ $return = $middleware->callResolveMiddleware($callable);
+ $this->assertEquals($middlewareInterface, $return);
+
+ $this->expectException(InvalidArgumentException::class);
+ $middleware->callResolveMiddleware('UnresolvableClass');
+ }
+
+ /**
+ * @covers \Engelsystem\Middleware\ResolvesMiddlewareTrait::resolveMiddleware
+ */
+ public function testResolveMiddlewareNoContainer()
+ {
+ $middlewareInterface = $this->getMockForAbstractClass(MiddlewareInterface::class);
+
+ $middleware = new ResolvesMiddlewareTraitImplementation();
+ $return = $middleware->callResolveMiddleware($middlewareInterface);
+
+ $this->assertEquals($middlewareInterface, $return);
+
+ $this->expectException(InvalidArgumentException::class);
+ $middleware->callResolveMiddleware('FooBarClass');
+ }
+}
diff --git a/tests/Unit/Middleware/RouteDispatcherServiceProviderTest.php b/tests/Unit/Middleware/RouteDispatcherServiceProviderTest.php
new file mode 100644
index 00000000..ca784c73
--- /dev/null
+++ b/tests/Unit/Middleware/RouteDispatcherServiceProviderTest.php
@@ -0,0 +1,65 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Middleware;
+
+use Engelsystem\Middleware\LegacyMiddleware;
+use Engelsystem\Middleware\RouteDispatcher;
+use Engelsystem\Middleware\RouteDispatcherServiceProvider;
+use Engelsystem\Test\Unit\ServiceProviderTest;
+use FastRoute\Dispatcher as FastRouteDispatcher;
+use Illuminate\Contracts\Container\ContextualBindingBuilder;
+use PHPUnit\Framework\MockObject\MockObject;
+use Psr\Http\Server\MiddlewareInterface;
+
+class RouteDispatcherServiceProviderTest extends ServiceProviderTest
+{
+ /**
+ * @covers \Engelsystem\Middleware\RouteDispatcherServiceProvider::register()
+ */
+ public function testRegister()
+ {
+ $bindingBuilder = $this->createMock(ContextualBindingBuilder::class);
+ $routeDispatcher = $this->getMockForAbstractClass(FastRouteDispatcher::class);
+
+ $app = $this->getApp(['alias', 'when']);
+
+ $app->expects($this->once())
+ ->method('alias')
+ ->with(RouteDispatcher::class, 'route.dispatcher');
+
+ $app->expects($this->exactly(2))
+ ->method('when')
+ ->with(RouteDispatcher::class)
+ ->willReturn($bindingBuilder);
+
+ $bindingBuilder->expects($this->exactly(2))
+ ->method('needs')
+ ->withConsecutive(
+ [FastRouteDispatcher::class],
+ [MiddlewareInterface::class]
+ )
+ ->willReturn($bindingBuilder);
+
+ $bindingBuilder->expects($this->exactly(2))
+ ->method('give')
+ ->with($this->callback(function ($subject) {
+ if (is_callable($subject)) {
+ $subject();
+ }
+
+ return is_callable($subject) || $subject == LegacyMiddleware::class;
+ }));
+
+ /** @var RouteDispatcherServiceProvider|MockObject $serviceProvider */
+ $serviceProvider = $this->getMockBuilder(RouteDispatcherServiceProvider::class)
+ ->setConstructorArgs([$app])
+ ->setMethods(['generateRouting'])
+ ->getMock();
+
+ $serviceProvider->expects($this->once())
+ ->method('generateRouting')
+ ->willReturn($routeDispatcher);
+
+ $serviceProvider->register();
+ }
+}
diff --git a/tests/Unit/Middleware/RouteDispatcherTest.php b/tests/Unit/Middleware/RouteDispatcherTest.php
new file mode 100644
index 00000000..edb2f158
--- /dev/null
+++ b/tests/Unit/Middleware/RouteDispatcherTest.php
@@ -0,0 +1,148 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Middleware;
+
+use Engelsystem\Middleware\RouteDispatcher;
+use FastRoute\Dispatcher as FastRouteDispatcher;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Message\UriInterface;
+use Psr\Http\Server\MiddlewareInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+
+class RouteDispatcherTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Middleware\RouteDispatcher::process
+ * @covers \Engelsystem\Middleware\RouteDispatcher::__construct
+ */
+ public function testProcess()
+ {
+ /** @var FastRouteDispatcher|MockObject $dispatcher */
+ /** @var ResponseInterface|MockObject $response */
+ /** @var ServerRequestInterface|MockObject $request */
+ /** @var RequestHandlerInterface|MockObject $handler */
+ list($dispatcher, $response, $request, $handler) = $this->getMocks();
+
+ $dispatcher->expects($this->once())
+ ->method('dispatch')
+ ->with('HEAD', '/foo!bar')
+ ->willReturn([FastRouteDispatcher::FOUND, $handler, ['foo' => 'bar', 'lorem' => 'ipsum']]);
+
+ $request->expects($this->exactly(3))
+ ->method('withAttribute')
+ ->withConsecutive(
+ ['route-request-handler', $handler],
+ ['foo', 'bar'],
+ ['lorem', 'ipsum']
+ )
+ ->willReturn($request);
+
+ $handler->expects($this->once())
+ ->method('handle')
+ ->with($request)
+ ->willReturn($response);
+
+ $middleware = new RouteDispatcher($dispatcher, $response);
+ $return = $middleware->process($request, $handler);
+ $this->assertEquals($response, $return);
+ }
+
+ /**
+ * @covers \Engelsystem\Middleware\RouteDispatcher::process
+ */
+ public function testProcessNotFound()
+ {
+ /** @var FastRouteDispatcher|MockObject $dispatcher */
+ /** @var ResponseInterface|MockObject $response */
+ /** @var ServerRequestInterface|MockObject $request */
+ /** @var RequestHandlerInterface|MockObject $handler */
+ list($dispatcher, $response, $request, $handler) = $this->getMocks();
+ /** @var MiddlewareInterface|MockObject $notFound */
+ $notFound = $this->createMock(MiddlewareInterface::class);
+
+ $dispatcher->expects($this->exactly(2))
+ ->method('dispatch')
+ ->with('HEAD', '/foo!bar')
+ ->willReturn([FastRouteDispatcher::NOT_FOUND]);
+
+ $response->expects($this->once())
+ ->method('withStatus')
+ ->with(404)
+ ->willReturn($response);
+
+ $notFound->expects($this->once())
+ ->method('process')
+ ->with($request, $handler)
+ ->willReturn($response);
+
+ $middleware = new RouteDispatcher($dispatcher, $response, $notFound);
+ $return = $middleware->process($request, $handler);
+ $this->assertEquals($response, $return);
+
+ $middleware = new RouteDispatcher($dispatcher, $response);
+ $return = $middleware->process($request, $handler);
+ $this->assertEquals($response, $return);
+ }
+
+ /**
+ * @covers \Engelsystem\Middleware\RouteDispatcher::process
+ */
+ public function testProcessNotAllowed()
+ {
+ /** @var FastRouteDispatcher|MockObject $dispatcher */
+ /** @var ResponseInterface|MockObject $response */
+ /** @var ServerRequestInterface|MockObject $request */
+ /** @var RequestHandlerInterface|MockObject $handler */
+ list($dispatcher, $response, $request, $handler) = $this->getMocks();
+
+ $dispatcher->expects($this->once())
+ ->method('dispatch')
+ ->with('HEAD', '/foo!bar')
+ ->willReturn([FastRouteDispatcher::METHOD_NOT_ALLOWED, ['POST', 'TEST']]);
+
+ $response->expects($this->once())
+ ->method('withStatus')
+ ->with(405)
+ ->willReturn($response);
+ $response->expects($this->once())
+ ->method('withHeader')
+ ->with('Allow', 'POST, TEST')
+ ->willReturn($response);
+
+ $middleware = new RouteDispatcher($dispatcher, $response);
+ $return = $middleware->process($request, $handler);
+ $this->assertEquals($response, $return);
+ }
+
+ /**
+ * @return array
+ */
+ protected function getMocks(): array
+ {
+ /** @var FastRouteDispatcher|MockObject $dispatcher */
+ $dispatcher = $this->getMockForAbstractClass(FastRouteDispatcher::class);
+ /** @var ResponseInterface|MockObject $response */
+ $response = $this->getMockForAbstractClass(ResponseInterface::class);
+ /** @var ServerRequestInterface|MockObject $request */
+ $request = $this->getMockForAbstractClass(ServerRequestInterface::class);
+ /** @var RequestHandlerInterface|MockObject $handler */
+ $handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
+ /** @var UriInterface|MockObject $uriInterface */
+ $uriInterface = $this->getMockForAbstractClass(UriInterface::class);
+
+ $request->expects($this->atLeastOnce())
+ ->method('getMethod')
+ ->willReturn('HEAD');
+ $request->expects($this->atLeastOnce())
+ ->method('getUri')
+ ->willReturn($uriInterface);
+ $uriInterface->expects($this->atLeastOnce())
+ ->method('getPath')
+ ->willReturn('/foo%21bar');
+
+ return array($dispatcher, $response, $request, $handler);
+ }
+}
diff --git a/tests/Unit/Middleware/Stub/HasStaticMethod.php b/tests/Unit/Middleware/Stub/HasStaticMethod.php
new file mode 100644
index 00000000..5ca2670e
--- /dev/null
+++ b/tests/Unit/Middleware/Stub/HasStaticMethod.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Middleware\Stub;
+
+class HasStaticMethod
+{
+ public static function foo() { }
+}
diff --git a/tests/Unit/Middleware/Stub/ResolvesMiddlewareTraitImplementation.php b/tests/Unit/Middleware/Stub/ResolvesMiddlewareTraitImplementation.php
new file mode 100644
index 00000000..2787d74b
--- /dev/null
+++ b/tests/Unit/Middleware/Stub/ResolvesMiddlewareTraitImplementation.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Middleware\Stub;
+
+use Engelsystem\Application;
+use Engelsystem\Middleware\ResolvesMiddlewareTrait;
+use InvalidArgumentException;
+use Psr\Http\Server\MiddlewareInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+
+class ResolvesMiddlewareTraitImplementation
+{
+ use ResolvesMiddlewareTrait;
+
+ /** @var Application */
+ protected $container;
+
+ /**
+ * @param Application $container
+ */
+ public function __construct(Application $container = null)
+ {
+ $this->container = $container;
+ }
+
+ /**
+ * @param string|callable|MiddlewareInterface|RequestHandlerInterface $middleware
+ * @return MiddlewareInterface|RequestHandlerInterface
+ * @throws InvalidArgumentException
+ */
+ public function callResolveMiddleware($middleware)
+ {
+ return $this->resolveMiddleware($middleware);
+ }
+}
diff --git a/tests/Unit/Routing/RoutingServiceProviderTest.php b/tests/Unit/Routing/RoutingServiceProviderTest.php
deleted file mode 100644
index ce3d7290..00000000
--- a/tests/Unit/Routing/RoutingServiceProviderTest.php
+++ /dev/null
@@ -1,64 +0,0 @@
-<?php
-
-namespace Engelsystem\Test\Unit\Routing;
-
-use Engelsystem\Config\Config;
-use Engelsystem\Routing\LegacyUrlGenerator;
-use Engelsystem\Routing\RoutingServiceProvider;
-use Engelsystem\Routing\UrlGenerator;
-use Engelsystem\Routing\UrlGeneratorInterface;
-use Engelsystem\Test\Unit\ServiceProviderTest;
-use PHPUnit_Framework_MockObject_MockObject as MockObject;
-
-class RoutingServiceProviderTest extends ServiceProviderTest
-{
- /**
- * @covers \Engelsystem\Routing\RoutingServiceProvider::register()
- */
- public function testRegister()
- {
- $app = $this->getApp(['make', 'instance', 'bind', 'get']);
- /** @var MockObject|Config $config */
- $config = $this->getMockBuilder(Config::class)->getMock();
- /** @var MockObject|UrlGeneratorInterface $urlGenerator */
- $urlGenerator = $this->getMockForAbstractClass(UrlGeneratorInterface::class);
- /** @var MockObject|UrlGeneratorInterface $legacyUrlGenerator */
- $legacyUrlGenerator = $this->getMockForAbstractClass(UrlGeneratorInterface::class);
-
- $config->expects($this->atLeastOnce())
- ->method('get')
- ->with('rewrite_urls')
- ->willReturnOnConsecutiveCalls(
- true,
- false
- );
-
- $this->setExpects($app, 'get', ['config'], $config, $this->atLeastOnce());
-
- $app->expects($this->atLeastOnce())
- ->method('make')
- ->withConsecutive(
- [UrlGenerator::class],
- [LegacyUrlGenerator::class]
- )
- ->willReturnOnConsecutiveCalls(
- $urlGenerator,
- $legacyUrlGenerator
- );
- $app->expects($this->atLeastOnce())
- ->method('instance')
- ->withConsecutive(
- ['routing.urlGenerator', $urlGenerator],
- ['routing.urlGenerator', $legacyUrlGenerator]
- );
- $this->setExpects(
- $app, 'bind',
- [UrlGeneratorInterface::class, 'routing.urlGenerator'], null,
- $this->atLeastOnce()
- );
-
- $serviceProvider = new RoutingServiceProvider($app);
- $serviceProvider->register();
- $serviceProvider->register();
- }
-}