From 9d34f371cb9c5ab0d60bd3158678b9cc9da6cc80 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sun, 2 Sep 2018 02:13:18 +0200 Subject: Added CreditsController --- config/routes.php | 9 +-- includes/pages/guest_credits.php | 17 ------ src/Controllers/BaseController.php | 8 +++ src/Controllers/CreditsController.php | 24 ++++++++ src/Middleware/LegacyMiddleware.php | 6 -- src/Middleware/RequestHandler.php | 31 +++++++++- templates/layouts/app.twig | 34 ++++++----- templates/pages/credits.html | 36 ----------- templates/pages/credits.twig | 42 +++++++++++++ tests/Unit/Controllers/CreditsControllerTest.php | 28 +++++++++ tests/Unit/Middleware/RequestHandlerTest.php | 77 +++++++++++++++++++++--- 11 files changed, 222 insertions(+), 90 deletions(-) delete mode 100644 includes/pages/guest_credits.php create mode 100644 src/Controllers/BaseController.php create mode 100644 src/Controllers/CreditsController.php delete mode 100644 templates/pages/credits.html create mode 100644 templates/pages/credits.twig create mode 100644 tests/Unit/Controllers/CreditsControllerTest.php diff --git a/config/routes.php b/config/routes.php index 5296dbc7..2267bc88 100644 --- a/config/routes.php +++ b/config/routes.php @@ -1,14 +1,7 @@ addRoute('GET', '/hello/{name}', function ($request) { - /** @var ServerRequestInterface $request */ - $name = $request->getAttribute('name'); - - return response(sprintf('Hello %s!', htmlspecialchars($name))); -}); +$route->get('/credits', 'CreditsController@index'); diff --git a/includes/pages/guest_credits.php b/includes/pages/guest_credits.php deleted file mode 100644 index 5f90c97c..00000000 --- a/includes/pages/guest_credits.php +++ /dev/null @@ -1,17 +0,0 @@ -response = $response; + } + + /** + * @return Response + */ + public function index() + { + return $this->response->withView('pages/credits.twig'); + } +} diff --git a/src/Middleware/LegacyMiddleware.php b/src/Middleware/LegacyMiddleware.php index f8c2a205..ebf456eb 100644 --- a/src/Middleware/LegacyMiddleware.php +++ b/src/Middleware/LegacyMiddleware.php @@ -18,7 +18,6 @@ class LegacyMiddleware implements MiddlewareInterface 'angeltypes', 'api', 'atom', - 'credits', 'ical', 'login', 'public_dashboard', @@ -249,11 +248,6 @@ class LegacyMiddleware implements MiddlewareInterface $title = admin_log_title(); $content = admin_log(); return [$title, $content]; - case 'credits': - require_once realpath(__DIR__ . '/../../includes/pages/guest_credits.php'); - $title = credits_title(); - $content = guest_credits(); - return [$title, $content]; } require_once realpath(__DIR__ . '/../../includes/pages/guest_start.php'); diff --git a/src/Middleware/RequestHandler.php b/src/Middleware/RequestHandler.php index e1381abf..ebe1ff9e 100644 --- a/src/Middleware/RequestHandler.php +++ b/src/Middleware/RequestHandler.php @@ -35,7 +35,7 @@ class RequestHandler implements MiddlewareInterface public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $requestHandler = $request->getAttribute('route-request-handler'); - $requestHandler = $this->resolveMiddleware($requestHandler); + $requestHandler = $this->resolveRequestHandler($requestHandler); if ($requestHandler instanceof MiddlewareInterface) { return $requestHandler->process($request, $handler); @@ -47,4 +47,33 @@ class RequestHandler implements MiddlewareInterface throw new InvalidArgumentException('Unable to process request handler of type ' . gettype($requestHandler)); } + + /** + * @param string|callable|MiddlewareInterface|RequestHandlerInterface $handler + * @return MiddlewareInterface|RequestHandlerInterface + */ + protected function resolveRequestHandler($handler) + { + if (is_string($handler) && strpos($handler, '@') !== false) { + list($class, $method) = explode('@', $handler, 2); + if (!class_exists($class) && !$this->container->has($class)) { + $class = sprintf('Engelsystem\\Controllers\\%s', $class); + } + + $handler = [$class, $method]; + } + + if ( + is_array($handler) + && is_string($handler[0]) + && ( + class_exists($handler[0]) + || $this->container->has($handler[0]) + ) + ) { + $handler[0] = $this->container->make($handler[0]); + } + + return $this->resolveMiddleware($handler); + } } diff --git a/templates/layouts/app.twig b/templates/layouts/app.twig index 20ea853b..4868a714 100644 --- a/templates/layouts/app.twig +++ b/templates/layouts/app.twig @@ -28,8 +28,10 @@ {% endblock %}
-
- {% block content %}{{ content|raw }}{% endblock %} +
+ {% block content %} + {{ content|raw }} + {% endblock %}
- - - - - - - - - + {% block scripts %} + + + + + + + + + + {% endblock %} {% endblock %} diff --git a/templates/pages/credits.html b/templates/pages/credits.html deleted file mode 100644 index 4e247113..00000000 --- a/templates/pages/credits.html +++ /dev/null @@ -1,36 +0,0 @@ -
-

Credits

-
-
-

Source code

-

- The original system was written by cookie. - It was then completely rewritten and enhanced by - msquare (maintainer), - MyIgel, - mortzu, - jplitza and - gnomus. -

-

- Please look at the - contributor list on github for a more complete version. -

-
-
-

Hosting

-

- Webspace, development platform and domain on engelsystem.de - is currently provided by would you buy this? (ichdasich) - and adminstrated by mortzu, - derf and ichdasich. -

-
-
-

Translation

-

- Many thanks for the german translation: e7p -

-
-
-
diff --git a/templates/pages/credits.twig b/templates/pages/credits.twig new file mode 100644 index 00000000..ff2bf873 --- /dev/null +++ b/templates/pages/credits.twig @@ -0,0 +1,42 @@ +{% extends "layouts/app.twig" %} + +{% block title %}{{ __('Credits') }}{% endblock %} + +{% block content %} +
+

Credits

+
+
+

Source code

+

+ The original system was written by cookie. + It was then completely rewritten and enhanced by + msquare (maintainer), + MyIgel, + mortzu, + jplitza and + gnomus. +

+

+ Please look at the + contributor list on github for a more complete version. +

+
+
+

Hosting

+

+ Webspace, development platform and domain on engelsystem.de + is currently provided by would you buy this? (ichdasich) + and adminstrated by mortzu, + derf and ichdasich. +

+
+
+

Translation

+

+ Many thanks for the german translation: e7p +

+
+
+
+{% endblock %} diff --git a/tests/Unit/Controllers/CreditsControllerTest.php b/tests/Unit/Controllers/CreditsControllerTest.php new file mode 100644 index 00000000..6f0200f2 --- /dev/null +++ b/tests/Unit/Controllers/CreditsControllerTest.php @@ -0,0 +1,28 @@ +createMock(Response::class); + + $response->expects($this->once()) + ->method('withView') + ->with('pages/credits.twig'); + + $controller = new CreditsController($response); + $controller->index(); + } +} diff --git a/tests/Unit/Middleware/RequestHandlerTest.php b/tests/Unit/Middleware/RequestHandlerTest.php index 896b55c3..b1ffbd33 100644 --- a/tests/Unit/Middleware/RequestHandlerTest.php +++ b/tests/Unit/Middleware/RequestHandlerTest.php @@ -38,15 +38,12 @@ class RequestHandlerTest extends TestCase 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); + /** @var MiddlewareInterface|MockObject $middlewareInterface */ + list($container, $request, $handler, $response, $middlewareInterface) = $this->getMocks(); - $middlewareInterface = $this->getMockForAbstractClass(MiddlewareInterface::class); $requestHandlerInterface = $this->getMockForAbstractClass(RequestHandlerInterface::class); $request->expects($this->exactly(3)) @@ -57,10 +54,10 @@ class RequestHandlerTest extends TestCase /** @var RequestHandler|MockObject $middleware */ $middleware = $this->getMockBuilder(RequestHandler::class) ->setConstructorArgs([$container]) - ->setMethods(['resolveMiddleware']) + ->setMethods(['resolveRequestHandler']) ->getMock(); $middleware->expects($this->exactly(3)) - ->method('resolveMiddleware') + ->method('resolveRequestHandler') ->with('FooBarClass') ->willReturnOnConsecutiveCalls( $middlewareInterface, @@ -86,4 +83,70 @@ class RequestHandlerTest extends TestCase $this->expectException(InvalidArgumentException::class); $middleware->process($request, $handler); } + + /** + * @covers \Engelsystem\Middleware\RequestHandler::resolveRequestHandler + */ + public function testResolveRequestHandler() + { + /** @var Application|MockObject $container */ + /** @var ServerRequestInterface|MockObject $request */ + /** @var RequestHandlerInterface|MockObject $handler */ + /** @var ResponseInterface|MockObject $response */ + /** @var MiddlewareInterface|MockObject $middlewareInterface */ + list($container, $request, $handler, $response, $middlewareInterface) = $this->getMocks(); + + $className = 'Engelsystem\\Controllers\\FooBarTestController'; + + $request->expects($this->exactly(1)) + ->method('getAttribute') + ->with('route-request-handler') + ->willReturn('FooBarTestController@showStuff'); + + /** @var RequestHandler|MockObject $middleware */ + $middleware = $this->getMockBuilder(RequestHandler::class) + ->setConstructorArgs([$container]) + ->setMethods(['resolveMiddleware']) + ->getMock(); + $middleware->expects($this->once()) + ->method('resolveMiddleware') + ->with([$middlewareInterface, 'showStuff']) + ->willReturn($middlewareInterface); + + $middlewareInterface->expects($this->once()) + ->method('process') + ->with($request, $handler) + ->willReturn($response); + + $container->expects($this->exactly(2)) + ->method('has') + ->withConsecutive(['FooBarTestController'], [$className]) + ->willReturnOnConsecutiveCalls(false, true); + $container->expects($this->once()) + ->method('make') + ->with($className) + ->willReturn($middlewareInterface); + + $return = $middleware->process($request, $handler); + $this->assertEquals($return, $response); + } + + /** + * @return array + */ + protected function getMocks(): array + { + /** @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); + /** @var MiddlewareInterface $middlewareInterface */ + $middlewareInterface = $this->getMockForAbstractClass(MiddlewareInterface::class); + + return array($container, $request, $handler, $response, $middlewareInterface); + } } -- cgit v1.2.3-54-g00ecf