From 512d9f4c46314e9bf8b60b819922032d0a1d4f67 Mon Sep 17 00:00:00 2001 From: Tom Mitchelmore Date: Tue, 26 Aug 2025 14:50:22 +0100 Subject: [PATCH 01/13] Upgrade dependencies --- composer.json | 22 +++++++++++----------- src/Route.php | 2 +- src/Router.php | 2 +- tests/ControllerTest.php | 19 +++++++++---------- tests/RouterDITest.php | 26 +++++++++++++------------- 5 files changed, 35 insertions(+), 36 deletions(-) diff --git a/composer.json b/composer.json index 058ee55..6c1b4c5 100755 --- a/composer.json +++ b/composer.json @@ -16,21 +16,21 @@ } ], "require": { - "php": "^7.3||^8.0", - "altorouter/altorouter": "^2.0.2", - "mindplay/middleman": "^3.0.3", - "php-di/invoker": "^2.3.0", - "psr/container": "^1.0", - "psr/http-message": "^1.0", + "php": "^8.1", + "altorouter/altorouter": "^2.0.3", + "laminas/laminas-diactoros": "^3.6.0", + "mindplay/middleman": "^4.0.4", + "php-di/invoker": "^2.3.6", + "psr/container": "^2.0.2", + "psr/http-message": "^2.0", "psr/http-server-middleware": "^1.0", - "spatie/macroable": "^1.0", - "laminas/laminas-diactoros": "^2.4" + "spatie/macroable": "^1.0" }, "require-dev": { - "php-di/php-di": "^6.3.4", - "phpunit/phpunit": "^9.5", - "php-coveralls/php-coveralls": "^2.4", "mockery/mockery": "^1.4.3", + "php-coveralls/php-coveralls": "^2.4", + "php-di/php-di": "^7.1.1", + "phpunit/phpunit": "^9.5", "squizlabs/php_codesniffer": "^3.6.0" }, "autoload": { diff --git a/src/Route.php b/src/Route.php index 9f74723..94d84d4 100644 --- a/src/Route.php +++ b/src/Route.php @@ -77,7 +77,7 @@ public function handle(ServerRequest $request, RouteParams $params): ResponseInt return $this->middlewareResolver->resolve($name); }); - return $dispatcher->dispatch($request); + return $dispatcher->handle($request); } private function gatherMiddleware(): array diff --git a/src/Router.php b/src/Router.php index 11d8374..27bfb03 100644 --- a/src/Router.php +++ b/src/Router.php @@ -194,7 +194,7 @@ function ($request) use ($route, $params) { return $this->middlewareResolver->resolve($name); }); - return $dispatcher->dispatch($request); + return $dispatcher->handle($request); } public function has(string $name) diff --git a/tests/ControllerTest.php b/tests/ControllerTest.php index 64f6b91..6355f08 100644 --- a/tests/ControllerTest.php +++ b/tests/ControllerTest.php @@ -3,7 +3,6 @@ namespace Rareloop\Router\Test; use Mockery; -use DI\ContainerBuilder; use PHPUnit\Framework\TestCase; use Rareloop\Router\Controller; use Rareloop\Router\ControllerMiddlewareOptions; @@ -18,7 +17,7 @@ class ControllerTest extends TestCase /** @test */ public function can_add_single_middleware_via_controller() { - $container = ContainerBuilder::buildDevContainer(); + $container = new \DI\Container(); $request = new ServerRequest([], [], '/test/123', 'GET'); $router = new Router($container); @@ -40,7 +39,7 @@ public function can_add_single_middleware_via_controller() /** @test */ public function can_resolve_middleware_on_a_controller_using_custom_resolver() { - $container = ContainerBuilder::buildDevContainer(); + $container = new \DI\Container(); $resolver = $this->createMockMiddlewareResolverWithHeader('X-Header', 'testing123'); $request = new ServerRequest([], [], '/test/123', 'GET'); $router = new Router($container, $resolver); @@ -63,7 +62,7 @@ public function can_resolve_middleware_on_a_controller_using_custom_resolver() /** @test */ public function can_add_multiple_middleware_as_array_via_controller() { - $container = ContainerBuilder::buildDevContainer(); + $container = new \DI\Container(); $request = new ServerRequest([], [], '/test/123', 'GET'); $router = new Router($container); @@ -100,7 +99,7 @@ public function controller_middleware_method_returns_options() /** @test */ public function middleware_can_be_limited_to_methods_using_only() { - $container = ContainerBuilder::buildDevContainer(); + $container = new \DI\Container(); $router = new Router($container); $controller = new MiddlewareProvidingController; @@ -119,7 +118,7 @@ public function middleware_can_be_limited_to_methods_using_only() /** @test */ public function middleware_can_be_limited_to_multiple_methods_using_only() { - $container = ContainerBuilder::buildDevContainer(); + $container = new \DI\Container(); $router = new Router($container); $controller = new MiddlewareProvidingController; @@ -138,7 +137,7 @@ public function middleware_can_be_limited_to_multiple_methods_using_only() /** @test */ public function middleware_can_be_limited_to_methods_using_except() { - $container = ContainerBuilder::buildDevContainer(); + $container = new \DI\Container(); $router = new Router($container); $controller = new MiddlewareProvidingController; @@ -157,7 +156,7 @@ public function middleware_can_be_limited_to_methods_using_except() /** @test */ public function middleware_can_be_limited_to_multiple_methods_using_except() { - $container = ContainerBuilder::buildDevContainer(); + $container = new \DI\Container(); $router = new Router($container); $controller = new MiddlewareProvidingController; @@ -188,10 +187,10 @@ protected function assertMiddlewareIsAppliedToMethods($router, $middlewareApplie $response = $router->match(new ServerRequest([], [], '/test/' . $method, 'GET')); if ($applied) { - $this->assertTrue($response->hasHeader('X-Header'), '`'.$method.'` should have middleware applied'); + $this->assertTrue($response->hasHeader('X-Header'), '`' . $method . '` should have middleware applied'); $this->assertSame('testing123', $response->getHeader('X-Header')[0]); } else { - $this->assertFalse($response->hasHeader('X-Header'), '`'.$method.'` should not have middleware applied'); + $this->assertFalse($response->hasHeader('X-Header'), '`' . $method . '` should not have middleware applied'); } } } diff --git a/tests/RouterDITest.php b/tests/RouterDITest.php index acbd433..807728f 100644 --- a/tests/RouterDITest.php +++ b/tests/RouterDITest.php @@ -16,7 +16,7 @@ class RouterDITest extends TestCase /** @test */ public function can_pass_a_container_into_constructor() { - $container = ContainerBuilder::buildDevContainer(); + $container = new \DI\Container(); $router = new Router($container); $this->assertInstanceOf(Router::class, $router); @@ -36,7 +36,7 @@ public function container_passed_to_constructor_must_be_psr_11_compatible() /** @test */ public function route_params_are_injected_into_closure() { - $container = ContainerBuilder::buildDevContainer(); + $container = new \DI\Container(); $router = new Router($container); $count = 0; @@ -60,7 +60,7 @@ public function route_params_are_injected_into_closure() /** @test */ public function typehints_are_injected_into_closure() { - $container = ContainerBuilder::buildDevContainer(); + $container = new \DI\Container(); $testServiceInstance = new TestService('abc123'); $container->set(TestService::class, $testServiceInstance); @@ -87,7 +87,7 @@ public function typehints_are_injected_into_closure() /** @test */ public function typehints_are_injected_into_closure_with_params() { - $container = ContainerBuilder::buildDevContainer(); + $container = new \DI\Container(); $testServiceInstance = new TestService('abc123'); $container->set(TestService::class, $testServiceInstance); @@ -116,7 +116,7 @@ public function typehints_are_injected_into_closure_with_params() /** @test */ public function route_params_are_injected_into_closure_regardless_of_param_order() { - $container = ContainerBuilder::buildDevContainer(); + $container = new \DI\Container(); $router = new Router($container); $count = 0; @@ -142,7 +142,7 @@ public function reflection_error_is_thrown_when_typehints_cant_be_resolved_from_ { $this->expectException(\ReflectionException::class); - $container = ContainerBuilder::buildDevContainer(); + $container = new \DI\Container(); $router = new Router($container); $router->get('/test/route', function (UndefinedType $test) {}); @@ -154,7 +154,7 @@ public function reflection_error_is_thrown_when_typehints_cant_be_resolved_from_ /** @test */ public function route_params_are_injected_into_controller_class() { - $container = ContainerBuilder::buildDevContainer(); + $container = new \DI\Container(); $router = new Router($container); $router->get('/posts/{postId}/comments/{commentId}', 'Rareloop\Router\Test\Controllers\TestController@expectsInjectedParams'); @@ -169,7 +169,7 @@ public function route_params_are_injected_into_controller_class() /** @test */ public function typehints_are_injected_into_controller_class() { - $container = ContainerBuilder::buildDevContainer(); + $container = new \DI\Container(); $testServiceInstance = new TestService('abc123'); $container->set(TestService::class, $testServiceInstance); @@ -187,7 +187,7 @@ public function typehints_are_injected_into_controller_class() /** @test */ public function typehints_are_injected_into_controller_class_with_params() { - $container = ContainerBuilder::buildDevContainer(); + $container = new \DI\Container(); $testServiceInstance = new TestService('abc123'); $container->set(TestService::class, $testServiceInstance); @@ -205,7 +205,7 @@ public function typehints_are_injected_into_controller_class_with_params() /** @test */ public function can_inject_request_object() { - $container = ContainerBuilder::buildDevContainer(); + $container = new \DI\Container(); $request = new ServerRequest([], [], '/test/route', 'GET'); $router = new Router($container); $count = 0; @@ -231,7 +231,7 @@ public function can_inject_request_object() /** @test */ public function can_inject_request_object_with_a_body() { - $container = ContainerBuilder::buildDevContainer(); + $container = new \DI\Container(); $request = new ServerRequest([], [], '/test/route', 'POST', 'php://input', [], [], [], 'post body'); $router = new Router($container); $count = 0; @@ -258,7 +258,7 @@ public function can_inject_request_object_with_a_body() /** @test */ public function can_inject_request_sub_class() { - $container = ContainerBuilder::buildDevContainer(); + $container = new \DI\Container(); $request = new ServerRequest([], [], '/test/route', 'GET'); $router = new Router($container); @@ -285,7 +285,7 @@ public function can_inject_request_sub_class() /** @test */ public function constructor_params_are_injected_into_controller_class() { - $container = ContainerBuilder::buildDevContainer(); + $container = new \DI\Container(); $router = new Router($container); $testServiceInstance = new TestService('abc123'); $container->set(TestService::class, $testServiceInstance); From 24951cd545e6c090b14f5408e5684ba67108f839 Mon Sep 17 00:00:00 2001 From: Tom Mitchelmore Date: Tue, 26 Aug 2025 14:51:44 +0100 Subject: [PATCH 02/13] Accept php 8.0 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6c1b4c5..fe735f6 100755 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ } ], "require": { - "php": "^8.1", + "php": "^8.0", "altorouter/altorouter": "^2.0.3", "laminas/laminas-diactoros": "^3.6.0", "mindplay/middleman": "^4.0.4", From 7e23ec398c330948213043c21e61e5fa4b46a5bc Mon Sep 17 00:00:00 2001 From: Tom Mitchelmore Date: Tue, 26 Aug 2025 15:01:53 +0100 Subject: [PATCH 03/13] Drop php 8.0 support --- .github/workflows/ci.yml | 75 ++++++++++++++++++++-------------------- composer.json | 2 +- 2 files changed, 38 insertions(+), 39 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1fb4f68..4ff63a7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,41 +3,40 @@ name: CI on: [push, pull_request] jobs: - build-test: - runs-on: ubuntu-latest - strategy: - matrix: - php_version: [8.0, 8.1, 8.2] - composer_flags: ['', '--prefer-lowest'] - - steps: - - uses: actions/checkout@v2 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php_version }} - extensions: xdebug - - - uses: php-actions/composer@v5 - with: - php_version: ${{ matrix.php_version }} - args: ${{ matrix.composer_flags }} - command: update - - - name: Run tests - run: ./vendor/bin/phpunit --coverage-clover ./tests/logs/clover.xml - env: - XDEBUG_MODE: coverage - - # - name: Submit coverage to Coveralls - # # We use php-coveralls library for this, as the official Coveralls GitHub Action lacks support for clover reports: - # # https://github.com/coverallsapp/github-action/issues/15 - # env: - # COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} - # COVERALLS_PARALLEL: true - # COVERALLS_FLAG_NAME: ${{ github.job }}-PHP-${{ matrix.php_version }} ${{ matrix.composer_flags }} - # run: | - # composer global require php-coveralls/php-coveralls - # ~/.composer/vendor/bin/php-coveralls -v - + build-test: + runs-on: ubuntu-latest + strategy: + matrix: + php_version: [8.1, 8.2] + composer_flags: ["", "--prefer-lowest"] + + steps: + - uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php_version }} + extensions: xdebug + + - uses: php-actions/composer@v5 + with: + php_version: ${{ matrix.php_version }} + args: ${{ matrix.composer_flags }} + command: update + + - name: Run tests + run: ./vendor/bin/phpunit --coverage-clover ./tests/logs/clover.xml + env: + XDEBUG_MODE: coverage + + # - name: Submit coverage to Coveralls + # # We use php-coveralls library for this, as the official Coveralls GitHub Action lacks support for clover reports: + # # https://github.com/coverallsapp/github-action/issues/15 + # env: + # COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} + # COVERALLS_PARALLEL: true + # COVERALLS_FLAG_NAME: ${{ github.job }}-PHP-${{ matrix.php_version }} ${{ matrix.composer_flags }} + # run: | + # composer global require php-coveralls/php-coveralls + # ~/.composer/vendor/bin/php-coveralls -v diff --git a/composer.json b/composer.json index fe735f6..6c1b4c5 100755 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ } ], "require": { - "php": "^8.0", + "php": "^8.1", "altorouter/altorouter": "^2.0.3", "laminas/laminas-diactoros": "^3.6.0", "mindplay/middleman": "^4.0.4", From 2cf89efeec263923af223c5db9c4c67fb08099d2 Mon Sep 17 00:00:00 2001 From: Tom Mitchelmore Date: Mon, 8 Sep 2025 10:03:12 +0100 Subject: [PATCH 04/13] Upgrade to php 8.4 --- composer.json | 3 +- phpunit.xml | 31 +++++-------- rector.php | 13 ++++++ src/Helpers/Formatting.php | 4 +- src/Route.php | 15 ++---- src/RouteAction.php | 28 +++++------ src/RouteGroup.php | 5 +- src/RouteParams.php | 4 +- src/Router.php | 20 ++++---- tests/ControllerTest.php | 4 +- .../TestConstructorParamController.php | 5 +- tests/Middleware/AddHeaderMiddleware.php | 7 +-- tests/RouteGroupTest.php | 8 +--- tests/RouteTest.php | 12 ++--- tests/RouterMiddlewareTest.php | 12 ++--- tests/RouterTest.php | 46 +++++-------------- tests/Services/TestService.php | 5 +- 17 files changed, 78 insertions(+), 144 deletions(-) create mode 100644 rector.php diff --git a/composer.json b/composer.json index 6c1b4c5..39707c5 100755 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ } ], "require": { - "php": "^8.1", + "php": "^8.4", "altorouter/altorouter": "^2.0.3", "laminas/laminas-diactoros": "^3.6.0", "mindplay/middleman": "^4.0.4", @@ -31,6 +31,7 @@ "php-coveralls/php-coveralls": "^2.4", "php-di/php-di": "^7.1.1", "phpunit/phpunit": "^9.5", + "rector/rector": "^2.1", "squizlabs/php_codesniffer": "^3.6.0" }, "autoload": { diff --git a/phpunit.xml b/phpunit.xml index 62992c9..7157380 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,22 +1,13 @@ - - - - tests - - - - - src/ - - + + + + src/ + + + + + tests + + diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..fd10b4d --- /dev/null +++ b/rector.php @@ -0,0 +1,13 @@ +withPaths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + ->withPhpSets(php84: true); diff --git a/src/Helpers/Formatting.php b/src/Helpers/Formatting.php index d784592..4a3c95c 100644 --- a/src/Helpers/Formatting.php +++ b/src/Helpers/Formatting.php @@ -6,7 +6,7 @@ class Formatting { public static function removeTrailingSlash($input) { - return rtrim($input, '/\\'); + return rtrim((string) $input, '/\\'); } public static function addTrailingSlash($input) @@ -16,7 +16,7 @@ public static function addTrailingSlash($input) public static function removeLeadingSlash($input) { - return ltrim($input, '/\\'); + return ltrim((string) $input, '/\\'); } public static function addLeadingSlash($input) diff --git a/src/Route.php b/src/Route.php index 94d84d4..60e5cc0 100644 --- a/src/Route.php +++ b/src/Route.php @@ -21,34 +21,27 @@ class Route use Macroable; private $uri; - private $methods = []; private $routeAction; private $name; - private $invoker = null; - private $middlewareResolver = null; private $middleware = []; private $paramConstraints = []; private $controllerName = null; private $controllerMethod = null; public function __construct( - array $methods, + private array $methods, string $uri, $action, - Invoker $invoker = null, - MiddlewareResolver $resolver = null + private ?\Rareloop\Router\Invoker $invoker = null, + private ?\Rareloop\Router\MiddlewareResolver $middlewareResolver = null ) { - $this->invoker = $invoker; - $this->middlewareResolver = $resolver; - - $this->methods = $methods; $this->setUri($uri); $this->setAction($action); } private function setUri($uri) { - $this->uri = rtrim($uri, ' /'); + $this->uri = rtrim((string) $uri, ' /'); } private function setAction($action) diff --git a/src/RouteAction.php b/src/RouteAction.php index 5bdb7eb..b48c948 100644 --- a/src/RouteAction.php +++ b/src/RouteAction.php @@ -29,7 +29,7 @@ class RouteAction * @param mixed $action * @param Rareloop/Router/Invoker $invoker */ - public function __construct($action, Invoker $invoker = null) + public function __construct($action, ?Invoker $invoker = null) { $this->invoker = $invoker; $this->callable = $this->createCallableFromAction($action); @@ -65,7 +65,7 @@ public function invoke(ServerRequestInterface $request, RouteParams $params) * @param mixed $action * @return callable */ - private function createCallableFromAction($action) : callable + private function createCallableFromAction($action): callable { // Check if this looks like it could be a class/method string if (!is_callable($action) && is_string($action)) { @@ -80,7 +80,7 @@ private function createCallableFromAction($action) : callable * * @return boolean */ - private function isControllerAction() : bool + private function isControllerAction(): bool { return !empty($this->controllerName) && !empty($this->controllerMethod); } @@ -127,7 +127,7 @@ private function createControllerFromClassName($className) * * @return bool */ - private function providesMiddleware() : bool + private function providesMiddleware(): bool { $controller = $this->getController(); @@ -143,7 +143,7 @@ private function providesMiddleware() : bool * * @return array */ - public function getMiddleware() : array + public function getMiddleware(): array { if (!$this->providesMiddleware()) { return []; @@ -151,15 +151,11 @@ public function getMiddleware() : array $allControllerMiddleware = array_filter( $this->getController()->getControllerMiddleware(), - function (ControllerMiddleware $middleware) { - return !$middleware->excludedForMethod($this->controllerMethod); - } + fn(ControllerMiddleware $middleware) => !$middleware->excludedForMethod($this->controllerMethod) ); return array_map( - function ($controllerMiddleware) { - return $controllerMiddleware->middleware(); - }, + fn($controllerMiddleware) => $controllerMiddleware->middleware(), $allControllerMiddleware ); } @@ -170,12 +166,12 @@ function ($controllerMiddleware) { * @param string $string e.g. `MyController@myMethod` * @return Closure */ - private function convertClassStringToFactory($string) : Closure + private function convertClassStringToFactory($string): Closure { $this->controllerName = null; $this->controllerMethod = null; - @list($className, $method) = explode('@', $string); + @[$className, $method] = explode('@', $string); if (!isset($className) || !isset($method)) { throw new RouteClassStringParseException('Could not parse route controller from string: `' . $string . '`'); @@ -204,9 +200,7 @@ private function convertClassStringToFactory($string) : Closure return [$controller, $method]; } - return function ($params = null) use ($controller, $method) { - return $controller->$method($params); - }; + return fn($params = null) => $controller->$method($params); }; } @@ -224,7 +218,7 @@ public function getActionName() } if (is_callable($this->callable, false, $callableName)) { - list($controller, $method) = explode('::', $callableName); + [$controller, $method] = explode('::', $callableName); if ($controller === 'Closure') { return $controller; diff --git a/src/RouteGroup.php b/src/RouteGroup.php index 54f3db5..30e47ac 100644 --- a/src/RouteGroup.php +++ b/src/RouteGroup.php @@ -9,12 +9,10 @@ class RouteGroup implements Routable { use VerbShortcutsTrait, Macroable; - - protected $router; protected $prefix; protected $middleware = []; - public function __construct($params, $router) + public function __construct($params, protected $router) { $prefix = null; $middleware = []; @@ -35,7 +33,6 @@ public function __construct($params, $router) } $this->prefix = is_string($prefix) ? trim($prefix, ' /') : null; - $this->router = $router; } private function appendPrefixToUri(string $uri) diff --git a/src/RouteParams.php b/src/RouteParams.php index 7c972bb..92bf421 100644 --- a/src/RouteParams.php +++ b/src/RouteParams.php @@ -5,11 +5,9 @@ class RouteParams implements \Iterator { private $position = 0; - private $params = []; - public function __construct(array $params) + public function __construct(private array $params) { - $this->params = $params; } public function __get($key) diff --git a/src/Router.php b/src/Router.php index 27bfb03..2d96f22 100644 --- a/src/Router.php +++ b/src/Router.php @@ -36,7 +36,7 @@ class Router implements Routable private $invoker = null; private $baseMiddleware = []; - public function __construct(ContainerInterface $container = null, MiddlewareResolver $resolver = null) + public function __construct(?ContainerInterface $container = null, ?MiddlewareResolver $resolver = null) { if (isset($container)) { $this->setContainer($container); @@ -79,7 +79,7 @@ private function convertRouteToAltoRouterUri(Route $route, AltoRouter $altoRoute { $output = $route->getUri(); - preg_match_all('/{\s*([a-zA-Z0-9]+\??)\s*}/s', $route->getUri(), $matches); + preg_match_all('/{\s*([a-zA-Z0-9]+\??)\s*}/s', (string) $route->getUri(), $matches); $paramConstraints = $route->getParamConstraints(); @@ -87,7 +87,7 @@ private function convertRouteToAltoRouterUri(Route $route, AltoRouter $altoRoute $match = $matches[0][$i]; $paramKey = $matches[1][$i]; - $optional = substr($paramKey, -1) === '?'; + $optional = str_ends_with($paramKey, '?'); $paramKey = trim($paramKey, '?'); $regex = $paramConstraints[$paramKey] ?? null; @@ -109,7 +109,7 @@ private function convertRouteToAltoRouterUri(Route $route, AltoRouter $altoRoute $output = str_replace($match, $replacement, $output); } - return ltrim($output, ' /'); + return ltrim((string) $output, ' /'); } public function map(array $verbs, string $uri, $callback): Route @@ -180,9 +180,7 @@ protected function handle($route, $request, $params) // Apply all the base middleware and trigger the route handler as the last in the chain $middlewares = array_merge($this->baseMiddleware, [ - function ($request) use ($route, $params) { - return $route->handle($request, $params); - }, + fn($request) => $route->handle($request, $params), ]); // Create and process the dispatcher @@ -199,9 +197,7 @@ function ($request) use ($route, $params) { public function has(string $name) { - $routes = array_filter($this->routes, function ($route) use ($name) { - return $route->getName() === $name; - }); + $routes = array_filter($this->routes, fn($route) => $route->getName() === $name); return count($routes) > 0; } @@ -227,7 +223,7 @@ public function url(string $name, $params = []) $regex = $paramConstraints[$key] ?? false; if ($regex) { - if (!preg_match('/' . $regex . '/', $value)) { + if (!preg_match('/' . $regex . '/', (string) $value)) { throw new RouteParamFailedConstraintException( 'Value `' . $value . '` for param `' . $key . '` fails constraint `' . $regex . '`' ); @@ -238,7 +234,7 @@ public function url(string $name, $params = []) try { return $this->altoRouter->generate($name, $params); - } catch (\Exception $e) { + } catch (\Exception) { throw new NamedRouteNotFoundException($name); } } diff --git a/tests/ControllerTest.php b/tests/ControllerTest.php index 6355f08..39a45ac 100644 --- a/tests/ControllerTest.php +++ b/tests/ControllerTest.php @@ -200,9 +200,7 @@ private function createMockMiddlewareResolverWithHeader($header, $value) $middleware = new AddHeaderMiddleware($header, $value); $resolver = Mockery::mock(MiddlewareResolver::class); $resolver->shouldReceive('resolve')->with('middleware-key')->andReturn($middleware); - $resolver->shouldReceive('resolve')->with(Mockery::type('callable'))->andReturnUsing(function ($argument) { - return $argument; - }); + $resolver->shouldReceive('resolve')->with(Mockery::type('callable'))->andReturnUsing(fn($argument) => $argument); return $resolver; } diff --git a/tests/Controllers/TestConstructorParamController.php b/tests/Controllers/TestConstructorParamController.php index 35ac1bc..89a8e9c 100644 --- a/tests/Controllers/TestConstructorParamController.php +++ b/tests/Controllers/TestConstructorParamController.php @@ -6,11 +6,8 @@ class TestConstructorParamController { - private $testService; - - public function __construct(TestService $testService) + public function __construct(private readonly TestService $testService) { - $this->testService = $testService; } public function returnTestServiceValue() diff --git a/tests/Middleware/AddHeaderMiddleware.php b/tests/Middleware/AddHeaderMiddleware.php index 4205d2b..b6b8cf7 100644 --- a/tests/Middleware/AddHeaderMiddleware.php +++ b/tests/Middleware/AddHeaderMiddleware.php @@ -10,13 +10,8 @@ class AddHeaderMiddleware implements MiddlewareInterface { - private $key; - private $value; - - public function __construct($key, $value) + public function __construct(private $key, private $value) { - $this->key = $key; - $this->value = $value; } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface diff --git a/tests/RouteGroupTest.php b/tests/RouteGroupTest.php index b334314..5fae11b 100644 --- a/tests/RouteGroupTest.php +++ b/tests/RouteGroupTest.php @@ -148,9 +148,7 @@ public function can_add_options_request_to_a_group() */ public function can_extend_post_behaviour_with_macros() { - RouteGroup::macro('testFunctionAddedByMacro', function () { - return 'abc123'; - }); + RouteGroup::macro('testFunctionAddedByMacro', fn() => 'abc123'); $queryBuilder = new RouteGroup([], new Router); @@ -175,8 +173,6 @@ class RouteGroupMixin { function testFunctionAddedByMixin() { - return function() { - return 'abc123'; - }; + return fn() => 'abc123'; } } diff --git a/tests/RouteTest.php b/tests/RouteTest.php index c5a7a03..a12b798 100644 --- a/tests/RouteTest.php +++ b/tests/RouteTest.php @@ -77,7 +77,7 @@ public function can_get_route_action_name_when_closure() public function can_get_route_action_name_when_callable() { $router = new Router; - $route = $router->get('test/123', [TestCallableController::class, 'testStatic']); + $route = $router->get('test/123', TestCallableController::testStatic(...)); $this->assertSame(TestCallableController::class.'@testStatic', $route->getActionName()); } @@ -87,7 +87,7 @@ public function can_get_route_action_name_when_callable_instance() { $router = new Router; $controller = new TestCallableController; - $route = $router->get('test/123', [$controller, 'test']); + $route = $router->get('test/123', $controller->test(...)); $this->assertSame(TestCallableController::class.'@test', $route->getActionName()); } @@ -106,9 +106,7 @@ public function can_get_route_action_name_when_controller_string() */ public function can_extend_post_behaviour_with_macros() { - Route::macro('testFunctionAddedByMacro', function () { - return 'abc123'; - }); + Route::macro('testFunctionAddedByMacro', fn() => 'abc123'); $queryBuilder = new Route(['GET'], '/test/url', function () {}); @@ -139,8 +137,6 @@ class RouteMixin { function testFunctionAddedByMixin() { - return function() { - return 'abc123'; - }; + return fn() => 'abc123'; } } diff --git a/tests/RouterMiddlewareTest.php b/tests/RouterMiddlewareTest.php index 8f6e2e3..5118692 100644 --- a/tests/RouterMiddlewareTest.php +++ b/tests/RouterMiddlewareTest.php @@ -133,9 +133,7 @@ public function can_add_middleware_to_a_group() $count++; $this->assertInstanceOf(RouteGroup::class, $group); - $group->get('all', function () { - return 'abc123'; - }); + $group->get('all', fn() => 'abc123'); }); $response = $router->match($request); @@ -157,9 +155,7 @@ public function can_add_single_middleware_to_a_group_without_wrapping_in_array() $count++; $this->assertInstanceOf(RouteGroup::class, $group); - $group->get('all', function () { - return 'abc123'; - }); + $group->get('all', fn() => 'abc123'); }); $response = $router->match($request); @@ -261,9 +257,7 @@ private function createMockMiddlewareResolverWithHeader($header, $value) $middleware = new AddHeaderMiddleware($header, $value); $resolver = Mockery::mock(MiddlewareResolver::class); $resolver->shouldReceive('resolve')->with('middleware-key')->andReturn($middleware); - $resolver->shouldReceive('resolve')->with(Mockery::type('callable'))->andReturnUsing(function ($argument) { - return $argument; - }); + $resolver->shouldReceive('resolve')->with(Mockery::type('callable'))->andReturnUsing(fn($argument) => $argument); return $resolver; } diff --git a/tests/RouterTest.php b/tests/RouterTest.php index 16184f7..3d57f8b 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -545,7 +545,7 @@ public function can_generate_canonical_uri_after_match_has_been_called() $route = $router->get('/posts/all', function () {})->name('test.name'); $request = new ServerRequest([], [], '/does/not/match', 'GET'); - $router->match($request, 'GET'); + $router->match($request); $this->assertSame('/posts/all/', $router->url('test.name')); } @@ -588,9 +588,7 @@ public function can_add_routes_in_a_group() $count++; $this->assertInstanceOf(RouteGroup::class, $group); - $group->get('all', function () { - return 'abc123'; - }); + $group->get('all', fn() => 'abc123'); }); $response = $router->match($request); @@ -610,9 +608,7 @@ public function can_add_routes_in_a_group_using_array_as_first_param() $count++; $this->assertInstanceOf(RouteGroup::class, $group); - $group->get('all', function () { - return 'abc123'; - }); + $group->get('all', fn() => 'abc123'); }); $response = $router->match($request); @@ -632,9 +628,7 @@ public function can_add_routes_in_a_group_using_array_as_first_param_with_no_pre $count++; $this->assertInstanceOf(RouteGroup::class, $group); - $group->get('all', function () { - return 'abc123'; - }); + $group->get('all', fn() => 'abc123'); }); $response = $router->match($request); @@ -654,9 +648,7 @@ public function group_prefixes_work_with_leading_slash() $count++; $this->assertInstanceOf(RouteGroup::class, $group); - $group->get('all', function () { - return 'abc123'; - }); + $group->get('all', fn() => 'abc123'); }); $response = $router->match($request); @@ -676,9 +668,7 @@ public function group_prefixes_work_with_trailing_slash() $count++; $this->assertInstanceOf(RouteGroup::class, $group); - $group->get('all', function () { - return 'abc123'; - }); + $group->get('all', fn() => 'abc123'); }); $response = $router->match($request); @@ -702,9 +692,7 @@ public function can_add_routes_in_nested_groups() $count++; $this->assertInstanceOf(RouteGroup::class, $group); - $group->get('all', function () { - return 'abc123'; - }); + $group->get('all', fn() => 'abc123'); }); }); $response = $router->match($request); @@ -729,9 +717,7 @@ public function can_add_routes_in_nested_groups_with_array_syntax() $count++; $this->assertInstanceOf(RouteGroup::class, $group); - $group->get('all', function () { - return 'abc123'; - }); + $group->get('all', fn() => 'abc123'); }); }); $response = $router->match($request); @@ -912,9 +898,7 @@ public function current_route_name_returns_null_when_match_not_yet_called() $request = new ServerRequest([], [], '/test/123', 'GET'); $router = new Router; - $route = $router->get('/test/123', function () { - return 'abc123'; - })->name('test123'); + $route = $router->get('/test/123', fn() => 'abc123')->name('test123'); $this->assertSame(null, $router->currentRouteName()); } @@ -925,9 +909,7 @@ public function current_route_name_returns_null_when_matched_route_has_no_name() $request = new ServerRequest([], [], '/test/123', 'GET'); $router = new Router; - $route = $router->get('/test/123', function () { - return 'abc123'; - }); + $route = $router->get('/test/123', fn() => 'abc123'); $response = $router->match($request); @@ -953,9 +935,7 @@ public function can_get_list_of_registered_routes() */ public function can_extend_post_behaviour_with_macros() { - Router::macro('testFunctionAddedByMacro', function () { - return 'abc123'; - }); + Router::macro('testFunctionAddedByMacro', fn() => 'abc123'); $queryBuilder = new Router(); @@ -980,8 +960,6 @@ class RouterMixin { function testFunctionAddedByMixin() { - return function() { - return 'abc123'; - }; + return fn() => 'abc123'; } } diff --git a/tests/Services/TestService.php b/tests/Services/TestService.php index 6007d6c..a61349e 100644 --- a/tests/Services/TestService.php +++ b/tests/Services/TestService.php @@ -4,10 +4,7 @@ class TestService { - public $value; - - public function __construct($value) + public function __construct(public $value) { - $this->value = $value; } } From 3b663d4f3e444b725acb253da45b044c7f5b3efa Mon Sep 17 00:00:00 2001 From: Tom Mitchelmore Date: Mon, 8 Sep 2025 11:37:14 +0100 Subject: [PATCH 05/13] Remove support for first class callables --- tests/RouteTest.php | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/tests/RouteTest.php b/tests/RouteTest.php index a12b798..534e144 100644 --- a/tests/RouteTest.php +++ b/tests/RouteTest.php @@ -77,28 +77,18 @@ public function can_get_route_action_name_when_closure() public function can_get_route_action_name_when_callable() { $router = new Router; - $route = $router->get('test/123', TestCallableController::testStatic(...)); + $route = $router->get('test/123', [TestCallableController::class, 'testStatic']); - $this->assertSame(TestCallableController::class.'@testStatic', $route->getActionName()); - } - - /** @test */ - public function can_get_route_action_name_when_callable_instance() - { - $router = new Router; - $controller = new TestCallableController; - $route = $router->get('test/123', $controller->test(...)); - - $this->assertSame(TestCallableController::class.'@test', $route->getActionName()); + $this->assertSame(TestCallableController::class . '@testStatic', $route->getActionName()); } /** @test */ public function can_get_route_action_name_when_controller_string() { $router = new Router; - $route = $router->get('test/123', TestCallableController::class.'@test'); + $route = $router->get('test/123', TestCallableController::class . '@test'); - $this->assertSame(TestCallableController::class.'@test', $route->getActionName()); + $this->assertSame(TestCallableController::class . '@test', $route->getActionName()); } /** From d57f9e12bc43738f697c71dbdd627b893bece2f9 Mon Sep 17 00:00:00 2001 From: Tom Mitchelmore Date: Mon, 8 Sep 2025 11:43:17 +0100 Subject: [PATCH 06/13] Update CI --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4ff63a7..ebfb3e7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php_version: [8.1, 8.2] + php_version: [8.4] composer_flags: ["", "--prefer-lowest"] steps: From 91f70567d110896f04d35d6ed9ff2aac1af65617 Mon Sep 17 00:00:00 2001 From: Tom Mitchelmore Date: Mon, 8 Sep 2025 11:51:20 +0100 Subject: [PATCH 07/13] Upgrade to phpunit 12 --- composer.json | 2 +- phpunit.xml | 12 +-- tests/ControllerMiddlewareOptionsTest.php | 15 +-- tests/ControllerMiddlewareTest.php | 5 +- tests/ControllerTest.php | 17 ++-- tests/FormattingTest.php | 13 +-- tests/ResponseFactoryTest.php | 9 +- tests/RouteGroupTest.php | 17 ++-- tests/RouteTest.php | 19 ++-- tests/RouterDITest.php | 29 +++--- tests/RouterMiddlewareTest.php | 23 ++--- tests/RouterParamsTest.php | 9 +- tests/RouterTest.php | 115 +++++++++++----------- tests/TypeHintRequestResolverTest.php | 7 +- 14 files changed, 152 insertions(+), 140 deletions(-) diff --git a/composer.json b/composer.json index 39707c5..8c06079 100755 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "mockery/mockery": "^1.4.3", "php-coveralls/php-coveralls": "^2.4", "php-di/php-di": "^7.1.1", - "phpunit/phpunit": "^9.5", + "phpunit/phpunit": "^12", "rector/rector": "^2.1", "squizlabs/php_codesniffer": "^3.6.0" }, diff --git a/phpunit.xml b/phpunit.xml index 7157380..18d4d24 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,13 +1,13 @@ - - - - src/ - - + tests + + + src/ + + diff --git a/tests/ControllerMiddlewareOptionsTest.php b/tests/ControllerMiddlewareOptionsTest.php index a5d340b..c2cc845 100644 --- a/tests/ControllerMiddlewareOptionsTest.php +++ b/tests/ControllerMiddlewareOptionsTest.php @@ -2,12 +2,13 @@ namespace Rareloop\Router\Test; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Rareloop\Router\ControllerMiddlewareOptions; class ControllerMiddlewareOptionsTest extends TestCase { - /** @test */ + #[Test] public function by_default_no_methods_are_excluded() { $options = new ControllerMiddlewareOptions; @@ -16,7 +17,7 @@ public function by_default_no_methods_are_excluded() $this->assertFalse($options->excludedForMethod('bar')); } - /** @test */ + #[Test] public function only_is_chainable() { $options = new ControllerMiddlewareOptions; @@ -24,7 +25,7 @@ public function only_is_chainable() $this->assertSame($options, $options->only('foo')); } - /** @test */ + #[Test] public function can_use_only_to_limit_methods() { $options = new ControllerMiddlewareOptions; @@ -35,7 +36,7 @@ public function can_use_only_to_limit_methods() $this->assertTrue($options->excludedForMethod('bar')); } - /** @test */ + #[Test] public function can_use_only_to_limit_multiple_methods() { $options = new ControllerMiddlewareOptions; @@ -47,7 +48,7 @@ public function can_use_only_to_limit_multiple_methods() $this->assertTrue($options->excludedForMethod('baz')); } - /** @test */ + #[Test] public function except_is_chainable() { $options = new ControllerMiddlewareOptions; @@ -55,7 +56,7 @@ public function except_is_chainable() $this->assertSame($options, $options->except('foo')); } - /** @test */ + #[Test] public function can_use_except_to_limit_methods() { $options = new ControllerMiddlewareOptions; @@ -66,7 +67,7 @@ public function can_use_except_to_limit_methods() $this->assertFalse($options->excludedForMethod('bar')); } - /** @test */ + #[Test] public function can_use_except_to_limit_multiple_methods() { $options = new ControllerMiddlewareOptions; diff --git a/tests/ControllerMiddlewareTest.php b/tests/ControllerMiddlewareTest.php index 114cca6..66e29e9 100644 --- a/tests/ControllerMiddlewareTest.php +++ b/tests/ControllerMiddlewareTest.php @@ -6,10 +6,11 @@ use Rareloop\Router\ControllerMiddleware; use Rareloop\Router\ControllerMiddlewareOptions; use Rareloop\Router\Test\Middleware\AddHeaderMiddleware; +use PHPUnit\Framework\Attributes\Test; class ControllerMiddlewareTest extends TestCase { - /** @test */ + #[Test] public function can_retrieve_middleware() { $middleware = new AddHeaderMiddleware('X-Header', 'testing123'); @@ -20,7 +21,7 @@ public function can_retrieve_middleware() $this->assertSame($middleware, $controllerMiddleware->middleware()); } - /** @test */ + #[Test] public function can_retrieve_options() { $middleware = new AddHeaderMiddleware('X-Header', 'testing123'); diff --git a/tests/ControllerTest.php b/tests/ControllerTest.php index 39a45ac..809b886 100644 --- a/tests/ControllerTest.php +++ b/tests/ControllerTest.php @@ -11,10 +11,11 @@ use Rareloop\Router\Test\Controllers\MiddlewareProvidingController; use Rareloop\Router\Test\Middleware\AddHeaderMiddleware; use Laminas\Diactoros\ServerRequest; +use PHPUnit\Framework\Attributes\Test; class ControllerTest extends TestCase { - /** @test */ + #[Test] public function can_add_single_middleware_via_controller() { $container = new \DI\Container(); @@ -36,7 +37,7 @@ public function can_add_single_middleware_via_controller() $this->assertSame('testing123', $response->getHeader('X-Header')[0]); } - /** @test */ + #[Test] public function can_resolve_middleware_on_a_controller_using_custom_resolver() { $container = new \DI\Container(); @@ -59,7 +60,7 @@ public function can_resolve_middleware_on_a_controller_using_custom_resolver() $this->assertSame('testing123', $response->getHeader('X-Header')[0]); } - /** @test */ + #[Test] public function can_add_multiple_middleware_as_array_via_controller() { $container = new \DI\Container(); @@ -86,7 +87,7 @@ public function can_add_multiple_middleware_as_array_via_controller() $this->assertSame('testing456', $response->getHeader('X-Header-2')[0]); } - /** @test */ + #[Test] public function controller_middleware_method_returns_options() { $controller = new MiddlewareProvidingController; @@ -96,7 +97,7 @@ public function controller_middleware_method_returns_options() $this->assertInstanceOf(ControllerMiddlewareOptions::class, $options); } - /** @test */ + #[Test] public function middleware_can_be_limited_to_methods_using_only() { $container = new \DI\Container(); @@ -115,7 +116,7 @@ public function middleware_can_be_limited_to_methods_using_only() $this->assertMiddlewareIsAppliedToMethods($router, $middlewareAppliedToMethods); } - /** @test */ + #[Test] public function middleware_can_be_limited_to_multiple_methods_using_only() { $container = new \DI\Container(); @@ -134,7 +135,7 @@ public function middleware_can_be_limited_to_multiple_methods_using_only() $this->assertMiddlewareIsAppliedToMethods($router, $middlewareAppliedToMethods); } - /** @test */ + #[Test] public function middleware_can_be_limited_to_methods_using_except() { $container = new \DI\Container(); @@ -153,7 +154,7 @@ public function middleware_can_be_limited_to_methods_using_except() $this->assertMiddlewareIsAppliedToMethods($router, $middlewareAppliedToMethods); } - /** @test */ + #[Test] public function middleware_can_be_limited_to_multiple_methods_using_except() { $container = new \DI\Container(); diff --git a/tests/FormattingTest.php b/tests/FormattingTest.php index fa718c3..c0a9dac 100644 --- a/tests/FormattingTest.php +++ b/tests/FormattingTest.php @@ -4,10 +4,11 @@ use PHPUnit\Framework\TestCase; use Rareloop\Router\Helpers\Formatting; +use PHPUnit\Framework\Attributes\Test; class FormattingTest extends TestCase { - /** @test */ + #[Test] public function can_remove_trialing_slash() { $string = 'string/'; @@ -15,7 +16,7 @@ public function can_remove_trialing_slash() $this->assertSame('string', Formatting::removeTrailingSlash($string)); } - /** @test */ + #[Test] public function can_add_trialing_slash() { $string = 'string'; @@ -23,7 +24,7 @@ public function can_add_trialing_slash() $this->assertSame('string/', Formatting::addTrailingSlash($string)); } - /** @test */ + #[Test] public function add_trialing_slash_does_not_produce_duplicates() { $string = 'string/'; @@ -31,7 +32,7 @@ public function add_trialing_slash_does_not_produce_duplicates() $this->assertSame('string/', Formatting::addTrailingSlash($string)); } - /** @test */ + #[Test] public function can_remove_leading_slash() { $string = '/string'; @@ -39,7 +40,7 @@ public function can_remove_leading_slash() $this->assertSame('string', Formatting::removeLeadingSlash($string)); } - /** @test */ + #[Test] public function can_add_leading_slash() { $string = 'string'; @@ -47,7 +48,7 @@ public function can_add_leading_slash() $this->assertSame('/string', Formatting::addLeadingSlash($string)); } - /** @test */ + #[Test] public function add_leading_slash_does_not_produce_duplicates() { $string = '/string'; diff --git a/tests/ResponseFactoryTest.php b/tests/ResponseFactoryTest.php index 3cc2eaf..d596149 100644 --- a/tests/ResponseFactoryTest.php +++ b/tests/ResponseFactoryTest.php @@ -10,6 +10,7 @@ use Laminas\Diactoros\Response\EmptyResponse; use Laminas\Diactoros\Response\TextResponse; use Laminas\Diactoros\ServerRequest; +use PHPUnit\Framework\Attributes\Test; class ResponseFactoryTest extends TestCase { @@ -24,7 +25,7 @@ public function setUp(): void $this->request = new ServerRequest([], [], '/test/123', 'GET'); } - /** @test */ + #[Test] public function when_passed_a_response_instance_the_same_object_is_returned() { $response = new TextResponse('Testing', 200); @@ -32,7 +33,7 @@ public function when_passed_a_response_instance_the_same_object_is_returned() $this->assertSame($response, ResponseFactory::create($this->request, $response)); } - /** @test */ + #[Test] public function when_passed_a_non_response_instance_a_response_object_is_returned() { $response = ResponseFactory::create($this->request, 'Testing'); @@ -41,7 +42,7 @@ public function when_passed_a_non_response_instance_a_response_object_is_returne $this->assertSame('Testing', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function when_nothing_is_passed_an_empty_response_object_is_returned() { $response = ResponseFactory::create($this->request); @@ -49,7 +50,7 @@ public function when_nothing_is_passed_an_empty_response_object_is_returned() $this->assertInstanceOf(EmptyResponse::class, $response); } - /** @test */ + #[Test] public function when_a_responsable_object_is_passed_the_response_object_is_returned() { $textResponse = new TextResponse('testing123'); diff --git a/tests/RouteGroupTest.php b/tests/RouteGroupTest.php index 5fae11b..1b7a43d 100644 --- a/tests/RouteGroupTest.php +++ b/tests/RouteGroupTest.php @@ -6,10 +6,11 @@ use Rareloop\Router\Route; use Rareloop\Router\RouteGroup; use Rareloop\Router\Router; +use PHPUnit\Framework\Attributes\Test; class RouteGroupTest extends TestCase { - /** @test */ + #[Test] public function group_function_is_chainable() { $router = new Router; @@ -17,7 +18,7 @@ public function group_function_is_chainable() $this->assertInstanceOf(Router::class, $router->group('test/123', function () {})); } - /** @test */ + #[Test] public function can_add_get_request_to_a_group() { $router = new Router; @@ -35,7 +36,7 @@ public function can_add_get_request_to_a_group() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_add_request_to_a_group_with_leading_slash() { $router = new Router; @@ -53,7 +54,7 @@ public function can_add_request_to_a_group_with_leading_slash() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_add_post_request_to_a_group() { $router = new Router; @@ -71,7 +72,7 @@ public function can_add_post_request_to_a_group() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_add_put_request_to_a_group() { $router = new Router; @@ -89,7 +90,7 @@ public function can_add_put_request_to_a_group() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_add_patch_request_to_a_group() { $router = new Router; @@ -107,7 +108,7 @@ public function can_add_patch_request_to_a_group() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_add_delete_request_to_a_group() { $router = new Router; @@ -125,7 +126,7 @@ public function can_add_delete_request_to_a_group() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_add_options_request_to_a_group() { $router = new Router; diff --git a/tests/RouteTest.php b/tests/RouteTest.php index 534e144..a9ebeab 100644 --- a/tests/RouteTest.php +++ b/tests/RouteTest.php @@ -3,6 +3,7 @@ namespace Rareloop\Router\Test; use InvalidArgumentException; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Rareloop\Router\Exceptions\RouteNameRedefinedException; use Rareloop\Router\Route; @@ -10,7 +11,7 @@ class RouteTest extends TestCase { - /** @test */ + #[Test] public function a_route_can_be_named() { $router = new Router; @@ -20,7 +21,7 @@ public function a_route_can_be_named() $this->assertTrue($router->has('test')); } - /** @test */ + #[Test] public function name_function_is_chainable() { $router = new Router; @@ -28,7 +29,7 @@ public function name_function_is_chainable() $this->assertInstanceOf(Route::class, $router->get('test/123', function () {})->name('test')); } - /** @test */ + #[Test] public function a_route_can_not_be_renamed() { $this->expectException(RouteNameRedefinedException::class); @@ -38,7 +39,7 @@ public function a_route_can_not_be_renamed() $route = $router->get('test/123', function () {})->name('test1')->name('test2'); } - /** @test */ + #[Test] public function where_function_is_chainable() { $router = new Router; @@ -46,7 +47,7 @@ public function where_function_is_chainable() $this->assertInstanceOf(Route::class, $router->get('test/{id}', function () {})->where('id', '[0-9]+')); } - /** @test */ + #[Test] public function where_function_is_chainable_when_passed_an_array() { $router = new Router; @@ -54,7 +55,7 @@ public function where_function_is_chainable_when_passed_an_array() $this->assertInstanceOf(Route::class, $router->get('test/{id}', function () {})->where(['id' => '[0-9]+'])); } - /** @test */ + #[Test] public function where_function_throws_exception_when_no_params_provided() { $this->expectException(InvalidArgumentException::class); @@ -64,7 +65,7 @@ public function where_function_throws_exception_when_no_params_provided() $this->assertInstanceOf(Route::class, $router->get('test/{id}', function () {})->where()); } - /** @test */ + #[Test] public function can_get_route_action_name_when_closure() { $router = new Router; @@ -73,7 +74,7 @@ public function can_get_route_action_name_when_closure() $this->assertSame('Closure', $route->getActionName()); } - /** @test */ + #[Test] public function can_get_route_action_name_when_callable() { $router = new Router; @@ -82,7 +83,7 @@ public function can_get_route_action_name_when_callable() $this->assertSame(TestCallableController::class . '@testStatic', $route->getActionName()); } - /** @test */ + #[Test] public function can_get_route_action_name_when_controller_string() { $router = new Router; diff --git a/tests/RouterDITest.php b/tests/RouterDITest.php index 807728f..01abe2f 100644 --- a/tests/RouterDITest.php +++ b/tests/RouterDITest.php @@ -10,10 +10,11 @@ use Rareloop\Router\Test\Requests\TestRequest; use Rareloop\Router\Test\Services\TestService; use Laminas\Diactoros\ServerRequest; +use PHPUnit\Framework\Attributes\Test; class RouterDITest extends TestCase { - /** @test */ + #[Test] public function can_pass_a_container_into_constructor() { $container = new \DI\Container(); @@ -22,7 +23,7 @@ public function can_pass_a_container_into_constructor() $this->assertInstanceOf(Router::class, $router); } - /** @test */ + #[Test] public function container_passed_to_constructor_must_be_psr_11_compatible() { $this->expectException(\TypeError::class); @@ -33,7 +34,7 @@ public function container_passed_to_constructor_must_be_psr_11_compatible() $this->assertInstanceOf(Router::class, $router); } - /** @test */ + #[Test] public function route_params_are_injected_into_closure() { $container = new \DI\Container(); @@ -57,7 +58,7 @@ public function route_params_are_injected_into_closure() $this->assertSame('abc', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function typehints_are_injected_into_closure() { $container = new \DI\Container(); @@ -84,7 +85,7 @@ public function typehints_are_injected_into_closure() $this->assertSame('abc', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function typehints_are_injected_into_closure_with_params() { $container = new \DI\Container(); @@ -113,7 +114,7 @@ public function typehints_are_injected_into_closure_with_params() $this->assertSame('abc', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function route_params_are_injected_into_closure_regardless_of_param_order() { $container = new \DI\Container(); @@ -137,7 +138,7 @@ public function route_params_are_injected_into_closure_regardless_of_param_order $this->assertSame('abc', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function reflection_error_is_thrown_when_typehints_cant_be_resolved_from_the_container() { $this->expectException(\ReflectionException::class); @@ -151,7 +152,7 @@ public function reflection_error_is_thrown_when_typehints_cant_be_resolved_from_ $response = $router->match($request); } - /** @test */ + #[Test] public function route_params_are_injected_into_controller_class() { $container = new \DI\Container(); @@ -166,7 +167,7 @@ public function route_params_are_injected_into_controller_class() $this->assertSame('$postId: 1 $commentId: 2', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function typehints_are_injected_into_controller_class() { $container = new \DI\Container(); @@ -184,7 +185,7 @@ public function typehints_are_injected_into_controller_class() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function typehints_are_injected_into_controller_class_with_params() { $container = new \DI\Container(); @@ -202,7 +203,7 @@ public function typehints_are_injected_into_controller_class_with_params() $this->assertSame('$postId: 1 $commentId: 2 TestService: abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_inject_request_object() { $container = new \DI\Container(); @@ -228,7 +229,7 @@ public function can_inject_request_object() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_inject_request_object_with_a_body() { $container = new \DI\Container(); @@ -255,7 +256,7 @@ public function can_inject_request_object_with_a_body() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_inject_request_sub_class() { $container = new \DI\Container(); @@ -282,7 +283,7 @@ public function can_inject_request_sub_class() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function constructor_params_are_injected_into_controller_class() { $container = new \DI\Container(); diff --git a/tests/RouterMiddlewareTest.php b/tests/RouterMiddlewareTest.php index 5118692..9ce57df 100644 --- a/tests/RouterMiddlewareTest.php +++ b/tests/RouterMiddlewareTest.php @@ -14,10 +14,11 @@ use Rareloop\Router\Test\Controllers\MiddlewareProvidingController; use Rareloop\Router\Test\Middleware\AddHeaderMiddleware; use Laminas\Diactoros\ServerRequest; +use PHPUnit\Framework\Attributes\Test; class RouterMiddlewareTest extends TestCase { - /** @test */ + #[Test] public function can_add_middleware_as_a_closure_to_a_route() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -43,7 +44,7 @@ public function can_add_middleware_as_a_closure_to_a_route() $this->assertSame('value', $response->getHeader('X-Key')[0]); } - /** @test */ + #[Test] public function can_add_middleware_as_an_object_to_a_route() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -63,7 +64,7 @@ public function can_add_middleware_as_an_object_to_a_route() $this->assertSame('value', $response->getHeader('X-Key')[0]); } - /** @test */ + #[Test] public function can_add_multiple_middleware_to_a_route_in_successive_calls() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -82,7 +83,7 @@ public function can_add_multiple_middleware_to_a_route_in_successive_calls() $this->assertSame('123', $response->getHeader('X-Key2')[0]); } - /** @test */ + #[Test] public function can_add_multiple_middleware_to_a_route_in_a_single_call() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -102,7 +103,7 @@ public function can_add_multiple_middleware_to_a_route_in_a_single_call() $this->assertSame('123', $response->getHeader('X-Key2')[0]); } - /** @test */ + #[Test] public function can_add_multiple_middleware_to_a_route_as_an_array() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -122,7 +123,7 @@ public function can_add_multiple_middleware_to_a_route_as_an_array() $this->assertSame('123', $response->getHeader('X-Key2')[0]); } - /** @test */ + #[Test] public function can_add_middleware_to_a_group() { $request = new ServerRequest([], [], '/all', 'GET'); @@ -144,7 +145,7 @@ public function can_add_middleware_to_a_group() $this->assertSame('abc', $response->getHeader('X-Key')[0]); } - /** @test */ + #[Test] public function can_add_single_middleware_to_a_group_without_wrapping_in_array() { $request = new ServerRequest([], [], '/all', 'GET'); @@ -166,7 +167,7 @@ public function can_add_single_middleware_to_a_group_without_wrapping_in_array() $this->assertSame('abc', $response->getHeader('X-Key')[0]); } - /** @test */ + #[Test] public function can_add_base_middleware_to_be_applied_to_all_routes() { $router = new Router; @@ -201,7 +202,7 @@ public function can_add_base_middleware_to_be_applied_to_all_routes() $this->assertSame('abc', $response2->getHeader('X-Key')[0]); } - /** @test */ + #[Test] public function can_resolve_middleware_on_a_route_using_a_custom_resolver() { $resolver = $this->createMockMiddlewareResolverWithHeader('X-Key', 'abc'); @@ -217,7 +218,7 @@ public function can_resolve_middleware_on_a_route_using_a_custom_resolver() $this->assertSame('abc', $response->getHeader('X-Key')[0]); } - /** @test */ + #[Test] public function can_resolve_middleware_on_a_group_using_a_custom_resolver() { $resolver = $this->createMockMiddlewareResolverWithHeader('X-Key', 'abc'); @@ -235,7 +236,7 @@ public function can_resolve_middleware_on_a_group_using_a_custom_resolver() $this->assertSame('abc', $response->getHeader('X-Key')[0]); } - /** @test */ + #[Test] public function can_resolve_global_middleware_using_a_custom_resolver() { $resolver = $this->createMockMiddlewareResolverWithHeader('X-Key', 'abc'); diff --git a/tests/RouterParamsTest.php b/tests/RouterParamsTest.php index 63ac8e6..976ccc0 100644 --- a/tests/RouterParamsTest.php +++ b/tests/RouterParamsTest.php @@ -4,10 +4,11 @@ use PHPUnit\Framework\TestCase; use Rareloop\Router\RouteParams; +use PHPUnit\Framework\Attributes\Test; class RouterParamsTest extends TestCase { - /** @test */ + #[Test] public function can_get_param_by_key() { $params = new RouteParams(['key' => 'value']); @@ -15,7 +16,7 @@ public function can_get_param_by_key() $this->assertSame('value', $params->key); } - /** @test */ + #[Test] public function can_iterate_all_keys_and_values() { $params = new RouteParams([ @@ -36,7 +37,7 @@ public function can_iterate_all_keys_and_values() $this->assertSame(['value1', 'value2', 'value3'], $values); } - /** @test */ + #[Test] public function return_null_when_a_key_is_not_found() { $params = new RouteParams(['key' => 'value']); @@ -44,7 +45,7 @@ public function return_null_when_a_key_is_not_found() $this->assertNull($params->invalid); } - /** @test */ + #[Test] public function can_get_params_as_array() { $data = ['key1' => 'value1', 'key2' => 'value2']; diff --git a/tests/RouterTest.php b/tests/RouterTest.php index 3d57f8b..2a864d7 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -18,10 +18,11 @@ use Laminas\Diactoros\Response; use Laminas\Diactoros\Response\TextResponse; use Laminas\Diactoros\ServerRequest; +use PHPUnit\Framework\Attributes\Test; class RouterTest extends TestCase { - /** @test */ + #[Test] public function map_returns_a_route_object() { $router = new Router; @@ -33,7 +34,7 @@ public function map_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - /** @test */ + #[Test] public function map_accepts_lowercase_verbs() { $router = new Router; @@ -43,7 +44,7 @@ public function map_accepts_lowercase_verbs() $this->assertSame(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], $route->getMethods()); } - /** @test */ + #[Test] public function get_returns_a_route_object() { $router = new Router; @@ -55,7 +56,7 @@ public function get_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - /** @test */ + #[Test] public function post_returns_a_route_object() { $router = new Router; @@ -67,7 +68,7 @@ public function post_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - /** @test */ + #[Test] public function patch_returns_a_route_object() { $router = new Router; @@ -79,7 +80,7 @@ public function patch_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - /** @test */ + #[Test] public function put_returns_a_route_object() { $router = new Router; @@ -91,7 +92,7 @@ public function put_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - /** @test */ + #[Test] public function delete_returns_a_route_object() { $router = new Router; @@ -103,7 +104,7 @@ public function delete_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - /** @test */ + #[Test] public function options_returns_a_route_object() { $router = new Router; @@ -115,7 +116,7 @@ public function options_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - /** @test */ + #[Test] public function map_removes_trailing_slash_from_uri() { $router = new Router; @@ -127,7 +128,7 @@ public function map_removes_trailing_slash_from_uri() $this->assertSame('/test/123', $route->getUri()); } - /** @test */ + #[Test] public function no_return_from_route_action_results_in_a_204_status_code() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -143,7 +144,7 @@ public function no_return_from_route_action_results_in_a_204_status_code() $this->assertSame(204, $response->getStatusCode()); } - /** @test */ + #[Test] public function leading_slash_is_optional_when_creating_a_route() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -159,7 +160,7 @@ public function leading_slash_is_optional_when_creating_a_route() $this->assertInstanceOf(ResponseInterface::class, $response); } - /** @test */ + #[Test] public function matching_root_path_does_not_trigger_error() { $request = new ServerRequest([], [], '/', 'GET'); @@ -174,7 +175,7 @@ public function matching_root_path_does_not_trigger_error() $this->assertSame(0, $count); } - /** @test */ + #[Test] public function can_match_a_route_for_root_path() { $request = new ServerRequest([], [], '/', 'GET'); @@ -189,7 +190,7 @@ public function can_match_a_route_for_root_path() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function match_returns_a_response_object() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -207,7 +208,7 @@ public function match_returns_a_response_object() $this->assertInstanceOf(ResponseInterface::class, $response); } - /** @test */ + #[Test] public function match_does_not_mutate_returned_response_object() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -222,7 +223,7 @@ public function match_does_not_mutate_returned_response_object() $this->assertSame($response, $routerResponse); } - /** @test */ + #[Test] public function match_returns_a_404_response_object_when_route_is_not_found() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -234,7 +235,7 @@ public function match_returns_a_404_response_object_when_route_is_not_found() $this->assertSame(404, $response->getStatusCode()); } - /** @test */ + #[Test] public function match_works_with_a_closure() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -252,7 +253,7 @@ public function match_works_with_a_closure() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function match_uri_with_trailing_when_route_has_been_defined_without_trailing_slash() { $request = new ServerRequest([], [], '/test/123/', 'GET'); @@ -270,7 +271,7 @@ public function match_uri_with_trailing_when_route_has_been_defined_without_trai $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function match_uri_with_trailing_when_route_has_been_defined_with_trailing_slash() { $request = new ServerRequest([], [], '/test/123/', 'GET'); @@ -288,7 +289,7 @@ public function match_uri_with_trailing_when_route_has_been_defined_with_trailin $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function match_uri_without_trailing_when_route_has_been_defined_without_trailing_slash() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -306,7 +307,7 @@ public function match_uri_without_trailing_when_route_has_been_defined_without_t $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function match_uri_without_trailing_when_route_has_been_defined_with_trailing_slash() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -324,7 +325,7 @@ public function match_uri_without_trailing_when_route_has_been_defined_with_trai $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function match_works_with_a_class_and_method_string() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -336,7 +337,7 @@ public function match_works_with_a_class_and_method_string() $this->assertSame('Hello World', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function match_throws_exception_with_invalid_class_and_string_method() { $this->expectException(RouteClassStringParseException::class); @@ -346,7 +347,7 @@ public function match_throws_exception_with_invalid_class_and_string_method() $route = $router->get('/test/123', 'Rareloop\Router\Test\Controllers\TestController:returnHelloWorld'); } - /** @test */ + #[Test] public function match_throws_exception_when_class_and_string_method_contains_an_unfound_class() { $this->expectException(RouteClassStringControllerNotFoundException::class); @@ -356,7 +357,7 @@ public function match_throws_exception_when_class_and_string_method_contains_an_ $route = $router->get('/test/123', 'Rareloop\Router\Test\Controllers\UndefinedController@returnHelloWorld'); } - /** @test */ + #[Test] public function match_throws_exception_when_class_and_string_method_contains_an_unfound_method() { $this->expectException(RouteClassStringMethodNotFoundException::class); @@ -366,7 +367,7 @@ public function match_throws_exception_when_class_and_string_method_contains_an_ $route = $router->get('/test/123', 'Rareloop\Router\Test\Controllers\TestController@undefinedMethod'); } - /** @test */ + #[Test] public function params_are_parsed_and_passed_into_callback_function() { $request = new ServerRequest([], [], '/posts/123/comments/abc', 'GET'); @@ -384,7 +385,7 @@ public function params_are_parsed_and_passed_into_callback_function() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function params_are_parsed_and_passed_into_callback_function_when_surrounded_by_whitespace() { $request = new ServerRequest([], [], '/posts/123/comments/abc', 'GET'); @@ -402,7 +403,7 @@ public function params_are_parsed_and_passed_into_callback_function_when_surroun $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_add_regex_constraints_on_params_as_key_value() { $matchingRequest = new ServerRequest([], [], '/posts/123/comments', 'GET'); @@ -419,7 +420,7 @@ public function can_add_regex_constraints_on_params_as_key_value() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_add_multiple_regex_constraints_on_params_as_key_value() { $matchingRequest = new ServerRequest([], [], '/posts/123/comments/abc', 'GET'); @@ -436,7 +437,7 @@ public function can_add_multiple_regex_constraints_on_params_as_key_value() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_add_regex_constraints_on_params_as_array() { $matchingRequest = new ServerRequest([], [], '/posts/123/comments', 'GET'); @@ -453,7 +454,7 @@ public function can_add_regex_constraints_on_params_as_array() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_add_multiple_regex_constraints_on_params_as_array() { $matchingRequest = new ServerRequest([], [], '/posts/123/comments/abc', 'GET'); @@ -473,7 +474,7 @@ public function can_add_multiple_regex_constraints_on_params_as_array() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_provide_optional_params() { $matchingRequest1 = new ServerRequest([], [], '/posts/123', 'GET'); @@ -494,7 +495,7 @@ public function can_provide_optional_params() $this->assertSame(2, $count); } - /** @test */ + #[Test] public function can_generate_canonical_uri_with_trailing_slash_for_named_route() { $router = new Router; @@ -504,7 +505,7 @@ public function can_generate_canonical_uri_with_trailing_slash_for_named_route() $this->assertSame('/posts/all/', $router->url('test.name')); } - /** @test */ + #[Test] public function can_generate_canonical_uri_with_trailing_slash_for_named_route_with_params() { $router = new Router; @@ -514,7 +515,7 @@ public function can_generate_canonical_uri_with_trailing_slash_for_named_route_w $this->assertSame('/posts/123/comments/', $router->url('test.name', ['id' => 123])); } - /** @test */ + #[Test] public function url_throws_exception_when_provided_params_fail_the_regex_constraints() { $this->expectException(RouteParamFailedConstraintException::class); @@ -528,7 +529,7 @@ public function url_throws_exception_when_provided_params_fail_the_regex_constra $router->url('test.name', ['id' => 123]); } - /** @test */ + #[Test] public function generating_a_url_for_a_named_route_that_doesnt_exist_throws_an_exception() { $this->expectException(NamedRouteNotFoundException::class); @@ -538,7 +539,7 @@ public function generating_a_url_for_a_named_route_that_doesnt_exist_throws_an_e $router->url('test.name'); } - /** @test */ + #[Test] public function can_generate_canonical_uri_after_match_has_been_called() { $router = new Router; @@ -550,7 +551,7 @@ public function can_generate_canonical_uri_after_match_has_been_called() $this->assertSame('/posts/all/', $router->url('test.name')); } - /** @test */ + #[Test] public function adding_routes_after_calling_url_throws_an_exception() { $this->expectException(TooLateToAddNewRouteException::class); @@ -563,7 +564,7 @@ public function adding_routes_after_calling_url_throws_an_exception() $route = $router->get('another/url', function () {}); } - /** @test */ + #[Test] public function adding_routes_after_calling_match_throws_an_exception() { $this->expectException(TooLateToAddNewRouteException::class); @@ -577,7 +578,7 @@ public function adding_routes_after_calling_match_throws_an_exception() $route = $router->get('another/url', function () {}); } - /** @test */ + #[Test] public function can_add_routes_in_a_group() { $request = new ServerRequest([], [], '/prefix/all', 'GET'); @@ -597,7 +598,7 @@ public function can_add_routes_in_a_group() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_add_routes_in_a_group_using_array_as_first_param() { $request = new ServerRequest([], [], '/prefix/all', 'GET'); @@ -617,7 +618,7 @@ public function can_add_routes_in_a_group_using_array_as_first_param() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_add_routes_in_a_group_using_array_as_first_param_with_no_prefix() { $request = new ServerRequest([], [], '/all', 'GET'); @@ -637,7 +638,7 @@ public function can_add_routes_in_a_group_using_array_as_first_param_with_no_pre $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function group_prefixes_work_with_leading_slash() { $request = new ServerRequest([], [], '/prefix/all', 'GET'); @@ -657,7 +658,7 @@ public function group_prefixes_work_with_leading_slash() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function group_prefixes_work_with_trailing_slash() { $request = new ServerRequest([], [], '/prefix/all', 'GET'); @@ -677,7 +678,7 @@ public function group_prefixes_work_with_trailing_slash() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_add_routes_in_nested_groups() { $request = new ServerRequest([], [], '/prefix/prefix2/all', 'GET'); @@ -702,7 +703,7 @@ public function can_add_routes_in_nested_groups() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_add_routes_in_nested_groups_with_array_syntax() { $request = new ServerRequest([], [], '/prefix/prefix2/all', 'GET'); @@ -727,7 +728,7 @@ public function can_add_routes_in_nested_groups_with_array_syntax() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_set_base_path() { $request = new ServerRequest([], [], '/base-path/prefix/all', 'GET'); @@ -746,7 +747,7 @@ public function can_set_base_path() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_set_base_path_without_trailing_slash() { $request = new ServerRequest([], [], '/base-path/prefix/all', 'GET'); @@ -765,7 +766,7 @@ public function can_set_base_path_without_trailing_slash() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_set_base_path_without_leading_slash() { $request = new ServerRequest([], [], '/base-path/prefix/all', 'GET'); @@ -784,7 +785,7 @@ public function can_set_base_path_without_leading_slash() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_set_base_path_without_leading_or_trailing_slash() { $request = new ServerRequest([], [], '/base-path/prefix/all', 'GET'); @@ -803,7 +804,7 @@ public function can_set_base_path_without_leading_or_trailing_slash() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_update_base_path_after_match_has_been_called() { $router = new Router; @@ -829,7 +830,7 @@ public function can_update_base_path_after_match_has_been_called() $this->assertSame('abc123', $response2->getBody()->getContents()); } - /** @test */ + #[Test] public function can_add_middleware_as_a_closure_to_a_route() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -854,7 +855,7 @@ public function can_add_middleware_as_a_closure_to_a_route() $this->assertSame('value', $response->getHeader('X-key')[0]); } - /** @test */ + #[Test] public function can_get_currently_matched_route() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -873,7 +874,7 @@ public function can_get_currently_matched_route() $this->assertSame($route, $router->currentRoute()); } - /** @test */ + #[Test] public function can_get_currently_matched_route_name() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -892,7 +893,7 @@ public function can_get_currently_matched_route_name() $this->assertSame('test123', $router->currentRouteName()); } - /** @test */ + #[Test] public function current_route_name_returns_null_when_match_not_yet_called() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -903,7 +904,7 @@ public function current_route_name_returns_null_when_match_not_yet_called() $this->assertSame(null, $router->currentRouteName()); } - /** @test */ + #[Test] public function current_route_name_returns_null_when_matched_route_has_no_name() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -916,7 +917,7 @@ public function current_route_name_returns_null_when_matched_route_has_no_name() $this->assertSame(null, $router->currentRouteName()); } - /** @test */ + #[Test] public function can_get_list_of_registered_routes() { $router = new Router; diff --git a/tests/TypeHintRequestResolverTest.php b/tests/TypeHintRequestResolverTest.php index 31efbef..26eccd8 100644 --- a/tests/TypeHintRequestResolverTest.php +++ b/tests/TypeHintRequestResolverTest.php @@ -5,10 +5,11 @@ use PHPUnit\Framework\TestCase; use Rareloop\Router\TypeHintRequestResolver; use Laminas\Diactoros\ServerRequest; +use PHPUnit\Framework\Attributes\Test; class TypeHintRequestResolverTest extends TestCase { - /** @test */ + #[Test] public function returns_resolved_parameters_when_no_request_is_set() { $reflectionFunction = new \ReflectionFunction(function () {}); @@ -20,7 +21,7 @@ public function returns_resolved_parameters_when_no_request_is_set() $this->assertSame($resolvedParameters, $params); } - /** @test */ + #[Test] public function can_resolve_a_request() { $request = new ServerRequest([], [], '/injected', 'GET'); @@ -33,7 +34,7 @@ public function can_resolve_a_request() $this->assertSame('/injected', $params[0]->getUri()->getPath()); } - /** @test */ + #[Test] public function does_not_attempt_to_resolve_params_that_have_already_been_resolved() { $preResolvedRequest = new ServerRequest([], [], '/pre/resolved', 'GET'); From 3c6b2d578afc1f076de7b8fbc195e9a1a30dafe9 Mon Sep 17 00:00:00 2001 From: Tom Mitchelmore Date: Tue, 7 Oct 2025 10:04:05 +0100 Subject: [PATCH 08/13] Re-support 8.1-8.4 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8c06079..e55b89f 100755 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ } ], "require": { - "php": "^8.4", + "php": ">=8.1", "altorouter/altorouter": "^2.0.3", "laminas/laminas-diactoros": "^3.6.0", "mindplay/middleman": "^4.0.4", From 5087a11ff09b3a7e825478e54dd44d1415191c2d Mon Sep 17 00:00:00 2001 From: Adam Tomat Date: Mon, 13 Oct 2025 16:06:17 +0100 Subject: [PATCH 09/13] Update ci.yml to include other PHP versions --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ebfb3e7..5622069 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php_version: [8.4] + php_version: [8.1, 8.2, 8.3, 8.4] composer_flags: ["", "--prefer-lowest"] steps: From 7aaea46d106cef85110fb0c661df3d5a7c8f013f Mon Sep 17 00:00:00 2001 From: Adam Tomat Date: Mon, 13 Oct 2025 16:17:32 +0100 Subject: [PATCH 10/13] Revert Rector changes --- composer.json | 1 - rector.php | 13 -- src/Helpers/Formatting.php | 4 +- src/Route.php | 15 +- src/RouteAction.php | 16 +- src/RouteGroup.php | 5 +- src/RouteParams.php | 4 +- src/Router.php | 18 +- tests/ControllerMiddlewareOptionsTest.php | 15 +- tests/ControllerMiddlewareTest.php | 5 +- tests/ControllerTest.php | 20 ++- .../TestConstructorParamController.php | 5 +- tests/FormattingTest.php | 13 +- tests/Middleware/AddHeaderMiddleware.php | 7 +- tests/ResponseFactoryTest.php | 9 +- tests/RouteGroupTest.php | 25 +-- tests/RouteTest.php | 43 +++-- tests/RouterDITest.php | 28 +-- tests/RouterMiddlewareTest.php | 35 ++-- tests/RouterParamsTest.php | 9 +- tests/RouterTest.php | 161 ++++++++++-------- tests/Services/TestService.php | 5 +- tests/TypeHintRequestResolverTest.php | 7 +- 23 files changed, 260 insertions(+), 203 deletions(-) delete mode 100644 rector.php diff --git a/composer.json b/composer.json index e55b89f..dfe131b 100755 --- a/composer.json +++ b/composer.json @@ -31,7 +31,6 @@ "php-coveralls/php-coveralls": "^2.4", "php-di/php-di": "^7.1.1", "phpunit/phpunit": "^12", - "rector/rector": "^2.1", "squizlabs/php_codesniffer": "^3.6.0" }, "autoload": { diff --git a/rector.php b/rector.php deleted file mode 100644 index fd10b4d..0000000 --- a/rector.php +++ /dev/null @@ -1,13 +0,0 @@ -withPaths([ - __DIR__ . '/src', - __DIR__ . '/tests', - ]) - ->withPhpSets(php84: true); diff --git a/src/Helpers/Formatting.php b/src/Helpers/Formatting.php index 4a3c95c..d784592 100644 --- a/src/Helpers/Formatting.php +++ b/src/Helpers/Formatting.php @@ -6,7 +6,7 @@ class Formatting { public static function removeTrailingSlash($input) { - return rtrim((string) $input, '/\\'); + return rtrim($input, '/\\'); } public static function addTrailingSlash($input) @@ -16,7 +16,7 @@ public static function addTrailingSlash($input) public static function removeLeadingSlash($input) { - return ltrim((string) $input, '/\\'); + return ltrim($input, '/\\'); } public static function addLeadingSlash($input) diff --git a/src/Route.php b/src/Route.php index 60e5cc0..d6f3a30 100644 --- a/src/Route.php +++ b/src/Route.php @@ -21,27 +21,34 @@ class Route use Macroable; private $uri; + private $methods = []; private $routeAction; private $name; + private $invoker = null; + private $middlewareResolver = null; private $middleware = []; private $paramConstraints = []; private $controllerName = null; private $controllerMethod = null; public function __construct( - private array $methods, + array $methods, string $uri, $action, - private ?\Rareloop\Router\Invoker $invoker = null, - private ?\Rareloop\Router\MiddlewareResolver $middlewareResolver = null + ?Invoker $invoker = null, + ?MiddlewareResolver $resolver = null ) { + $this->invoker = $invoker; + $this->middlewareResolver = $resolver; + + $this->methods = $methods; $this->setUri($uri); $this->setAction($action); } private function setUri($uri) { - $this->uri = rtrim((string) $uri, ' /'); + $this->uri = rtrim($uri, ' /'); } private function setAction($action) diff --git a/src/RouteAction.php b/src/RouteAction.php index b48c948..cfcb9ca 100644 --- a/src/RouteAction.php +++ b/src/RouteAction.php @@ -151,11 +151,15 @@ public function getMiddleware(): array $allControllerMiddleware = array_filter( $this->getController()->getControllerMiddleware(), - fn(ControllerMiddleware $middleware) => !$middleware->excludedForMethod($this->controllerMethod) + function (ControllerMiddleware $middleware) { + return !$middleware->excludedForMethod($this->controllerMethod); + } ); return array_map( - fn($controllerMiddleware) => $controllerMiddleware->middleware(), + function ($controllerMiddleware) { + return $controllerMiddleware->middleware(); + }, $allControllerMiddleware ); } @@ -171,7 +175,7 @@ private function convertClassStringToFactory($string): Closure $this->controllerName = null; $this->controllerMethod = null; - @[$className, $method] = explode('@', $string); + @list($className, $method) = explode('@', $string); if (!isset($className) || !isset($method)) { throw new RouteClassStringParseException('Could not parse route controller from string: `' . $string . '`'); @@ -200,7 +204,9 @@ private function convertClassStringToFactory($string): Closure return [$controller, $method]; } - return fn($params = null) => $controller->$method($params); + return function ($params = null) use ($controller, $method) { + return $controller->$method($params); + }; }; } @@ -218,7 +224,7 @@ public function getActionName() } if (is_callable($this->callable, false, $callableName)) { - [$controller, $method] = explode('::', $callableName); + list($controller, $method) = explode('::', $callableName); if ($controller === 'Closure') { return $controller; diff --git a/src/RouteGroup.php b/src/RouteGroup.php index 30e47ac..54f3db5 100644 --- a/src/RouteGroup.php +++ b/src/RouteGroup.php @@ -9,10 +9,12 @@ class RouteGroup implements Routable { use VerbShortcutsTrait, Macroable; + + protected $router; protected $prefix; protected $middleware = []; - public function __construct($params, protected $router) + public function __construct($params, $router) { $prefix = null; $middleware = []; @@ -33,6 +35,7 @@ public function __construct($params, protected $router) } $this->prefix = is_string($prefix) ? trim($prefix, ' /') : null; + $this->router = $router; } private function appendPrefixToUri(string $uri) diff --git a/src/RouteParams.php b/src/RouteParams.php index 92bf421..7c972bb 100644 --- a/src/RouteParams.php +++ b/src/RouteParams.php @@ -5,9 +5,11 @@ class RouteParams implements \Iterator { private $position = 0; + private $params = []; - public function __construct(private array $params) + public function __construct(array $params) { + $this->params = $params; } public function __get($key) diff --git a/src/Router.php b/src/Router.php index 2d96f22..e38d1af 100644 --- a/src/Router.php +++ b/src/Router.php @@ -79,7 +79,7 @@ private function convertRouteToAltoRouterUri(Route $route, AltoRouter $altoRoute { $output = $route->getUri(); - preg_match_all('/{\s*([a-zA-Z0-9]+\??)\s*}/s', (string) $route->getUri(), $matches); + preg_match_all('/{\s*([a-zA-Z0-9]+\??)\s*}/s', $route->getUri(), $matches); $paramConstraints = $route->getParamConstraints(); @@ -87,7 +87,7 @@ private function convertRouteToAltoRouterUri(Route $route, AltoRouter $altoRoute $match = $matches[0][$i]; $paramKey = $matches[1][$i]; - $optional = str_ends_with($paramKey, '?'); + $optional = substr($paramKey, -1) === '?'; $paramKey = trim($paramKey, '?'); $regex = $paramConstraints[$paramKey] ?? null; @@ -109,7 +109,7 @@ private function convertRouteToAltoRouterUri(Route $route, AltoRouter $altoRoute $output = str_replace($match, $replacement, $output); } - return ltrim((string) $output, ' /'); + return ltrim($output, ' /'); } public function map(array $verbs, string $uri, $callback): Route @@ -180,7 +180,9 @@ protected function handle($route, $request, $params) // Apply all the base middleware and trigger the route handler as the last in the chain $middlewares = array_merge($this->baseMiddleware, [ - fn($request) => $route->handle($request, $params), + function ($request) use ($route, $params) { + return $route->handle($request, $params); + }, ]); // Create and process the dispatcher @@ -197,7 +199,9 @@ protected function handle($route, $request, $params) public function has(string $name) { - $routes = array_filter($this->routes, fn($route) => $route->getName() === $name); + $routes = array_filter($this->routes, function ($route) use ($name) { + return $route->getName() === $name; + }); return count($routes) > 0; } @@ -223,7 +227,7 @@ public function url(string $name, $params = []) $regex = $paramConstraints[$key] ?? false; if ($regex) { - if (!preg_match('/' . $regex . '/', (string) $value)) { + if (!preg_match('/' . $regex . '/', $value)) { throw new RouteParamFailedConstraintException( 'Value `' . $value . '` for param `' . $key . '` fails constraint `' . $regex . '`' ); @@ -234,7 +238,7 @@ public function url(string $name, $params = []) try { return $this->altoRouter->generate($name, $params); - } catch (\Exception) { + } catch (\Exception $e) { throw new NamedRouteNotFoundException($name); } } diff --git a/tests/ControllerMiddlewareOptionsTest.php b/tests/ControllerMiddlewareOptionsTest.php index c2cc845..a5d340b 100644 --- a/tests/ControllerMiddlewareOptionsTest.php +++ b/tests/ControllerMiddlewareOptionsTest.php @@ -2,13 +2,12 @@ namespace Rareloop\Router\Test; -use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Rareloop\Router\ControllerMiddlewareOptions; class ControllerMiddlewareOptionsTest extends TestCase { - #[Test] + /** @test */ public function by_default_no_methods_are_excluded() { $options = new ControllerMiddlewareOptions; @@ -17,7 +16,7 @@ public function by_default_no_methods_are_excluded() $this->assertFalse($options->excludedForMethod('bar')); } - #[Test] + /** @test */ public function only_is_chainable() { $options = new ControllerMiddlewareOptions; @@ -25,7 +24,7 @@ public function only_is_chainable() $this->assertSame($options, $options->only('foo')); } - #[Test] + /** @test */ public function can_use_only_to_limit_methods() { $options = new ControllerMiddlewareOptions; @@ -36,7 +35,7 @@ public function can_use_only_to_limit_methods() $this->assertTrue($options->excludedForMethod('bar')); } - #[Test] + /** @test */ public function can_use_only_to_limit_multiple_methods() { $options = new ControllerMiddlewareOptions; @@ -48,7 +47,7 @@ public function can_use_only_to_limit_multiple_methods() $this->assertTrue($options->excludedForMethod('baz')); } - #[Test] + /** @test */ public function except_is_chainable() { $options = new ControllerMiddlewareOptions; @@ -56,7 +55,7 @@ public function except_is_chainable() $this->assertSame($options, $options->except('foo')); } - #[Test] + /** @test */ public function can_use_except_to_limit_methods() { $options = new ControllerMiddlewareOptions; @@ -67,7 +66,7 @@ public function can_use_except_to_limit_methods() $this->assertFalse($options->excludedForMethod('bar')); } - #[Test] + /** @test */ public function can_use_except_to_limit_multiple_methods() { $options = new ControllerMiddlewareOptions; diff --git a/tests/ControllerMiddlewareTest.php b/tests/ControllerMiddlewareTest.php index 66e29e9..114cca6 100644 --- a/tests/ControllerMiddlewareTest.php +++ b/tests/ControllerMiddlewareTest.php @@ -6,11 +6,10 @@ use Rareloop\Router\ControllerMiddleware; use Rareloop\Router\ControllerMiddlewareOptions; use Rareloop\Router\Test\Middleware\AddHeaderMiddleware; -use PHPUnit\Framework\Attributes\Test; class ControllerMiddlewareTest extends TestCase { - #[Test] + /** @test */ public function can_retrieve_middleware() { $middleware = new AddHeaderMiddleware('X-Header', 'testing123'); @@ -21,7 +20,7 @@ public function can_retrieve_middleware() $this->assertSame($middleware, $controllerMiddleware->middleware()); } - #[Test] + /** @test */ public function can_retrieve_options() { $middleware = new AddHeaderMiddleware('X-Header', 'testing123'); diff --git a/tests/ControllerTest.php b/tests/ControllerTest.php index 809b886..5814800 100644 --- a/tests/ControllerTest.php +++ b/tests/ControllerTest.php @@ -15,7 +15,7 @@ class ControllerTest extends TestCase { - #[Test] + /** @test */ public function can_add_single_middleware_via_controller() { $container = new \DI\Container(); @@ -37,7 +37,7 @@ public function can_add_single_middleware_via_controller() $this->assertSame('testing123', $response->getHeader('X-Header')[0]); } - #[Test] + /** @test */ public function can_resolve_middleware_on_a_controller_using_custom_resolver() { $container = new \DI\Container(); @@ -60,7 +60,7 @@ public function can_resolve_middleware_on_a_controller_using_custom_resolver() $this->assertSame('testing123', $response->getHeader('X-Header')[0]); } - #[Test] + /** @test */ public function can_add_multiple_middleware_as_array_via_controller() { $container = new \DI\Container(); @@ -87,7 +87,7 @@ public function can_add_multiple_middleware_as_array_via_controller() $this->assertSame('testing456', $response->getHeader('X-Header-2')[0]); } - #[Test] + /** @test */ public function controller_middleware_method_returns_options() { $controller = new MiddlewareProvidingController; @@ -97,7 +97,7 @@ public function controller_middleware_method_returns_options() $this->assertInstanceOf(ControllerMiddlewareOptions::class, $options); } - #[Test] + /** @test */ public function middleware_can_be_limited_to_methods_using_only() { $container = new \DI\Container(); @@ -116,7 +116,7 @@ public function middleware_can_be_limited_to_methods_using_only() $this->assertMiddlewareIsAppliedToMethods($router, $middlewareAppliedToMethods); } - #[Test] + /** @test */ public function middleware_can_be_limited_to_multiple_methods_using_only() { $container = new \DI\Container(); @@ -135,7 +135,7 @@ public function middleware_can_be_limited_to_multiple_methods_using_only() $this->assertMiddlewareIsAppliedToMethods($router, $middlewareAppliedToMethods); } - #[Test] + /** @test */ public function middleware_can_be_limited_to_methods_using_except() { $container = new \DI\Container(); @@ -154,7 +154,7 @@ public function middleware_can_be_limited_to_methods_using_except() $this->assertMiddlewareIsAppliedToMethods($router, $middlewareAppliedToMethods); } - #[Test] + /** @test */ public function middleware_can_be_limited_to_multiple_methods_using_except() { $container = new \DI\Container(); @@ -201,7 +201,9 @@ private function createMockMiddlewareResolverWithHeader($header, $value) $middleware = new AddHeaderMiddleware($header, $value); $resolver = Mockery::mock(MiddlewareResolver::class); $resolver->shouldReceive('resolve')->with('middleware-key')->andReturn($middleware); - $resolver->shouldReceive('resolve')->with(Mockery::type('callable'))->andReturnUsing(fn($argument) => $argument); + $resolver->shouldReceive('resolve')->with(Mockery::type('callable'))->andReturnUsing(function ($argument) { + return $argument; + }); return $resolver; } diff --git a/tests/Controllers/TestConstructorParamController.php b/tests/Controllers/TestConstructorParamController.php index 89a8e9c..35ac1bc 100644 --- a/tests/Controllers/TestConstructorParamController.php +++ b/tests/Controllers/TestConstructorParamController.php @@ -6,8 +6,11 @@ class TestConstructorParamController { - public function __construct(private readonly TestService $testService) + private $testService; + + public function __construct(TestService $testService) { + $this->testService = $testService; } public function returnTestServiceValue() diff --git a/tests/FormattingTest.php b/tests/FormattingTest.php index c0a9dac..fa718c3 100644 --- a/tests/FormattingTest.php +++ b/tests/FormattingTest.php @@ -4,11 +4,10 @@ use PHPUnit\Framework\TestCase; use Rareloop\Router\Helpers\Formatting; -use PHPUnit\Framework\Attributes\Test; class FormattingTest extends TestCase { - #[Test] + /** @test */ public function can_remove_trialing_slash() { $string = 'string/'; @@ -16,7 +15,7 @@ public function can_remove_trialing_slash() $this->assertSame('string', Formatting::removeTrailingSlash($string)); } - #[Test] + /** @test */ public function can_add_trialing_slash() { $string = 'string'; @@ -24,7 +23,7 @@ public function can_add_trialing_slash() $this->assertSame('string/', Formatting::addTrailingSlash($string)); } - #[Test] + /** @test */ public function add_trialing_slash_does_not_produce_duplicates() { $string = 'string/'; @@ -32,7 +31,7 @@ public function add_trialing_slash_does_not_produce_duplicates() $this->assertSame('string/', Formatting::addTrailingSlash($string)); } - #[Test] + /** @test */ public function can_remove_leading_slash() { $string = '/string'; @@ -40,7 +39,7 @@ public function can_remove_leading_slash() $this->assertSame('string', Formatting::removeLeadingSlash($string)); } - #[Test] + /** @test */ public function can_add_leading_slash() { $string = 'string'; @@ -48,7 +47,7 @@ public function can_add_leading_slash() $this->assertSame('/string', Formatting::addLeadingSlash($string)); } - #[Test] + /** @test */ public function add_leading_slash_does_not_produce_duplicates() { $string = '/string'; diff --git a/tests/Middleware/AddHeaderMiddleware.php b/tests/Middleware/AddHeaderMiddleware.php index b6b8cf7..4205d2b 100644 --- a/tests/Middleware/AddHeaderMiddleware.php +++ b/tests/Middleware/AddHeaderMiddleware.php @@ -10,8 +10,13 @@ class AddHeaderMiddleware implements MiddlewareInterface { - public function __construct(private $key, private $value) + private $key; + private $value; + + public function __construct($key, $value) { + $this->key = $key; + $this->value = $value; } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface diff --git a/tests/ResponseFactoryTest.php b/tests/ResponseFactoryTest.php index d596149..3cc2eaf 100644 --- a/tests/ResponseFactoryTest.php +++ b/tests/ResponseFactoryTest.php @@ -10,7 +10,6 @@ use Laminas\Diactoros\Response\EmptyResponse; use Laminas\Diactoros\Response\TextResponse; use Laminas\Diactoros\ServerRequest; -use PHPUnit\Framework\Attributes\Test; class ResponseFactoryTest extends TestCase { @@ -25,7 +24,7 @@ public function setUp(): void $this->request = new ServerRequest([], [], '/test/123', 'GET'); } - #[Test] + /** @test */ public function when_passed_a_response_instance_the_same_object_is_returned() { $response = new TextResponse('Testing', 200); @@ -33,7 +32,7 @@ public function when_passed_a_response_instance_the_same_object_is_returned() $this->assertSame($response, ResponseFactory::create($this->request, $response)); } - #[Test] + /** @test */ public function when_passed_a_non_response_instance_a_response_object_is_returned() { $response = ResponseFactory::create($this->request, 'Testing'); @@ -42,7 +41,7 @@ public function when_passed_a_non_response_instance_a_response_object_is_returne $this->assertSame('Testing', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function when_nothing_is_passed_an_empty_response_object_is_returned() { $response = ResponseFactory::create($this->request); @@ -50,7 +49,7 @@ public function when_nothing_is_passed_an_empty_response_object_is_returned() $this->assertInstanceOf(EmptyResponse::class, $response); } - #[Test] + /** @test */ public function when_a_responsable_object_is_passed_the_response_object_is_returned() { $textResponse = new TextResponse('testing123'); diff --git a/tests/RouteGroupTest.php b/tests/RouteGroupTest.php index 1b7a43d..b334314 100644 --- a/tests/RouteGroupTest.php +++ b/tests/RouteGroupTest.php @@ -6,11 +6,10 @@ use Rareloop\Router\Route; use Rareloop\Router\RouteGroup; use Rareloop\Router\Router; -use PHPUnit\Framework\Attributes\Test; class RouteGroupTest extends TestCase { - #[Test] + /** @test */ public function group_function_is_chainable() { $router = new Router; @@ -18,7 +17,7 @@ public function group_function_is_chainable() $this->assertInstanceOf(Router::class, $router->group('test/123', function () {})); } - #[Test] + /** @test */ public function can_add_get_request_to_a_group() { $router = new Router; @@ -36,7 +35,7 @@ public function can_add_get_request_to_a_group() $this->assertSame(1, $count); } - #[Test] + /** @test */ public function can_add_request_to_a_group_with_leading_slash() { $router = new Router; @@ -54,7 +53,7 @@ public function can_add_request_to_a_group_with_leading_slash() $this->assertSame(1, $count); } - #[Test] + /** @test */ public function can_add_post_request_to_a_group() { $router = new Router; @@ -72,7 +71,7 @@ public function can_add_post_request_to_a_group() $this->assertSame(1, $count); } - #[Test] + /** @test */ public function can_add_put_request_to_a_group() { $router = new Router; @@ -90,7 +89,7 @@ public function can_add_put_request_to_a_group() $this->assertSame(1, $count); } - #[Test] + /** @test */ public function can_add_patch_request_to_a_group() { $router = new Router; @@ -108,7 +107,7 @@ public function can_add_patch_request_to_a_group() $this->assertSame(1, $count); } - #[Test] + /** @test */ public function can_add_delete_request_to_a_group() { $router = new Router; @@ -126,7 +125,7 @@ public function can_add_delete_request_to_a_group() $this->assertSame(1, $count); } - #[Test] + /** @test */ public function can_add_options_request_to_a_group() { $router = new Router; @@ -149,7 +148,9 @@ public function can_add_options_request_to_a_group() */ public function can_extend_post_behaviour_with_macros() { - RouteGroup::macro('testFunctionAddedByMacro', fn() => 'abc123'); + RouteGroup::macro('testFunctionAddedByMacro', function () { + return 'abc123'; + }); $queryBuilder = new RouteGroup([], new Router); @@ -174,6 +175,8 @@ class RouteGroupMixin { function testFunctionAddedByMixin() { - return fn() => 'abc123'; + return function() { + return 'abc123'; + }; } } diff --git a/tests/RouteTest.php b/tests/RouteTest.php index a9ebeab..c5a7a03 100644 --- a/tests/RouteTest.php +++ b/tests/RouteTest.php @@ -3,7 +3,6 @@ namespace Rareloop\Router\Test; use InvalidArgumentException; -use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Rareloop\Router\Exceptions\RouteNameRedefinedException; use Rareloop\Router\Route; @@ -11,7 +10,7 @@ class RouteTest extends TestCase { - #[Test] + /** @test */ public function a_route_can_be_named() { $router = new Router; @@ -21,7 +20,7 @@ public function a_route_can_be_named() $this->assertTrue($router->has('test')); } - #[Test] + /** @test */ public function name_function_is_chainable() { $router = new Router; @@ -29,7 +28,7 @@ public function name_function_is_chainable() $this->assertInstanceOf(Route::class, $router->get('test/123', function () {})->name('test')); } - #[Test] + /** @test */ public function a_route_can_not_be_renamed() { $this->expectException(RouteNameRedefinedException::class); @@ -39,7 +38,7 @@ public function a_route_can_not_be_renamed() $route = $router->get('test/123', function () {})->name('test1')->name('test2'); } - #[Test] + /** @test */ public function where_function_is_chainable() { $router = new Router; @@ -47,7 +46,7 @@ public function where_function_is_chainable() $this->assertInstanceOf(Route::class, $router->get('test/{id}', function () {})->where('id', '[0-9]+')); } - #[Test] + /** @test */ public function where_function_is_chainable_when_passed_an_array() { $router = new Router; @@ -55,7 +54,7 @@ public function where_function_is_chainable_when_passed_an_array() $this->assertInstanceOf(Route::class, $router->get('test/{id}', function () {})->where(['id' => '[0-9]+'])); } - #[Test] + /** @test */ public function where_function_throws_exception_when_no_params_provided() { $this->expectException(InvalidArgumentException::class); @@ -65,7 +64,7 @@ public function where_function_throws_exception_when_no_params_provided() $this->assertInstanceOf(Route::class, $router->get('test/{id}', function () {})->where()); } - #[Test] + /** @test */ public function can_get_route_action_name_when_closure() { $router = new Router; @@ -74,22 +73,32 @@ public function can_get_route_action_name_when_closure() $this->assertSame('Closure', $route->getActionName()); } - #[Test] + /** @test */ public function can_get_route_action_name_when_callable() { $router = new Router; $route = $router->get('test/123', [TestCallableController::class, 'testStatic']); - $this->assertSame(TestCallableController::class . '@testStatic', $route->getActionName()); + $this->assertSame(TestCallableController::class.'@testStatic', $route->getActionName()); } - #[Test] + /** @test */ + public function can_get_route_action_name_when_callable_instance() + { + $router = new Router; + $controller = new TestCallableController; + $route = $router->get('test/123', [$controller, 'test']); + + $this->assertSame(TestCallableController::class.'@test', $route->getActionName()); + } + + /** @test */ public function can_get_route_action_name_when_controller_string() { $router = new Router; - $route = $router->get('test/123', TestCallableController::class . '@test'); + $route = $router->get('test/123', TestCallableController::class.'@test'); - $this->assertSame(TestCallableController::class . '@test', $route->getActionName()); + $this->assertSame(TestCallableController::class.'@test', $route->getActionName()); } /** @@ -97,7 +106,9 @@ public function can_get_route_action_name_when_controller_string() */ public function can_extend_post_behaviour_with_macros() { - Route::macro('testFunctionAddedByMacro', fn() => 'abc123'); + Route::macro('testFunctionAddedByMacro', function () { + return 'abc123'; + }); $queryBuilder = new Route(['GET'], '/test/url', function () {}); @@ -128,6 +139,8 @@ class RouteMixin { function testFunctionAddedByMixin() { - return fn() => 'abc123'; + return function() { + return 'abc123'; + }; } } diff --git a/tests/RouterDITest.php b/tests/RouterDITest.php index 01abe2f..5d7d67c 100644 --- a/tests/RouterDITest.php +++ b/tests/RouterDITest.php @@ -14,7 +14,7 @@ class RouterDITest extends TestCase { - #[Test] + /** @test */ public function can_pass_a_container_into_constructor() { $container = new \DI\Container(); @@ -23,7 +23,7 @@ public function can_pass_a_container_into_constructor() $this->assertInstanceOf(Router::class, $router); } - #[Test] + /** @test */ public function container_passed_to_constructor_must_be_psr_11_compatible() { $this->expectException(\TypeError::class); @@ -34,7 +34,7 @@ public function container_passed_to_constructor_must_be_psr_11_compatible() $this->assertInstanceOf(Router::class, $router); } - #[Test] + /** @test */ public function route_params_are_injected_into_closure() { $container = new \DI\Container(); @@ -58,7 +58,7 @@ public function route_params_are_injected_into_closure() $this->assertSame('abc', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function typehints_are_injected_into_closure() { $container = new \DI\Container(); @@ -85,7 +85,7 @@ public function typehints_are_injected_into_closure() $this->assertSame('abc', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function typehints_are_injected_into_closure_with_params() { $container = new \DI\Container(); @@ -114,7 +114,7 @@ public function typehints_are_injected_into_closure_with_params() $this->assertSame('abc', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function route_params_are_injected_into_closure_regardless_of_param_order() { $container = new \DI\Container(); @@ -138,7 +138,7 @@ public function route_params_are_injected_into_closure_regardless_of_param_order $this->assertSame('abc', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function reflection_error_is_thrown_when_typehints_cant_be_resolved_from_the_container() { $this->expectException(\ReflectionException::class); @@ -152,7 +152,7 @@ public function reflection_error_is_thrown_when_typehints_cant_be_resolved_from_ $response = $router->match($request); } - #[Test] + /** @test */ public function route_params_are_injected_into_controller_class() { $container = new \DI\Container(); @@ -167,7 +167,7 @@ public function route_params_are_injected_into_controller_class() $this->assertSame('$postId: 1 $commentId: 2', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function typehints_are_injected_into_controller_class() { $container = new \DI\Container(); @@ -185,7 +185,7 @@ public function typehints_are_injected_into_controller_class() $this->assertSame('abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function typehints_are_injected_into_controller_class_with_params() { $container = new \DI\Container(); @@ -203,7 +203,7 @@ public function typehints_are_injected_into_controller_class_with_params() $this->assertSame('$postId: 1 $commentId: 2 TestService: abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function can_inject_request_object() { $container = new \DI\Container(); @@ -229,7 +229,7 @@ public function can_inject_request_object() $this->assertSame('abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function can_inject_request_object_with_a_body() { $container = new \DI\Container(); @@ -256,7 +256,7 @@ public function can_inject_request_object_with_a_body() $this->assertSame('abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function can_inject_request_sub_class() { $container = new \DI\Container(); @@ -283,7 +283,7 @@ public function can_inject_request_sub_class() $this->assertSame('abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function constructor_params_are_injected_into_controller_class() { $container = new \DI\Container(); diff --git a/tests/RouterMiddlewareTest.php b/tests/RouterMiddlewareTest.php index 9ce57df..8f6e2e3 100644 --- a/tests/RouterMiddlewareTest.php +++ b/tests/RouterMiddlewareTest.php @@ -14,11 +14,10 @@ use Rareloop\Router\Test\Controllers\MiddlewareProvidingController; use Rareloop\Router\Test\Middleware\AddHeaderMiddleware; use Laminas\Diactoros\ServerRequest; -use PHPUnit\Framework\Attributes\Test; class RouterMiddlewareTest extends TestCase { - #[Test] + /** @test */ public function can_add_middleware_as_a_closure_to_a_route() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -44,7 +43,7 @@ public function can_add_middleware_as_a_closure_to_a_route() $this->assertSame('value', $response->getHeader('X-Key')[0]); } - #[Test] + /** @test */ public function can_add_middleware_as_an_object_to_a_route() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -64,7 +63,7 @@ public function can_add_middleware_as_an_object_to_a_route() $this->assertSame('value', $response->getHeader('X-Key')[0]); } - #[Test] + /** @test */ public function can_add_multiple_middleware_to_a_route_in_successive_calls() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -83,7 +82,7 @@ public function can_add_multiple_middleware_to_a_route_in_successive_calls() $this->assertSame('123', $response->getHeader('X-Key2')[0]); } - #[Test] + /** @test */ public function can_add_multiple_middleware_to_a_route_in_a_single_call() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -103,7 +102,7 @@ public function can_add_multiple_middleware_to_a_route_in_a_single_call() $this->assertSame('123', $response->getHeader('X-Key2')[0]); } - #[Test] + /** @test */ public function can_add_multiple_middleware_to_a_route_as_an_array() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -123,7 +122,7 @@ public function can_add_multiple_middleware_to_a_route_as_an_array() $this->assertSame('123', $response->getHeader('X-Key2')[0]); } - #[Test] + /** @test */ public function can_add_middleware_to_a_group() { $request = new ServerRequest([], [], '/all', 'GET'); @@ -134,7 +133,9 @@ public function can_add_middleware_to_a_group() $count++; $this->assertInstanceOf(RouteGroup::class, $group); - $group->get('all', fn() => 'abc123'); + $group->get('all', function () { + return 'abc123'; + }); }); $response = $router->match($request); @@ -145,7 +146,7 @@ public function can_add_middleware_to_a_group() $this->assertSame('abc', $response->getHeader('X-Key')[0]); } - #[Test] + /** @test */ public function can_add_single_middleware_to_a_group_without_wrapping_in_array() { $request = new ServerRequest([], [], '/all', 'GET'); @@ -156,7 +157,9 @@ public function can_add_single_middleware_to_a_group_without_wrapping_in_array() $count++; $this->assertInstanceOf(RouteGroup::class, $group); - $group->get('all', fn() => 'abc123'); + $group->get('all', function () { + return 'abc123'; + }); }); $response = $router->match($request); @@ -167,7 +170,7 @@ public function can_add_single_middleware_to_a_group_without_wrapping_in_array() $this->assertSame('abc', $response->getHeader('X-Key')[0]); } - #[Test] + /** @test */ public function can_add_base_middleware_to_be_applied_to_all_routes() { $router = new Router; @@ -202,7 +205,7 @@ public function can_add_base_middleware_to_be_applied_to_all_routes() $this->assertSame('abc', $response2->getHeader('X-Key')[0]); } - #[Test] + /** @test */ public function can_resolve_middleware_on_a_route_using_a_custom_resolver() { $resolver = $this->createMockMiddlewareResolverWithHeader('X-Key', 'abc'); @@ -218,7 +221,7 @@ public function can_resolve_middleware_on_a_route_using_a_custom_resolver() $this->assertSame('abc', $response->getHeader('X-Key')[0]); } - #[Test] + /** @test */ public function can_resolve_middleware_on_a_group_using_a_custom_resolver() { $resolver = $this->createMockMiddlewareResolverWithHeader('X-Key', 'abc'); @@ -236,7 +239,7 @@ public function can_resolve_middleware_on_a_group_using_a_custom_resolver() $this->assertSame('abc', $response->getHeader('X-Key')[0]); } - #[Test] + /** @test */ public function can_resolve_global_middleware_using_a_custom_resolver() { $resolver = $this->createMockMiddlewareResolverWithHeader('X-Key', 'abc'); @@ -258,7 +261,9 @@ private function createMockMiddlewareResolverWithHeader($header, $value) $middleware = new AddHeaderMiddleware($header, $value); $resolver = Mockery::mock(MiddlewareResolver::class); $resolver->shouldReceive('resolve')->with('middleware-key')->andReturn($middleware); - $resolver->shouldReceive('resolve')->with(Mockery::type('callable'))->andReturnUsing(fn($argument) => $argument); + $resolver->shouldReceive('resolve')->with(Mockery::type('callable'))->andReturnUsing(function ($argument) { + return $argument; + }); return $resolver; } diff --git a/tests/RouterParamsTest.php b/tests/RouterParamsTest.php index 976ccc0..63ac8e6 100644 --- a/tests/RouterParamsTest.php +++ b/tests/RouterParamsTest.php @@ -4,11 +4,10 @@ use PHPUnit\Framework\TestCase; use Rareloop\Router\RouteParams; -use PHPUnit\Framework\Attributes\Test; class RouterParamsTest extends TestCase { - #[Test] + /** @test */ public function can_get_param_by_key() { $params = new RouteParams(['key' => 'value']); @@ -16,7 +15,7 @@ public function can_get_param_by_key() $this->assertSame('value', $params->key); } - #[Test] + /** @test */ public function can_iterate_all_keys_and_values() { $params = new RouteParams([ @@ -37,7 +36,7 @@ public function can_iterate_all_keys_and_values() $this->assertSame(['value1', 'value2', 'value3'], $values); } - #[Test] + /** @test */ public function return_null_when_a_key_is_not_found() { $params = new RouteParams(['key' => 'value']); @@ -45,7 +44,7 @@ public function return_null_when_a_key_is_not_found() $this->assertNull($params->invalid); } - #[Test] + /** @test */ public function can_get_params_as_array() { $data = ['key1' => 'value1', 'key2' => 'value2']; diff --git a/tests/RouterTest.php b/tests/RouterTest.php index 2a864d7..16184f7 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -18,11 +18,10 @@ use Laminas\Diactoros\Response; use Laminas\Diactoros\Response\TextResponse; use Laminas\Diactoros\ServerRequest; -use PHPUnit\Framework\Attributes\Test; class RouterTest extends TestCase { - #[Test] + /** @test */ public function map_returns_a_route_object() { $router = new Router; @@ -34,7 +33,7 @@ public function map_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - #[Test] + /** @test */ public function map_accepts_lowercase_verbs() { $router = new Router; @@ -44,7 +43,7 @@ public function map_accepts_lowercase_verbs() $this->assertSame(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], $route->getMethods()); } - #[Test] + /** @test */ public function get_returns_a_route_object() { $router = new Router; @@ -56,7 +55,7 @@ public function get_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - #[Test] + /** @test */ public function post_returns_a_route_object() { $router = new Router; @@ -68,7 +67,7 @@ public function post_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - #[Test] + /** @test */ public function patch_returns_a_route_object() { $router = new Router; @@ -80,7 +79,7 @@ public function patch_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - #[Test] + /** @test */ public function put_returns_a_route_object() { $router = new Router; @@ -92,7 +91,7 @@ public function put_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - #[Test] + /** @test */ public function delete_returns_a_route_object() { $router = new Router; @@ -104,7 +103,7 @@ public function delete_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - #[Test] + /** @test */ public function options_returns_a_route_object() { $router = new Router; @@ -116,7 +115,7 @@ public function options_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - #[Test] + /** @test */ public function map_removes_trailing_slash_from_uri() { $router = new Router; @@ -128,7 +127,7 @@ public function map_removes_trailing_slash_from_uri() $this->assertSame('/test/123', $route->getUri()); } - #[Test] + /** @test */ public function no_return_from_route_action_results_in_a_204_status_code() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -144,7 +143,7 @@ public function no_return_from_route_action_results_in_a_204_status_code() $this->assertSame(204, $response->getStatusCode()); } - #[Test] + /** @test */ public function leading_slash_is_optional_when_creating_a_route() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -160,7 +159,7 @@ public function leading_slash_is_optional_when_creating_a_route() $this->assertInstanceOf(ResponseInterface::class, $response); } - #[Test] + /** @test */ public function matching_root_path_does_not_trigger_error() { $request = new ServerRequest([], [], '/', 'GET'); @@ -175,7 +174,7 @@ public function matching_root_path_does_not_trigger_error() $this->assertSame(0, $count); } - #[Test] + /** @test */ public function can_match_a_route_for_root_path() { $request = new ServerRequest([], [], '/', 'GET'); @@ -190,7 +189,7 @@ public function can_match_a_route_for_root_path() $this->assertSame(1, $count); } - #[Test] + /** @test */ public function match_returns_a_response_object() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -208,7 +207,7 @@ public function match_returns_a_response_object() $this->assertInstanceOf(ResponseInterface::class, $response); } - #[Test] + /** @test */ public function match_does_not_mutate_returned_response_object() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -223,7 +222,7 @@ public function match_does_not_mutate_returned_response_object() $this->assertSame($response, $routerResponse); } - #[Test] + /** @test */ public function match_returns_a_404_response_object_when_route_is_not_found() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -235,7 +234,7 @@ public function match_returns_a_404_response_object_when_route_is_not_found() $this->assertSame(404, $response->getStatusCode()); } - #[Test] + /** @test */ public function match_works_with_a_closure() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -253,7 +252,7 @@ public function match_works_with_a_closure() $this->assertSame('abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function match_uri_with_trailing_when_route_has_been_defined_without_trailing_slash() { $request = new ServerRequest([], [], '/test/123/', 'GET'); @@ -271,7 +270,7 @@ public function match_uri_with_trailing_when_route_has_been_defined_without_trai $this->assertSame('abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function match_uri_with_trailing_when_route_has_been_defined_with_trailing_slash() { $request = new ServerRequest([], [], '/test/123/', 'GET'); @@ -289,7 +288,7 @@ public function match_uri_with_trailing_when_route_has_been_defined_with_trailin $this->assertSame('abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function match_uri_without_trailing_when_route_has_been_defined_without_trailing_slash() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -307,7 +306,7 @@ public function match_uri_without_trailing_when_route_has_been_defined_without_t $this->assertSame('abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function match_uri_without_trailing_when_route_has_been_defined_with_trailing_slash() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -325,7 +324,7 @@ public function match_uri_without_trailing_when_route_has_been_defined_with_trai $this->assertSame('abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function match_works_with_a_class_and_method_string() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -337,7 +336,7 @@ public function match_works_with_a_class_and_method_string() $this->assertSame('Hello World', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function match_throws_exception_with_invalid_class_and_string_method() { $this->expectException(RouteClassStringParseException::class); @@ -347,7 +346,7 @@ public function match_throws_exception_with_invalid_class_and_string_method() $route = $router->get('/test/123', 'Rareloop\Router\Test\Controllers\TestController:returnHelloWorld'); } - #[Test] + /** @test */ public function match_throws_exception_when_class_and_string_method_contains_an_unfound_class() { $this->expectException(RouteClassStringControllerNotFoundException::class); @@ -357,7 +356,7 @@ public function match_throws_exception_when_class_and_string_method_contains_an_ $route = $router->get('/test/123', 'Rareloop\Router\Test\Controllers\UndefinedController@returnHelloWorld'); } - #[Test] + /** @test */ public function match_throws_exception_when_class_and_string_method_contains_an_unfound_method() { $this->expectException(RouteClassStringMethodNotFoundException::class); @@ -367,7 +366,7 @@ public function match_throws_exception_when_class_and_string_method_contains_an_ $route = $router->get('/test/123', 'Rareloop\Router\Test\Controllers\TestController@undefinedMethod'); } - #[Test] + /** @test */ public function params_are_parsed_and_passed_into_callback_function() { $request = new ServerRequest([], [], '/posts/123/comments/abc', 'GET'); @@ -385,7 +384,7 @@ public function params_are_parsed_and_passed_into_callback_function() $this->assertSame(1, $count); } - #[Test] + /** @test */ public function params_are_parsed_and_passed_into_callback_function_when_surrounded_by_whitespace() { $request = new ServerRequest([], [], '/posts/123/comments/abc', 'GET'); @@ -403,7 +402,7 @@ public function params_are_parsed_and_passed_into_callback_function_when_surroun $this->assertSame(1, $count); } - #[Test] + /** @test */ public function can_add_regex_constraints_on_params_as_key_value() { $matchingRequest = new ServerRequest([], [], '/posts/123/comments', 'GET'); @@ -420,7 +419,7 @@ public function can_add_regex_constraints_on_params_as_key_value() $this->assertSame(1, $count); } - #[Test] + /** @test */ public function can_add_multiple_regex_constraints_on_params_as_key_value() { $matchingRequest = new ServerRequest([], [], '/posts/123/comments/abc', 'GET'); @@ -437,7 +436,7 @@ public function can_add_multiple_regex_constraints_on_params_as_key_value() $this->assertSame(1, $count); } - #[Test] + /** @test */ public function can_add_regex_constraints_on_params_as_array() { $matchingRequest = new ServerRequest([], [], '/posts/123/comments', 'GET'); @@ -454,7 +453,7 @@ public function can_add_regex_constraints_on_params_as_array() $this->assertSame(1, $count); } - #[Test] + /** @test */ public function can_add_multiple_regex_constraints_on_params_as_array() { $matchingRequest = new ServerRequest([], [], '/posts/123/comments/abc', 'GET'); @@ -474,7 +473,7 @@ public function can_add_multiple_regex_constraints_on_params_as_array() $this->assertSame(1, $count); } - #[Test] + /** @test */ public function can_provide_optional_params() { $matchingRequest1 = new ServerRequest([], [], '/posts/123', 'GET'); @@ -495,7 +494,7 @@ public function can_provide_optional_params() $this->assertSame(2, $count); } - #[Test] + /** @test */ public function can_generate_canonical_uri_with_trailing_slash_for_named_route() { $router = new Router; @@ -505,7 +504,7 @@ public function can_generate_canonical_uri_with_trailing_slash_for_named_route() $this->assertSame('/posts/all/', $router->url('test.name')); } - #[Test] + /** @test */ public function can_generate_canonical_uri_with_trailing_slash_for_named_route_with_params() { $router = new Router; @@ -515,7 +514,7 @@ public function can_generate_canonical_uri_with_trailing_slash_for_named_route_w $this->assertSame('/posts/123/comments/', $router->url('test.name', ['id' => 123])); } - #[Test] + /** @test */ public function url_throws_exception_when_provided_params_fail_the_regex_constraints() { $this->expectException(RouteParamFailedConstraintException::class); @@ -529,7 +528,7 @@ public function url_throws_exception_when_provided_params_fail_the_regex_constra $router->url('test.name', ['id' => 123]); } - #[Test] + /** @test */ public function generating_a_url_for_a_named_route_that_doesnt_exist_throws_an_exception() { $this->expectException(NamedRouteNotFoundException::class); @@ -539,19 +538,19 @@ public function generating_a_url_for_a_named_route_that_doesnt_exist_throws_an_e $router->url('test.name'); } - #[Test] + /** @test */ public function can_generate_canonical_uri_after_match_has_been_called() { $router = new Router; $route = $router->get('/posts/all', function () {})->name('test.name'); $request = new ServerRequest([], [], '/does/not/match', 'GET'); - $router->match($request); + $router->match($request, 'GET'); $this->assertSame('/posts/all/', $router->url('test.name')); } - #[Test] + /** @test */ public function adding_routes_after_calling_url_throws_an_exception() { $this->expectException(TooLateToAddNewRouteException::class); @@ -564,7 +563,7 @@ public function adding_routes_after_calling_url_throws_an_exception() $route = $router->get('another/url', function () {}); } - #[Test] + /** @test */ public function adding_routes_after_calling_match_throws_an_exception() { $this->expectException(TooLateToAddNewRouteException::class); @@ -578,7 +577,7 @@ public function adding_routes_after_calling_match_throws_an_exception() $route = $router->get('another/url', function () {}); } - #[Test] + /** @test */ public function can_add_routes_in_a_group() { $request = new ServerRequest([], [], '/prefix/all', 'GET'); @@ -589,7 +588,9 @@ public function can_add_routes_in_a_group() $count++; $this->assertInstanceOf(RouteGroup::class, $group); - $group->get('all', fn() => 'abc123'); + $group->get('all', function () { + return 'abc123'; + }); }); $response = $router->match($request); @@ -598,7 +599,7 @@ public function can_add_routes_in_a_group() $this->assertSame('abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function can_add_routes_in_a_group_using_array_as_first_param() { $request = new ServerRequest([], [], '/prefix/all', 'GET'); @@ -609,7 +610,9 @@ public function can_add_routes_in_a_group_using_array_as_first_param() $count++; $this->assertInstanceOf(RouteGroup::class, $group); - $group->get('all', fn() => 'abc123'); + $group->get('all', function () { + return 'abc123'; + }); }); $response = $router->match($request); @@ -618,7 +621,7 @@ public function can_add_routes_in_a_group_using_array_as_first_param() $this->assertSame('abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function can_add_routes_in_a_group_using_array_as_first_param_with_no_prefix() { $request = new ServerRequest([], [], '/all', 'GET'); @@ -629,7 +632,9 @@ public function can_add_routes_in_a_group_using_array_as_first_param_with_no_pre $count++; $this->assertInstanceOf(RouteGroup::class, $group); - $group->get('all', fn() => 'abc123'); + $group->get('all', function () { + return 'abc123'; + }); }); $response = $router->match($request); @@ -638,7 +643,7 @@ public function can_add_routes_in_a_group_using_array_as_first_param_with_no_pre $this->assertSame('abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function group_prefixes_work_with_leading_slash() { $request = new ServerRequest([], [], '/prefix/all', 'GET'); @@ -649,7 +654,9 @@ public function group_prefixes_work_with_leading_slash() $count++; $this->assertInstanceOf(RouteGroup::class, $group); - $group->get('all', fn() => 'abc123'); + $group->get('all', function () { + return 'abc123'; + }); }); $response = $router->match($request); @@ -658,7 +665,7 @@ public function group_prefixes_work_with_leading_slash() $this->assertSame('abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function group_prefixes_work_with_trailing_slash() { $request = new ServerRequest([], [], '/prefix/all', 'GET'); @@ -669,7 +676,9 @@ public function group_prefixes_work_with_trailing_slash() $count++; $this->assertInstanceOf(RouteGroup::class, $group); - $group->get('all', fn() => 'abc123'); + $group->get('all', function () { + return 'abc123'; + }); }); $response = $router->match($request); @@ -678,7 +687,7 @@ public function group_prefixes_work_with_trailing_slash() $this->assertSame('abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function can_add_routes_in_nested_groups() { $request = new ServerRequest([], [], '/prefix/prefix2/all', 'GET'); @@ -693,7 +702,9 @@ public function can_add_routes_in_nested_groups() $count++; $this->assertInstanceOf(RouteGroup::class, $group); - $group->get('all', fn() => 'abc123'); + $group->get('all', function () { + return 'abc123'; + }); }); }); $response = $router->match($request); @@ -703,7 +714,7 @@ public function can_add_routes_in_nested_groups() $this->assertSame('abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function can_add_routes_in_nested_groups_with_array_syntax() { $request = new ServerRequest([], [], '/prefix/prefix2/all', 'GET'); @@ -718,7 +729,9 @@ public function can_add_routes_in_nested_groups_with_array_syntax() $count++; $this->assertInstanceOf(RouteGroup::class, $group); - $group->get('all', fn() => 'abc123'); + $group->get('all', function () { + return 'abc123'; + }); }); }); $response = $router->match($request); @@ -728,7 +741,7 @@ public function can_add_routes_in_nested_groups_with_array_syntax() $this->assertSame('abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function can_set_base_path() { $request = new ServerRequest([], [], '/base-path/prefix/all', 'GET'); @@ -747,7 +760,7 @@ public function can_set_base_path() $this->assertSame('abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function can_set_base_path_without_trailing_slash() { $request = new ServerRequest([], [], '/base-path/prefix/all', 'GET'); @@ -766,7 +779,7 @@ public function can_set_base_path_without_trailing_slash() $this->assertSame('abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function can_set_base_path_without_leading_slash() { $request = new ServerRequest([], [], '/base-path/prefix/all', 'GET'); @@ -785,7 +798,7 @@ public function can_set_base_path_without_leading_slash() $this->assertSame('abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function can_set_base_path_without_leading_or_trailing_slash() { $request = new ServerRequest([], [], '/base-path/prefix/all', 'GET'); @@ -804,7 +817,7 @@ public function can_set_base_path_without_leading_or_trailing_slash() $this->assertSame('abc123', $response->getBody()->getContents()); } - #[Test] + /** @test */ public function can_update_base_path_after_match_has_been_called() { $router = new Router; @@ -830,7 +843,7 @@ public function can_update_base_path_after_match_has_been_called() $this->assertSame('abc123', $response2->getBody()->getContents()); } - #[Test] + /** @test */ public function can_add_middleware_as_a_closure_to_a_route() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -855,7 +868,7 @@ public function can_add_middleware_as_a_closure_to_a_route() $this->assertSame('value', $response->getHeader('X-key')[0]); } - #[Test] + /** @test */ public function can_get_currently_matched_route() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -874,7 +887,7 @@ public function can_get_currently_matched_route() $this->assertSame($route, $router->currentRoute()); } - #[Test] + /** @test */ public function can_get_currently_matched_route_name() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -893,31 +906,35 @@ public function can_get_currently_matched_route_name() $this->assertSame('test123', $router->currentRouteName()); } - #[Test] + /** @test */ public function current_route_name_returns_null_when_match_not_yet_called() { $request = new ServerRequest([], [], '/test/123', 'GET'); $router = new Router; - $route = $router->get('/test/123', fn() => 'abc123')->name('test123'); + $route = $router->get('/test/123', function () { + return 'abc123'; + })->name('test123'); $this->assertSame(null, $router->currentRouteName()); } - #[Test] + /** @test */ public function current_route_name_returns_null_when_matched_route_has_no_name() { $request = new ServerRequest([], [], '/test/123', 'GET'); $router = new Router; - $route = $router->get('/test/123', fn() => 'abc123'); + $route = $router->get('/test/123', function () { + return 'abc123'; + }); $response = $router->match($request); $this->assertSame(null, $router->currentRouteName()); } - #[Test] + /** @test */ public function can_get_list_of_registered_routes() { $router = new Router; @@ -936,7 +953,9 @@ public function can_get_list_of_registered_routes() */ public function can_extend_post_behaviour_with_macros() { - Router::macro('testFunctionAddedByMacro', fn() => 'abc123'); + Router::macro('testFunctionAddedByMacro', function () { + return 'abc123'; + }); $queryBuilder = new Router(); @@ -961,6 +980,8 @@ class RouterMixin { function testFunctionAddedByMixin() { - return fn() => 'abc123'; + return function() { + return 'abc123'; + }; } } diff --git a/tests/Services/TestService.php b/tests/Services/TestService.php index a61349e..6007d6c 100644 --- a/tests/Services/TestService.php +++ b/tests/Services/TestService.php @@ -4,7 +4,10 @@ class TestService { - public function __construct(public $value) + public $value; + + public function __construct($value) { + $this->value = $value; } } diff --git a/tests/TypeHintRequestResolverTest.php b/tests/TypeHintRequestResolverTest.php index 26eccd8..31efbef 100644 --- a/tests/TypeHintRequestResolverTest.php +++ b/tests/TypeHintRequestResolverTest.php @@ -5,11 +5,10 @@ use PHPUnit\Framework\TestCase; use Rareloop\Router\TypeHintRequestResolver; use Laminas\Diactoros\ServerRequest; -use PHPUnit\Framework\Attributes\Test; class TypeHintRequestResolverTest extends TestCase { - #[Test] + /** @test */ public function returns_resolved_parameters_when_no_request_is_set() { $reflectionFunction = new \ReflectionFunction(function () {}); @@ -21,7 +20,7 @@ public function returns_resolved_parameters_when_no_request_is_set() $this->assertSame($resolvedParameters, $params); } - #[Test] + /** @test */ public function can_resolve_a_request() { $request = new ServerRequest([], [], '/injected', 'GET'); @@ -34,7 +33,7 @@ public function can_resolve_a_request() $this->assertSame('/injected', $params[0]->getUri()->getPath()); } - #[Test] + /** @test */ public function does_not_attempt_to_resolve_params_that_have_already_been_resolved() { $preResolvedRequest = new ServerRequest([], [], '/pre/resolved', 'GET'); From 837274e8008d0247c7d72f313354c816bce732e0 Mon Sep 17 00:00:00 2001 From: Adam Tomat Date: Mon, 13 Oct 2025 16:18:34 +0100 Subject: [PATCH 11/13] Remove unused namespace --- tests/ControllerTest.php | 1 - tests/RouterDITest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/ControllerTest.php b/tests/ControllerTest.php index 5814800..6355f08 100644 --- a/tests/ControllerTest.php +++ b/tests/ControllerTest.php @@ -11,7 +11,6 @@ use Rareloop\Router\Test\Controllers\MiddlewareProvidingController; use Rareloop\Router\Test\Middleware\AddHeaderMiddleware; use Laminas\Diactoros\ServerRequest; -use PHPUnit\Framework\Attributes\Test; class ControllerTest extends TestCase { diff --git a/tests/RouterDITest.php b/tests/RouterDITest.php index 5d7d67c..807728f 100644 --- a/tests/RouterDITest.php +++ b/tests/RouterDITest.php @@ -10,7 +10,6 @@ use Rareloop\Router\Test\Requests\TestRequest; use Rareloop\Router\Test\Services\TestService; use Laminas\Diactoros\ServerRequest; -use PHPUnit\Framework\Attributes\Test; class RouterDITest extends TestCase { From c4ab8c595f5bc82ee6846b156a36d339eda558e3 Mon Sep 17 00:00:00 2001 From: Adam Tomat Date: Mon, 13 Oct 2025 16:26:05 +0100 Subject: [PATCH 12/13] Fix unit tests --- tests/ControllerMiddlewareOptionsTest.php | 15 +-- tests/ControllerMiddlewareTest.php | 5 +- tests/ControllerTest.php | 17 +-- tests/FormattingTest.php | 13 +-- tests/ResponseFactoryTest.php | 9 +- tests/RouteGroupTest.php | 27 +++-- tests/RouteTest.php | 43 ++++---- tests/RouterDITest.php | 29 ++--- tests/RouterMiddlewareTest.php | 23 ++-- tests/RouterParamsTest.php | 9 +- tests/RouterTest.php | 125 +++++++++++----------- tests/TypeHintRequestResolverTest.php | 9 +- 12 files changed, 162 insertions(+), 162 deletions(-) diff --git a/tests/ControllerMiddlewareOptionsTest.php b/tests/ControllerMiddlewareOptionsTest.php index a5d340b..4df418a 100644 --- a/tests/ControllerMiddlewareOptionsTest.php +++ b/tests/ControllerMiddlewareOptionsTest.php @@ -4,10 +4,11 @@ use PHPUnit\Framework\TestCase; use Rareloop\Router\ControllerMiddlewareOptions; +use PHPUnit\Framework\Attributes\Test; class ControllerMiddlewareOptionsTest extends TestCase { - /** @test */ + #[Test] public function by_default_no_methods_are_excluded() { $options = new ControllerMiddlewareOptions; @@ -16,7 +17,7 @@ public function by_default_no_methods_are_excluded() $this->assertFalse($options->excludedForMethod('bar')); } - /** @test */ + #[Test] public function only_is_chainable() { $options = new ControllerMiddlewareOptions; @@ -24,7 +25,7 @@ public function only_is_chainable() $this->assertSame($options, $options->only('foo')); } - /** @test */ + #[Test] public function can_use_only_to_limit_methods() { $options = new ControllerMiddlewareOptions; @@ -35,7 +36,7 @@ public function can_use_only_to_limit_methods() $this->assertTrue($options->excludedForMethod('bar')); } - /** @test */ + #[Test] public function can_use_only_to_limit_multiple_methods() { $options = new ControllerMiddlewareOptions; @@ -47,7 +48,7 @@ public function can_use_only_to_limit_multiple_methods() $this->assertTrue($options->excludedForMethod('baz')); } - /** @test */ + #[Test] public function except_is_chainable() { $options = new ControllerMiddlewareOptions; @@ -55,7 +56,7 @@ public function except_is_chainable() $this->assertSame($options, $options->except('foo')); } - /** @test */ + #[Test] public function can_use_except_to_limit_methods() { $options = new ControllerMiddlewareOptions; @@ -66,7 +67,7 @@ public function can_use_except_to_limit_methods() $this->assertFalse($options->excludedForMethod('bar')); } - /** @test */ + #[Test] public function can_use_except_to_limit_multiple_methods() { $options = new ControllerMiddlewareOptions; diff --git a/tests/ControllerMiddlewareTest.php b/tests/ControllerMiddlewareTest.php index 114cca6..66e29e9 100644 --- a/tests/ControllerMiddlewareTest.php +++ b/tests/ControllerMiddlewareTest.php @@ -6,10 +6,11 @@ use Rareloop\Router\ControllerMiddleware; use Rareloop\Router\ControllerMiddlewareOptions; use Rareloop\Router\Test\Middleware\AddHeaderMiddleware; +use PHPUnit\Framework\Attributes\Test; class ControllerMiddlewareTest extends TestCase { - /** @test */ + #[Test] public function can_retrieve_middleware() { $middleware = new AddHeaderMiddleware('X-Header', 'testing123'); @@ -20,7 +21,7 @@ public function can_retrieve_middleware() $this->assertSame($middleware, $controllerMiddleware->middleware()); } - /** @test */ + #[Test] public function can_retrieve_options() { $middleware = new AddHeaderMiddleware('X-Header', 'testing123'); diff --git a/tests/ControllerTest.php b/tests/ControllerTest.php index 6355f08..217ed04 100644 --- a/tests/ControllerTest.php +++ b/tests/ControllerTest.php @@ -11,10 +11,11 @@ use Rareloop\Router\Test\Controllers\MiddlewareProvidingController; use Rareloop\Router\Test\Middleware\AddHeaderMiddleware; use Laminas\Diactoros\ServerRequest; +use PHPUnit\Framework\Attributes\Test; class ControllerTest extends TestCase { - /** @test */ + #[Test] public function can_add_single_middleware_via_controller() { $container = new \DI\Container(); @@ -36,7 +37,7 @@ public function can_add_single_middleware_via_controller() $this->assertSame('testing123', $response->getHeader('X-Header')[0]); } - /** @test */ + #[Test] public function can_resolve_middleware_on_a_controller_using_custom_resolver() { $container = new \DI\Container(); @@ -59,7 +60,7 @@ public function can_resolve_middleware_on_a_controller_using_custom_resolver() $this->assertSame('testing123', $response->getHeader('X-Header')[0]); } - /** @test */ + #[Test] public function can_add_multiple_middleware_as_array_via_controller() { $container = new \DI\Container(); @@ -86,7 +87,7 @@ public function can_add_multiple_middleware_as_array_via_controller() $this->assertSame('testing456', $response->getHeader('X-Header-2')[0]); } - /** @test */ + #[Test] public function controller_middleware_method_returns_options() { $controller = new MiddlewareProvidingController; @@ -96,7 +97,7 @@ public function controller_middleware_method_returns_options() $this->assertInstanceOf(ControllerMiddlewareOptions::class, $options); } - /** @test */ + #[Test] public function middleware_can_be_limited_to_methods_using_only() { $container = new \DI\Container(); @@ -115,7 +116,7 @@ public function middleware_can_be_limited_to_methods_using_only() $this->assertMiddlewareIsAppliedToMethods($router, $middlewareAppliedToMethods); } - /** @test */ + #[Test] public function middleware_can_be_limited_to_multiple_methods_using_only() { $container = new \DI\Container(); @@ -134,7 +135,7 @@ public function middleware_can_be_limited_to_multiple_methods_using_only() $this->assertMiddlewareIsAppliedToMethods($router, $middlewareAppliedToMethods); } - /** @test */ + #[Test] public function middleware_can_be_limited_to_methods_using_except() { $container = new \DI\Container(); @@ -153,7 +154,7 @@ public function middleware_can_be_limited_to_methods_using_except() $this->assertMiddlewareIsAppliedToMethods($router, $middlewareAppliedToMethods); } - /** @test */ + #[Test] public function middleware_can_be_limited_to_multiple_methods_using_except() { $container = new \DI\Container(); diff --git a/tests/FormattingTest.php b/tests/FormattingTest.php index fa718c3..c0a9dac 100644 --- a/tests/FormattingTest.php +++ b/tests/FormattingTest.php @@ -4,10 +4,11 @@ use PHPUnit\Framework\TestCase; use Rareloop\Router\Helpers\Formatting; +use PHPUnit\Framework\Attributes\Test; class FormattingTest extends TestCase { - /** @test */ + #[Test] public function can_remove_trialing_slash() { $string = 'string/'; @@ -15,7 +16,7 @@ public function can_remove_trialing_slash() $this->assertSame('string', Formatting::removeTrailingSlash($string)); } - /** @test */ + #[Test] public function can_add_trialing_slash() { $string = 'string'; @@ -23,7 +24,7 @@ public function can_add_trialing_slash() $this->assertSame('string/', Formatting::addTrailingSlash($string)); } - /** @test */ + #[Test] public function add_trialing_slash_does_not_produce_duplicates() { $string = 'string/'; @@ -31,7 +32,7 @@ public function add_trialing_slash_does_not_produce_duplicates() $this->assertSame('string/', Formatting::addTrailingSlash($string)); } - /** @test */ + #[Test] public function can_remove_leading_slash() { $string = '/string'; @@ -39,7 +40,7 @@ public function can_remove_leading_slash() $this->assertSame('string', Formatting::removeLeadingSlash($string)); } - /** @test */ + #[Test] public function can_add_leading_slash() { $string = 'string'; @@ -47,7 +48,7 @@ public function can_add_leading_slash() $this->assertSame('/string', Formatting::addLeadingSlash($string)); } - /** @test */ + #[Test] public function add_leading_slash_does_not_produce_duplicates() { $string = '/string'; diff --git a/tests/ResponseFactoryTest.php b/tests/ResponseFactoryTest.php index 3cc2eaf..d596149 100644 --- a/tests/ResponseFactoryTest.php +++ b/tests/ResponseFactoryTest.php @@ -10,6 +10,7 @@ use Laminas\Diactoros\Response\EmptyResponse; use Laminas\Diactoros\Response\TextResponse; use Laminas\Diactoros\ServerRequest; +use PHPUnit\Framework\Attributes\Test; class ResponseFactoryTest extends TestCase { @@ -24,7 +25,7 @@ public function setUp(): void $this->request = new ServerRequest([], [], '/test/123', 'GET'); } - /** @test */ + #[Test] public function when_passed_a_response_instance_the_same_object_is_returned() { $response = new TextResponse('Testing', 200); @@ -32,7 +33,7 @@ public function when_passed_a_response_instance_the_same_object_is_returned() $this->assertSame($response, ResponseFactory::create($this->request, $response)); } - /** @test */ + #[Test] public function when_passed_a_non_response_instance_a_response_object_is_returned() { $response = ResponseFactory::create($this->request, 'Testing'); @@ -41,7 +42,7 @@ public function when_passed_a_non_response_instance_a_response_object_is_returne $this->assertSame('Testing', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function when_nothing_is_passed_an_empty_response_object_is_returned() { $response = ResponseFactory::create($this->request); @@ -49,7 +50,7 @@ public function when_nothing_is_passed_an_empty_response_object_is_returned() $this->assertInstanceOf(EmptyResponse::class, $response); } - /** @test */ + #[Test] public function when_a_responsable_object_is_passed_the_response_object_is_returned() { $textResponse = new TextResponse('testing123'); diff --git a/tests/RouteGroupTest.php b/tests/RouteGroupTest.php index b334314..43cc948 100644 --- a/tests/RouteGroupTest.php +++ b/tests/RouteGroupTest.php @@ -6,10 +6,11 @@ use Rareloop\Router\Route; use Rareloop\Router\RouteGroup; use Rareloop\Router\Router; +use PHPUnit\Framework\Attributes\Test; class RouteGroupTest extends TestCase { - /** @test */ + #[Test] public function group_function_is_chainable() { $router = new Router; @@ -17,7 +18,7 @@ public function group_function_is_chainable() $this->assertInstanceOf(Router::class, $router->group('test/123', function () {})); } - /** @test */ + #[Test] public function can_add_get_request_to_a_group() { $router = new Router; @@ -35,7 +36,7 @@ public function can_add_get_request_to_a_group() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_add_request_to_a_group_with_leading_slash() { $router = new Router; @@ -53,7 +54,7 @@ public function can_add_request_to_a_group_with_leading_slash() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_add_post_request_to_a_group() { $router = new Router; @@ -71,7 +72,7 @@ public function can_add_post_request_to_a_group() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_add_put_request_to_a_group() { $router = new Router; @@ -89,7 +90,7 @@ public function can_add_put_request_to_a_group() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_add_patch_request_to_a_group() { $router = new Router; @@ -107,7 +108,7 @@ public function can_add_patch_request_to_a_group() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_add_delete_request_to_a_group() { $router = new Router; @@ -125,7 +126,7 @@ public function can_add_delete_request_to_a_group() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_add_options_request_to_a_group() { $router = new Router; @@ -143,9 +144,7 @@ public function can_add_options_request_to_a_group() $this->assertSame(1, $count); } - /** - * @test - */ + #[Test] public function can_extend_post_behaviour_with_macros() { RouteGroup::macro('testFunctionAddedByMacro', function () { @@ -158,9 +157,7 @@ public function can_extend_post_behaviour_with_macros() $this->assertSame('abc123', RouteGroup::testFunctionAddedByMacro()); } - /** - * @test - */ + #[Test] public function can_extend_post_behaviour_with_mixin() { RouteGroup::mixin(new RouteGroupMixin); @@ -175,7 +172,7 @@ class RouteGroupMixin { function testFunctionAddedByMixin() { - return function() { + return function () { return 'abc123'; }; } diff --git a/tests/RouteTest.php b/tests/RouteTest.php index c5a7a03..cdfad2f 100644 --- a/tests/RouteTest.php +++ b/tests/RouteTest.php @@ -2,15 +2,16 @@ namespace Rareloop\Router\Test; +use Rareloop\Router\Route; +use Rareloop\Router\Router; use InvalidArgumentException; use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\Test; use Rareloop\Router\Exceptions\RouteNameRedefinedException; -use Rareloop\Router\Route; -use Rareloop\Router\Router; class RouteTest extends TestCase { - /** @test */ + #[Test] public function a_route_can_be_named() { $router = new Router; @@ -20,7 +21,7 @@ public function a_route_can_be_named() $this->assertTrue($router->has('test')); } - /** @test */ + #[Test] public function name_function_is_chainable() { $router = new Router; @@ -28,7 +29,7 @@ public function name_function_is_chainable() $this->assertInstanceOf(Route::class, $router->get('test/123', function () {})->name('test')); } - /** @test */ + #[Test] public function a_route_can_not_be_renamed() { $this->expectException(RouteNameRedefinedException::class); @@ -38,7 +39,7 @@ public function a_route_can_not_be_renamed() $route = $router->get('test/123', function () {})->name('test1')->name('test2'); } - /** @test */ + #[Test] public function where_function_is_chainable() { $router = new Router; @@ -46,7 +47,7 @@ public function where_function_is_chainable() $this->assertInstanceOf(Route::class, $router->get('test/{id}', function () {})->where('id', '[0-9]+')); } - /** @test */ + #[Test] public function where_function_is_chainable_when_passed_an_array() { $router = new Router; @@ -54,7 +55,7 @@ public function where_function_is_chainable_when_passed_an_array() $this->assertInstanceOf(Route::class, $router->get('test/{id}', function () {})->where(['id' => '[0-9]+'])); } - /** @test */ + #[Test] public function where_function_throws_exception_when_no_params_provided() { $this->expectException(InvalidArgumentException::class); @@ -64,7 +65,7 @@ public function where_function_throws_exception_when_no_params_provided() $this->assertInstanceOf(Route::class, $router->get('test/{id}', function () {})->where()); } - /** @test */ + #[Test] public function can_get_route_action_name_when_closure() { $router = new Router; @@ -73,37 +74,35 @@ public function can_get_route_action_name_when_closure() $this->assertSame('Closure', $route->getActionName()); } - /** @test */ + #[Test] public function can_get_route_action_name_when_callable() { $router = new Router; $route = $router->get('test/123', [TestCallableController::class, 'testStatic']); - $this->assertSame(TestCallableController::class.'@testStatic', $route->getActionName()); + $this->assertSame(TestCallableController::class . '@testStatic', $route->getActionName()); } - /** @test */ + #[Test] public function can_get_route_action_name_when_callable_instance() { $router = new Router; $controller = new TestCallableController; $route = $router->get('test/123', [$controller, 'test']); - $this->assertSame(TestCallableController::class.'@test', $route->getActionName()); + $this->assertSame(TestCallableController::class . '@test', $route->getActionName()); } - /** @test */ + #[Test] public function can_get_route_action_name_when_controller_string() { $router = new Router; - $route = $router->get('test/123', TestCallableController::class.'@test'); + $route = $router->get('test/123', TestCallableController::class . '@test'); - $this->assertSame(TestCallableController::class.'@test', $route->getActionName()); + $this->assertSame(TestCallableController::class . '@test', $route->getActionName()); } - /** - * @test - */ + #[Test] public function can_extend_post_behaviour_with_macros() { Route::macro('testFunctionAddedByMacro', function () { @@ -116,9 +115,7 @@ public function can_extend_post_behaviour_with_macros() $this->assertSame('abc123', Route::testFunctionAddedByMacro()); } - /** - * @test - */ + #[Test] public function can_extend_post_behaviour_with_mixin() { Route::mixin(new RouteMixin); @@ -139,7 +136,7 @@ class RouteMixin { function testFunctionAddedByMixin() { - return function() { + return function () { return 'abc123'; }; } diff --git a/tests/RouterDITest.php b/tests/RouterDITest.php index 807728f..01abe2f 100644 --- a/tests/RouterDITest.php +++ b/tests/RouterDITest.php @@ -10,10 +10,11 @@ use Rareloop\Router\Test\Requests\TestRequest; use Rareloop\Router\Test\Services\TestService; use Laminas\Diactoros\ServerRequest; +use PHPUnit\Framework\Attributes\Test; class RouterDITest extends TestCase { - /** @test */ + #[Test] public function can_pass_a_container_into_constructor() { $container = new \DI\Container(); @@ -22,7 +23,7 @@ public function can_pass_a_container_into_constructor() $this->assertInstanceOf(Router::class, $router); } - /** @test */ + #[Test] public function container_passed_to_constructor_must_be_psr_11_compatible() { $this->expectException(\TypeError::class); @@ -33,7 +34,7 @@ public function container_passed_to_constructor_must_be_psr_11_compatible() $this->assertInstanceOf(Router::class, $router); } - /** @test */ + #[Test] public function route_params_are_injected_into_closure() { $container = new \DI\Container(); @@ -57,7 +58,7 @@ public function route_params_are_injected_into_closure() $this->assertSame('abc', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function typehints_are_injected_into_closure() { $container = new \DI\Container(); @@ -84,7 +85,7 @@ public function typehints_are_injected_into_closure() $this->assertSame('abc', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function typehints_are_injected_into_closure_with_params() { $container = new \DI\Container(); @@ -113,7 +114,7 @@ public function typehints_are_injected_into_closure_with_params() $this->assertSame('abc', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function route_params_are_injected_into_closure_regardless_of_param_order() { $container = new \DI\Container(); @@ -137,7 +138,7 @@ public function route_params_are_injected_into_closure_regardless_of_param_order $this->assertSame('abc', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function reflection_error_is_thrown_when_typehints_cant_be_resolved_from_the_container() { $this->expectException(\ReflectionException::class); @@ -151,7 +152,7 @@ public function reflection_error_is_thrown_when_typehints_cant_be_resolved_from_ $response = $router->match($request); } - /** @test */ + #[Test] public function route_params_are_injected_into_controller_class() { $container = new \DI\Container(); @@ -166,7 +167,7 @@ public function route_params_are_injected_into_controller_class() $this->assertSame('$postId: 1 $commentId: 2', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function typehints_are_injected_into_controller_class() { $container = new \DI\Container(); @@ -184,7 +185,7 @@ public function typehints_are_injected_into_controller_class() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function typehints_are_injected_into_controller_class_with_params() { $container = new \DI\Container(); @@ -202,7 +203,7 @@ public function typehints_are_injected_into_controller_class_with_params() $this->assertSame('$postId: 1 $commentId: 2 TestService: abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_inject_request_object() { $container = new \DI\Container(); @@ -228,7 +229,7 @@ public function can_inject_request_object() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_inject_request_object_with_a_body() { $container = new \DI\Container(); @@ -255,7 +256,7 @@ public function can_inject_request_object_with_a_body() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_inject_request_sub_class() { $container = new \DI\Container(); @@ -282,7 +283,7 @@ public function can_inject_request_sub_class() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function constructor_params_are_injected_into_controller_class() { $container = new \DI\Container(); diff --git a/tests/RouterMiddlewareTest.php b/tests/RouterMiddlewareTest.php index 8f6e2e3..c42eae5 100644 --- a/tests/RouterMiddlewareTest.php +++ b/tests/RouterMiddlewareTest.php @@ -14,10 +14,11 @@ use Rareloop\Router\Test\Controllers\MiddlewareProvidingController; use Rareloop\Router\Test\Middleware\AddHeaderMiddleware; use Laminas\Diactoros\ServerRequest; +use PHPUnit\Framework\Attributes\Test; class RouterMiddlewareTest extends TestCase { - /** @test */ + #[Test] public function can_add_middleware_as_a_closure_to_a_route() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -43,7 +44,7 @@ public function can_add_middleware_as_a_closure_to_a_route() $this->assertSame('value', $response->getHeader('X-Key')[0]); } - /** @test */ + #[Test] public function can_add_middleware_as_an_object_to_a_route() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -63,7 +64,7 @@ public function can_add_middleware_as_an_object_to_a_route() $this->assertSame('value', $response->getHeader('X-Key')[0]); } - /** @test */ + #[Test] public function can_add_multiple_middleware_to_a_route_in_successive_calls() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -82,7 +83,7 @@ public function can_add_multiple_middleware_to_a_route_in_successive_calls() $this->assertSame('123', $response->getHeader('X-Key2')[0]); } - /** @test */ + #[Test] public function can_add_multiple_middleware_to_a_route_in_a_single_call() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -102,7 +103,7 @@ public function can_add_multiple_middleware_to_a_route_in_a_single_call() $this->assertSame('123', $response->getHeader('X-Key2')[0]); } - /** @test */ + #[Test] public function can_add_multiple_middleware_to_a_route_as_an_array() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -122,7 +123,7 @@ public function can_add_multiple_middleware_to_a_route_as_an_array() $this->assertSame('123', $response->getHeader('X-Key2')[0]); } - /** @test */ + #[Test] public function can_add_middleware_to_a_group() { $request = new ServerRequest([], [], '/all', 'GET'); @@ -146,7 +147,7 @@ public function can_add_middleware_to_a_group() $this->assertSame('abc', $response->getHeader('X-Key')[0]); } - /** @test */ + #[Test] public function can_add_single_middleware_to_a_group_without_wrapping_in_array() { $request = new ServerRequest([], [], '/all', 'GET'); @@ -170,7 +171,7 @@ public function can_add_single_middleware_to_a_group_without_wrapping_in_array() $this->assertSame('abc', $response->getHeader('X-Key')[0]); } - /** @test */ + #[Test] public function can_add_base_middleware_to_be_applied_to_all_routes() { $router = new Router; @@ -205,7 +206,7 @@ public function can_add_base_middleware_to_be_applied_to_all_routes() $this->assertSame('abc', $response2->getHeader('X-Key')[0]); } - /** @test */ + #[Test] public function can_resolve_middleware_on_a_route_using_a_custom_resolver() { $resolver = $this->createMockMiddlewareResolverWithHeader('X-Key', 'abc'); @@ -221,7 +222,7 @@ public function can_resolve_middleware_on_a_route_using_a_custom_resolver() $this->assertSame('abc', $response->getHeader('X-Key')[0]); } - /** @test */ + #[Test] public function can_resolve_middleware_on_a_group_using_a_custom_resolver() { $resolver = $this->createMockMiddlewareResolverWithHeader('X-Key', 'abc'); @@ -239,7 +240,7 @@ public function can_resolve_middleware_on_a_group_using_a_custom_resolver() $this->assertSame('abc', $response->getHeader('X-Key')[0]); } - /** @test */ + #[Test] public function can_resolve_global_middleware_using_a_custom_resolver() { $resolver = $this->createMockMiddlewareResolverWithHeader('X-Key', 'abc'); diff --git a/tests/RouterParamsTest.php b/tests/RouterParamsTest.php index 63ac8e6..976ccc0 100644 --- a/tests/RouterParamsTest.php +++ b/tests/RouterParamsTest.php @@ -4,10 +4,11 @@ use PHPUnit\Framework\TestCase; use Rareloop\Router\RouteParams; +use PHPUnit\Framework\Attributes\Test; class RouterParamsTest extends TestCase { - /** @test */ + #[Test] public function can_get_param_by_key() { $params = new RouteParams(['key' => 'value']); @@ -15,7 +16,7 @@ public function can_get_param_by_key() $this->assertSame('value', $params->key); } - /** @test */ + #[Test] public function can_iterate_all_keys_and_values() { $params = new RouteParams([ @@ -36,7 +37,7 @@ public function can_iterate_all_keys_and_values() $this->assertSame(['value1', 'value2', 'value3'], $values); } - /** @test */ + #[Test] public function return_null_when_a_key_is_not_found() { $params = new RouteParams(['key' => 'value']); @@ -44,7 +45,7 @@ public function return_null_when_a_key_is_not_found() $this->assertNull($params->invalid); } - /** @test */ + #[Test] public function can_get_params_as_array() { $data = ['key1' => 'value1', 'key2' => 'value2']; diff --git a/tests/RouterTest.php b/tests/RouterTest.php index 16184f7..bde398d 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -18,10 +18,11 @@ use Laminas\Diactoros\Response; use Laminas\Diactoros\Response\TextResponse; use Laminas\Diactoros\ServerRequest; +use PHPUnit\Framework\Attributes\Test; class RouterTest extends TestCase { - /** @test */ + #[Test] public function map_returns_a_route_object() { $router = new Router; @@ -33,7 +34,7 @@ public function map_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - /** @test */ + #[Test] public function map_accepts_lowercase_verbs() { $router = new Router; @@ -43,7 +44,7 @@ public function map_accepts_lowercase_verbs() $this->assertSame(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], $route->getMethods()); } - /** @test */ + #[Test] public function get_returns_a_route_object() { $router = new Router; @@ -55,7 +56,7 @@ public function get_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - /** @test */ + #[Test] public function post_returns_a_route_object() { $router = new Router; @@ -67,7 +68,7 @@ public function post_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - /** @test */ + #[Test] public function patch_returns_a_route_object() { $router = new Router; @@ -79,7 +80,7 @@ public function patch_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - /** @test */ + #[Test] public function put_returns_a_route_object() { $router = new Router; @@ -91,7 +92,7 @@ public function put_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - /** @test */ + #[Test] public function delete_returns_a_route_object() { $router = new Router; @@ -103,7 +104,7 @@ public function delete_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - /** @test */ + #[Test] public function options_returns_a_route_object() { $router = new Router; @@ -115,7 +116,7 @@ public function options_returns_a_route_object() $this->assertSame('/test/123', $route->getUri()); } - /** @test */ + #[Test] public function map_removes_trailing_slash_from_uri() { $router = new Router; @@ -127,7 +128,7 @@ public function map_removes_trailing_slash_from_uri() $this->assertSame('/test/123', $route->getUri()); } - /** @test */ + #[Test] public function no_return_from_route_action_results_in_a_204_status_code() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -143,7 +144,7 @@ public function no_return_from_route_action_results_in_a_204_status_code() $this->assertSame(204, $response->getStatusCode()); } - /** @test */ + #[Test] public function leading_slash_is_optional_when_creating_a_route() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -159,7 +160,7 @@ public function leading_slash_is_optional_when_creating_a_route() $this->assertInstanceOf(ResponseInterface::class, $response); } - /** @test */ + #[Test] public function matching_root_path_does_not_trigger_error() { $request = new ServerRequest([], [], '/', 'GET'); @@ -174,7 +175,7 @@ public function matching_root_path_does_not_trigger_error() $this->assertSame(0, $count); } - /** @test */ + #[Test] public function can_match_a_route_for_root_path() { $request = new ServerRequest([], [], '/', 'GET'); @@ -189,7 +190,7 @@ public function can_match_a_route_for_root_path() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function match_returns_a_response_object() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -207,7 +208,7 @@ public function match_returns_a_response_object() $this->assertInstanceOf(ResponseInterface::class, $response); } - /** @test */ + #[Test] public function match_does_not_mutate_returned_response_object() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -222,7 +223,7 @@ public function match_does_not_mutate_returned_response_object() $this->assertSame($response, $routerResponse); } - /** @test */ + #[Test] public function match_returns_a_404_response_object_when_route_is_not_found() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -234,7 +235,7 @@ public function match_returns_a_404_response_object_when_route_is_not_found() $this->assertSame(404, $response->getStatusCode()); } - /** @test */ + #[Test] public function match_works_with_a_closure() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -252,7 +253,7 @@ public function match_works_with_a_closure() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function match_uri_with_trailing_when_route_has_been_defined_without_trailing_slash() { $request = new ServerRequest([], [], '/test/123/', 'GET'); @@ -270,7 +271,7 @@ public function match_uri_with_trailing_when_route_has_been_defined_without_trai $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function match_uri_with_trailing_when_route_has_been_defined_with_trailing_slash() { $request = new ServerRequest([], [], '/test/123/', 'GET'); @@ -288,7 +289,7 @@ public function match_uri_with_trailing_when_route_has_been_defined_with_trailin $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function match_uri_without_trailing_when_route_has_been_defined_without_trailing_slash() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -306,7 +307,7 @@ public function match_uri_without_trailing_when_route_has_been_defined_without_t $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function match_uri_without_trailing_when_route_has_been_defined_with_trailing_slash() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -324,7 +325,7 @@ public function match_uri_without_trailing_when_route_has_been_defined_with_trai $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function match_works_with_a_class_and_method_string() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -336,7 +337,7 @@ public function match_works_with_a_class_and_method_string() $this->assertSame('Hello World', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function match_throws_exception_with_invalid_class_and_string_method() { $this->expectException(RouteClassStringParseException::class); @@ -346,7 +347,7 @@ public function match_throws_exception_with_invalid_class_and_string_method() $route = $router->get('/test/123', 'Rareloop\Router\Test\Controllers\TestController:returnHelloWorld'); } - /** @test */ + #[Test] public function match_throws_exception_when_class_and_string_method_contains_an_unfound_class() { $this->expectException(RouteClassStringControllerNotFoundException::class); @@ -356,7 +357,7 @@ public function match_throws_exception_when_class_and_string_method_contains_an_ $route = $router->get('/test/123', 'Rareloop\Router\Test\Controllers\UndefinedController@returnHelloWorld'); } - /** @test */ + #[Test] public function match_throws_exception_when_class_and_string_method_contains_an_unfound_method() { $this->expectException(RouteClassStringMethodNotFoundException::class); @@ -366,7 +367,7 @@ public function match_throws_exception_when_class_and_string_method_contains_an_ $route = $router->get('/test/123', 'Rareloop\Router\Test\Controllers\TestController@undefinedMethod'); } - /** @test */ + #[Test] public function params_are_parsed_and_passed_into_callback_function() { $request = new ServerRequest([], [], '/posts/123/comments/abc', 'GET'); @@ -384,7 +385,7 @@ public function params_are_parsed_and_passed_into_callback_function() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function params_are_parsed_and_passed_into_callback_function_when_surrounded_by_whitespace() { $request = new ServerRequest([], [], '/posts/123/comments/abc', 'GET'); @@ -402,7 +403,7 @@ public function params_are_parsed_and_passed_into_callback_function_when_surroun $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_add_regex_constraints_on_params_as_key_value() { $matchingRequest = new ServerRequest([], [], '/posts/123/comments', 'GET'); @@ -419,7 +420,7 @@ public function can_add_regex_constraints_on_params_as_key_value() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_add_multiple_regex_constraints_on_params_as_key_value() { $matchingRequest = new ServerRequest([], [], '/posts/123/comments/abc', 'GET'); @@ -436,7 +437,7 @@ public function can_add_multiple_regex_constraints_on_params_as_key_value() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_add_regex_constraints_on_params_as_array() { $matchingRequest = new ServerRequest([], [], '/posts/123/comments', 'GET'); @@ -453,7 +454,7 @@ public function can_add_regex_constraints_on_params_as_array() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_add_multiple_regex_constraints_on_params_as_array() { $matchingRequest = new ServerRequest([], [], '/posts/123/comments/abc', 'GET'); @@ -473,7 +474,7 @@ public function can_add_multiple_regex_constraints_on_params_as_array() $this->assertSame(1, $count); } - /** @test */ + #[Test] public function can_provide_optional_params() { $matchingRequest1 = new ServerRequest([], [], '/posts/123', 'GET'); @@ -494,7 +495,7 @@ public function can_provide_optional_params() $this->assertSame(2, $count); } - /** @test */ + #[Test] public function can_generate_canonical_uri_with_trailing_slash_for_named_route() { $router = new Router; @@ -504,7 +505,7 @@ public function can_generate_canonical_uri_with_trailing_slash_for_named_route() $this->assertSame('/posts/all/', $router->url('test.name')); } - /** @test */ + #[Test] public function can_generate_canonical_uri_with_trailing_slash_for_named_route_with_params() { $router = new Router; @@ -514,7 +515,7 @@ public function can_generate_canonical_uri_with_trailing_slash_for_named_route_w $this->assertSame('/posts/123/comments/', $router->url('test.name', ['id' => 123])); } - /** @test */ + #[Test] public function url_throws_exception_when_provided_params_fail_the_regex_constraints() { $this->expectException(RouteParamFailedConstraintException::class); @@ -528,7 +529,7 @@ public function url_throws_exception_when_provided_params_fail_the_regex_constra $router->url('test.name', ['id' => 123]); } - /** @test */ + #[Test] public function generating_a_url_for_a_named_route_that_doesnt_exist_throws_an_exception() { $this->expectException(NamedRouteNotFoundException::class); @@ -538,7 +539,7 @@ public function generating_a_url_for_a_named_route_that_doesnt_exist_throws_an_e $router->url('test.name'); } - /** @test */ + #[Test] public function can_generate_canonical_uri_after_match_has_been_called() { $router = new Router; @@ -550,7 +551,7 @@ public function can_generate_canonical_uri_after_match_has_been_called() $this->assertSame('/posts/all/', $router->url('test.name')); } - /** @test */ + #[Test] public function adding_routes_after_calling_url_throws_an_exception() { $this->expectException(TooLateToAddNewRouteException::class); @@ -563,7 +564,7 @@ public function adding_routes_after_calling_url_throws_an_exception() $route = $router->get('another/url', function () {}); } - /** @test */ + #[Test] public function adding_routes_after_calling_match_throws_an_exception() { $this->expectException(TooLateToAddNewRouteException::class); @@ -577,7 +578,7 @@ public function adding_routes_after_calling_match_throws_an_exception() $route = $router->get('another/url', function () {}); } - /** @test */ + #[Test] public function can_add_routes_in_a_group() { $request = new ServerRequest([], [], '/prefix/all', 'GET'); @@ -599,7 +600,7 @@ public function can_add_routes_in_a_group() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_add_routes_in_a_group_using_array_as_first_param() { $request = new ServerRequest([], [], '/prefix/all', 'GET'); @@ -621,7 +622,7 @@ public function can_add_routes_in_a_group_using_array_as_first_param() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_add_routes_in_a_group_using_array_as_first_param_with_no_prefix() { $request = new ServerRequest([], [], '/all', 'GET'); @@ -643,7 +644,7 @@ public function can_add_routes_in_a_group_using_array_as_first_param_with_no_pre $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function group_prefixes_work_with_leading_slash() { $request = new ServerRequest([], [], '/prefix/all', 'GET'); @@ -665,7 +666,7 @@ public function group_prefixes_work_with_leading_slash() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function group_prefixes_work_with_trailing_slash() { $request = new ServerRequest([], [], '/prefix/all', 'GET'); @@ -687,7 +688,7 @@ public function group_prefixes_work_with_trailing_slash() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_add_routes_in_nested_groups() { $request = new ServerRequest([], [], '/prefix/prefix2/all', 'GET'); @@ -714,7 +715,7 @@ public function can_add_routes_in_nested_groups() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_add_routes_in_nested_groups_with_array_syntax() { $request = new ServerRequest([], [], '/prefix/prefix2/all', 'GET'); @@ -741,7 +742,7 @@ public function can_add_routes_in_nested_groups_with_array_syntax() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_set_base_path() { $request = new ServerRequest([], [], '/base-path/prefix/all', 'GET'); @@ -760,7 +761,7 @@ public function can_set_base_path() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_set_base_path_without_trailing_slash() { $request = new ServerRequest([], [], '/base-path/prefix/all', 'GET'); @@ -779,7 +780,7 @@ public function can_set_base_path_without_trailing_slash() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_set_base_path_without_leading_slash() { $request = new ServerRequest([], [], '/base-path/prefix/all', 'GET'); @@ -798,7 +799,7 @@ public function can_set_base_path_without_leading_slash() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_set_base_path_without_leading_or_trailing_slash() { $request = new ServerRequest([], [], '/base-path/prefix/all', 'GET'); @@ -817,7 +818,7 @@ public function can_set_base_path_without_leading_or_trailing_slash() $this->assertSame('abc123', $response->getBody()->getContents()); } - /** @test */ + #[Test] public function can_update_base_path_after_match_has_been_called() { $router = new Router; @@ -843,7 +844,7 @@ public function can_update_base_path_after_match_has_been_called() $this->assertSame('abc123', $response2->getBody()->getContents()); } - /** @test */ + #[Test] public function can_add_middleware_as_a_closure_to_a_route() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -868,7 +869,7 @@ public function can_add_middleware_as_a_closure_to_a_route() $this->assertSame('value', $response->getHeader('X-key')[0]); } - /** @test */ + #[Test] public function can_get_currently_matched_route() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -887,7 +888,7 @@ public function can_get_currently_matched_route() $this->assertSame($route, $router->currentRoute()); } - /** @test */ + #[Test] public function can_get_currently_matched_route_name() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -906,7 +907,7 @@ public function can_get_currently_matched_route_name() $this->assertSame('test123', $router->currentRouteName()); } - /** @test */ + #[Test] public function current_route_name_returns_null_when_match_not_yet_called() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -919,7 +920,7 @@ public function current_route_name_returns_null_when_match_not_yet_called() $this->assertSame(null, $router->currentRouteName()); } - /** @test */ + #[Test] public function current_route_name_returns_null_when_matched_route_has_no_name() { $request = new ServerRequest([], [], '/test/123', 'GET'); @@ -934,7 +935,7 @@ public function current_route_name_returns_null_when_matched_route_has_no_name() $this->assertSame(null, $router->currentRouteName()); } - /** @test */ + #[Test] public function can_get_list_of_registered_routes() { $router = new Router; @@ -948,9 +949,7 @@ public function can_get_list_of_registered_routes() $this->assertContains($route2, $routes); } - /** - * @test - */ + #[Test] public function can_extend_post_behaviour_with_macros() { Router::macro('testFunctionAddedByMacro', function () { @@ -963,9 +962,7 @@ public function can_extend_post_behaviour_with_macros() $this->assertSame('abc123', Router::testFunctionAddedByMacro()); } - /** - * @test - */ + #[Test] public function can_extend_post_behaviour_with_mixin() { Router::mixin(new RouterMixin); @@ -980,7 +977,7 @@ class RouterMixin { function testFunctionAddedByMixin() { - return function() { + return function () { return 'abc123'; }; } diff --git a/tests/TypeHintRequestResolverTest.php b/tests/TypeHintRequestResolverTest.php index 31efbef..de5e3d5 100644 --- a/tests/TypeHintRequestResolverTest.php +++ b/tests/TypeHintRequestResolverTest.php @@ -3,12 +3,13 @@ namespace Rareloop\Router\Test; use PHPUnit\Framework\TestCase; -use Rareloop\Router\TypeHintRequestResolver; use Laminas\Diactoros\ServerRequest; +use PHPUnit\Framework\Attributes\Test; +use Rareloop\Router\TypeHintRequestResolver; class TypeHintRequestResolverTest extends TestCase { - /** @test */ + #[Test] public function returns_resolved_parameters_when_no_request_is_set() { $reflectionFunction = new \ReflectionFunction(function () {}); @@ -20,7 +21,7 @@ public function returns_resolved_parameters_when_no_request_is_set() $this->assertSame($resolvedParameters, $params); } - /** @test */ + #[Test] public function can_resolve_a_request() { $request = new ServerRequest([], [], '/injected', 'GET'); @@ -33,7 +34,7 @@ public function can_resolve_a_request() $this->assertSame('/injected', $params[0]->getUri()->getPath()); } - /** @test */ + #[Test] public function does_not_attempt_to_resolve_params_that_have_already_been_resolved() { $preResolvedRequest = new ServerRequest([], [], '/pre/resolved', 'GET'); From 911656ca690cb791a485d9547de2a436a5546a03 Mon Sep 17 00:00:00 2001 From: Adam Tomat Date: Mon, 13 Oct 2025 16:31:12 +0100 Subject: [PATCH 13/13] Roll back phpunit version --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index dfe131b..5ff217b 100755 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "mockery/mockery": "^1.4.3", "php-coveralls/php-coveralls": "^2.4", "php-di/php-di": "^7.1.1", - "phpunit/phpunit": "^12", + "phpunit/phpunit": "^10.5", "squizlabs/php_codesniffer": "^3.6.0" }, "autoload": { @@ -49,4 +49,4 @@ "config": { "sort-packages": true } -} +} \ No newline at end of file