From 870fd6aa20e4f51804dc0149f1246092d1609788 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 1 May 2025 15:11:13 +0300 Subject: [PATCH] Tests was migrated to Pest --- composer.json | 8 ++- config/route.php | 13 ++++ phpunit.xml | 14 +++++ src/Application.php | 13 ++++ src/Facades/Action.php | 13 ++++ src/Facades/Name.php | 13 ++++ src/Helpers/Action.php | 13 ++++ src/Helpers/Name.php | 15 ++++- src/Providers/RoutingServiceProvider.php | 13 ++++ src/Routing/Route.php | 13 ++++ src/Routing/Router.php | 20 +++++- src/ServiceProvider.php | 13 ++++ tests/Extenders/RouteNameExtender.php | 26 -------- .../Fixtures/Extenders/RouteNameExtender.php | 43 +++++++++++++ tests/Helpers/routes.php | 34 ++++++++++ tests/Pest.php | 20 ++++++ tests/Routes/BasicTest.php | 48 -------------- tests/Routes/CollisionTest.php | 20 ------ tests/Routes/ExtendedRoutesTest.php | 55 ---------------- tests/Routes/MixedCaseTest.php | 20 ------ tests/Routes/MultiParametersTest.php | 27 -------- tests/Routes/NotDefinedTest.php | 19 ------ tests/Routes/ProtectedRoutesTest.php | 19 ------ tests/Routes/ResourcesTest.php | 32 ---------- tests/Routes/WithParametersTest.php | 27 -------- tests/TestCase.php | 20 +++--- tests/Unit/Routes/BasicTest.php | 63 +++++++++++++++++++ tests/Unit/Routes/CollisionTest.php | 27 ++++++++ tests/Unit/Routes/ExtendedRoutesTest.php | 61 ++++++++++++++++++ tests/Unit/Routes/MixedCaseTest.php | 27 ++++++++ tests/Unit/Routes/MultiParametersTest.php | 36 +++++++++++ tests/Unit/Routes/NotDefinedTest.php | 24 +++++++ tests/Unit/Routes/ProtectedRoutesTest.php | 28 +++++++++ tests/Unit/Routes/ResourcesTest.php | 39 ++++++++++++ tests/Unit/Routes/WithParametersTest.php | 36 +++++++++++ .../app/Http/Controllers/ApiController.php | 13 ++++ .../app/Http/Controllers/SomeController.php | 13 ++++ .../app/Http/Controllers/WebController.php | 13 ++++ workbench/app/Models/User.php | 13 ++++ workbench/bootstrap/app.php | 13 ++++ workbench/bootstrap/providers.php | 13 ++++ workbench/config/view.php | 13 ++++ workbench/routes/api.php | 13 ++++ workbench/routes/basic.php | 13 ++++ workbench/routes/collision.php | 13 ++++ workbench/routes/extended.php | 13 ++++ workbench/routes/mixed_case.php | 13 ++++ workbench/routes/parameters.php | 13 ++++ workbench/routes/protected.php | 13 ++++ 49 files changed, 788 insertions(+), 306 deletions(-) delete mode 100644 tests/Extenders/RouteNameExtender.php create mode 100644 tests/Fixtures/Extenders/RouteNameExtender.php create mode 100644 tests/Helpers/routes.php create mode 100644 tests/Pest.php delete mode 100644 tests/Routes/BasicTest.php delete mode 100644 tests/Routes/CollisionTest.php delete mode 100644 tests/Routes/ExtendedRoutesTest.php delete mode 100644 tests/Routes/MixedCaseTest.php delete mode 100644 tests/Routes/MultiParametersTest.php delete mode 100644 tests/Routes/NotDefinedTest.php delete mode 100644 tests/Routes/ProtectedRoutesTest.php delete mode 100644 tests/Routes/ResourcesTest.php delete mode 100644 tests/Routes/WithParametersTest.php create mode 100644 tests/Unit/Routes/BasicTest.php create mode 100644 tests/Unit/Routes/CollisionTest.php create mode 100644 tests/Unit/Routes/ExtendedRoutesTest.php create mode 100644 tests/Unit/Routes/MixedCaseTest.php create mode 100644 tests/Unit/Routes/MultiParametersTest.php create mode 100644 tests/Unit/Routes/NotDefinedTest.php create mode 100644 tests/Unit/Routes/ProtectedRoutesTest.php create mode 100644 tests/Unit/Routes/ResourcesTest.php create mode 100644 tests/Unit/Routes/WithParametersTest.php diff --git a/composer.json b/composer.json index 0226128..cd20f97 100644 --- a/composer.json +++ b/composer.json @@ -37,7 +37,8 @@ }, "require-dev": { "orchestra/testbench": "^9.0 || ^10.0", - "phpunit/phpunit": "^11.0 || ^12.0" + "pestphp/pest": "^3.8", + "pestphp/pest-plugin-type-coverage": "^3.5" }, "minimum-stability": "stable", "prefer-stable": true, @@ -58,6 +59,7 @@ "dragon-code/codestyler": true, "ergebnis/composer-normalize": true, "friendsofphp/php-cs-fixer": true, + "pestphp/pest-plugin": true, "symfony/thanks": true }, "preferred-install": "dist", @@ -78,7 +80,7 @@ "build": "@php vendor/bin/testbench workbench:build --ansi", "clear": "@php vendor/bin/testbench package:purge-skeleton --ansi", "prepare": "@php vendor/bin/testbench package:discover --ansi", - "test": "@php vendor/bin/phpunit", - "test-coverage": "@test --coverage-text --coverage-html=.build/phpunit/" + "test": "@php vendor/bin/pest --compact", + "test-coverage": "@test --type-coverage" } } diff --git a/config/route.php b/config/route.php index 30b2cc7..c9a0b81 100644 --- a/config/route.php +++ b/config/route.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); return [ diff --git a/phpunit.xml b/phpunit.xml index 89a12c3..ca3ee90 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,4 +1,18 @@ + + + + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); namespace DragonCode\LaravelRouteNames; diff --git a/src/Facades/Action.php b/src/Facades/Action.php index 6b6bacd..66aedf4 100644 --- a/src/Facades/Action.php +++ b/src/Facades/Action.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); namespace DragonCode\LaravelRouteNames\Facades; diff --git a/src/Facades/Name.php b/src/Facades/Name.php index 7fbf038..2edbd0f 100644 --- a/src/Facades/Name.php +++ b/src/Facades/Name.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); namespace DragonCode\LaravelRouteNames\Facades; diff --git a/src/Helpers/Action.php b/src/Helpers/Action.php index c6520fc..c17af5a 100644 --- a/src/Helpers/Action.php +++ b/src/Helpers/Action.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); namespace DragonCode\LaravelRouteNames\Helpers; diff --git a/src/Helpers/Name.php b/src/Helpers/Name.php index c9681c7..be07dd5 100644 --- a/src/Helpers/Name.php +++ b/src/Helpers/Name.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); namespace DragonCode\LaravelRouteNames\Helpers; @@ -16,7 +29,7 @@ public function get(array $methods, string $uri): string $suffix = $this->getMethodSuffix($methods, $uri); return $resolved - ->when($this->doesntSame($resolved, $suffix), static fn ($items) => $items->push($suffix)) + ->when($this->doesntSame($resolved, $suffix), static fn (Collection $items) => $items->push($suffix)) ->implode('.'); } diff --git a/src/Providers/RoutingServiceProvider.php b/src/Providers/RoutingServiceProvider.php index 357303e..65bd4f7 100644 --- a/src/Providers/RoutingServiceProvider.php +++ b/src/Providers/RoutingServiceProvider.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); namespace DragonCode\LaravelRouteNames\Providers; diff --git a/src/Routing/Route.php b/src/Routing/Route.php index 5c7b490..5ec1f70 100644 --- a/src/Routing/Route.php +++ b/src/Routing/Route.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); namespace DragonCode\LaravelRouteNames\Routing; diff --git a/src/Routing/Router.php b/src/Routing/Router.php index 3b481d8..df31c2b 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); namespace DragonCode\LaravelRouteNames\Routing; @@ -8,8 +21,11 @@ class Router extends BaseRouter { - public function newRoute($methods, $uri, $action): Route - { + public function newRoute( + $methods, // @pest-ignore-type + $uri, // @pest-ignore-type + $action // @pest-ignore-type + ): Route { return (new Route($methods, $uri, $action)) ->setRouter($this) ->setContainer($this->container); diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 574ccb7..bb3ecd6 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); namespace DragonCode\LaravelRouteNames; diff --git a/tests/Extenders/RouteNameExtender.php b/tests/Extenders/RouteNameExtender.php deleted file mode 100644 index 46156b2..0000000 --- a/tests/Extenders/RouteNameExtender.php +++ /dev/null @@ -1,26 +0,0 @@ -has($route->uri()) ? $this->replace($name) : $name; - } - - protected function has(string $uri): bool - { - return Str::of($uri)->startsWith('api/v1'); - } - - protected function replace(string $name): string - { - return Str::replaceFirst('api.v1', 'api.v2', $name); - } -} diff --git a/tests/Fixtures/Extenders/RouteNameExtender.php b/tests/Fixtures/Extenders/RouteNameExtender.php new file mode 100644 index 0000000..3a91852 --- /dev/null +++ b/tests/Fixtures/Extenders/RouteNameExtender.php @@ -0,0 +1,43 @@ + + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + +declare(strict_types=1); + +namespace Tests\Fixtures\Extenders; + +use DragonCode\LaravelRouteNames\Routing\Route; +use Illuminate\Support\Str; + +class RouteNameExtender +{ + public function __invoke(string $name, Route $route): string + { + if ($this->has($route->uri())) { + return $this->replace($name); + } + + return $name; + } + + protected function has(string $uri): bool + { + return Str::of($uri)->startsWith('api/v1'); + } + + protected function replace(string $name): string + { + return Str::replaceFirst('api.v1', 'api.v2', $name); + } +} diff --git a/tests/Helpers/routes.php b/tests/Helpers/routes.php new file mode 100644 index 0000000..da0b0af --- /dev/null +++ b/tests/Helpers/routes.php @@ -0,0 +1,34 @@ + + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + +declare(strict_types=1); + +use Illuminate\Routing\Route as RouteCore; +use Illuminate\Support\Facades\Route; +use Workbench\App\Http\Controllers\SomeController; + +function routeAction(string $action, string $controller = SomeController::class): ?RouteCore +{ + return Route::getRoutes()->getByAction($controller . '@' . $action); +} + +function routeName(string $action, string $controller = SomeController::class): string +{ + if ($route = routeAction($action, $controller)) { + return $route->getName(); + } + + throw new RuntimeException(sprintf('Unknown route action: %s@%s.', $controller, $action)); +} diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..2b4d61d --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,20 @@ + + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + +declare(strict_types=1); + +pest() + ->extend(Tests\TestCase::class) + ->in('Unit'); diff --git a/tests/Routes/BasicTest.php b/tests/Routes/BasicTest.php deleted file mode 100644 index bdc0cc8..0000000 --- a/tests/Routes/BasicTest.php +++ /dev/null @@ -1,48 +0,0 @@ -assertSame('index', $this->getRouteName('foo')); - $this->assertSame('store', $this->getRouteName('bar')); - $this->assertSame('update', $this->getRouteName('baz')); - $this->assertSame('destroy', $this->getRouteName('baq')); - $this->assertSame('patch', $this->getRouteName('baw')); - $this->assertSame('options', $this->getRouteName('bae')); - - $this->assertSame('simple.index', $this->getRouteName('simpleFoo')); - $this->assertSame('simple.store', $this->getRouteName('simpleBar')); - $this->assertSame('simple.update', $this->getRouteName('simpleBaz')); - $this->assertSame('simple.destroy', $this->getRouteName('simpleBaq')); - $this->assertSame('simple.patch', $this->getRouteName('simpleBaw')); - $this->assertSame('simple.options', $this->getRouteName('simpleBae')); - - $this->assertSame('simple.edit.show', $this->getRouteName('simpleEditFoo')); - $this->assertSame('simple.edit.store', $this->getRouteName('simpleEditBar')); - $this->assertSame('simple.edit.update', $this->getRouteName('simpleEditBaz')); - $this->assertSame('simple.edit.destroy', $this->getRouteName('simpleEditBaq')); - $this->assertSame('simple.edit.patch', $this->getRouteName('simpleEditBaw')); - $this->assertSame('simple.edit.options', $this->getRouteName('simpleEditBae')); - - $this->assertSame('simple.update.show', $this->getRouteName('simpleUpdateFoo')); - $this->assertSame('simple.update.store', $this->getRouteName('simpleUpdateBar')); - $this->assertSame('simple.update', $this->getRouteName('simpleUpdateBaz')); - $this->assertSame('simple.update.destroy', $this->getRouteName('simpleUpdateBaq')); - $this->assertSame('simple.update.patch', $this->getRouteName('simpleUpdateBaw')); - $this->assertSame('simple.update.options', $this->getRouteName('simpleUpdateBae')); - - $this->assertSame('simple.destroy.show', $this->getRouteName('simpleDestroyFoo')); - $this->assertSame('simple.destroy.store', $this->getRouteName('simpleDestroyBar')); - $this->assertSame('simple.destroy.update', $this->getRouteName('simpleDestroyBaz')); - $this->assertSame('simple.destroy', $this->getRouteName('simpleDestroyBaq')); - $this->assertSame('simple.destroy.patch', $this->getRouteName('simpleDestroyBaw')); - $this->assertSame('simple.destroy.options', $this->getRouteName('simpleDestroyBae')); - } -} diff --git a/tests/Routes/CollisionTest.php b/tests/Routes/CollisionTest.php deleted file mode 100644 index 7c16c66..0000000 --- a/tests/Routes/CollisionTest.php +++ /dev/null @@ -1,20 +0,0 @@ -assertSame('collision.get.show', $this->getRouteName('collisionGet')); - $this->assertSame('collision.post.store', $this->getRouteName('collisionPost')); - $this->assertSame('collision.put.update', $this->getRouteName('collisionPut')); - $this->assertSame('collision.delete.destroy', $this->getRouteName('collisionDelete')); - $this->assertSame('collision.patch', $this->getRouteName('collisionPatch')); - $this->assertSame('collision.options', $this->getRouteName('collisionOptions')); - } -} diff --git a/tests/Routes/ExtendedRoutesTest.php b/tests/Routes/ExtendedRoutesTest.php deleted file mode 100644 index 77985d2..0000000 --- a/tests/Routes/ExtendedRoutesTest.php +++ /dev/null @@ -1,55 +0,0 @@ -assertSame('api.v1.extended.index', $this->getRouteName('extendedFoo')); - $this->assertSame('api.v1.extended.store', $this->getRouteName('extendedBar')); - $this->assertSame('api.v1.extended.update', $this->getRouteName('extendedBaz')); - $this->assertSame('api.v1.extended.destroy', $this->getRouteName('extendedBaq')); - $this->assertSame('api.v1.extended.patch', $this->getRouteName('extendedBaw')); - $this->assertSame('api.v1.extended.options', $this->getRouteName('extendedBae')); - } - - public function testWebCallback(): void - { - Config::set( - 'route.names.extender', - static function (string $name, Route $route): string { - return Str::of($route->uri())->startsWith('api/v1') - ? Str::replaceFirst('api.v1', 'api', $name) - : $name; - } - ); - - $this->assertSame('api.extended.index', $this->getRouteName('extendedFoo')); - $this->assertSame('api.extended.store', $this->getRouteName('extendedBar')); - $this->assertSame('api.extended.update', $this->getRouteName('extendedBaz')); - $this->assertSame('api.extended.destroy', $this->getRouteName('extendedBaq')); - $this->assertSame('api.extended.patch', $this->getRouteName('extendedBaw')); - $this->assertSame('api.extended.options', $this->getRouteName('extendedBae')); - } - - public function testWebSpecialCallback(): void - { - Config::set('route.names.extender', RouteNameExtender::class); - - $this->assertSame('api.v2.extended.index', $this->getRouteName('extendedFoo')); - $this->assertSame('api.v2.extended.store', $this->getRouteName('extendedBar')); - $this->assertSame('api.v2.extended.update', $this->getRouteName('extendedBaz')); - $this->assertSame('api.v2.extended.destroy', $this->getRouteName('extendedBaq')); - $this->assertSame('api.v2.extended.patch', $this->getRouteName('extendedBaw')); - $this->assertSame('api.v2.extended.options', $this->getRouteName('extendedBae')); - } -} diff --git a/tests/Routes/MixedCaseTest.php b/tests/Routes/MixedCaseTest.php deleted file mode 100644 index e5367d5..0000000 --- a/tests/Routes/MixedCaseTest.php +++ /dev/null @@ -1,20 +0,0 @@ -assertSame('mixed-case.case.index', $this->getRouteName('caseFoo')); - $this->assertSame('mixed-case.case.store', $this->getRouteName('caseBar')); - $this->assertSame('mixed-case.case.update', $this->getRouteName('caseBaz')); - $this->assertSame('mixed-case.case.destroy', $this->getRouteName('caseBaq')); - $this->assertSame('mixed-case.case.patch', $this->getRouteName('caseBaw')); - $this->assertSame('mixed-case.case.options', $this->getRouteName('caseBae')); - } -} diff --git a/tests/Routes/MultiParametersTest.php b/tests/Routes/MultiParametersTest.php deleted file mode 100644 index 7d2508b..0000000 --- a/tests/Routes/MultiParametersTest.php +++ /dev/null @@ -1,27 +0,0 @@ -assertSame('multi.foo.bar.qwerty.index', $this->getRouteName('multiFoo')); - $this->assertSame('multi.foo.bar.qwerty.store', $this->getRouteName('multiBar')); - $this->assertSame('multi.foo.bar.qwerty.update', $this->getRouteName('multiBaz')); - $this->assertSame('multi.foo.bar.qwerty.destroy', $this->getRouteName('multiBaq')); - $this->assertSame('multi.foo.bar.qwerty.patch', $this->getRouteName('multiBaw')); - $this->assertSame('multi.foo.bar.qwerty.options', $this->getRouteName('multiBae')); - - $this->assertSame('multi.simple.foo.bar.qwerty.index', $this->getRouteName('multiEndWithFoo')); - $this->assertSame('multi.simple.foo.bar.qwerty.store', $this->getRouteName('multiEndWithBar')); - $this->assertSame('multi.simple.foo.bar.qwerty.update', $this->getRouteName('multiEndWithBaz')); - $this->assertSame('multi.simple.foo.bar.qwerty.destroy', $this->getRouteName('multiEndWithBaq')); - $this->assertSame('multi.simple.foo.bar.qwerty.patch', $this->getRouteName('multiEndWithBaw')); - $this->assertSame('multi.simple.foo.bar.qwerty.options', $this->getRouteName('multiEndWithBae')); - } -} diff --git a/tests/Routes/NotDefinedTest.php b/tests/Routes/NotDefinedTest.php deleted file mode 100644 index be00be7..0000000 --- a/tests/Routes/NotDefinedTest.php +++ /dev/null @@ -1,19 +0,0 @@ -expectException(RouteNotFoundException::class); - $this->expectExceptionMessage('Route [my_pages] not defined.'); - - route('my_pages'); - } -} diff --git a/tests/Routes/ProtectedRoutesTest.php b/tests/Routes/ProtectedRoutesTest.php deleted file mode 100644 index 11cdf9d..0000000 --- a/tests/Routes/ProtectedRoutesTest.php +++ /dev/null @@ -1,19 +0,0 @@ -assertSame('pretty-routes.list', $this->getRouteName('prettyRoutesList')); - $this->assertSame('pretty-routes.clear', $this->getRouteName('prettyRoutesClear')); - - $this->assertSame('telescope', $this->getRouteName('telescopeShow')); - $this->assertSame('telescope.telescope-api.views.show', $this->getRouteName('telescopeViewsShow')); - } -} diff --git a/tests/Routes/ResourcesTest.php b/tests/Routes/ResourcesTest.php deleted file mode 100644 index fbc1dd5..0000000 --- a/tests/Routes/ResourcesTest.php +++ /dev/null @@ -1,32 +0,0 @@ -assertSame('api.resources.photos.index', $this->getRouteName('index', WebController::class)); - $this->assertSame('api.resources.photos.create', $this->getRouteName('create', WebController::class)); - $this->assertSame('api.resources.photos.store', $this->getRouteName('store', WebController::class)); - $this->assertSame('api.resources.photos.show', $this->getRouteName('show', WebController::class)); - $this->assertSame('api.resources.photos.edit', $this->getRouteName('edit', WebController::class)); - $this->assertSame('api.resources.photos.update', $this->getRouteName('update', WebController::class)); - $this->assertSame('api.resources.photos.destroy', $this->getRouteName('destroy', WebController::class)); - } - - public function testApi(): void - { - $this->assertSame('api.resources.comments.index', $this->getRouteName('index', ApiController::class)); - $this->assertSame('api.resources.comments.store', $this->getRouteName('store', ApiController::class)); - $this->assertSame('api.resources.comments.show', $this->getRouteName('show', ApiController::class)); - $this->assertSame('api.resources.comments.update', $this->getRouteName('update', ApiController::class)); - $this->assertSame('api.resources.comments.destroy', $this->getRouteName('destroy', ApiController::class)); - } -} diff --git a/tests/Routes/WithParametersTest.php b/tests/Routes/WithParametersTest.php deleted file mode 100644 index 8d76334..0000000 --- a/tests/Routes/WithParametersTest.php +++ /dev/null @@ -1,27 +0,0 @@ -assertSame('show', $this->getRouteName('parameterFoo')); - $this->assertSame('store', $this->getRouteName('parameterBar')); - $this->assertSame('update', $this->getRouteName('parameterBaz')); - $this->assertSame('destroy', $this->getRouteName('parameterBaq')); - $this->assertSame('patch', $this->getRouteName('parameterBaw')); - $this->assertSame('options', $this->getRouteName('parameterBae')); - - $this->assertSame('parameters.simple.show', $this->getRouteName('parameterSimpleFoo')); - $this->assertSame('parameters.simple.store', $this->getRouteName('parameterSimpleBar')); - $this->assertSame('parameters.simple.update', $this->getRouteName('parameterSimpleBaz')); - $this->assertSame('parameters.simple.destroy', $this->getRouteName('parameterSimpleBaq')); - $this->assertSame('parameters.simple.patch', $this->getRouteName('parameterSimpleBaw')); - $this->assertSame('parameters.simple.options', $this->getRouteName('parameterSimpleBae')); - } -} diff --git a/tests/TestCase.php b/tests/TestCase.php index e973a38..60c63c1 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,12 +1,23 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); namespace Tests; -use Illuminate\Support\Facades\Route; use Orchestra\Testbench\TestCase as BaseTestCase; -use Workbench\App\Http\Controllers\SomeController; abstract class TestCase extends BaseTestCase { @@ -14,9 +25,4 @@ public static function applicationBasePathUsingWorkbench(): ?string { return __DIR__ . '/../workbench'; } - - protected function getRouteName(string $action, string $controller = SomeController::class): string - { - return Route::getRoutes()->getByAction($controller . '@' . $action)->getName(); - } } diff --git a/tests/Unit/Routes/BasicTest.php b/tests/Unit/Routes/BasicTest.php new file mode 100644 index 0000000..1a0051a --- /dev/null +++ b/tests/Unit/Routes/BasicTest.php @@ -0,0 +1,63 @@ + + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + +declare(strict_types=1); + +namespace Tests\Unit\Routes; + +it('default', function () { + expect(routeName('foo'))->toBe('index'); + expect(routeName('bar'))->toBe('store'); + expect(routeName('baz'))->toBe('update'); + expect(routeName('baq'))->toBe('destroy'); + expect(routeName('baw'))->toBe('patch'); + expect(routeName('bae'))->toBe('options'); +}); + +it('simple', function () { + expect(routeName('simpleFoo'))->toBe('simple.index'); + expect(routeName('simpleBar'))->toBe('simple.store'); + expect(routeName('simpleBaz'))->toBe('simple.update'); + expect(routeName('simpleBaq'))->toBe('simple.destroy'); + expect(routeName('simpleBaw'))->toBe('simple.patch'); + expect(routeName('simpleBae'))->toBe('simple.options'); +}); + +it('edit', function () { + expect(routeName('simpleEditFoo'))->toBe('simple.edit.show'); + expect(routeName('simpleEditBar'))->toBe('simple.edit.store'); + expect(routeName('simpleEditBaz'))->toBe('simple.edit.update'); + expect(routeName('simpleEditBaq'))->toBe('simple.edit.destroy'); + expect(routeName('simpleEditBaw'))->toBe('simple.edit.patch'); + expect(routeName('simpleEditBae'))->toBe('simple.edit.options'); +}); + +it('update', function () { + expect(routeName('simpleUpdateFoo'))->toBe('simple.update.show'); + expect(routeName('simpleUpdateBar'))->toBe('simple.update.store'); + expect(routeName('simpleUpdateBaz'))->toBe('simple.update'); + expect(routeName('simpleUpdateBaq'))->toBe('simple.update.destroy'); + expect(routeName('simpleUpdateBaw'))->toBe('simple.update.patch'); + expect(routeName('simpleUpdateBae'))->toBe('simple.update.options'); +}); + +it('destroy', function () { + expect(routeName('simpleDestroyFoo'))->toBe('simple.destroy.show'); + expect(routeName('simpleDestroyBar'))->toBe('simple.destroy.store'); + expect(routeName('simpleDestroyBaz'))->toBe('simple.destroy.update'); + expect(routeName('simpleDestroyBaq'))->toBe('simple.destroy'); + expect(routeName('simpleDestroyBaw'))->toBe('simple.destroy.patch'); + expect(routeName('simpleDestroyBae'))->toBe('simple.destroy.options'); +}); diff --git a/tests/Unit/Routes/CollisionTest.php b/tests/Unit/Routes/CollisionTest.php new file mode 100644 index 0000000..4ad6bcd --- /dev/null +++ b/tests/Unit/Routes/CollisionTest.php @@ -0,0 +1,27 @@ + + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + +declare(strict_types=1); + +namespace Tests\Unit\Routes; + +it('default', function () { + expect(routeName('collisionGet'))->toBe('collision.get.show'); + expect(routeName('collisionPost'))->toBe('collision.post.store'); + expect(routeName('collisionPut'))->toBe('collision.put.update'); + expect(routeName('collisionDelete'))->toBe('collision.delete.destroy'); + expect(routeName('collisionPatch'))->toBe('collision.patch'); + expect(routeName('collisionOptions'))->toBe('collision.options'); +}); diff --git a/tests/Unit/Routes/ExtendedRoutesTest.php b/tests/Unit/Routes/ExtendedRoutesTest.php new file mode 100644 index 0000000..264c7eb --- /dev/null +++ b/tests/Unit/Routes/ExtendedRoutesTest.php @@ -0,0 +1,61 @@ + + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + +declare(strict_types=1); + +namespace Tests\Unit\Routes; + +use DragonCode\LaravelRouteNames\Routing\Route; +use Illuminate\Support\Facades\Config; +use Illuminate\Support\Str; +use Tests\Fixtures\Extenders\RouteNameExtender; + +it('default', function () { + expect(routeName('extendedFoo'))->toBe('api.v1.extended.index'); + expect(routeName('extendedBar'))->toBe('api.v1.extended.store'); + expect(routeName('extendedBaz'))->toBe('api.v1.extended.update'); + expect(routeName('extendedBaq'))->toBe('api.v1.extended.destroy'); + expect(routeName('extendedBaw'))->toBe('api.v1.extended.patch'); + expect(routeName('extendedBae'))->toBe('api.v1.extended.options'); +}); + +it('closure extender', function () { + Config::set( + 'route.names.extender', + static function (string $name, Route $route): string { + return Str::of($route->uri())->startsWith('api/v1') + ? Str::replaceFirst('api.v1', 'api', $name) + : $name; + } + ); + + expect(routeName('extendedFoo'))->toBe('api.extended.index'); + expect(routeName('extendedBar'))->toBe('api.extended.store'); + expect(routeName('extendedBaz'))->toBe('api.extended.update'); + expect(routeName('extendedBaq'))->toBe('api.extended.destroy'); + expect(routeName('extendedBaw'))->toBe('api.extended.patch'); + expect(routeName('extendedBae'))->toBe('api.extended.options'); +}); + +it('extender class', function () { + Config::set('route.names.extender', RouteNameExtender::class); + + expect(routeName('extendedFoo'))->toBe('api.v2.extended.index'); + expect(routeName('extendedBar'))->toBe('api.v2.extended.store'); + expect(routeName('extendedBaz'))->toBe('api.v2.extended.update'); + expect(routeName('extendedBaq'))->toBe('api.v2.extended.destroy'); + expect(routeName('extendedBaw'))->toBe('api.v2.extended.patch'); + expect(routeName('extendedBae'))->toBe('api.v2.extended.options'); +}); diff --git a/tests/Unit/Routes/MixedCaseTest.php b/tests/Unit/Routes/MixedCaseTest.php new file mode 100644 index 0000000..710f12a --- /dev/null +++ b/tests/Unit/Routes/MixedCaseTest.php @@ -0,0 +1,27 @@ + + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + +declare(strict_types=1); + +namespace Tests\Unit\Routes; + +it('default', function () { + expect(routeName('caseFoo'))->toBe('mixed-case.case.index'); + expect(routeName('caseBar'))->toBe('mixed-case.case.store'); + expect(routeName('caseBaz'))->toBe('mixed-case.case.update'); + expect(routeName('caseBaq'))->toBe('mixed-case.case.destroy'); + expect(routeName('caseBaw'))->toBe('mixed-case.case.patch'); + expect(routeName('caseBae'))->toBe('mixed-case.case.options'); +}); diff --git a/tests/Unit/Routes/MultiParametersTest.php b/tests/Unit/Routes/MultiParametersTest.php new file mode 100644 index 0000000..bf3fa6a --- /dev/null +++ b/tests/Unit/Routes/MultiParametersTest.php @@ -0,0 +1,36 @@ + + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + +declare(strict_types=1); + +namespace Tests\Unit\Routes; + +it('default', function () { + expect(routeName('multiFoo'))->toBe('multi.foo.bar.qwerty.index'); + expect(routeName('multiBar'))->toBe('multi.foo.bar.qwerty.store'); + expect(routeName('multiBaz'))->toBe('multi.foo.bar.qwerty.update'); + expect(routeName('multiBaq'))->toBe('multi.foo.bar.qwerty.destroy'); + expect(routeName('multiBaw'))->toBe('multi.foo.bar.qwerty.patch'); + expect(routeName('multiBae'))->toBe('multi.foo.bar.qwerty.options'); +}); + +it('with ends', function () { + expect(routeName('multiEndWithFoo'))->toBe('multi.simple.foo.bar.qwerty.index'); + expect(routeName('multiEndWithBar'))->toBe('multi.simple.foo.bar.qwerty.store'); + expect(routeName('multiEndWithBaz'))->toBe('multi.simple.foo.bar.qwerty.update'); + expect(routeName('multiEndWithBaq'))->toBe('multi.simple.foo.bar.qwerty.destroy'); + expect(routeName('multiEndWithBaw'))->toBe('multi.simple.foo.bar.qwerty.patch'); + expect(routeName('multiEndWithBae'))->toBe('multi.simple.foo.bar.qwerty.options'); +}); diff --git a/tests/Unit/Routes/NotDefinedTest.php b/tests/Unit/Routes/NotDefinedTest.php new file mode 100644 index 0000000..bac10ab --- /dev/null +++ b/tests/Unit/Routes/NotDefinedTest.php @@ -0,0 +1,24 @@ + + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + +declare(strict_types=1); + +namespace Tests\Unit\Routes; + +use Symfony\Component\Routing\Exception\RouteNotFoundException; + +it('default', function () { + route('my_pages'); +})->throws(RouteNotFoundException::class, 'Route [my_pages] not defined.'); diff --git a/tests/Unit/Routes/ProtectedRoutesTest.php b/tests/Unit/Routes/ProtectedRoutesTest.php new file mode 100644 index 0000000..5ab3fb3 --- /dev/null +++ b/tests/Unit/Routes/ProtectedRoutesTest.php @@ -0,0 +1,28 @@ + + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + +declare(strict_types=1); + +namespace Tests\Unit\Routes; + +it('pretty routes', function () { + expect(routeName('prettyRoutesList'))->toBe('pretty-routes.list'); + expect(routeName('prettyRoutesClear'))->toBe('pretty-routes.clear'); +}); + +it('telescope', function () { + expect(routeName('telescopeShow'))->toBe('telescope'); + expect(routeName('telescopeViewsShow'))->toBe('telescope.telescope-api.views.show'); +}); diff --git a/tests/Unit/Routes/ResourcesTest.php b/tests/Unit/Routes/ResourcesTest.php new file mode 100644 index 0000000..0c99153 --- /dev/null +++ b/tests/Unit/Routes/ResourcesTest.php @@ -0,0 +1,39 @@ + + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + +declare(strict_types=1); + +namespace Tests\Unit\Routes; + +use Workbench\App\Http\Controllers\ApiController; +use Workbench\App\Http\Controllers\WebController; + +it('web resources', function () { + expect(routeName('index', WebController::class))->toBe('api.resources.photos.index'); + expect(routeName('create', WebController::class))->toBe('api.resources.photos.create'); + expect(routeName('store', WebController::class))->toBe('api.resources.photos.store'); + expect(routeName('show', WebController::class))->toBe('api.resources.photos.show'); + expect(routeName('edit', WebController::class))->toBe('api.resources.photos.edit'); + expect(routeName('update', WebController::class))->toBe('api.resources.photos.update'); + expect(routeName('destroy', WebController::class))->toBe('api.resources.photos.destroy'); +}); + +it('api resources', function () { + expect(routeName('index', ApiController::class))->toBe('api.resources.comments.index'); + expect(routeName('store', ApiController::class))->toBe('api.resources.comments.store'); + expect(routeName('show', ApiController::class))->toBe('api.resources.comments.show'); + expect(routeName('update', ApiController::class))->toBe('api.resources.comments.update'); + expect(routeName('destroy', ApiController::class))->toBe('api.resources.comments.destroy'); +}); diff --git a/tests/Unit/Routes/WithParametersTest.php b/tests/Unit/Routes/WithParametersTest.php new file mode 100644 index 0000000..455e23b --- /dev/null +++ b/tests/Unit/Routes/WithParametersTest.php @@ -0,0 +1,36 @@ + + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + +declare(strict_types=1); + +namespace Tests\Unit\Routes; + +it('default', function () { + expect(routeName('parameterFoo'))->toBe('show'); + expect(routeName('parameterBar'))->toBe('store'); + expect(routeName('parameterBaz'))->toBe('update'); + expect(routeName('parameterBaq'))->toBe('destroy'); + expect(routeName('parameterBaw'))->toBe('patch'); + expect(routeName('parameterBae'))->toBe('options'); +}); + +it('parameters', function () { + expect(routeName('parameterSimpleFoo'))->toBe('parameters.simple.show'); + expect(routeName('parameterSimpleBar'))->toBe('parameters.simple.store'); + expect(routeName('parameterSimpleBaz'))->toBe('parameters.simple.update'); + expect(routeName('parameterSimpleBaq'))->toBe('parameters.simple.destroy'); + expect(routeName('parameterSimpleBaw'))->toBe('parameters.simple.patch'); + expect(routeName('parameterSimpleBae'))->toBe('parameters.simple.options'); +}); diff --git a/workbench/app/Http/Controllers/ApiController.php b/workbench/app/Http/Controllers/ApiController.php index 4ee5ab0..3e43c74 100644 --- a/workbench/app/Http/Controllers/ApiController.php +++ b/workbench/app/Http/Controllers/ApiController.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); namespace Workbench\App\Http\Controllers; diff --git a/workbench/app/Http/Controllers/SomeController.php b/workbench/app/Http/Controllers/SomeController.php index 1da724b..ecf545b 100644 --- a/workbench/app/Http/Controllers/SomeController.php +++ b/workbench/app/Http/Controllers/SomeController.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); namespace Workbench\App\Http\Controllers; diff --git a/workbench/app/Http/Controllers/WebController.php b/workbench/app/Http/Controllers/WebController.php index c336c0f..abda713 100644 --- a/workbench/app/Http/Controllers/WebController.php +++ b/workbench/app/Http/Controllers/WebController.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); namespace Workbench\App\Http\Controllers; diff --git a/workbench/app/Models/User.php b/workbench/app/Models/User.php index a563953..93be274 100644 --- a/workbench/app/Models/User.php +++ b/workbench/app/Models/User.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); namespace Workbench\App\Models; diff --git a/workbench/bootstrap/app.php b/workbench/bootstrap/app.php index d06bc04..78f1ccd 100644 --- a/workbench/bootstrap/app.php +++ b/workbench/bootstrap/app.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); use DragonCode\LaravelRouteNames\Application; diff --git a/workbench/bootstrap/providers.php b/workbench/bootstrap/providers.php index 98bd104..a8cf4d5 100644 --- a/workbench/bootstrap/providers.php +++ b/workbench/bootstrap/providers.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); use DragonCode\LaravelRouteNames\ServiceProvider; diff --git a/workbench/config/view.php b/workbench/config/view.php index f975a7f..699795d 100644 --- a/workbench/config/view.php +++ b/workbench/config/view.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); return [ diff --git a/workbench/routes/api.php b/workbench/routes/api.php index 4b46949..a0278cc 100644 --- a/workbench/routes/api.php +++ b/workbench/routes/api.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); use Illuminate\Support\Facades\Route; diff --git a/workbench/routes/basic.php b/workbench/routes/basic.php index 4770a74..571e1e9 100644 --- a/workbench/routes/basic.php +++ b/workbench/routes/basic.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); use Illuminate\Support\Facades\Route; diff --git a/workbench/routes/collision.php b/workbench/routes/collision.php index d44b9e1..6e52772 100644 --- a/workbench/routes/collision.php +++ b/workbench/routes/collision.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); use Illuminate\Support\Facades\Route; diff --git a/workbench/routes/extended.php b/workbench/routes/extended.php index 153114a..ef7a5ff 100644 --- a/workbench/routes/extended.php +++ b/workbench/routes/extended.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); use Illuminate\Support\Facades\Route; diff --git a/workbench/routes/mixed_case.php b/workbench/routes/mixed_case.php index 319ced0..7559f25 100644 --- a/workbench/routes/mixed_case.php +++ b/workbench/routes/mixed_case.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); use Illuminate\Support\Facades\Route; diff --git a/workbench/routes/parameters.php b/workbench/routes/parameters.php index 66abe03..97180f5 100644 --- a/workbench/routes/parameters.php +++ b/workbench/routes/parameters.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); use Illuminate\Support\Facades\Route; diff --git a/workbench/routes/protected.php b/workbench/routes/protected.php index 2a9610b..ccfc869 100644 --- a/workbench/routes/protected.php +++ b/workbench/routes/protected.php @@ -1,5 +1,18 @@ + * @copyright 2025 Andrey Helldar + * @license MIT + * + * @see https://github.com/TheDragonCode/laravel-route-names + */ + declare(strict_types=1); use Illuminate\Support\Facades\Route;