From 6f1ca1ed44db19a7b2ba9fbb986c27a458ed8b76 Mon Sep 17 00:00:00 2001 From: MarioRadu Date: Tue, 11 Feb 2025 16:51:44 +0200 Subject: [PATCH 1/8] removed dotkernel/dot-controller and replaced with handlers Signed-off-by: MarioRadu --- composer.json | 1 - config/autoload/mezzio.global.php | 2 +- src/App/src/ConfigProvider.php | 9 +++- src/App/src/Factory/IndexHandlerFactory.php | 28 +++++++++++ src/App/src/Handler/IndexHandler.php | 26 ++++++++++ src/App/src/RoutesDelegator.php | 24 +++++++++ .../app/{home.html.twig => index.html.twig} | 0 src/App/templates/error/403.html.twig | 17 ------- src/App/templates/error/404.html.twig | 4 +- src/App/templates/error/500.html.twig | 15 ++++++ src/App/templates/error/error.html.twig | 20 -------- src/App/templates/layout/default.html.twig | 12 +++-- src/Page/src/ConfigProvider.php | 12 +++-- src/Page/src/Controller/PageController.php | 50 ------------------- ...rFactory.php => AboutUsHandlerFactory.php} | 16 ++---- .../src/Factory/WhoWeAreHandlerFactory.php | 28 +++++++++++ src/Page/src/Handler/AboutUsHandler.php | 26 ++++++++++ src/Page/src/Handler/WhoWeAreHandler.php | 26 ++++++++++ src/Page/src/RoutesDelegator.php | 8 +-- test/Unit/App/ConfigProviderTest.php | 8 +++ test/Unit/Page/ConfigProviderTest.php | 2 - 21 files changed, 215 insertions(+), 119 deletions(-) create mode 100644 src/App/src/Factory/IndexHandlerFactory.php create mode 100644 src/App/src/Handler/IndexHandler.php create mode 100644 src/App/src/RoutesDelegator.php rename src/App/templates/app/{home.html.twig => index.html.twig} (100%) delete mode 100644 src/App/templates/error/403.html.twig create mode 100644 src/App/templates/error/500.html.twig delete mode 100644 src/App/templates/error/error.html.twig delete mode 100644 src/Page/src/Controller/PageController.php rename src/Page/src/Factory/{PageControllerFactory.php => AboutUsHandlerFactory.php} (53%) create mode 100644 src/Page/src/Factory/WhoWeAreHandlerFactory.php create mode 100644 src/Page/src/Handler/AboutUsHandler.php create mode 100644 src/Page/src/Handler/WhoWeAreHandler.php diff --git a/composer.json b/composer.json index 9a3617f..47f02dc 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,6 @@ }, "require": { "php": "~8.2.0 || ~8.3.0 || ~8.4.0", - "dotkernel/dot-controller": "^4.0.1", "dotkernel/dot-errorhandler": "^4.1.1", "laminas/laminas-component-installer": "^3.5.0", "laminas/laminas-config-aggregator": "^1.17.0", diff --git a/config/autoload/mezzio.global.php b/config/autoload/mezzio.global.php index 64d701e..eee8580 100644 --- a/config/autoload/mezzio.global.php +++ b/config/autoload/mezzio.global.php @@ -18,7 +18,7 @@ // generating responses. 'error_handler' => [ 'template_404' => 'error::404', - 'template_error' => 'error::error', + 'template_error' => 'error::500', ], ], ]; diff --git a/src/App/src/ConfigProvider.php b/src/App/src/ConfigProvider.php index 5c068ff..ca2f572 100644 --- a/src/App/src/ConfigProvider.php +++ b/src/App/src/ConfigProvider.php @@ -4,6 +4,9 @@ namespace Light\App; +use Light\App\Factory\IndexHandlerFactory; +use Light\App\Handler\IndexHandler; + class ConfigProvider { public function __invoke(): array @@ -16,7 +19,11 @@ public function __invoke(): array public function getDependencies(): array { - return []; + return [ + 'factories' => [ + IndexHandler::class => IndexHandlerFactory::class, + ], + ]; } public function getTemplates(): array diff --git a/src/App/src/Factory/IndexHandlerFactory.php b/src/App/src/Factory/IndexHandlerFactory.php new file mode 100644 index 0000000..b33b22b --- /dev/null +++ b/src/App/src/Factory/IndexHandlerFactory.php @@ -0,0 +1,28 @@ +get(TemplateRendererInterface::class); + assert($template instanceof TemplateRendererInterface); + + return new IndexHandler($template); + } +} diff --git a/src/App/src/Handler/IndexHandler.php b/src/App/src/Handler/IndexHandler.php new file mode 100644 index 0000000..8275400 --- /dev/null +++ b/src/App/src/Handler/IndexHandler.php @@ -0,0 +1,26 @@ +template->render('app::index') + ); + } +} diff --git a/src/App/src/RoutesDelegator.php b/src/App/src/RoutesDelegator.php new file mode 100644 index 0000000..04cd8c4 --- /dev/null +++ b/src/App/src/RoutesDelegator.php @@ -0,0 +1,24 @@ +get('/', [IndexHandler::class], 'app.index'); + + return $app; + } +} diff --git a/src/App/templates/app/home.html.twig b/src/App/templates/app/index.html.twig similarity index 100% rename from src/App/templates/app/home.html.twig rename to src/App/templates/app/index.html.twig diff --git a/src/App/templates/error/403.html.twig b/src/App/templates/error/403.html.twig deleted file mode 100644 index 2e0324a..0000000 --- a/src/App/templates/error/403.html.twig +++ /dev/null @@ -1,17 +0,0 @@ -{% extends '@layout/default.html.twig' %} - -{% block title %}403 Forbidden{% endblock %} - -{% block content %} -
-
-

Oops!

-

This is awkward.

-

You don't have enough permissions to access the requested content!

-

- You are looking for something that doesn't exist or may have moved. Check out one of the links on this page - or head back to Home. -

-
-
-{% endblock %} diff --git a/src/App/templates/error/404.html.twig b/src/App/templates/error/404.html.twig index 4578393..a08e96f 100644 --- a/src/App/templates/error/404.html.twig +++ b/src/App/templates/error/404.html.twig @@ -9,9 +9,9 @@

Oops!

This is awkward.

404

-

+

You are looking for something that doesn't exist or may have moved. Check out one of the links on this page - or head back to Home. + or head back to Home.

diff --git a/src/App/templates/error/500.html.twig b/src/App/templates/error/500.html.twig new file mode 100644 index 0000000..b917e8c --- /dev/null +++ b/src/App/templates/error/500.html.twig @@ -0,0 +1,15 @@ +{% extends '@layout/default.html.twig' %} + +{% block title %}500 Internal Server Error{% endblock %} +{% block canonical %}{% endblock %} + +{% block content %} +
+
+

Oops!

+

This is awkward.

+

500

+

{{ reason }}

+
+
+{% endblock %} diff --git a/src/App/templates/error/error.html.twig b/src/App/templates/error/error.html.twig deleted file mode 100644 index b42b77a..0000000 --- a/src/App/templates/error/error.html.twig +++ /dev/null @@ -1,20 +0,0 @@ -{% extends '@layout/default.html.twig' %} - -{% block title %}{{ status }} {{ reason }}{% endblock %} - -{% block content %} -
-
-

Oops!

-

This is awkward.

-

{{ status }}

-

{{ reason }}

- {% if status == 404 %} -

- You are looking for something that doesn't exist or may have moved. Check out one of the links on this page - or head back to Home. -

- {% endif %} -
-
-{% endblock %} diff --git a/src/App/templates/layout/default.html.twig b/src/App/templates/layout/default.html.twig index 22e499f..94c1028 100644 --- a/src/App/templates/layout/default.html.twig +++ b/src/App/templates/layout/default.html.twig @@ -7,7 +7,9 @@ {% block title %}{% endblock %} | Dotkernel Light V1 {% block canonical %} - + {% if routeName is defined and routeName is not empty %} + + {% endif %} {% endblock %} @@ -46,16 +48,16 @@ diff --git a/src/Page/src/ConfigProvider.php b/src/Page/src/ConfigProvider.php index c4b7455..14c5786 100644 --- a/src/Page/src/ConfigProvider.php +++ b/src/Page/src/ConfigProvider.php @@ -4,9 +4,11 @@ namespace Light\Page; -use Light\Page\Controller\PageController; -use Light\Page\Factory\PageControllerFactory; +use Light\Page\Factory\AboutUsHandlerFactory; use Light\Page\Factory\PageServiceFactory; +use Light\Page\Factory\WhoWeAreHandlerFactory; +use Light\Page\Handler\AboutUsHandler; +use Light\Page\Handler\WhoWeAreHandler; use Light\Page\Service\PageService; use Light\Page\Service\PageServiceInterface; use Mezzio\Application; @@ -26,12 +28,14 @@ public function getDependencies(): array return [ 'delegators' => [ Application::class => [ + \Light\App\RoutesDelegator::class, RoutesDelegator::class, ], ], 'factories' => [ - PageController::class => PageControllerFactory::class, - PageService::class => PageServiceFactory::class, + AboutUsHandler::class => AboutUsHandlerFactory::class, + WhoWeAreHandler::class => WhoWeAreHandlerFactory::class, + PageService::class => PageServiceFactory::class, ], 'aliases' => [ PageServiceInterface::class => PageService::class, diff --git a/src/Page/src/Controller/PageController.php b/src/Page/src/Controller/PageController.php deleted file mode 100644 index e0438cf..0000000 --- a/src/Page/src/Controller/PageController.php +++ /dev/null @@ -1,50 +0,0 @@ -template->render('app::home') - ); - } - - public function homeAction(): ResponseInterface - { - return new HtmlResponse( - $this->template->render('app::home', ['routeName' => 'home']) - ); - } - - public function aboutUsAction(): ResponseInterface - { - return new HtmlResponse( - $this->template->render('page::about') - ); - } - - public function whoWeAreAction(): ResponseInterface - { - return new HtmlResponse( - $this->template->render('page::who-we-are') - ); - } -} diff --git a/src/Page/src/Factory/PageControllerFactory.php b/src/Page/src/Factory/AboutUsHandlerFactory.php similarity index 53% rename from src/Page/src/Factory/PageControllerFactory.php rename to src/Page/src/Factory/AboutUsHandlerFactory.php index 740ae6c..f9b7677 100644 --- a/src/Page/src/Factory/PageControllerFactory.php +++ b/src/Page/src/Factory/AboutUsHandlerFactory.php @@ -4,9 +4,7 @@ namespace Light\Page\Factory; -use Light\Page\Controller\PageController; -use Light\Page\Service\PageServiceInterface; -use Mezzio\Router\RouterInterface; +use Light\Page\Handler\AboutUsHandler; use Mezzio\Template\TemplateRendererInterface; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; @@ -14,23 +12,17 @@ use function assert; -class PageControllerFactory +class AboutUsHandlerFactory { /** * @throws NotFoundExceptionInterface * @throws ContainerExceptionInterface */ - public function __invoke(ContainerInterface $container, string $requestedName): PageController + public function __invoke(ContainerInterface $container, string $requestedName): AboutUsHandler { - $pageService = $container->get(PageServiceInterface::class); - assert($pageService instanceof PageServiceInterface); - - $router = $container->get(RouterInterface::class); - assert($router instanceof RouterInterface); - $template = $container->get(TemplateRendererInterface::class); assert($template instanceof TemplateRendererInterface); - return new PageController($pageService, $router, $template); + return new AboutUsHandler($template); } } diff --git a/src/Page/src/Factory/WhoWeAreHandlerFactory.php b/src/Page/src/Factory/WhoWeAreHandlerFactory.php new file mode 100644 index 0000000..45b4006 --- /dev/null +++ b/src/Page/src/Factory/WhoWeAreHandlerFactory.php @@ -0,0 +1,28 @@ +get(TemplateRendererInterface::class); + assert($template instanceof TemplateRendererInterface); + + return new WhoWeAreHandler($template); + } +} diff --git a/src/Page/src/Handler/AboutUsHandler.php b/src/Page/src/Handler/AboutUsHandler.php new file mode 100644 index 0000000..04b039a --- /dev/null +++ b/src/Page/src/Handler/AboutUsHandler.php @@ -0,0 +1,26 @@ +template->render('page::about') + ); + } +} diff --git a/src/Page/src/Handler/WhoWeAreHandler.php b/src/Page/src/Handler/WhoWeAreHandler.php new file mode 100644 index 0000000..9f98d8e --- /dev/null +++ b/src/Page/src/Handler/WhoWeAreHandler.php @@ -0,0 +1,26 @@ +template->render('page::who-we-are') + ); + } +} diff --git a/src/Page/src/RoutesDelegator.php b/src/Page/src/RoutesDelegator.php index 0bb5de5..7c5631c 100644 --- a/src/Page/src/RoutesDelegator.php +++ b/src/Page/src/RoutesDelegator.php @@ -4,7 +4,8 @@ namespace Light\Page; -use Light\Page\Controller\PageController; +use Light\Page\Handler\AboutUsHandler; +use Light\Page\Handler\WhoWeAreHandler; use Mezzio\Application; use Psr\Container\ContainerInterface; @@ -17,9 +18,8 @@ public function __invoke(ContainerInterface $container, string $serviceName, cal $app = $callback(); assert($app instanceof Application); - $app->get('/', [PageController::class], 'home'); - - $app->get('/page[/{action}]', [PageController::class], 'page'); + $app->get('/page/about-us', [AboutUsHandler::class], 'page.about-us'); + $app->get('/page/who-we-are', [WhoWeAreHandler::class], 'page.who-we-are'); return $app; } diff --git a/test/Unit/App/ConfigProviderTest.php b/test/Unit/App/ConfigProviderTest.php index f01fc1d..3b34e27 100644 --- a/test/Unit/App/ConfigProviderTest.php +++ b/test/Unit/App/ConfigProviderTest.php @@ -5,6 +5,7 @@ namespace LightTest\Unit\App; use Light\App\ConfigProvider; +use Light\App\Handler\IndexHandler; use PHPUnit\Framework\TestCase; class ConfigProviderTest extends TestCase @@ -23,6 +24,13 @@ public function testConfigHasDependencies(): void $this->assertArrayHasKey('dependencies', $this->config); } + public function testDependenciesHasFactories(): void + { + $this->assertArrayHasKey('factories', $this->config['dependencies']); + $this->assertIsArray($this->config['dependencies']['factories']); + $this->assertArrayHasKey(IndexHandler::class, $this->config['dependencies']['factories']); + } + public function testConfigHasTemplates(): void { $this->assertArrayHasKey('templates', $this->config); diff --git a/test/Unit/Page/ConfigProviderTest.php b/test/Unit/Page/ConfigProviderTest.php index 0b5d254..12f8aee 100644 --- a/test/Unit/Page/ConfigProviderTest.php +++ b/test/Unit/Page/ConfigProviderTest.php @@ -5,7 +5,6 @@ namespace LightTest\Unit\Page; use Light\Page\ConfigProvider; -use Light\Page\Controller\PageController; use Light\Page\RoutesDelegator; use Light\Page\Service\PageService; use Light\Page\Service\PageServiceInterface; @@ -49,7 +48,6 @@ public function testDependenciesHasFactories(): void { $this->assertArrayHasKey('factories', $this->config['dependencies']); $this->assertIsArray($this->config['dependencies']['factories']); - $this->assertArrayHasKey(PageController::class, $this->config['dependencies']['factories']); $this->assertArrayHasKey(PageService::class, $this->config['dependencies']['factories']); } From 00f18464376c988dbb383ad59a70094675f32f58 Mon Sep 17 00:00:00 2001 From: MarioRadu Date: Tue, 11 Feb 2025 16:54:21 +0200 Subject: [PATCH 2/8] refactored namespaces as aliases Signed-off-by: MarioRadu --- src/Page/src/ConfigProvider.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Page/src/ConfigProvider.php b/src/Page/src/ConfigProvider.php index 14c5786..d3a624e 100644 --- a/src/Page/src/ConfigProvider.php +++ b/src/Page/src/ConfigProvider.php @@ -11,6 +11,8 @@ use Light\Page\Handler\WhoWeAreHandler; use Light\Page\Service\PageService; use Light\Page\Service\PageServiceInterface; +use Light\App\RoutesDelegator as AppRoutesDelegator; +use Light\Page\RoutesDelegator as PageRoutesDelegator; use Mezzio\Application; class ConfigProvider @@ -28,8 +30,8 @@ public function getDependencies(): array return [ 'delegators' => [ Application::class => [ - \Light\App\RoutesDelegator::class, - RoutesDelegator::class, + AppRoutesDelegator::class, + PageRoutesDelegator::class, ], ], 'factories' => [ From ef2593ba9de7e278181864fa22da1b27f2edd15d Mon Sep 17 00:00:00 2001 From: MarioRadu Date: Tue, 11 Feb 2025 16:57:03 +0200 Subject: [PATCH 3/8] refactored namespaces as aliases Signed-off-by: MarioRadu --- src/Page/src/ConfigProvider.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Page/src/ConfigProvider.php b/src/Page/src/ConfigProvider.php index d3a624e..f846c88 100644 --- a/src/Page/src/ConfigProvider.php +++ b/src/Page/src/ConfigProvider.php @@ -4,15 +4,15 @@ namespace Light\Page; +use Light\App\RoutesDelegator as AppRoutesDelegator; use Light\Page\Factory\AboutUsHandlerFactory; use Light\Page\Factory\PageServiceFactory; use Light\Page\Factory\WhoWeAreHandlerFactory; use Light\Page\Handler\AboutUsHandler; use Light\Page\Handler\WhoWeAreHandler; +use Light\Page\RoutesDelegator as PageRoutesDelegator; use Light\Page\Service\PageService; use Light\Page\Service\PageServiceInterface; -use Light\App\RoutesDelegator as AppRoutesDelegator; -use Light\Page\RoutesDelegator as PageRoutesDelegator; use Mezzio\Application; class ConfigProvider From d34d42f821674ffd09ce6a4d9fc70abc9e57fffe Mon Sep 17 00:00:00 2001 From: MarioRadu Date: Wed, 12 Feb 2025 11:11:45 +0200 Subject: [PATCH 4/8] refactored individual handlers to single handler for static pages Signed-off-by: MarioRadu --- src/App/src/ConfigProvider.php | 6 +++ src/App/templates/layout/default.html.twig | 4 +- src/Page/src/ConfigProvider.php | 15 +++---- ...dlerFactory.php => PageHandlerFactory.php} | 19 ++++---- .../src/Factory/WhoWeAreHandlerFactory.php | 28 ------------ src/Page/src/Handler/AboutUsHandler.php | 26 ----------- src/Page/src/Handler/PageHandler.php | 43 +++++++++++++++++++ src/Page/src/Handler/WhoWeAreHandler.php | 26 ----------- src/Page/src/RoutesDelegator.php | 6 +-- 9 files changed, 68 insertions(+), 105 deletions(-) rename src/Page/src/Factory/{AboutUsHandlerFactory.php => PageHandlerFactory.php} (60%) delete mode 100644 src/Page/src/Factory/WhoWeAreHandlerFactory.php delete mode 100644 src/Page/src/Handler/AboutUsHandler.php create mode 100644 src/Page/src/Handler/PageHandler.php delete mode 100644 src/Page/src/Handler/WhoWeAreHandler.php diff --git a/src/App/src/ConfigProvider.php b/src/App/src/ConfigProvider.php index ca2f572..9b0b5b4 100644 --- a/src/App/src/ConfigProvider.php +++ b/src/App/src/ConfigProvider.php @@ -6,6 +6,7 @@ use Light\App\Factory\IndexHandlerFactory; use Light\App\Handler\IndexHandler; +use Mezzio\Application; class ConfigProvider { @@ -20,6 +21,11 @@ public function __invoke(): array public function getDependencies(): array { return [ + 'delegators' => [ + Application::class => [ + RoutesDelegator::class, + ], + ], 'factories' => [ IndexHandler::class => IndexHandlerFactory::class, ], diff --git a/src/App/templates/layout/default.html.twig b/src/App/templates/layout/default.html.twig index 94c1028..244d878 100644 --- a/src/App/templates/layout/default.html.twig +++ b/src/App/templates/layout/default.html.twig @@ -49,8 +49,8 @@ diff --git a/src/Page/src/Factory/PageHandlerFactory.php b/src/Page/src/Factory/PageHandlerFactory.php index 3576493..bebae30 100644 --- a/src/Page/src/Factory/PageHandlerFactory.php +++ b/src/Page/src/Factory/PageHandlerFactory.php @@ -5,7 +5,6 @@ namespace Light\Page\Factory; use Light\Page\Handler\PageHandler; -use Mezzio\Handler\NotFoundHandler; use Mezzio\Template\TemplateRendererInterface; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; @@ -24,9 +23,6 @@ public function __invoke(ContainerInterface $container, string $requestedName): $template = $container->get(TemplateRendererInterface::class); assert($template instanceof TemplateRendererInterface); - $notFoundHandler = $container->get(NotFoundHandler::class); - assert($notFoundHandler instanceof NotFoundHandler); - - return new PageHandler($template, $notFoundHandler); + return new PageHandler($template); } } diff --git a/src/Page/src/Handler/PageHandler.php b/src/Page/src/Handler/PageHandler.php index 5e5fe0c..6cb7b14 100644 --- a/src/Page/src/Handler/PageHandler.php +++ b/src/Page/src/Handler/PageHandler.php @@ -5,41 +5,25 @@ namespace Light\Page\Handler; use Laminas\Diactoros\Response\HtmlResponse; -use Mezzio\Handler\NotFoundHandler; +use Mezzio\Router\RouteResult; use Mezzio\Template\TemplateRendererInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; -use Twig\Error\LoaderError; class PageHandler implements RequestHandlerInterface { public function __construct( protected TemplateRendererInterface $template, - protected NotFoundHandler $notFoundHandler, ) { } public function handle(ServerRequestInterface $request): ResponseInterface { - $actionTemplateMapping = [ - 'about-us' => 'page::about', - 'who-we-are' => 'page::who-we-are', - ]; + $template = $request->getAttribute(RouteResult::class)->getMatchedRouteName(); - $action = $request->getAttribute('action', 'index'); - - $template = $actionTemplateMapping[$action] ?? null; - if (null === $template) { - return $this->notFoundHandler->handle($request); - } - - try { - return new HtmlResponse( - $this->template->render($template) - ); - } catch (LoaderError) { - return $this->notFoundHandler->handle($request); - } + return new HtmlResponse( + $this->template->render($template) + ); } } diff --git a/src/Page/src/RoutesDelegator.php b/src/Page/src/RoutesDelegator.php index 966590a..40ebe63 100644 --- a/src/Page/src/RoutesDelegator.php +++ b/src/Page/src/RoutesDelegator.php @@ -6,18 +6,34 @@ use Light\Page\Handler\PageHandler; use Mezzio\Application; +use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; +use Psr\Container\NotFoundExceptionInterface; use function assert; +use function sprintf; class RoutesDelegator { + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function __invoke(ContainerInterface $container, string $serviceName, callable $callback): Application { $app = $callback(); assert($app instanceof Application); - $app->get('/page[/{action}]', [PageHandler::class], 'page'); + $routes = $container->get('config')['routes'] ?? []; + foreach ($routes as $moduleName => $moduleRoutes) { + foreach ($moduleRoutes as $routeUri => $templateName) { + $app->get( + sprintf('/%s/%s', $moduleName, $routeUri), + [PageHandler::class], + sprintf('%s::%s', $moduleName, $templateName) + ); + } + } return $app; } diff --git a/test/Unit/Page/Handler/PageHandlerTest.php b/test/Unit/Page/Handler/PageHandlerTest.php new file mode 100644 index 0000000..272cba9 --- /dev/null +++ b/test/Unit/Page/Handler/PageHandlerTest.php @@ -0,0 +1,59 @@ +createMock(PageHandler::class); + + $this->assertContainsOnlyInstancesOf(RequestHandlerInterface::class, [$handler]); + } + + /** + * @throws Exception + */ + public function testHandle(): void + { + $routeName = 'test_route_name'; + $request = $this->createMock(ServerRequestInterface::class); + $template = $this->createMock(TemplateRendererInterface::class); + $routeResult = $this->createMock(RouteResult::class); + + $routeResult + ->method('getMatchedRouteName') + ->willReturn($routeName); + + $request + ->method('getAttribute') + ->with(RouteResult::class) + ->willReturn($routeResult); + + $template + ->method('render') + ->with($routeName) + ->willReturn('

' . $routeName . '

'); + + $handler = new PageHandler($template); + + $response = $handler->handle($request); + + $this->assertInstanceOf(HtmlResponse::class, $response); + $this->assertSame('

' . $routeName . '

', $response->getBody()->getContents()); + } +} diff --git a/test/Unit/Page/RoutesDelegatorTest.php b/test/Unit/Page/RoutesDelegatorTest.php index 89a14b7..9acf195 100644 --- a/test/Unit/Page/RoutesDelegatorTest.php +++ b/test/Unit/Page/RoutesDelegatorTest.php @@ -4,12 +4,16 @@ namespace LightTest\Unit\Page; +use Light\Page\Handler\PageHandler; use Light\Page\RoutesDelegator; use Mezzio\Application; +use Mezzio\Router\Route; use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; +use function sprintf; + class RoutesDelegatorTest extends TestCase { /** @@ -17,11 +21,37 @@ class RoutesDelegatorTest extends TestCase */ public function testWillInvoke(): void { - $application = (new RoutesDelegator())( - $this->createMock(ContainerInterface::class), + $moduleName = 'test'; + $routeName = 'test_route_name'; + $routeUri = sprintf('/%s/%s', $moduleName, $routeName); + $templateName = sprintf('%s::%s', $moduleName, $routeName); + + $container = $this->createMock(ContainerInterface::class); + $app = $this->createMock(Application::class); + + $app->method('get')->willReturn($this->createMock(Route::class)); + $app + ->expects($this->exactly(1)) + ->method('get') + ->willReturnCallback(function (...$args) use ($routeUri, $templateName) { + $this->assertSame($routeUri, $args[0]); + $this->assertSame([[PageHandler::class]], [$args[1]]); + $this->assertSame($templateName, $args[2]); + }); + + $container->method('get')->with('config')->willReturn([ + 'routes' => [ + $moduleName => [ + $routeName => $routeName, + ], + ], + ]); + + $application = (new RoutesDelegator())( + $container, '', - function () { - return $this->createMock(Application::class); + $callback = function () use ($app) { + return $app; } ); From ceb606201ccc4e24270bafa26962607df48378d0 Mon Sep 17 00:00:00 2001 From: MarioRadu Date: Thu, 13 Feb 2025 15:22:45 +0200 Subject: [PATCH 7/8] replaced controllers with handlers & unit tests Signed-off-by: MarioRadu --- src/App/src/Factory/IndexHandlerFactory.php | 3 ++- src/Page/src/Factory/PageHandlerFactory.php | 1 + src/Page/src/Factory/PageServiceFactory.php | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/App/src/Factory/IndexHandlerFactory.php b/src/App/src/Factory/IndexHandlerFactory.php index b33b22b..2bccc6a 100644 --- a/src/App/src/Factory/IndexHandlerFactory.php +++ b/src/App/src/Factory/IndexHandlerFactory.php @@ -15,8 +15,9 @@ class IndexHandlerFactory { /** - * @throws NotFoundExceptionInterface + * @param class-string $requestedName * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ public function __invoke(ContainerInterface $container, string $requestedName): IndexHandler { diff --git a/src/Page/src/Factory/PageHandlerFactory.php b/src/Page/src/Factory/PageHandlerFactory.php index bebae30..e571d7f 100644 --- a/src/Page/src/Factory/PageHandlerFactory.php +++ b/src/Page/src/Factory/PageHandlerFactory.php @@ -15,6 +15,7 @@ class PageHandlerFactory { /** + * @param class-string $requestedName * @throws NotFoundExceptionInterface * @throws ContainerExceptionInterface */ diff --git a/src/Page/src/Factory/PageServiceFactory.php b/src/Page/src/Factory/PageServiceFactory.php index 92ca84b..61c26c4 100644 --- a/src/Page/src/Factory/PageServiceFactory.php +++ b/src/Page/src/Factory/PageServiceFactory.php @@ -10,6 +10,9 @@ class PageServiceFactory { + /** + * @param class-string $requestedName + */ public function __invoke(ContainerInterface $container, string $requestedName): PageServiceInterface { return new PageService(); From bc0446c984ee81d14b04da9ba4a2f3a0633184b5 Mon Sep 17 00:00:00 2001 From: MarioRadu Date: Fri, 14 Feb 2025 12:05:30 +0200 Subject: [PATCH 8/8] replaced controllers with handlers & unit tests Signed-off-by: MarioRadu --- src/App/templates/error/404.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App/templates/error/404.html.twig b/src/App/templates/error/404.html.twig index a08e96f..c13e27b 100644 --- a/src/App/templates/error/404.html.twig +++ b/src/App/templates/error/404.html.twig @@ -11,7 +11,7 @@

404

You are looking for something that doesn't exist or may have moved. Check out one of the links on this page - or head back to Home. + or head back to Home.