diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 83ef9b3..9652006 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -18,6 +18,7 @@ jobs: - "8.3" - "8.4" - "8.5" + - "8.6" steps: - name: Checkout diff --git a/.github/workflows/qodana_code_quality.yml b/.github/workflows/qodana_code_quality.yml index fbe7de0..899f98d 100644 --- a/.github/workflows/qodana_code_quality.yml +++ b/.github/workflows/qodana_code_quality.yml @@ -16,7 +16,7 @@ jobs: checks: write strategy: matrix: - php-versions: [ '8.3', '8.4', '8.5' ] + php-versions: [ '8.3', '8.4', '8.5', '8.6' ] steps: - uses: actions/checkout@v7 with: diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index 7390951..94c1ee7 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -4,7 +4,7 @@ on: name: Run PHPStan checks jobs: - mutation: + phpstan: name: PHPStan ${{ matrix.php }}-${{ matrix.os }} runs-on: ${{ matrix.os }} @@ -18,6 +18,7 @@ jobs: - "8.3" - "8.4" - "8.5" + - "8.6" steps: - name: Checkout diff --git a/CHANGELOG.md b/CHANGELOG.md index f33f4f8..7e5178c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,117 @@ # Changelog +## 1.6.0 - TBD + +### Changed + +* Widened `dotkernel/dot-errorhandler` to `^5.1.0 || ^6.0.0` +* Bumped the lower bounds of `laminas/*`, `mezzio/*` and dev dependencies +* Moved the page routes from `local.php.dist` to `Light\Page\ConfigProvider` +* `GetPageViewHandler` throws a `RuntimeException` when no route matched +* Masked session and authorization values in the error log +* Composer clears the configuration cache after install and update +* Tests use real `Route` and `RouteResult` objects +* PHPUnit fails on deprecations, notices, warnings and risky tests +* Vite config and npm vulnerability fixes by [@georgeStan](https://github.com/georgeStan) in [#120](https://github.com/dotkernel/light/pull/120) and [#123](https://github.com/dotkernel/light/pull/123) +* Aligned README with dotkernel.com wording by [@arhimede](https://github.com/arhimede) in [#119](https://github.com/dotkernel/light/pull/119) +* Renovate dependency and action updates in #118 to #127 + +### Added + +* Support for PHP 8.6 +* `dotkernel/dot-log`, `laminas/laminas-diactoros` and `laminas/laminas-servicemanager` as direct requirements +* Tests for the App module, the factories and the page routes + +### Deprecated + +* Nothing + +### Removed + +* Nothing + +### Fixed + +* `composer check` failed because the tests doubled final `mezzio-router` classes + +## 1.5.0 - 2026-08-03 + +### Changed + +* Issue [#72](https://github.com/dotkernel/light/issues/72): Bumped `phpunit/phpunit` to `^12.5.23` by [@OStefan2001](https://github.com/OStefan2001) in [#73](https://github.com/dotkernel/light/pull/73) +* Updated the Qodana action by [@arhimede](https://github.com/arhimede) in [#71](https://github.com/dotkernel/light/pull/71) and [#74](https://github.com/dotkernel/light/pull/74) +* Updated README and `package.json` by [@bidi47](https://github.com/bidi47) in [#95](https://github.com/dotkernel/light/pull/95) +* Cleaned branch `1.0` by [@georgeStan](https://github.com/georgeStan) in [#114](https://github.com/dotkernel/light/pull/114) +* Fixed ambiguities in README by [@arhimede](https://github.com/arhimede) in [#116](https://github.com/dotkernel/light/pull/116) +* Renovate dependency and action updates in #76 to #113 + +### Added + +* Issue [#97](https://github.com/dotkernel/light/issues/97): Workflow that builds the JS and CSS assets by [@alexmerlin](https://github.com/alexmerlin) in [#98](https://github.com/dotkernel/light/pull/98) +* Renovate configuration in [#75](https://github.com/dotkernel/light/pull/75) + +### Deprecated + +* Nothing + +### Removed + +* Nothing + +### Fixed + +* Nothing + +## 1.4.0 - 2025-10-31 + +### Changed + +* Issue [#61](https://github.com/dotkernel/light/issues/61): Increased PHPStan level by [@Howriq](https://github.com/Howriq) in [#63](https://github.com/dotkernel/light/pull/63) +* Updated the Qodana workflow by [@arhimede](https://github.com/arhimede) in [#64](https://github.com/dotkernel/light/pull/64) +* Updated the Mezzio version by [@arhimede](https://github.com/arhimede) in [#65](https://github.com/dotkernel/light/pull/65) +* Updated the Packagist dependency badge by [@arhimede](https://github.com/arhimede) in [#66](https://github.com/dotkernel/light/pull/66) + +### Added + +* Support for PHP 8.5 by [@arhimede](https://github.com/arhimede) in [#68](https://github.com/dotkernel/light/pull/68) + +### Deprecated + +* Nothing + +### Removed + +* Nothing + +### Fixed + +* Nothing + +## 1.3.0 - 2025-04-29 + +### Changed + +* Replaced webpack with Vite by [@georgeStan](https://github.com/georgeStan) in [#50](https://github.com/dotkernel/light/pull/50) +* Updated the title and added PSR-15 on the index page by [@arhimede](https://github.com/arhimede) in [#54](https://github.com/dotkernel/light/pull/54) +* Updated `.gitignore` by [@alexmerlin](https://github.com/alexmerlin) in [#55](https://github.com/dotkernel/light/pull/55) +* Updated the README section about static modules by [@bidi47](https://github.com/bidi47) in [#60](https://github.com/dotkernel/light/pull/60) + +### Added + +* Nothing + +### Deprecated + +* Nothing + +### Removed + +* Removed `.DS_Store` files by [@arhimede](https://github.com/arhimede) in [#56](https://github.com/dotkernel/light/pull/56) and [#57](https://github.com/dotkernel/light/pull/57) + +### Fixed + +* Nothing + ## 1.2.0 - 2025-03-03 Implemented `PSR-15`-compliant request handlers, which replace request controllers. diff --git a/README.md b/README.md index 38990b4..452d0e9 100644 --- a/README.md +++ b/README.md @@ -71,8 +71,8 @@ Documentation is available at: https://docs.dotkernel.org/light-documentation/ ## Requirements -- **PHP** 8.3, 8.4 or 8.5 (`~8.3.0 || ~8.4.0 || ~8.5.0`). - All three versions are covered by CI. +- **PHP** 8.3, 8.4, 8.5 or 8.6 (`~8.3.0 || ~8.4.0 || ~8.5.0 || ~8.6.0`). + All four versions are covered by CI. - **Node.js** `^20.19.0 || >=22.12.0` — required by the Vite and Sass versions used to build the interface. npm ships bundled with Node.js, so any supported Node.js release provides a compatible npm. @@ -143,7 +143,8 @@ cp config/autoload/local.php.dist config/autoload/local.php ``` > A Composer hook normally creates this file for you, but it is registered on `post-update-cmd` only, so it does not run for installs performed from an existing `composer.lock`. -> The file is git-ignored and holds your local settings — `application.url` and the page routes are defined here and nowhere else. +> The file is git-ignored and holds your local settings, for example `application.url`. +> The page routes are defined in `src/Page/src/ConfigProvider.php`. ## Development Mode @@ -209,6 +210,7 @@ You should see the `Dotkernel Light` welcome page. **NOTE:** +- `composer install` and `composer update` clear the configuration cache automatically. - If you are getting exceptions or errors regarding some missing services, clear the configuration cache: ```shell diff --git a/composer.json b/composer.json index 0fd72d2..ed5d328 100644 --- a/composer.json +++ b/composer.json @@ -26,24 +26,27 @@ } }, "require": { - "php": "~8.3.0 || ~8.4.0 || ~8.5.0", - "dotkernel/dot-errorhandler": "^5.0.0", - "laminas/laminas-component-installer": "^3.5.0", - "laminas/laminas-config-aggregator": "^1.17.0", - "mezzio/mezzio": "^3.24.0", - "mezzio/mezzio-fastroute": "^3.13.0", - "mezzio/mezzio-twigrenderer": "^2.17.0" + "php": "~8.3.0 || ~8.4.0 || ~8.5.0 || ~8.6.0", + "dotkernel/dot-log": "^5.4.0", + "dotkernel/dot-errorhandler": "^6.0.0", + "laminas/laminas-component-installer": "^3.9.0", + "laminas/laminas-config-aggregator": "^1.21.0", + "laminas/laminas-diactoros": "^3.10.0", + "laminas/laminas-servicemanager": "^4.6.0", + "mezzio/mezzio": "^3.29.0", + "mezzio/mezzio-fastroute": "^3.15.0", + "mezzio/mezzio-twigrenderer": "^2.21.0" }, "require-dev": { - "filp/whoops": "^2.17.0", - "laminas/laminas-coding-standard": "^3.0.1", - "laminas/laminas-development-mode": "^3.13.0", - "phpstan/phpstan": "^2.1.2", - "phpstan/phpstan-phpunit": "^2.0.4", + "filp/whoops": "^2.19.0", + "laminas/laminas-coding-standard": "^3.1.0", + "laminas/laminas-development-mode": "^3.16.0", + "phpstan/phpstan": "^2.4.0", + "phpstan/phpstan-phpunit": "^2.1.7", "phpunit/phpunit": "^12.5.23", "roave/security-advisories": "dev-master", - "symfony/var-dumper": "^7.2.3", - "vincentlanglet/twig-cs-fixer": "^4.0.0" + "symfony/var-dumper": "^7.2.3 || ^8.0.0", + "vincentlanglet/twig-cs-fixer": "^4.2.0" }, "autoload": { "psr-4": { @@ -61,7 +64,11 @@ "@development-enable" ], "post-update-cmd": [ - "php bin/composer-post-install-script.php" + "php bin/composer-post-install-script.php", + "@clear-config-cache" + ], + "post-install-cmd": [ + "@clear-config-cache" ], "development-disable": "laminas-development-mode disable", "development-enable": "laminas-development-mode enable", diff --git a/config/autoload/error-handling.global.php b/config/autoload/error-handling.global.php index bf31f2a..968e00b 100644 --- a/config/autoload/error-handling.global.php +++ b/config/autoload/error-handling.global.php @@ -1,23 +1,32 @@ 'dot-log.default_logger', ExtraProvider::CONFIG_KEY => [ CookieProvider::class => [ - 'enabled' => false, -// 'processor' => [ -// 'class' => \Dot\ErrorHandler\Extra\Processor\CookieProcessor::class, -// 'replacementStrategy' => \Dot\ErrorHandler\Extra\ReplacementStrategy::Full, -// 'sensitiveParameters' => [ -// \Dot\ErrorHandler\Extra\Processor\ProcessorInterface::ALL, -// ], -// ], + 'enabled' => false, + 'processor' => [ + 'class' => CookieProcessor::class, + 'replacementStrategy' => ReplacementStrategy::Full, + 'sensitiveParameters' => [ + ProcessorInterface::ALL, + ], + ], ], HeaderProvider::class => [ - 'enabled' => false, -// 'processor' => [ -// 'class' => \Dot\ErrorHandler\Extra\Processor\HeaderProcessor::class, -// 'replacementStrategy' => \Dot\ErrorHandler\Extra\ReplacementStrategy::Full, -// 'sensitiveParameters' => [], -// ], + 'enabled' => false, + 'processor' => [ + 'class' => HeaderProcessor::class, + 'replacementStrategy' => ReplacementStrategy::Full, + 'sensitiveParameters' => [ + 'authorization', + 'proxy-authorization', + ], + ], ], RequestProvider::class => [ - 'enabled' => false, -// 'processor' => [ -// 'class' => \Dot\ErrorHandler\Extra\Processor\RequestProcessor::class, -// 'replacementStrategy' => \Dot\ErrorHandler\Extra\ReplacementStrategy::Full, -// 'sensitiveParameters' => [ -// 'password', -// ], -// ], + 'enabled' => false, + 'processor' => [ + 'class' => RequestProcessor::class, + 'replacementStrategy' => ReplacementStrategy::Full, + 'sensitiveParameters' => [ + 'password', + ], + ], ], ServerProvider::class => [ - 'enabled' => true, -// 'processor' => [ -// 'class' => \Dot\ErrorHandler\Extra\Processor\ServerProcessor::class, -// 'replacementStrategy' => \Dot\ErrorHandler\Extra\ReplacementStrategy::Full, -// 'sensitiveParameters' => [ -// \Dot\ErrorHandler\Extra\Processor\ProcessorInterface::ALL, -// ], -// ], + 'enabled' => true, + 'processor' => [ + 'class' => ServerProcessor::class, + 'replacementStrategy' => ReplacementStrategy::Full, + 'sensitiveParameters' => [ + 'phpsessid', + 'php_auth_pw', + 'http_authorization', + 'http_proxy_authorization', + ], + ], ], SessionProvider::class => [ - 'enabled' => false, -// 'processor' => [ -// 'class' => \Dot\ErrorHandler\Extra\Processor\SessionProcessor::class, -// 'replacementStrategy' => \Dot\ErrorHandler\Extra\ReplacementStrategy::Full, -// 'sensitiveParameters' => [], -// ], + 'enabled' => false, + 'processor' => [ + 'class' => SessionProcessor::class, + ], ], TraceProvider::class => [ - 'enabled' => true, -// 'processor' => [ -// 'class' => \Dot\ErrorHandler\Extra\Processor\TraceProcessor::class, -// 'replacementStrategy' => \Dot\ErrorHandler\Extra\ReplacementStrategy::Full, -// 'sensitiveParameters' => [], -// ], + 'enabled' => true, + 'processor' => [ + 'class' => TraceProcessor::class, + ], ], ], ], diff --git a/config/autoload/local.php.dist b/config/autoload/local.php.dist index 7dba947..5b50b90 100644 --- a/config/autoload/local.php.dist +++ b/config/autoload/local.php.dist @@ -15,10 +15,4 @@ return [ 'application' => [ 'url' => $baseUrl, ], - 'routes' => [ - 'page' => [ - 'about' => 'about', - 'who-we-are' => 'who-we-are', - ], - ], ]; diff --git a/phpunit.xml b/phpunit.xml index b98b821..c894171 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -6,14 +6,17 @@ displayDetailsOnPhpunitNotices="true" displayDetailsOnTestsThatTriggerDeprecations="true" displayDetailsOnPhpunitDeprecations="true" - stopOnError="true"> + failOnDeprecation="true" + failOnNotice="true" + failOnWarning="true" + failOnRisky="true" + failOnPhpunitDeprecation="true"> ./test/Unit ./test/Unit/UnitTest.php - ./src diff --git a/src/App/src/ConfigProvider.php b/src/App/src/ConfigProvider.php index 82b5fcc..a096824 100644 --- a/src/App/src/ConfigProvider.php +++ b/src/App/src/ConfigProvider.php @@ -11,7 +11,7 @@ class ConfigProvider { /** - @return array{ + * @return array{ * dependencies: array, * templates: array, * } diff --git a/src/Page/src/ConfigProvider.php b/src/Page/src/ConfigProvider.php index 6041f62..396c8ea 100644 --- a/src/Page/src/ConfigProvider.php +++ b/src/Page/src/ConfigProvider.php @@ -14,8 +14,9 @@ class ConfigProvider { /** - @return array{ + * @return array{ * dependencies: array, + * routes: array, * templates: array, * } */ @@ -23,12 +24,13 @@ public function __invoke(): array { return [ 'dependencies' => $this->getDependencies(), + 'routes' => $this->getRoutes(), 'templates' => $this->getTemplates(), ]; } /** - @return array{ + * @return array{ * delegators: array>, * factories: array, * aliases: array @@ -53,7 +55,23 @@ public function getDependencies(): array } /** - @return array{ + * Page routes. Each key is the URI segment after "/page/", and each value is the template name. + * The RoutesDelegator registers each entry as "/page/" with the route name "page::". + * + * @return array{page: array} + */ + public function getRoutes(): array + { + return [ + 'page' => [ + 'about' => 'about', + 'who-we-are' => 'who-we-are', + ], + ]; + } + + /** + * @return array{ * paths: array{page: array{literal-string&non-falsy-string}} * } */ diff --git a/src/Page/src/Handler/GetPageViewHandler.php b/src/Page/src/Handler/GetPageViewHandler.php index 210bd6b..9e842fc 100644 --- a/src/Page/src/Handler/GetPageViewHandler.php +++ b/src/Page/src/Handler/GetPageViewHandler.php @@ -10,6 +10,7 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; +use RuntimeException; class GetPageViewHandler implements RequestHandlerInterface { @@ -20,10 +21,14 @@ public function __construct( public function handle(ServerRequestInterface $request): ResponseInterface { - $template = $request->getAttribute(RouteResult::class)->getMatchedRouteName(); + $routeResult = $request->getAttribute(RouteResult::class); + $routeName = $routeResult instanceof RouteResult ? $routeResult->getMatchedRouteName() : false; + if ($routeName === false || $routeName === '') { + throw new RuntimeException('The page handler needs a matched route. Route the request before dispatch.'); + } return new HtmlResponse( - $this->template->render($template) + $this->template->render($routeName) ); } } diff --git a/test/Unit/App/ConfigProviderTest.php b/test/Unit/App/ConfigProviderTest.php index 970ad88..523f07f 100644 --- a/test/Unit/App/ConfigProviderTest.php +++ b/test/Unit/App/ConfigProviderTest.php @@ -13,7 +13,7 @@ class ConfigProviderTest extends TestCase /** @var array>*/ protected array $config = []; - protected function setup(): void + protected function setUp(): void { parent::setUp(); diff --git a/test/Unit/App/Factory/GetIndexViewHandlerFactoryTest.php b/test/Unit/App/Factory/GetIndexViewHandlerFactoryTest.php new file mode 100644 index 0000000..5f3a5cf --- /dev/null +++ b/test/Unit/App/Factory/GetIndexViewHandlerFactoryTest.php @@ -0,0 +1,29 @@ +createMock(TemplateRendererInterface::class); + $template->expects($this->once()) + ->method('render') + ->with('app::index') + ->willReturn('

index

'); + $container = new InMemoryContainer([TemplateRendererInterface::class => $template]); + + $handler = (new GetIndexViewHandlerFactory())($container, GetIndexViewHandler::class); + + $this->assertSame('

index

', (string) $handler->handle(new ServerRequest())->getBody()); + } +} diff --git a/test/Unit/App/Handler/GetIndexViewHandlerTest.php b/test/Unit/App/Handler/GetIndexViewHandlerTest.php new file mode 100644 index 0000000..69d4238 --- /dev/null +++ b/test/Unit/App/Handler/GetIndexViewHandlerTest.php @@ -0,0 +1,28 @@ +createMock(TemplateRendererInterface::class); + $template->expects($this->once()) + ->method('render') + ->with('app::index') + ->willReturn('

index

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

index

', (string) $response->getBody()); + } +} diff --git a/test/Unit/App/RoutesDelegatorTest.php b/test/Unit/App/RoutesDelegatorTest.php new file mode 100644 index 0000000..653fb7c --- /dev/null +++ b/test/Unit/App/RoutesDelegatorTest.php @@ -0,0 +1,32 @@ +createStub(MiddlewareInterface::class), ['GET'], 'app::index'); + + $app = $this->createMock(Application::class); + $app->expects($this->once()) + ->method('get') + ->with('/', [GetIndexViewHandler::class], 'app::index') + ->willReturn($route); + + $callback = static fn (): Application => $app; + $result = (new RoutesDelegator())(new InMemoryContainer(), Application::class, $callback); + + $this->assertSame($app, $result); + } +} diff --git a/test/Unit/Fixture/InMemoryContainer.php b/test/Unit/Fixture/InMemoryContainer.php new file mode 100644 index 0000000..59fc9fe --- /dev/null +++ b/test/Unit/Fixture/InMemoryContainer.php @@ -0,0 +1,37 @@ + $services + */ + public function __construct( + private readonly array $services = [], + ) { + } + + public function get(string $id): mixed + { + if (! $this->has($id)) { + throw new ServiceNotFoundException($id); + } + + return $this->services[$id]; + } + + public function has(string $id): bool + { + return array_key_exists($id, $this->services); + } +} diff --git a/test/Unit/Fixture/ServiceNotFoundException.php b/test/Unit/Fixture/ServiceNotFoundException.php new file mode 100644 index 0000000..751514e --- /dev/null +++ b/test/Unit/Fixture/ServiceNotFoundException.php @@ -0,0 +1,18 @@ +>*/ protected array $config = []; - protected function setup(): void + protected function setUp(): void { parent::setUp(); @@ -59,6 +59,19 @@ public function testDependenciesHasAliases(): void $this->assertArrayHasKey(PageServiceInterface::class, $this->config['dependencies']['aliases']); } + public function testConfigHasPageRoutes(): void + { + $this->assertSame( + [ + 'page' => [ + 'about' => 'about', + 'who-we-are' => 'who-we-are', + ], + ], + $this->config['routes'] + ); + } + public function testGetTemplates(): void { $this->assertArrayHasKey('paths', $this->config['templates']); diff --git a/test/Unit/Page/Factory/GetPageViewHandlerFactoryTest.php b/test/Unit/Page/Factory/GetPageViewHandlerFactoryTest.php new file mode 100644 index 0000000..4932fc3 --- /dev/null +++ b/test/Unit/Page/Factory/GetPageViewHandlerFactoryTest.php @@ -0,0 +1,35 @@ +createMock(TemplateRendererInterface::class); + $template->expects($this->once()) + ->method('render') + ->with('page::about') + ->willReturn('

about

'); + $container = new InMemoryContainer([TemplateRendererInterface::class => $template]); + + $handler = (new GetPageViewHandlerFactory())($container, GetPageViewHandler::class); + + $route = new Route('/page/about', $this->createStub(MiddlewareInterface::class), ['GET'], 'page::about'); + $request = (new ServerRequest())->withAttribute(RouteResult::class, RouteResult::fromRoute($route)); + + $this->assertSame('

about

', (string) $handler->handle($request)->getBody()); + } +} diff --git a/test/Unit/Page/Handler/GetPageViewHandlerTest.php b/test/Unit/Page/Handler/GetPageViewHandlerTest.php new file mode 100644 index 0000000..7b9a258 --- /dev/null +++ b/test/Unit/Page/Handler/GetPageViewHandlerTest.php @@ -0,0 +1,57 @@ +createStub(MiddlewareInterface::class), ['GET'], $routeName); + $request = (new ServerRequest())->withAttribute(RouteResult::class, RouteResult::fromRoute($route)); + + $template = $this->createMock(TemplateRendererInterface::class); + $template->expects($this->once()) + ->method('render') + ->with($routeName) + ->willReturn('

' . $routeName . '

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

' . $routeName . '

', (string) $response->getBody()); + } + + public function testHandleThrowsWithoutRouteResult(): void + { + $handler = new GetPageViewHandler($this->createStub(TemplateRendererInterface::class)); + + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('The page handler needs a matched route.'); + + $handler->handle(new ServerRequest()); + } + + public function testHandleThrowsWhenRoutingFailed(): void + { + $handler = new GetPageViewHandler($this->createStub(TemplateRendererInterface::class)); + $request = (new ServerRequest())->withAttribute(RouteResult::class, RouteResult::fromRouteFailure(null)); + + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('The page handler needs a matched route.'); + + $handler->handle($request); + } +} diff --git a/test/Unit/Page/Handler/PageHandlerTest.php b/test/Unit/Page/Handler/PageHandlerTest.php deleted file mode 100644 index 61400db..0000000 --- a/test/Unit/Page/Handler/PageHandlerTest.php +++ /dev/null @@ -1,57 +0,0 @@ -createStub(GetPageViewHandler::class); - - $this->assertContainsOnlyInstancesOf(RequestHandlerInterface::class, [$handler]); - } - - /** - * @throws Exception - */ - public function testHandle(): void - { - $routeName = 'test_route_name'; - $request = $this->createStub(ServerRequestInterface::class); - $template = $this->createStub(TemplateRendererInterface::class); - $routeResult = $this->createStub(RouteResult::class); - - $routeResult - ->method('getMatchedRouteName') - ->willReturn($routeName); - - $request - ->method('getAttribute') - ->willReturn($routeResult); - - $template - ->method('render') - ->willReturn('

' . $routeName . '

'); - - $handler = new GetPageViewHandler($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 bd94d63..f9d3217 100644 --- a/test/Unit/Page/RoutesDelegatorTest.php +++ b/test/Unit/Page/RoutesDelegatorTest.php @@ -8,57 +8,36 @@ use Light\Page\RoutesDelegator; use Mezzio\Application; use Mezzio\Router\Route; -use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\TestCase; -use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; -use Psr\Container\NotFoundExceptionInterface; - -use function sprintf; +use Psr\Http\Server\MiddlewareInterface; class RoutesDelegatorTest extends TestCase { - /** - * @throws ContainerExceptionInterface - * @throws Exception - * @throws NotFoundExceptionInterface - */ - public function testWillInvoke(): void + public function testRegistersOneGetRoutePerConfiguredPage(): void { - $moduleName = 'test'; - $routeName = 'test_route_name'; - $routeUri = sprintf('/%s/%s', $moduleName, $routeName); - $templateName = sprintf('%s::%s', $moduleName, $routeName); - $container = $this->createStub(ContainerInterface::class); - $app = $this->createMock(Application::class); - - $app->method('get')->willReturn($this->createStub(Route::class)); - $app - ->expects($this->exactly(1)) - ->method('get') - ->willReturnCallback(function (...$args) use ($routeUri, $templateName) { - $this->assertSame($routeUri, $args[0]); - $this->assertSame([GetPageViewHandler::class], [$args[1]]); - $this->assertSame($templateName, $args[2]); - }); - $container->method('get')->willReturn([ 'routes' => [ - $moduleName => [ - $routeName => $routeName, - ], + 'page' => ['about' => 'about'], ], ]); - $application = (new RoutesDelegator())( - $container, - '', - $callback = function () use ($app) { - return $app; - } + $route = new Route( + '/page/about', + $this->createStub(MiddlewareInterface::class), + ['GET'], + 'page::about' ); - $this->assertContainsOnlyInstancesOf(Application::class, [$application]); + $app = $this->createMock(Application::class); + $app->expects($this->once()) + ->method('get') + ->with('/page/about', GetPageViewHandler::class, 'page::about') + ->willReturn($route); + + $result = (new RoutesDelegator())($container, Application::class, static fn (): Application => $app); + + $this->assertSame($app, $result); } }