From c697bf16ee330ad4c89e2e7c0b71f5b9cfea9550 Mon Sep 17 00:00:00 2001 From: Ivan Vermeyen Date: Thu, 6 Apr 2023 21:57:29 +0200 Subject: [PATCH 01/14] Properly restore locale after catching RouteNotFoundException (#88) Partially fixes (#58) --- src/Illuminate/Routing/UrlGenerator.php | 24 ++++---- src/LocalizedUrlGenerator.php | 4 +- .../Illuminate/Routing/UrlGeneratorTest.php | 60 +++++++++++++++++++ 3 files changed, 76 insertions(+), 12 deletions(-) diff --git a/src/Illuminate/Routing/UrlGenerator.php b/src/Illuminate/Routing/UrlGenerator.php index 3be13f9..8e1e081 100644 --- a/src/Illuminate/Routing/UrlGenerator.php +++ b/src/Illuminate/Routing/UrlGenerator.php @@ -32,11 +32,13 @@ public function route($name, $parameters = [], $absolute = true, $locale = null) App::setLocale($locale); } - $url = parent::route($resolvedName, $parameters, $absolute); - - // Restore the current locale if needed. - if ($locale !== null && $locale !== $currentLocale) { - App::setLocale($currentLocale); + try { + $url = parent::route($resolvedName, $parameters, $absolute); + } finally { + // Restore the current locale if needed. + if ($locale !== null && $locale !== $currentLocale) { + App::setLocale($currentLocale); + } } return $url; @@ -66,11 +68,13 @@ public function signedRoute($name, $parameters = [], $expiration = null, $absolu App::setLocale($locale); } - $url = parent::signedRoute($resolvedName, $parameters, $expiration, $absolute); - - // Restore the current locale if needed. - if ($locale !== null && $locale !== $currentLocale) { - App::setLocale($currentLocale); + try { + $url = parent::signedRoute($resolvedName, $parameters, $expiration, $absolute); + } finally { + // Restore the current locale if needed. + if ($locale !== null && $locale !== $currentLocale) { + App::setLocale($currentLocale); + } } return $url; diff --git a/src/LocalizedUrlGenerator.php b/src/LocalizedUrlGenerator.php index 0ae6302..4be6ece 100644 --- a/src/LocalizedUrlGenerator.php +++ b/src/LocalizedUrlGenerator.php @@ -5,12 +5,12 @@ use CodeZero\LocalizedRoutes\Facades\LocaleConfig; use CodeZero\UrlBuilder\UrlBuilder; use Illuminate\Support\Facades\URL; -use InvalidArgumentException; use Illuminate\Support\Collection; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Request; use Illuminate\Contracts\Routing\UrlRoutable; +use Symfony\Component\Routing\Exception\RouteNotFoundException; class LocalizedUrlGenerator { @@ -118,7 +118,7 @@ protected function generateNamedRouteURL(string $locale, array $parameters = [], { try { return URL::route($this->route->getName(), $parameters, $absolute, $locale); - } catch (InvalidArgumentException $e) { + } catch (RouteNotFoundException $e) { return ''; } } diff --git a/tests/Unit/Illuminate/Routing/UrlGeneratorTest.php b/tests/Unit/Illuminate/Routing/UrlGeneratorTest.php index 03ea62c..60b9b58 100644 --- a/tests/Unit/Illuminate/Routing/UrlGeneratorTest.php +++ b/tests/Unit/Illuminate/Routing/UrlGeneratorTest.php @@ -340,6 +340,66 @@ public function it_generates_a_temporary_signed_route_url_for_a_specific_locale( $this->get($expiredUrl)->assertSee('Expired Signature'); } + /** @test */ + public function it_throws_a_route_not_found_exception_for_missing_route_names_when_generating_a_route_url() + { + $this->expectException(RouteNotFoundException::class); + + URL::route('missing.route'); + } + + /** @test */ + public function the_app_locale_is_correctly_restored_when_catching_a_route_not_found_exception_when_generating_a_route_url() + { + $this->setAppLocale('en'); + + try { + URL::route('missing.route', [], true, 'nl'); + } catch (RouteNotFoundException $exception) {} + + $this->assertEquals('en', App::getLocale()); + } + + /** @test */ + public function it_throws_a_route_not_found_exception_for_missing_route_names_when_generating_a_signed_route_url() + { + $this->expectException(RouteNotFoundException::class); + + URL::signedRoute('missing.route'); + } + + /** @test */ + public function the_app_locale_is_correctly_restored_when_catching_a_route_not_found_exception_when_generating_a_signed_route_url() + { + $this->setAppLocale('en'); + + try { + URL::signedRoute('missing.route', [], null, true, 'nl'); + } catch (RouteNotFoundException $exception) {} + + $this->assertEquals('en', App::getLocale()); + } + + /** @test */ + public function it_throws_a_route_not_found_exception_for_missing_route_names_when_generating_a_temporary_signed_route_url() + { + $this->expectException(RouteNotFoundException::class); + + URL::temporarySignedRoute('missing.route', now()->addMinutes(30)); + } + + /** @test */ + public function the_app_locale_is_correctly_restored_when_catching_a_route_not_found_exception_when_generating_a_temporary_signed_route_url() + { + $this->setAppLocale('en'); + + try { + URL::temporarySignedRoute('missing.route', now()->addMinutes(30), [], true, 'nl'); + } catch (RouteNotFoundException $exception) {} + + $this->assertEquals('en', App::getLocale()); + } + /** @test */ public function it_allows_routes_to_be_cached() { From 8d9a9e0db44f0756b6264f2fd24fe0ab312a67d3 Mon Sep 17 00:00:00 2001 From: Ivan Vermeyen Date: Sat, 8 Apr 2023 15:45:39 +0200 Subject: [PATCH 02/14] Inject Request instead of using the facade (#91) --- src/LocalizedUrlGenerator.php | 19 +++- src/Macros/Route/HasLocalizedMacro.php | 7 +- src/Macros/Route/IsFallbackMacro.php | 4 +- src/Macros/Route/IsLocalizedMacro.php | 22 +--- .../Detectors/RouteActionDetector.php | 21 +++- src/Middleware/Detectors/UrlDetector.php | 23 +++- src/RouteHelper.php | 103 ++++++++++++++++++ 7 files changed, 165 insertions(+), 34 deletions(-) create mode 100644 src/RouteHelper.php diff --git a/src/LocalizedUrlGenerator.php b/src/LocalizedUrlGenerator.php index 4be6ece..7e02718 100644 --- a/src/LocalizedUrlGenerator.php +++ b/src/LocalizedUrlGenerator.php @@ -4,16 +4,22 @@ use CodeZero\LocalizedRoutes\Facades\LocaleConfig; use CodeZero\UrlBuilder\UrlBuilder; +use Illuminate\Http\Request; use Illuminate\Support\Facades\URL; use Illuminate\Support\Collection; use Illuminate\Support\Facades\App; -use Illuminate\Support\Facades\Route; -use Illuminate\Support\Facades\Request; use Illuminate\Contracts\Routing\UrlRoutable; use Symfony\Component\Routing\Exception\RouteNotFoundException; class LocalizedUrlGenerator { + /** + * The current Request. + * + * @var \Illuminate\Http\Request + */ + protected $request; + /** * The current Route. * @@ -23,10 +29,13 @@ class LocalizedUrlGenerator /** * Create a new LocalizedUrlGenerator instance. + * + * @param \Illuminate\Http\Request $request */ - public function __construct() + public function __construct(Request $request) { - $this->route = Route::current(); + $this->request = $request; + $this->route = $request->route(); } /** @@ -41,7 +50,7 @@ public function __construct() */ public function generateFromRequest(string $locale = null, $parameters = null, bool $absolute = true, bool $keepQuery = true): string { - $urlBuilder = UrlBuilder::make(Request::fullUrl()); + $urlBuilder = UrlBuilder::make($this->request->fullUrl()); $requestQueryString = $urlBuilder->getQuery(); $currentDomain = $urlBuilder->getHost(); diff --git a/src/Macros/Route/HasLocalizedMacro.php b/src/Macros/Route/HasLocalizedMacro.php index 1714bb4..aceb977 100644 --- a/src/Macros/Route/HasLocalizedMacro.php +++ b/src/Macros/Route/HasLocalizedMacro.php @@ -2,6 +2,7 @@ namespace CodeZero\LocalizedRoutes\Macros\Route; +use CodeZero\LocalizedRoutes\RouteHelper; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Route; @@ -14,10 +15,8 @@ class HasLocalizedMacro */ public static function register() { - Route::macro('hasLocalized', function ($name, $locale = null) { - $locale = $locale ?: App::getLocale(); - - return $this->routes->hasNamedRoute("{$locale}.{$name}"); + Route::macro('hasLocalized', function (string $name, ?string $locale = null) { + return App::make(RouteHelper::class)->hasLocalized($name, $locale); }); } } diff --git a/src/Macros/Route/IsFallbackMacro.php b/src/Macros/Route/IsFallbackMacro.php index 50f234f..7e34f22 100644 --- a/src/Macros/Route/IsFallbackMacro.php +++ b/src/Macros/Route/IsFallbackMacro.php @@ -2,6 +2,8 @@ namespace CodeZero\LocalizedRoutes\Macros\Route; +use CodeZero\LocalizedRoutes\RouteHelper; +use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Route; class IsFallbackMacro @@ -14,7 +16,7 @@ class IsFallbackMacro public static function register() { Route::macro('isFallback', function () { - return Route::current() && Route::current()->isFallback; + return App::make(RouteHelper::class)->isFallback(); }); } } diff --git a/src/Macros/Route/IsLocalizedMacro.php b/src/Macros/Route/IsLocalizedMacro.php index b5b0081..998f85e 100644 --- a/src/Macros/Route/IsLocalizedMacro.php +++ b/src/Macros/Route/IsLocalizedMacro.php @@ -2,8 +2,8 @@ namespace CodeZero\LocalizedRoutes\Macros\Route; -use CodeZero\LocalizedRoutes\Facades\LocaleConfig; -use Illuminate\Support\Collection; +use CodeZero\LocalizedRoutes\RouteHelper; +use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Route; class IsLocalizedMacro @@ -16,23 +16,7 @@ class IsLocalizedMacro public static function register() { Route::macro('isLocalized', function ($patterns = null, $locales = '*') { - if ($patterns === null) { - $routeAction = LocaleConfig::getRouteAction(); - $route = Route::current(); - - return $route && $route->getAction($routeAction) !== null; - } - - $locales = Collection::make($locales); - $names = Collection::make(); - - Collection::make($patterns)->each(function ($name) use ($locales, $names) { - $locales->each(function ($locale) use ($name, $names) { - $names->push($locale . '.' . $name); - }); - }); - - return Route::is($names->all()); + return App::make(RouteHelper::class)->isLocalized($patterns, $locales); }); } } diff --git a/src/Middleware/Detectors/RouteActionDetector.php b/src/Middleware/Detectors/RouteActionDetector.php index d851724..c319041 100644 --- a/src/Middleware/Detectors/RouteActionDetector.php +++ b/src/Middleware/Detectors/RouteActionDetector.php @@ -2,11 +2,28 @@ namespace CodeZero\LocalizedRoutes\Middleware\Detectors; +use Illuminate\Http\Request; use Illuminate\Support\Facades\Config; -use Illuminate\Support\Facades\Request; class RouteActionDetector implements Detector { + /** + * The current Route. + * + * @var \Illuminate\Routing\Route + */ + protected $route; + + /** + * Create a new RouteActionDetector instance. + * + * @param \Illuminate\Http\Request $request + */ + public function __construct(Request $request) + { + $this->route = $request->route(); + } + /** * Detect the locale. * @@ -16,6 +33,6 @@ public function detect() { $action = Config::get('localized-routes.route_action'); - return Request::route()->getAction($action); + return $this->route->getAction($action); } } diff --git a/src/Middleware/Detectors/UrlDetector.php b/src/Middleware/Detectors/UrlDetector.php index 8d3b575..88b23e8 100644 --- a/src/Middleware/Detectors/UrlDetector.php +++ b/src/Middleware/Detectors/UrlDetector.php @@ -3,10 +3,27 @@ namespace CodeZero\LocalizedRoutes\Middleware\Detectors; use CodeZero\LocalizedRoutes\Facades\LocaleConfig; -use Illuminate\Support\Facades\Request; +use Illuminate\Http\Request; class UrlDetector implements Detector { + /** + * The current Request. + * + * @var \Illuminate\Http\Request + */ + protected $request; + + /** + * Create a new UrlDetector instance. + * + * @param \Illuminate\Http\Request $request + */ + public function __construct(Request $request) + { + $this->request = $request; + } + /** * Detect the locale. * @@ -14,7 +31,7 @@ class UrlDetector implements Detector */ public function detect() { - $slug = Request::segment(1); + $slug = $this->request->segment(1); // If supported locales is a simple array like ['en', 'nl'] // just return the slug and let the calling code check if it is supported. @@ -25,7 +42,7 @@ public function detect() // Find the locale that belongs to the custom domain or slug. // Return the original slug as fallback. // The calling code should validate and handle it. - $domain = Request::getHttpHost(); + $domain = $this->request->getHttpHost(); $locale = LocaleConfig::findLocaleByDomain($domain) ?? LocaleConfig::findLocaleBySlug($slug) ?? $slug; return $locale; diff --git a/src/RouteHelper.php b/src/RouteHelper.php new file mode 100644 index 0000000..19ba9b3 --- /dev/null +++ b/src/RouteHelper.php @@ -0,0 +1,103 @@ +route = $request->route(); + } + + /** + * Check if the current route is a fallback route. + * + * @return bool + */ + public function isFallback(): bool + { + return $this->route && $this->route->isFallback; + } + + /** + * Check if the current route is localized. + * + * @param string|array $patterns + * @param string|array $locales + * + * @return bool + */ + public function isLocalized($patterns = null, $locales = '*'): bool + { + return $patterns === null + ? $this->isCurrentRouteLocalized() + : $this->isCurrentRouteLocalizedWithNamePattern($patterns, $locales); + } + + /** + * Check if a localized route exists. + * + * @param string $name + * @param string|null $locale + * + * @return bool + */ + public function hasLocalized(string $name, ?string $locale = null): bool + { + $locale = $locale ?: App::getLocale(); + + return Route::has("{$locale}.{$name}"); + } + + /** + * Check if the current route is localized. + * + * @return bool + */ + protected function isCurrentRouteLocalized(): bool + { + $routeAction = LocaleConfig::getRouteAction(); + + return $this->route && $this->route->getAction($routeAction) !== null; + } + + /** + * Check if the current route is localized and has a specific name. + * + * @param string|array $patterns + * @param string|array $locales + * + * @return bool + */ + protected function isCurrentRouteLocalizedWithNamePattern($patterns = null, $locales = '*'): bool + { + $locales = Collection::make($locales); + $names = Collection::make(); + + Collection::make($patterns)->each(function ($name) use ($locales, $names) { + $locales->each(function ($locale) use ($name, $names) { + $names->push($locale . '.' . $name); + }); + }); + + return Route::is($names->all()); + } +} From 3efa560b183f4cd718483ac31103c8726a60da1b Mon Sep 17 00:00:00 2001 From: Ivan Vermeyen Date: Sat, 8 Apr 2023 15:48:49 +0200 Subject: [PATCH 03/14] Make sure $this->route is not null --- src/LocalizedUrlGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LocalizedUrlGenerator.php b/src/LocalizedUrlGenerator.php index 7e02718..4282bc2 100644 --- a/src/LocalizedUrlGenerator.php +++ b/src/LocalizedUrlGenerator.php @@ -162,7 +162,7 @@ protected function is404(): bool */ protected function isFallback(): bool { - return $this->route->isFallback; + return $this->routeExists() && $this->route->isFallback; } /** From 386a0e618b75b3dccd45c0e0062a63b297c90c56 Mon Sep 17 00:00:00 2001 From: Ivan Vermeyen Date: Sat, 8 Apr 2023 15:57:08 +0200 Subject: [PATCH 04/14] Tweak route match code (#92) --- src/Controllers/FallbackController.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Controllers/FallbackController.php b/src/Controllers/FallbackController.php index f1790d7..4025d07 100644 --- a/src/Controllers/FallbackController.php +++ b/src/Controllers/FallbackController.php @@ -54,9 +54,7 @@ protected function redirectResponse() */ protected function findRouteByUrl(string $url) { - return Collection::make(Route::getRoutes())->first(function ($route) use ($url) { - return $route->matches(Request::create($url)); - }); + return Route::getRoutes()->match(Request::create($url)); } /** From 355672da4aa1fdccc3e35a6d4ec3de84d68617eb Mon Sep 17 00:00:00 2001 From: Ivan Vermeyen Date: Tue, 25 Apr 2023 16:28:57 +0200 Subject: [PATCH 05/14] Remove unused import --- src/Controllers/FallbackController.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Controllers/FallbackController.php b/src/Controllers/FallbackController.php index 4025d07..0f44583 100644 --- a/src/Controllers/FallbackController.php +++ b/src/Controllers/FallbackController.php @@ -4,7 +4,6 @@ use Illuminate\Http\Request; use Illuminate\Routing\Controller; -use Illuminate\Support\Collection; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\Response; From 8bdb93116a6405d0a535271aa576ee09d3d23174 Mon Sep 17 00:00:00 2001 From: Ivan Vermeyen Date: Tue, 25 Apr 2023 16:30:23 +0200 Subject: [PATCH 06/14] Register facade with Composer (#94) --- composer.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1ec1d2c..db71701 100644 --- a/composer.json +++ b/composer.json @@ -49,7 +49,10 @@ "laravel": { "providers": [ "CodeZero\\LocalizedRoutes\\LocalizedRoutesServiceProvider" - ] + ], + "aliases": { + "LocaleConfig": "CodeZero\\LocalizedRoutes\\Facades\\LocaleConfig" + } }, "preload-files": [ "src/helpers.php" From fac6e4a7bb32e2c42bb7aca53d3336d450ab3cba Mon Sep 17 00:00:00 2001 From: Ivan Vermeyen Date: Tue, 2 May 2023 12:47:12 +0200 Subject: [PATCH 07/14] Handle duplicate route and query string parameter keys --- src/LocalizedUrlGenerator.php | 7 +- .../Macros/Route/LocalizedUrlMacroTest.php | 68 +++++++++++++++++++ 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/src/LocalizedUrlGenerator.php b/src/LocalizedUrlGenerator.php index 4282bc2..d8c8099 100644 --- a/src/LocalizedUrlGenerator.php +++ b/src/LocalizedUrlGenerator.php @@ -83,12 +83,9 @@ public function generateFromRequest(string $locale = null, $parameters = null, b $this->determineQueryStringParameters($requestQueryString, $queryStringParameters, $keepQuery) ); - // Merge the route parameters with the query string parameters, if any. - $namedRouteParameters = array_merge($routeParameters, $urlBuilder->getQuery()); - // Generate the URL using the route's name, if possible. - if ($url = $this->generateNamedRouteURL($locale, $namedRouteParameters, $absolute)) { - return $url; + if ($url = $this->generateNamedRouteURL($locale, $routeParameters, $absolute)) { + return $urlBuilder->getQueryString() ? $url . '?' . $urlBuilder->getQueryString() : $url; } // If a named route could not be resolved, replace the parameter diff --git a/tests/Feature/Macros/Route/LocalizedUrlMacroTest.php b/tests/Feature/Macros/Route/LocalizedUrlMacroTest.php index 4230317..9e3f2ce 100644 --- a/tests/Feature/Macros/Route/LocalizedUrlMacroTest.php +++ b/tests/Feature/Macros/Route/LocalizedUrlMacroTest.php @@ -987,6 +987,74 @@ public function it_ignores_query_string_parameters_using_unnamed_routes() ], $response->original); } + /** @test */ + public function it_prefers_route_parameters_before_query_string_parameters_with_the_same_name_in_unnamed_routes() + { + $this->withoutExceptionHandling(); + $this->setSupportedLocales(['en', 'nl']); + + $model = (new ModelOneWithRouteBinding([ + 'slug' => [ + 'en' => 'en-slug', + 'nl' => 'nl-slug', + ], + ]))->setKeyName('slug'); + + App::instance(ModelOneWithRouteBinding::class, $model); + + Route::localized(function () use ($model) { + Route::get('route/{slug}', function (ModelOneWithRouteBinding $slug) { + return [ + 'current' => Route::localizedUrl(), + 'en' => Route::localizedUrl('en'), + 'nl' => Route::localizedUrl('nl'), + ]; + })->middleware(['web']); + }); + + $response = $this->call('GET', '/en/route/en-slug?slug=duplicate'); + $response->assertOk(); + $this->assertEquals([ + 'current' => URL::to('/en/route/en-slug?slug=duplicate'), + 'en' => URL::to('/en/route/en-slug?slug=duplicate'), + 'nl' => URL::to('/nl/route/nl-slug?slug=duplicate'), + ], $response->original); + } + + /** @test */ + public function it_prefers_route_parameters_before_query_string_parameters_with_the_same_name_in_named_routes() + { + $this->withoutExceptionHandling(); + $this->setSupportedLocales(['en', 'nl']); + + $model = (new ModelOneWithRouteBinding([ + 'slug' => [ + 'en' => 'en-slug', + 'nl' => 'nl-slug', + ], + ]))->setKeyName('slug'); + + App::instance(ModelOneWithRouteBinding::class, $model); + + Route::localized(function () use ($model) { + Route::get('route/{slug}', function (ModelOneWithRouteBinding $slug) { + return [ + 'current' => Route::localizedUrl(), + 'en' => Route::localizedUrl('en'), + 'nl' => Route::localizedUrl('nl'), + ]; + })->middleware(['web'])->name('test'); + }); + + $response = $this->call('GET', '/en/route/en-slug?slug=duplicate'); + $response->assertOk(); + $this->assertEquals([ + 'current' => URL::to('/en/route/en-slug?slug=duplicate'), + 'en' => URL::to('/en/route/en-slug?slug=duplicate'), + 'nl' => URL::to('/nl/route/nl-slug?slug=duplicate'), + ], $response->original); + } + /** @test */ public function it_allows_optional_parameters_with_named_routes() { From cd5c2b90edefb68568f21e9589df71490a5a5e58 Mon Sep 17 00:00:00 2001 From: Alberto Peripolli Date: Wed, 24 May 2023 15:00:23 +0200 Subject: [PATCH 08/14] Allow $slug parameter to be null in LocaleConfig (#97) Slug on findLocaleBySlug can be null --- src/LocaleConfig.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/LocaleConfig.php b/src/LocaleConfig.php index 91be4d6..a833e32 100644 --- a/src/LocaleConfig.php +++ b/src/LocaleConfig.php @@ -184,11 +184,11 @@ public function findDomainByLocale(string $locale): ?string /** * Find the locale that belongs to the given slug. * - * @param string $slug + * @param ?string $slug * * @return string|null */ - public function findLocaleBySlug(string $slug): ?string + public function findLocaleBySlug(?string $slug): ?string { if ($this->hasCustomDomains()) { return null; From 0ebb3acd58b4b635ec9d6d20de9a1770da0c9ced Mon Sep 17 00:00:00 2001 From: Ivan Vermeyen Date: Thu, 1 Jun 2023 22:21:52 +0200 Subject: [PATCH 09/14] Call getRouteKey() instead of accessing the key directly --- src/LocalizedUrlGenerator.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/LocalizedUrlGenerator.php b/src/LocalizedUrlGenerator.php index d8c8099..5e48ea8 100644 --- a/src/LocalizedUrlGenerator.php +++ b/src/LocalizedUrlGenerator.php @@ -353,8 +353,8 @@ protected function getLocalizedRouteKey(string $key, UrlRoutable $model, string App::setLocale($locale); - $bindingField = $this->getBindingFieldFor($key, $model); - $routeKey = $model->$bindingField; + $bindingField = $this->getBindingFieldFor($key); + $routeKey = $bindingField ? $model->$bindingField : $model->getRouteKey(); App::setLocale($originalLocale); @@ -369,12 +369,11 @@ protected function getLocalizedRouteKey(string $key, UrlRoutable $model, string * If you did not use a custom key, we'll use the default route key. * * @param string|int $key - * @param \Illuminate\Contracts\Routing\UrlRoutable $model * * @return string|null */ - protected function getBindingFieldFor($key, UrlRoutable $model): ?string + protected function getBindingFieldFor($key): ?string { - return $this->route->bindingFieldFor($key) ?: $model->getRouteKeyName(); + return $this->route->bindingFieldFor($key); } } From f05f3f826b1d97d77dd5d7c374aad58cfcc74ea4 Mon Sep 17 00:00:00 2001 From: Ivan Vermeyen Date: Wed, 22 Nov 2023 13:22:38 +0100 Subject: [PATCH 10/14] Add required Composer version to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 60280fa..f450950 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ A convenient way to set up and use localized routes in a Laravel app. - PHP >= 7.2.5 - Laravel >= 7.0 +- Composer ^2.3 (for [codezero/composer-preload-files](https://github.com/codezero-be/composer-preload-files)) ## ⬆ Upgrade From fff1b463532feb796645da616062617467261b3f Mon Sep 17 00:00:00 2001 From: Taner <224194+tanerkay@users.noreply.github.com> Date: Tue, 13 Feb 2024 13:54:34 +0100 Subject: [PATCH 11/14] Do not override global config when setting scoped options (#104) --- src/LocalizedRoutesRegistrar.php | 3 --- tests/Feature/Middleware/SetLocaleTest.php | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/LocalizedRoutesRegistrar.php b/src/LocalizedRoutesRegistrar.php index cd3992a..b10a733 100644 --- a/src/LocalizedRoutesRegistrar.php +++ b/src/LocalizedRoutesRegistrar.php @@ -26,9 +26,6 @@ public function register(Closure $closure, array $options = []): void return; } - LocaleConfig::setSupportedLocales($locales); - LocaleConfig::setOmittedLocale($omittedLocale); - $localeRouteAction = LocaleConfig::getRouteAction(); $usingDomains = LocaleConfig::hasCustomDomains(); $usingCustomSlugs = LocaleConfig::hasCustomSlugs(); diff --git a/tests/Feature/Middleware/SetLocaleTest.php b/tests/Feature/Middleware/SetLocaleTest.php index 519a6dc..cabc5d6 100644 --- a/tests/Feature/Middleware/SetLocaleTest.php +++ b/tests/Feature/Middleware/SetLocaleTest.php @@ -2,6 +2,7 @@ namespace CodeZero\LocalizedRoutes\Tests\Feature\Middleware; +use CodeZero\LocalizedRoutes\Facades\LocaleConfig; use CodeZero\LocalizedRoutes\Middleware\SetLocale; use CodeZero\LocalizedRoutes\Tests\TestCase; use Illuminate\Database\Eloquent\Model; @@ -299,4 +300,23 @@ public function it_sets_the_locale_of_routes_with_scoped_config() $response = $this->get('de/with-scoped-config'); $this->assertEquals('de', $response->original); } + + /** @test */ + public function that_scoped_config_does_not_override_global_config(): void + { + $this->setSupportedLocales(['en']); + + Route::localized(function () { + Route::get('with-scoped-config', function () { + return App::getLocale(); + })->middleware(['web', SetLocale::class]); + }, [ + 'supported_locales' => ['en', 'nl'], + ]); + + $this->assertEquals( + ['en'], + LocaleConfig::getSupportedLocales() + ); + } } From 1b49057c4108092ad3c1aa03ea5058e2e02010a2 Mon Sep 17 00:00:00 2001 From: Ivan Vermeyen Date: Mon, 11 Mar 2024 11:16:56 +0100 Subject: [PATCH 12/14] Add support Laravel 11 and PHPUnit 10 (#107) * Bump PHPUnit dependencies * Ignore PHPUnit cache folder * Adopt PHP attributes in test classes * Add return types to test methods * Define test classes as `final` * Update .gitignore * Support only Laravel 10 and newer * Migrate PHPUnit XML * Add instructions to upgrade to Laravel 11 * Add Laravel 11 middleware instructions to README --------- Co-authored-by: Shift --- .github/workflows/run-tests.yml | 23 +-- .gitignore | 1 + README.md | 51 +++-- UPGRADE.md | 20 ++ composer.json | 10 +- phpunit.xml.dist | 22 +-- .../Macros/Route/IsLocalizedMacroTest.php | 27 +-- .../Macros/Route/LocalizedMacroTest.php | 39 ++-- .../Macros/Route/LocalizedUrlMacroTest.php | 175 +++++++++--------- tests/Feature/Middleware/SetLocaleTest.php | 61 +++--- tests/Feature/RedirectToLocalizedTest.php | 19 +- tests/Feature/RouteModelBindingTest.php | 15 +- .../Illuminate/Routing/RedirectorTest.php | 27 +-- .../Illuminate/Routing/UrlGeneratorTest.php | 111 +++++------ tests/Unit/LocaleConfigTest.php | 71 +++---- tests/Unit/Middleware/LocaleHandlerTest.php | 35 ++-- 16 files changed, 370 insertions(+), 337 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index db03362..bd29bc6 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -8,28 +8,17 @@ jobs: strategy: fail-fast: true matrix: - php: [ 8.0, 8.1 ] - laravel: [ 8.*, 9.* , 10.*] + php: [ 8.1, 8.2, 8.3 ] + laravel: [ 10.* , 11.*] dependency-version: [ prefer-stable ] exclude: - - laravel: 10.* - php: 8.0 + - laravel: 11.* + php: 8.1 include: - - laravel: 7.* - php: 7.2 - testbench: 5.* - - laravel: 7.* - php: 8.0 - testbench: 5.* - - laravel: 8.* - php: 7.3 - testbench: 6.* - - laravel: 8.* - testbench: 6.* - - laravel: 9.* - testbench: 7.* - laravel: 10.* testbench: 8.* + - laravel: 11.* + testbench: 9.* name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} diff --git a/.gitignore b/.gitignore index fe1052a..667fe37 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /vendor /phpunit.xml composer.lock +/.phpunit.cache /.phpunit.result.cache diff --git a/README.md b/README.md index f450950..c304a1d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Laravel Localized Routes [![GitHub release](https://img.shields.io/github/release/codezero-be/laravel-localized-routes.svg?style=flat-square)](https://github.com/codezero-be/laravel-localized-routes/releases) -[![Laravel](https://img.shields.io/badge/laravel-10-red?style=flat-square&logo=laravel&logoColor=white)](https://laravel.com) +[![Laravel](https://img.shields.io/badge/laravel-11-red?style=flat-square&logo=laravel&logoColor=white)](https://laravel.com) [![License](https://img.shields.io/packagist/l/codezero/laravel-localized-routes.svg?style=flat-square)](LICENSE.md) [![Build Status](https://img.shields.io/github/actions/workflow/status/codezero-be/laravel-localized-routes/run-tests.yml?style=flat-square&logo=github&logoColor=white&label=tests)](https://github.com/codezero-be/laravel-localized-routes/actions) [![Code Coverage](https://img.shields.io/codacy/coverage/a5db8a1321664e67900c96eadc575ece/master?style=flat-square)](https://app.codacy.com/gh/codezero-be/laravel-localized-routes) @@ -53,8 +53,8 @@ A convenient way to set up and use localized routes in a Laravel app. ## ✅ Requirements -- PHP >= 7.2.5 -- Laravel >= 7.0 +- PHP >= 8.1 +- Laravel >= 10 - Composer ^2.3 (for [codezero/composer-preload-files](https://github.com/codezero-be/composer-preload-files)) ## ⬆ Upgrade @@ -153,30 +153,43 @@ Route::localized(function () { ## 🧩 Add Middleware to Update App Locale By default, the app locale will always be what you configured in `config/app.php`. -To automatically update the app locale, you need to register the middleware. +To automatically update the app locale, you need to register the middleware in the `web` middleware group. +Make sure to add it after `StartSession` and before `SubstituteBindings`. -Add the middleware to the `web` middleware group in `app/Http/Kernel.php`: +The order of the middleware is important if you are using localized route keys (translated slugs)! +The session needs to be active when setting the locale, and the locale needs to be set when substituting the route bindings. + +### Laravel 11 and newer: + +Add the middleware to the `web` middleware group in `bootstrap/app.php`. ```php -protected $middlewareGroups = [ - 'web' => [ - //... - \CodeZero\LocalizedRoutes\Middleware\SetLocale::class, - ], -]; +// bootstrap/app.php +->withMiddleware(function (Middleware $middleware) { + $middleware->web(remove: [ + \Illuminate\Routing\Middleware\SubstituteBindings::class, + ]); + $middleware->web(append: [ + \CodeZero\Localizer\Middleware\SetLocale::class, + \Illuminate\Routing\Middleware\SubstituteBindings::class, + ]); +}) ``` -You also need to add the middleware to the `$middlewarePriority` array in `app/Http/Kernel.php`. -If you don't see the `$middlewarePriority` array, you can copy it from the parent class `Illuminate\Foundation\Http\Kernel`. +### Laravel 10: -Make sure to add it after `StartSession` and before `SubstituteBindings` to trigger it in the correct order: +Add the middleware to the `web` middleware group in `app/Http/Kernel.php`. ```php -protected $middlewarePriority = [ - \Illuminate\Session\Middleware\StartSession::class, // <= after this - //... - \CodeZero\LocalizedRoutes\Middleware\SetLocale::class, - \Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this +// app/Http/Kernel.php +protected $middlewareGroups = [ + 'web' => [ + //... + \Illuminate\Session\Middleware\StartSession::class, // <= after this + //... + \CodeZero\Localizer\Middleware\SetLocale::class, + \Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this + ], ]; ``` diff --git a/UPGRADE.md b/UPGRADE.md index c766187..097b977 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,25 @@ # Upgrade Guide +## Upgrading To 4.0 From 3.x + +### ➡ Minimum Requirements Updated + +Due to PHP and PHPUnit version constraints with Laravel 11, we dropped support for Laravel 7.x, 8.x and 9.x. + +- The minimum PHP version required is now 8.1 +- The minimum Laravel version required is now 10 + +--- + +### ➡ Re-register Middleware + +Laravel 11 no longer has a `app/Http/Kernel.php` to register middleware. +This is now handled in `bootstrap/app.php`. + +🔸 **Actions Required** + +If you use Laravel 11, register the middleware in `bootstrap/app.php` as described in the README. + ## Upgrading To 3.0 From 2.x This upgrade contains a number of small but breaking changes, as well as a huge internal makeover. diff --git a/composer.json b/composer.json index db71701..6a5f313 100644 --- a/composer.json +++ b/composer.json @@ -20,17 +20,17 @@ } ], "require": { - "php": "^7.2.5|^8.0", + "php": "^8.1", "codezero/browser-locale": "^3.0", "codezero/composer-preload-files": "^1.0", - "codezero/laravel-uri-translator": "^1.0", + "codezero/laravel-uri-translator": "^2.0", "codezero/php-url-builder": "^1.0", - "illuminate/support": "^7.0|^8.0|^9.0|^10.0" + "illuminate/support": "^10.0|^11.0" }, "require-dev": { "mockery/mockery": "^1.3.3", - "orchestra/testbench": "^5.0|^6.0|^7.0|^8.0", - "phpunit/phpunit": "^8.0|^9.0" + "orchestra/testbench": "^8.0|^9.0", + "phpunit/phpunit": "^10.5" }, "scripts": { "test": "phpunit" diff --git a/phpunit.xml.dist b/phpunit.xml.dist index e1988a6..dc4366a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,13 +1,13 @@ - + stopOnFailure="false" + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" + cacheDirectory=".phpunit.cache" + backupStaticProperties="false"> ./tests/Unit @@ -16,15 +16,15 @@ ./tests/Feature - - - ./src - - + + + ./src + + diff --git a/tests/Feature/Macros/Route/IsLocalizedMacroTest.php b/tests/Feature/Macros/Route/IsLocalizedMacroTest.php index 6831a7a..70e963c 100644 --- a/tests/Feature/Macros/Route/IsLocalizedMacroTest.php +++ b/tests/Feature/Macros/Route/IsLocalizedMacroTest.php @@ -2,13 +2,14 @@ namespace CodeZero\LocalizedRoutes\Tests\Feature\Macros\Route; +use PHPUnit\Framework\Attributes\Test; use CodeZero\LocalizedRoutes\Tests\TestCase; use Illuminate\Support\Facades\Route; -class IsLocalizedMacroTest extends TestCase +final class IsLocalizedMacroTest extends TestCase { - /** @test */ - public function it_checks_if_the_current_route_is_localized() + #[Test] + public function it_checks_if_the_current_route_is_localized(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -36,8 +37,8 @@ public function it_checks_if_the_current_route_is_localized() $this->assertEquals('false', $response->original); } - /** @test */ - public function it_checks_if_the_current_route_has_a_name_with_any_locale_prefix() + #[Test] + public function it_checks_if_the_current_route_has_a_name_with_any_locale_prefix(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -69,8 +70,8 @@ public function it_checks_if_the_current_route_has_a_name_with_any_locale_prefix $this->assertEquals('false', $response->original); } - /** @test */ - public function it_checks_if_the_current_route_has_a_name_with_a_specific_locale_prefix() + #[Test] + public function it_checks_if_the_current_route_has_a_name_with_a_specific_locale_prefix(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -90,8 +91,8 @@ public function it_checks_if_the_current_route_has_a_name_with_a_specific_locale $this->assertEquals('false', $response->original); } - /** @test */ - public function it_checks_if_the_current_route_has_a_name_that_is_in_an_array_of_names_with_any_locale_prefix() + #[Test] + public function it_checks_if_the_current_route_has_a_name_that_is_in_an_array_of_names_with_any_locale_prefix(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -135,8 +136,8 @@ public function it_checks_if_the_current_route_has_a_name_that_is_in_an_array_of $this->assertEquals('false', $response->original); } - /** @test */ - public function it_checks_if_the_current_route_has_a_name_that_is_in_an_array_of_names_with_a_specific_locale_prefix() + #[Test] + public function it_checks_if_the_current_route_has_a_name_that_is_in_an_array_of_names_with_a_specific_locale_prefix(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -180,8 +181,8 @@ public function it_checks_if_the_current_route_has_a_name_that_is_in_an_array_of $this->assertEquals('false', $response->original); } - /** @test */ - public function it_checks_if_the_current_route_has_a_name_with_a_locale_prefix_in_an_array() + #[Test] + public function it_checks_if_the_current_route_has_a_name_with_a_locale_prefix_in_an_array(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl', 'fr']); diff --git a/tests/Feature/Macros/Route/LocalizedMacroTest.php b/tests/Feature/Macros/Route/LocalizedMacroTest.php index cbfcfe8..fd802a7 100644 --- a/tests/Feature/Macros/Route/LocalizedMacroTest.php +++ b/tests/Feature/Macros/Route/LocalizedMacroTest.php @@ -2,14 +2,15 @@ namespace CodeZero\LocalizedRoutes\Tests\Feature\Macros\Route; +use PHPUnit\Framework\Attributes\Test; use CodeZero\LocalizedRoutes\Tests\TestCase; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Route; -class LocalizedMacroTest extends TestCase +final class LocalizedMacroTest extends TestCase { - /** @test */ - public function it_registers_a_route_for_each_locale() + #[Test] + public function it_registers_a_route_for_each_locale(): void { $this->setSupportedLocales(['en', 'nl']); @@ -35,8 +36,8 @@ public function it_registers_a_route_for_each_locale() $this->assertContains('nl/route', $uris); } - /** @test */ - public function it_registers_a_root_route_for_each_locale() + #[Test] + public function it_registers_a_root_route_for_each_locale(): void { $this->setSupportedLocales(['en', 'nl']); @@ -58,8 +59,8 @@ public function it_registers_a_root_route_for_each_locale() $this->assertContains('nl', $uris); } - /** @test */ - public function it_registers_a_url_without_prefix_for_a_configured_main_locale() + #[Test] + public function it_registers_a_url_without_prefix_for_a_configured_main_locale(): void { $this->setSupportedLocales(['en', 'nl']); $this->setOmittedLocale('en'); @@ -82,8 +83,8 @@ public function it_registers_a_url_without_prefix_for_a_configured_main_locale() $this->assertContains('nl/about', $uris); } - /** @test */ - public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale() + #[Test] + public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale(): void { $this->setSupportedLocales(['en', 'nl']); $this->setOmittedLocale('en'); @@ -99,8 +100,8 @@ public function it_registers_routes_in_the_correct_order_without_prefix_for_a_co ); } - /** @test */ - public function it_maps_a_custom_slug_to_each_locale() + #[Test] + public function it_maps_a_custom_slug_to_each_locale(): void { $this->setSupportedLocales([ 'en' => 'english', @@ -123,8 +124,8 @@ public function it_maps_a_custom_slug_to_each_locale() $this->assertEquals('dutch', $route->uri); } - /** @test */ - public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale_with_custom_slugs() + #[Test] + public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale_with_custom_slugs(): void { $this->setSupportedLocales([ 'en' => 'english', @@ -143,8 +144,8 @@ public function it_registers_routes_in_the_correct_order_without_prefix_for_a_co ); } - /** @test */ - public function it_maps_a_custom_domain_to_each_locale() + #[Test] + public function it_maps_a_custom_domain_to_each_locale(): void { $this->setSupportedLocales([ 'en' => 'english-domain.com', @@ -169,8 +170,8 @@ public function it_maps_a_custom_domain_to_each_locale() $this->assertEquals('/', $route->uri); } - /** @test */ - public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale_with_domains() + #[Test] + public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale_with_domains(): void { $this->setSupportedLocales([ 'en' => 'english-domain.com', @@ -208,8 +209,8 @@ public function it_registers_routes_in_the_correct_order_without_prefix_for_a_co $this->assertEquals('{slug}', $route->uri); } - /** @test */ - public function it_uses_scoped_config_options() + #[Test] + public function it_uses_scoped_config_options(): void { $this->setSupportedLocales(['en']); $this->setOmittedLocale(null); diff --git a/tests/Feature/Macros/Route/LocalizedUrlMacroTest.php b/tests/Feature/Macros/Route/LocalizedUrlMacroTest.php index 9e3f2ce..9c0594e 100644 --- a/tests/Feature/Macros/Route/LocalizedUrlMacroTest.php +++ b/tests/Feature/Macros/Route/LocalizedUrlMacroTest.php @@ -2,6 +2,7 @@ namespace CodeZero\LocalizedRoutes\Tests\Feature\Macros\Route; +use PHPUnit\Framework\Attributes\Test; use CodeZero\LocalizedRoutes\Middleware\SetLocale; use CodeZero\LocalizedRoutes\Tests\Stubs\Models\ModelOneWithRouteBinding; use CodeZero\LocalizedRoutes\Tests\Stubs\Models\ModelTwoWithRouteBinding; @@ -15,10 +16,10 @@ use Illuminate\Support\Facades\URL; use Illuminate\Support\Facades\View; -class LocalizedUrlMacroTest extends TestCase +final class LocalizedUrlMacroTest extends TestCase { - /** @test */ - public function it_generates_urls_with_default_localized_route_keys_for_the_current_route_using_route_model_binding() + #[Test] + public function it_generates_urls_with_default_localized_route_keys_for_the_current_route_using_route_model_binding(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -51,8 +52,8 @@ public function it_generates_urls_with_default_localized_route_keys_for_the_curr ], $response->original); } - /** @test */ - public function it_generates_urls_for_the_current_route_with_different_models_using_route_model_binding() + #[Test] + public function it_generates_urls_for_the_current_route_with_different_models_using_route_model_binding(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -93,8 +94,8 @@ public function it_generates_urls_for_the_current_route_with_different_models_us ], $response->original); } - /** @test */ - public function it_generates_urls_with_custom_localized_route_keys_for_the_current_route_using_route_model_binding() + #[Test] + public function it_generates_urls_with_custom_localized_route_keys_for_the_current_route_using_route_model_binding(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -127,8 +128,8 @@ public function it_generates_urls_with_custom_localized_route_keys_for_the_curre ], $response->original); } - /** @test */ - public function you_can_implement_an_interface_and_let_your_model_return_custom_parameters_with_route_model_binding() + #[Test] + public function you_can_implement_an_interface_and_let_your_model_return_custom_parameters_with_route_model_binding(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -162,8 +163,8 @@ public function you_can_implement_an_interface_and_let_your_model_return_custom_ ], $response->original); } - /** @test */ - public function it_cannot_guess_a_localized_route_key_without_route_model_binding() + #[Test] + public function it_cannot_guess_a_localized_route_key_without_route_model_binding(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -196,8 +197,8 @@ public function it_cannot_guess_a_localized_route_key_without_route_model_bindin ], $response->original); } - /** @test */ - public function you_can_pass_it_a_model_with_a_localized_route_key_without_route_model_binding() + #[Test] + public function you_can_pass_it_a_model_with_a_localized_route_key_without_route_model_binding(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -230,8 +231,8 @@ public function you_can_pass_it_a_model_with_a_localized_route_key_without_route ], $response->original); } - /** @test */ - public function you_can_pass_it_a_closure_that_returns_the_parameters_without_route_model_binding() + #[Test] + public function you_can_pass_it_a_closure_that_returns_the_parameters_without_route_model_binding(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -269,8 +270,8 @@ public function you_can_pass_it_a_closure_that_returns_the_parameters_without_ro ], $response->original); } - /** @test */ - public function it_handles_unnamed_non_localized_routes() + #[Test] + public function it_handles_unnamed_non_localized_routes(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -307,8 +308,8 @@ public function it_handles_unnamed_non_localized_routes() ], $response->original); } - /** @test */ - public function it_handles_unnamed_localized_routes() + #[Test] + public function it_handles_unnamed_localized_routes(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -347,8 +348,8 @@ public function it_handles_unnamed_localized_routes() ], $response->original); } - /** @test */ - public function it_returns_the_current_url_for_existing_non_localized_routes() + #[Test] + public function it_returns_the_current_url_for_existing_non_localized_routes(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -370,8 +371,8 @@ public function it_returns_the_current_url_for_existing_non_localized_routes() ], $response->original); } - /** @test */ - public function it_returns_the_url_for_existing_unnamed_localized_routes_using_custom_slugs() + #[Test] + public function it_returns_the_url_for_existing_unnamed_localized_routes_using_custom_slugs(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales([ @@ -399,8 +400,8 @@ public function it_returns_the_url_for_existing_unnamed_localized_routes_using_c ], $response->original); } - /** @test */ - public function it_returns_the_url_for_existing_named_localized_routes_using_custom_slugs() + #[Test] + public function it_returns_the_url_for_existing_named_localized_routes_using_custom_slugs(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales([ @@ -428,8 +429,8 @@ public function it_returns_the_url_for_existing_named_localized_routes_using_cus ], $response->original); } - /** @test */ - public function it_returns_the_url_for_existing_unnamed_localized_routes_using_domains() + #[Test] + public function it_returns_the_url_for_existing_unnamed_localized_routes_using_domains(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales([ @@ -458,8 +459,8 @@ public function it_returns_the_url_for_existing_unnamed_localized_routes_using_d ], $response->original); } - /** @test */ - public function it_returns_the_url_for_existing_named_localized_routes_using_domains() + #[Test] + public function it_returns_the_url_for_existing_named_localized_routes_using_domains(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales([ @@ -488,8 +489,8 @@ public function it_returns_the_url_for_existing_named_localized_routes_using_dom ], $response->original); } - /** @test */ - public function the_macro_does_not_blow_up_on_a_default_404_error() + #[Test] + public function the_macro_does_not_blow_up_on_a_default_404_error(): void { // Although a default 404 has no Route::current() and is no real View, the composer still triggers. // Custom 404 views that trigger the macro still don't have a Route::current(). @@ -502,8 +503,8 @@ public function the_macro_does_not_blow_up_on_a_default_404_error() $response->assertResponseHasNoView(); } - /** @test */ - public function a_404_receives_the_correct_localized_url_from_a_view_composer() + #[Test] + public function a_404_receives_the_correct_localized_url_from_a_view_composer(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -519,8 +520,8 @@ public function a_404_receives_the_correct_localized_url_from_a_view_composer() $this->assertEquals(URL::to('/nl/route/does/not/exist'), trim($response->original)); } - /** @test */ - public function a_404_is_not_localized_when_triggered_by_a_non_existing_route() + #[Test] + public function a_404_is_not_localized_when_triggered_by_a_non_existing_route(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -532,8 +533,8 @@ public function a_404_is_not_localized_when_triggered_by_a_non_existing_route() $this->assertEquals('en', trim($response->original)); } - /** @test */ - public function a_404_is_localized_when_a_registered_route_throws_a_not_found_exception() + #[Test] + public function a_404_is_localized_when_a_registered_route_throws_a_not_found_exception(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -551,8 +552,8 @@ public function a_404_is_localized_when_a_registered_route_throws_a_not_found_ex $this->assertEquals('nl', trim($response->original)); } - /** @test */ - public function a_404_is_localized_when_a_registered_route_throws_a_model_not_found_exception() + #[Test] + public function a_404_is_localized_when_a_registered_route_throws_a_model_not_found_exception(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -570,8 +571,8 @@ public function a_404_is_localized_when_a_registered_route_throws_a_model_not_fo $this->assertEquals('nl', trim($response->original)); } - /** @test */ - public function a_fallback_route_is_not_triggered_when_a_registered_route_throws_a_not_found_exception() + #[Test] + public function a_fallback_route_is_not_triggered_when_a_registered_route_throws_a_not_found_exception(): void { Route::get('abort', function () { return abort(404); @@ -587,8 +588,8 @@ public function a_fallback_route_is_not_triggered_when_a_registered_route_throws $this->assertNotEquals('fallback', $response->original); } - /** @test */ - public function a_fallback_route_is_not_triggered_when_a_registered_route_throws_a_model_not_found_exception() + #[Test] + public function a_fallback_route_is_not_triggered_when_a_registered_route_throws_a_model_not_found_exception(): void { Route::get('route/{model}', function ($model) { throw new ModelNotFoundException(); @@ -604,8 +605,8 @@ public function a_fallback_route_is_not_triggered_when_a_registered_route_throws $this->assertNotEquals('fallback', $response->original); } - /** @test */ - public function it_returns_a_localized_url_for_a_localized_fallback_route() + #[Test] + public function it_returns_a_localized_url_for_a_localized_fallback_route(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -630,8 +631,8 @@ public function it_returns_a_localized_url_for_a_localized_fallback_route() ], $response->original); } - /** @test */ - public function it_returns_a_localized_url_for_a_non_localized_fallback_route_if_the_url_contains_a_supported_locale() + #[Test] + public function it_returns_a_localized_url_for_a_non_localized_fallback_route_if_the_url_contains_a_supported_locale(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -654,8 +655,8 @@ public function it_returns_a_localized_url_for_a_non_localized_fallback_route_if ], $response->original); } - /** @test */ - public function it_returns_a_localized_url_for_a_non_localized_fallback_route_if_the_url_does_not_contain_a_supported_locale() + #[Test] + public function it_returns_a_localized_url_for_a_non_localized_fallback_route_if_the_url_does_not_contain_a_supported_locale(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -678,8 +679,8 @@ public function it_returns_a_localized_url_for_a_non_localized_fallback_route_if ], $response->original); } - /** @test */ - public function it_returns_a_localized_url_for_a_non_localized_fallback_route_when_omitting_the_main_locale() + #[Test] + public function it_returns_a_localized_url_for_a_non_localized_fallback_route_when_omitting_the_main_locale(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -703,8 +704,8 @@ public function it_returns_a_localized_url_for_a_non_localized_fallback_route_wh ], $response->original); } - /** @test */ - public function it_returns_a_localized_url_for_a_non_localized_fallback_route_when_using_custom_domains() + #[Test] + public function it_returns_a_localized_url_for_a_non_localized_fallback_route_when_using_custom_domains(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales([ @@ -730,8 +731,8 @@ public function it_returns_a_localized_url_for_a_non_localized_fallback_route_wh ], $response->original); } - /** @test */ - public function it_generates_non_absolute_urls_for_existing_routes() + #[Test] + public function it_generates_non_absolute_urls_for_existing_routes(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -754,8 +755,8 @@ public function it_generates_non_absolute_urls_for_existing_routes() ], $response->original); } - /** @test */ - public function it_generates_non_absolute_urls_for_non_existing_routes() + #[Test] + public function it_generates_non_absolute_urls_for_non_existing_routes(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -771,8 +772,8 @@ public function it_generates_non_absolute_urls_for_non_existing_routes() $this->assertEquals('/en/route/does/not/exist', trim($response->original)); } - /** @test */ - public function it_returns_a_url_with_query_string_for_existing_non_localized_unnamed_routes() + #[Test] + public function it_returns_a_url_with_query_string_for_existing_non_localized_unnamed_routes(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -794,8 +795,8 @@ public function it_returns_a_url_with_query_string_for_existing_non_localized_un ], $response->original); } - /** @test */ - public function it_returns_a_url_with_query_string_for_existing_localized_unnamed_routes() + #[Test] + public function it_returns_a_url_with_query_string_for_existing_localized_unnamed_routes(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -820,8 +821,8 @@ public function it_returns_a_url_with_query_string_for_existing_localized_unname ], $response->original); } - /** @test */ - public function it_returns_a_url_with_query_string_for_existing_non_localized_named_routes() + #[Test] + public function it_returns_a_url_with_query_string_for_existing_non_localized_named_routes(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -843,8 +844,8 @@ public function it_returns_a_url_with_query_string_for_existing_non_localized_na ], $response->original); } - /** @test */ - public function it_returns_a_url_with_query_string_for_existing_localized_named_routes() + #[Test] + public function it_returns_a_url_with_query_string_for_existing_localized_named_routes(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -869,8 +870,8 @@ public function it_returns_a_url_with_query_string_for_existing_localized_named_ ], $response->original); } - /** @test */ - public function it_returns_a_url_without_query_string_for_existing_localized_named_routes() + #[Test] + public function it_returns_a_url_without_query_string_for_existing_localized_named_routes(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -895,8 +896,8 @@ public function it_returns_a_url_without_query_string_for_existing_localized_nam ], $response->original); } - /** @test */ - public function it_accepts_query_string_parameters_using_named_routes() + #[Test] + public function it_accepts_query_string_parameters_using_named_routes(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -918,8 +919,8 @@ public function it_accepts_query_string_parameters_using_named_routes() ], $response->original); } - /** @test */ - public function it_ignores_query_string_parameters_using_named_routes() + #[Test] + public function it_ignores_query_string_parameters_using_named_routes(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -941,8 +942,8 @@ public function it_ignores_query_string_parameters_using_named_routes() ], $response->original); } - /** @test */ - public function it_accepts_query_string_parameters_using_unnamed_routes() + #[Test] + public function it_accepts_query_string_parameters_using_unnamed_routes(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -964,8 +965,8 @@ public function it_accepts_query_string_parameters_using_unnamed_routes() ], $response->original); } - /** @test */ - public function it_ignores_query_string_parameters_using_unnamed_routes() + #[Test] + public function it_ignores_query_string_parameters_using_unnamed_routes(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -987,8 +988,8 @@ public function it_ignores_query_string_parameters_using_unnamed_routes() ], $response->original); } - /** @test */ - public function it_prefers_route_parameters_before_query_string_parameters_with_the_same_name_in_unnamed_routes() + #[Test] + public function it_prefers_route_parameters_before_query_string_parameters_with_the_same_name_in_unnamed_routes(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -1021,8 +1022,8 @@ public function it_prefers_route_parameters_before_query_string_parameters_with_ ], $response->original); } - /** @test */ - public function it_prefers_route_parameters_before_query_string_parameters_with_the_same_name_in_named_routes() + #[Test] + public function it_prefers_route_parameters_before_query_string_parameters_with_the_same_name_in_named_routes(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -1055,8 +1056,8 @@ public function it_prefers_route_parameters_before_query_string_parameters_with_ ], $response->original); } - /** @test */ - public function it_allows_optional_parameters_with_named_routes() + #[Test] + public function it_allows_optional_parameters_with_named_routes(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -1078,8 +1079,8 @@ public function it_allows_optional_parameters_with_named_routes() ], $response->original); } - /** @test */ - public function it_allows_optional_parameters_with_unnamed_routes() + #[Test] + public function it_allows_optional_parameters_with_unnamed_routes(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -1101,8 +1102,8 @@ public function it_allows_optional_parameters_with_unnamed_routes() ], $response->original); } - /** @test */ - public function it_handles_capitalized_parameter_names() + #[Test] + public function it_handles_capitalized_parameter_names(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -1124,8 +1125,8 @@ public function it_handles_capitalized_parameter_names() ], $response->original); } - /** @test */ - public function it_returns_a_url_with_translated_slugs_for_named_routes() + #[Test] + public function it_returns_a_url_with_translated_slugs_for_named_routes(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); diff --git a/tests/Feature/Middleware/SetLocaleTest.php b/tests/Feature/Middleware/SetLocaleTest.php index cabc5d6..4e04d8e 100644 --- a/tests/Feature/Middleware/SetLocaleTest.php +++ b/tests/Feature/Middleware/SetLocaleTest.php @@ -2,6 +2,7 @@ namespace CodeZero\LocalizedRoutes\Tests\Feature\Middleware; +use PHPUnit\Framework\Attributes\Test; use CodeZero\LocalizedRoutes\Facades\LocaleConfig; use CodeZero\LocalizedRoutes\Middleware\SetLocale; use CodeZero\LocalizedRoutes\Tests\TestCase; @@ -11,10 +12,10 @@ use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Route; -class SetLocaleTest extends TestCase +final class SetLocaleTest extends TestCase { - /** @test */ - public function it_looks_for_a_locale_in_a_custom_route_action() + #[Test] + public function it_looks_for_a_locale_in_a_custom_route_action(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -34,8 +35,8 @@ public function it_looks_for_a_locale_in_a_custom_route_action() $this->assertEquals('nl', $response->original); } - /** @test */ - public function it_looks_for_a_locale_in_the_url() + #[Test] + public function it_looks_for_a_locale_in_the_url(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -51,8 +52,8 @@ public function it_looks_for_a_locale_in_the_url() $this->assertEquals('nl', $response->original); } - /** @test */ - public function it_looks_for_custom_slugs() + #[Test] + public function it_looks_for_custom_slugs(): void { $this->setSupportedLocales([ 'en' => 'english', @@ -71,8 +72,8 @@ public function it_looks_for_custom_slugs() $this->assertEquals('nl', $response->original); } - /** @test */ - public function it_looks_for_custom_domains() + #[Test] + public function it_looks_for_custom_domains(): void { $this->setSupportedLocales([ 'en' => 'english.test', @@ -93,8 +94,8 @@ public function it_looks_for_custom_domains() $this->assertEquals('nl', $response->original); } - /** @test */ - public function it_checks_for_a_configured_omitted_locale() + #[Test] + public function it_checks_for_a_configured_omitted_locale(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -112,8 +113,8 @@ public function it_checks_for_a_configured_omitted_locale() $this->assertEquals('nl', $response->original); } - /** @test */ - public function it_looks_for_a_locale_on_the_authenticated_user() + #[Test] + public function it_looks_for_a_locale_on_the_authenticated_user(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -133,8 +134,8 @@ public function it_looks_for_a_locale_on_the_authenticated_user() $this->assertEquals('nl', $response->original); } - /** @test */ - public function it_will_bypass_missing_attribute_exception_if_the_locale_attribute_is_missing_on_the_user_model() + #[Test] + public function it_will_bypass_missing_attribute_exception_if_the_locale_attribute_is_missing_on_the_user_model(): void { $this->skipTestIfLaravelVersionIsLowerThan('9.35.0'); @@ -156,8 +157,8 @@ public function it_will_bypass_missing_attribute_exception_if_the_locale_attribu $this->assertEquals('en', $response->original); } - /** @test */ - public function it_looks_for_a_locale_in_the_session() + #[Test] + public function it_looks_for_a_locale_in_the_session(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -175,8 +176,8 @@ public function it_looks_for_a_locale_in_the_session() $this->assertEquals('nl', $response->original); } - /** @test */ - public function it_looks_for_a_locale_in_a_cookie() + #[Test] + public function it_looks_for_a_locale_in_a_cookie(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -195,8 +196,8 @@ public function it_looks_for_a_locale_in_a_cookie() $this->assertEquals('nl', $response->original); } - /** @test */ - public function it_looks_for_a_locale_in_the_browser() + #[Test] + public function it_looks_for_a_locale_in_the_browser(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -214,8 +215,8 @@ public function it_looks_for_a_locale_in_the_browser() $this->assertEquals('nl', $response->original); } - /** @test */ - public function it_returns_the_best_match_when_a_browser_locale_is_used() + #[Test] + public function it_returns_the_best_match_when_a_browser_locale_is_used(): void { $this->setSupportedLocales(['en', 'nl', 'fr']); $this->setAppLocale('en'); @@ -233,8 +234,8 @@ public function it_returns_the_best_match_when_a_browser_locale_is_used() $this->assertEquals('nl', $response->original); } - /** @test */ - public function it_looks_for_the_current_app_locale() + #[Test] + public function it_looks_for_the_current_app_locale(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('nl'); @@ -250,8 +251,8 @@ public function it_looks_for_the_current_app_locale() $this->assertEquals('nl', $response->original); } - /** @test */ - public function trusted_detectors_ignore_supported_locales_and_may_set_any_locale() + #[Test] + public function trusted_detectors_ignore_supported_locales_and_may_set_any_locale(): void { $this->setSupportedLocales(['en']); $this->setAppLocale('en'); @@ -275,8 +276,8 @@ public function trusted_detectors_ignore_supported_locales_and_may_set_any_local $this->assertEquals('nl', $response->original); } - /** @test */ - public function it_sets_the_locale_of_routes_with_scoped_config() + #[Test] + public function it_sets_the_locale_of_routes_with_scoped_config(): void { $this->setSupportedLocales(['en']); $this->setAppLocale('en'); @@ -301,7 +302,7 @@ public function it_sets_the_locale_of_routes_with_scoped_config() $this->assertEquals('de', $response->original); } - /** @test */ + #[Test] public function that_scoped_config_does_not_override_global_config(): void { $this->setSupportedLocales(['en']); diff --git a/tests/Feature/RedirectToLocalizedTest.php b/tests/Feature/RedirectToLocalizedTest.php index db85947..f131c7e 100644 --- a/tests/Feature/RedirectToLocalizedTest.php +++ b/tests/Feature/RedirectToLocalizedTest.php @@ -2,13 +2,14 @@ namespace CodeZero\LocalizedRoutes\Tests\Feature; +use PHPUnit\Framework\Attributes\Test; use CodeZero\LocalizedRoutes\Tests\TestCase; use Illuminate\Support\Facades\Route; -class RedirectToLocalizedTest extends TestCase +final class RedirectToLocalizedTest extends TestCase { - /** @test */ - public function it_redirects_to_the_localized_url() + #[Test] + public function it_redirects_to_the_localized_url(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -34,8 +35,8 @@ public function it_redirects_to_the_localized_url() $this->get('nl/about')->assertOk(); } - /** @test */ - public function it_redirects_when_default_locale_slug_is_omitted() + #[Test] + public function it_redirects_when_default_locale_slug_is_omitted(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en', 'nl']); @@ -60,8 +61,8 @@ public function it_redirects_when_default_locale_slug_is_omitted() $this->get('nl/about')->assertOk(); } - /** @test */ - public function it_throws_404_and_does_not_redirect_if_no_localized_route_is_registered() + #[Test] + public function it_throws_404_and_does_not_redirect_if_no_localized_route_is_registered(): void { $this->setSupportedLocales(['en', 'nl']); $this->setRedirectToLocalizedUrls(true); @@ -72,8 +73,8 @@ public function it_throws_404_and_does_not_redirect_if_no_localized_route_is_reg $this->get('missing')->assertNotFound(); } - /** @test */ - public function it_redirects_to_the_localized_url_with_custom_slugs() + #[Test] + public function it_redirects_to_the_localized_url_with_custom_slugs(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales([ diff --git a/tests/Feature/RouteModelBindingTest.php b/tests/Feature/RouteModelBindingTest.php index 11ed182..fc4fdd7 100644 --- a/tests/Feature/RouteModelBindingTest.php +++ b/tests/Feature/RouteModelBindingTest.php @@ -2,16 +2,17 @@ namespace CodeZero\LocalizedRoutes\Tests\Feature; +use PHPUnit\Framework\Attributes\Test; use CodeZero\LocalizedRoutes\Middleware\SetLocale; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Route; use CodeZero\LocalizedRoutes\Tests\TestCase; use CodeZero\LocalizedRoutes\Tests\Stubs\Models\ModelOneWithRouteBinding; -class RouteModelBindingTest extends TestCase +final class RouteModelBindingTest extends TestCase { - /** @test */ - public function it_loads_a_route_with_a_localized_route_key_based_on_the_active_locale() + #[Test] + public function it_loads_a_route_with_a_localized_route_key_based_on_the_active_locale(): void { $this->setSupportedLocales(['en', 'nl']); @@ -44,8 +45,8 @@ public function it_loads_a_route_with_a_localized_route_key_based_on_the_active_ $this->get('en/test/nl-slug')->assertNotFound(); } - /** @test */ - public function it_loads_a_route_with_a_custom_localized_route_key_based_on_the_active_locale() + #[Test] + public function it_loads_a_route_with_a_custom_localized_route_key_based_on_the_active_locale(): void { $this->setSupportedLocales(['en', 'nl']); @@ -78,8 +79,8 @@ public function it_loads_a_route_with_a_custom_localized_route_key_based_on_the_ $this->get('en/test/nl-slug')->assertNotFound(); } - /** @test */ - public function it_loads_a_route_with_a_localized_route_key_with_custom_slugs() + #[Test] + public function it_loads_a_route_with_a_localized_route_key_with_custom_slugs(): void { $this->setSupportedLocales([ 'en' => 'english', diff --git a/tests/Unit/Illuminate/Routing/RedirectorTest.php b/tests/Unit/Illuminate/Routing/RedirectorTest.php index 4eb773d..37fec9e 100644 --- a/tests/Unit/Illuminate/Routing/RedirectorTest.php +++ b/tests/Unit/Illuminate/Routing/RedirectorTest.php @@ -2,15 +2,16 @@ namespace CodeZero\LocalizedRoutes\Tests\Unit\Illuminate\Routing; +use PHPUnit\Framework\Attributes\Test; use CodeZero\LocalizedRoutes\Tests\TestCase; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\URL; -class RedirectorTest extends TestCase +final class RedirectorTest extends TestCase { - /** @test */ - public function it_redirects_to_a_named_route_in_the_current_locale() + #[Test] + public function it_redirects_to_a_named_route_in_the_current_locale(): void { $this->setAppLocale('en'); @@ -24,8 +25,8 @@ public function it_redirects_to_a_named_route_in_the_current_locale() $response->assertRedirect('/en/target/route'); } - /** @test */ - public function it_redirects_to_a_named_route_in_a_specific_locale() + #[Test] + public function it_redirects_to_a_named_route_in_a_specific_locale(): void { $this->setAppLocale('en'); @@ -40,8 +41,8 @@ public function it_redirects_to_a_named_route_in_a_specific_locale() $response->assertRedirect('/nl/target/route'); } - /** @test */ - public function it_redirects_to_a_signed_route_in_the_current_locale() + #[Test] + public function it_redirects_to_a_signed_route_in_the_current_locale(): void { $this->setAppLocale('en'); @@ -55,8 +56,8 @@ public function it_redirects_to_a_signed_route_in_the_current_locale() $response->assertRedirect(URL::signedRoute('target.route')); } - /** @test */ - public function it_redirects_to_a_signed_route_in_a_specific_locale() + #[Test] + public function it_redirects_to_a_signed_route_in_a_specific_locale(): void { $this->setAppLocale('en'); @@ -71,8 +72,8 @@ public function it_redirects_to_a_signed_route_in_a_specific_locale() $response->assertRedirect(URL::signedRoute('target.route', [], null, true, 'nl')); } - /** @test */ - public function it_redirects_to_a_temporary_signed_route_in_the_current_locale() + #[Test] + public function it_redirects_to_a_temporary_signed_route_in_the_current_locale(): void { $this->setAppLocale('en'); @@ -86,8 +87,8 @@ public function it_redirects_to_a_temporary_signed_route_in_the_current_locale() $response->assertRedirect(URL::temporarySignedRoute('target.route', now()->addMinutes(30))); } - /** @test */ - public function it_redirects_to_a_temporary_signed_route_in_a_specific_locale() + #[Test] + public function it_redirects_to_a_temporary_signed_route_in_a_specific_locale(): void { $this->setAppLocale('en'); diff --git a/tests/Unit/Illuminate/Routing/UrlGeneratorTest.php b/tests/Unit/Illuminate/Routing/UrlGeneratorTest.php index 60b9b58..fad63be 100644 --- a/tests/Unit/Illuminate/Routing/UrlGeneratorTest.php +++ b/tests/Unit/Illuminate/Routing/UrlGeneratorTest.php @@ -2,6 +2,7 @@ namespace CodeZero\LocalizedRoutes\Tests\Unit\Illuminate\Routing; +use PHPUnit\Framework\Attributes\Test; use CodeZero\LocalizedRoutes\Tests\Stubs\Controller; use CodeZero\LocalizedRoutes\Tests\Stubs\Models\ModelOneWithRouteBinding; use CodeZero\LocalizedRoutes\Tests\TestCase; @@ -13,17 +14,17 @@ use InvalidArgumentException; use Symfony\Component\Routing\Exception\RouteNotFoundException; -class UrlGeneratorTest extends TestCase +final class UrlGeneratorTest extends TestCase { - /** @test */ - public function it_binds_our_custom_url_generator_class() + #[Test] + public function it_binds_our_custom_url_generator_class(): void { $this->assertInstanceOf(UrlGenerator::class, App::make('url')); $this->assertInstanceOf(UrlGenerator::class, App::make('redirect')->getUrlGenerator()); } - /** @test */ - public function it_gets_the_url_of_a_named_route_as_usual() + #[Test] + public function it_gets_the_url_of_a_named_route_as_usual(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -45,8 +46,8 @@ public function it_gets_the_url_of_a_named_route_as_usual() $this->assertEquals(URL::to('nl/route/name'), URL::route('nl.route.name')); } - /** @test */ - public function it_gets_the_url_of_a_route_in_the_current_locale_if_the_given_route_name_does_not_exist() + #[Test] + public function it_gets_the_url_of_a_route_in_the_current_locale_if_the_given_route_name_does_not_exist(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -56,8 +57,8 @@ public function it_gets_the_url_of_a_route_in_the_current_locale_if_the_given_ro $this->assertEquals(URL::to('en/route'), URL::route('route.name')); } - /** @test */ - public function it_throws_if_no_valid_route_can_be_found() + #[Test] + public function it_throws_if_no_valid_route_can_be_found(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -69,8 +70,8 @@ public function it_throws_if_no_valid_route_can_be_found() URL::route('route'); } - /** @test */ - public function it_throws_if_no_valid_localized_route_can_be_found() + #[Test] + public function it_throws_if_no_valid_localized_route_can_be_found(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -82,8 +83,8 @@ public function it_throws_if_no_valid_localized_route_can_be_found() URL::route('route.name'); } - /** @test */ - public function it_gets_the_url_of_a_route_in_the_given_locale() + #[Test] + public function it_gets_the_url_of_a_route_in_the_given_locale(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -96,8 +97,8 @@ public function it_gets_the_url_of_a_route_in_the_given_locale() $this->assertEquals(URL::to('nl/route'), URL::route('nl.route.name', [], true, 'nl')); } - /** @test */ - public function it_gets_the_url_of_a_route_in_the_given_locale_when_using_custom_domains() + #[Test] + public function it_gets_the_url_of_a_route_in_the_given_locale_when_using_custom_domains(): void { $this->setSupportedLocales([ 'en' => 'en.domain.test', @@ -113,8 +114,8 @@ public function it_gets_the_url_of_a_route_in_the_given_locale_when_using_custom $this->assertEquals('http://nl.domain.test/route', URL::route('nl.route.name', [], true, 'nl')); } - /** @test */ - public function it_gets_the_url_of_a_route_in_the_given_locale_when_using_custom_slugs() + #[Test] + public function it_gets_the_url_of_a_route_in_the_given_locale_when_using_custom_slugs(): void { $this->setSupportedLocales([ 'en' => 'english', @@ -129,8 +130,8 @@ public function it_gets_the_url_of_a_route_in_the_given_locale_when_using_custom $this->assertEquals(URL::to('dutch/route'), URL::route('nl.route.name', [], true, 'nl')); } - /** @test */ - public function it_always_gets_the_url_of_a_localized_route_if_a_locale_is_specified() + #[Test] + public function it_always_gets_the_url_of_a_localized_route_if_a_locale_is_specified(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -141,8 +142,8 @@ public function it_always_gets_the_url_of_a_localized_route_if_a_locale_is_speci $this->assertEquals(URL::to('nl/route'), URL::route('route.name', [], true, 'nl')); } - /** @test */ - public function it_returns_a_registered_non_localized_url_if_a_localized_version_does_not_exist() + #[Test] + public function it_returns_a_registered_non_localized_url_if_a_localized_version_does_not_exist(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -153,8 +154,8 @@ public function it_returns_a_registered_non_localized_url_if_a_localized_version $this->assertEquals(URL::to('route'), URL::route('route.name', [], true, 'en')); } - /** @test */ - public function it_throws_if_no_valid_route_can_be_found_for_the_given_locale() + #[Test] + public function it_throws_if_no_valid_route_can_be_found_for_the_given_locale(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -166,8 +167,8 @@ public function it_throws_if_no_valid_route_can_be_found_for_the_given_locale() URL::route('en.route.name', [], true, 'nl'); } - /** @test */ - function it_uses_a_fallback_locale_when_the_requested_locale_is_unsupported() + #[Test] + function it_uses_a_fallback_locale_when_the_requested_locale_is_unsupported(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -181,8 +182,8 @@ function it_uses_a_fallback_locale_when_the_requested_locale_is_unsupported() $this->assertEquals(URL::to('en/route'), URL::route('route', [], true, 'fr')); } - /** @test */ - public function it_uses_a_fallback_locale_when_the_requested_locale_is_not_registered() + #[Test] + public function it_uses_a_fallback_locale_when_the_requested_locale_is_not_registered(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -194,8 +195,8 @@ public function it_uses_a_fallback_locale_when_the_requested_locale_is_not_regis $this->assertEquals(URL::to('en/route'), URL::route('route', [], true, 'nl')); } - /** @test */ - public function it_throws_if_you_do_not_specify_a_name_for_a_localized_route() + #[Test] + public function it_throws_if_you_do_not_specify_a_name_for_a_localized_route(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -207,8 +208,8 @@ public function it_throws_if_you_do_not_specify_a_name_for_a_localized_route() URL::route('en.', [], true, 'en'); } - /** @test */ - public function it_generates_a_url_for_a_route_with_a_default_localized_route_key() + #[Test] + public function it_generates_a_url_for_a_route_with_a_default_localized_route_key(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -230,8 +231,8 @@ public function it_generates_a_url_for_a_route_with_a_default_localized_route_ke $this->assertEquals(URL::to('nl/route/nl-slug'), URL::route('route.name', [$model], true, 'nl')); } - /** @test */ - public function it_generates_a_url_for_a_route_with_a_custom_localized_route_key() + #[Test] + public function it_generates_a_url_for_a_route_with_a_custom_localized_route_key(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -253,8 +254,8 @@ public function it_generates_a_url_for_a_route_with_a_custom_localized_route_key $this->assertEquals(URL::to('nl/route/nl-slug'), URL::route('route.name', [$model], true, 'nl')); } - /** @test */ - public function it_generates_a_signed_route_url_for_the_current_locale() + #[Test] + public function it_generates_a_signed_route_url_for_the_current_locale(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -275,8 +276,8 @@ public function it_generates_a_signed_route_url_for_the_current_locale() $this->get($tamperedUrl)->assertSee('Invalid Signature'); } - /** @test */ - public function it_generates_a_signed_route_url_for_a_specific_locale() + #[Test] + public function it_generates_a_signed_route_url_for_a_specific_locale(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -297,8 +298,8 @@ public function it_generates_a_signed_route_url_for_a_specific_locale() $this->get($tamperedUrl)->assertSee('Invalid Signature'); } - /** @test */ - public function it_generates_a_temporary_signed_route_url_for_the_current_locale() + #[Test] + public function it_generates_a_temporary_signed_route_url_for_the_current_locale(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -318,8 +319,8 @@ public function it_generates_a_temporary_signed_route_url_for_the_current_locale $this->get($expiredUrl)->assertSee('Expired Signature'); } - /** @test */ - public function it_generates_a_temporary_signed_route_url_for_a_specific_locale() + #[Test] + public function it_generates_a_temporary_signed_route_url_for_a_specific_locale(): void { $this->setSupportedLocales(['en', 'nl']); $this->setAppLocale('en'); @@ -340,16 +341,16 @@ public function it_generates_a_temporary_signed_route_url_for_a_specific_locale( $this->get($expiredUrl)->assertSee('Expired Signature'); } - /** @test */ - public function it_throws_a_route_not_found_exception_for_missing_route_names_when_generating_a_route_url() + #[Test] + public function it_throws_a_route_not_found_exception_for_missing_route_names_when_generating_a_route_url(): void { $this->expectException(RouteNotFoundException::class); URL::route('missing.route'); } - /** @test */ - public function the_app_locale_is_correctly_restored_when_catching_a_route_not_found_exception_when_generating_a_route_url() + #[Test] + public function the_app_locale_is_correctly_restored_when_catching_a_route_not_found_exception_when_generating_a_route_url(): void { $this->setAppLocale('en'); @@ -360,16 +361,16 @@ public function the_app_locale_is_correctly_restored_when_catching_a_route_not_f $this->assertEquals('en', App::getLocale()); } - /** @test */ - public function it_throws_a_route_not_found_exception_for_missing_route_names_when_generating_a_signed_route_url() + #[Test] + public function it_throws_a_route_not_found_exception_for_missing_route_names_when_generating_a_signed_route_url(): void { $this->expectException(RouteNotFoundException::class); URL::signedRoute('missing.route'); } - /** @test */ - public function the_app_locale_is_correctly_restored_when_catching_a_route_not_found_exception_when_generating_a_signed_route_url() + #[Test] + public function the_app_locale_is_correctly_restored_when_catching_a_route_not_found_exception_when_generating_a_signed_route_url(): void { $this->setAppLocale('en'); @@ -380,16 +381,16 @@ public function the_app_locale_is_correctly_restored_when_catching_a_route_not_f $this->assertEquals('en', App::getLocale()); } - /** @test */ - public function it_throws_a_route_not_found_exception_for_missing_route_names_when_generating_a_temporary_signed_route_url() + #[Test] + public function it_throws_a_route_not_found_exception_for_missing_route_names_when_generating_a_temporary_signed_route_url(): void { $this->expectException(RouteNotFoundException::class); URL::temporarySignedRoute('missing.route', now()->addMinutes(30)); } - /** @test */ - public function the_app_locale_is_correctly_restored_when_catching_a_route_not_found_exception_when_generating_a_temporary_signed_route_url() + #[Test] + public function the_app_locale_is_correctly_restored_when_catching_a_route_not_found_exception_when_generating_a_temporary_signed_route_url(): void { $this->setAppLocale('en'); @@ -400,8 +401,8 @@ public function the_app_locale_is_correctly_restored_when_catching_a_route_not_f $this->assertEquals('en', App::getLocale()); } - /** @test */ - public function it_allows_routes_to_be_cached() + #[Test] + public function it_allows_routes_to_be_cached(): void { $this->withoutExceptionHandling(); $this->setSupportedLocales(['en']); diff --git a/tests/Unit/LocaleConfigTest.php b/tests/Unit/LocaleConfigTest.php index 0c82693..aec6024 100644 --- a/tests/Unit/LocaleConfigTest.php +++ b/tests/Unit/LocaleConfigTest.php @@ -2,13 +2,14 @@ namespace CodeZero\LocalizedRoutes\Tests\Unit; +use PHPUnit\Framework\Attributes\Test; use CodeZero\LocalizedRoutes\LocaleConfig; use CodeZero\LocalizedRoutes\Tests\TestCase; -class LocaleConfigTest extends TestCase +final class LocaleConfigTest extends TestCase { - /** @test */ - public function it_gets_the_supported_locales() + #[Test] + public function it_gets_the_supported_locales(): void { $config = new LocaleConfig(['supported_locales' => null]); $this->assertEquals([], $config->getSupportedLocales()); @@ -23,16 +24,16 @@ public function it_gets_the_supported_locales() $this->assertEquals(['en' => 'english.test', 'nl' => 'dutch.test'], $config->getSupportedLocales()); } - /** @test */ - public function it_sets_the_supported_locales() + #[Test] + public function it_sets_the_supported_locales(): void { $config = new LocaleConfig(['supported_locales' => null]); $config->setSupportedLocales(['en' => 'english', 'nl' => 'dutch']); $this->assertEquals(['en' => 'english', 'nl' => 'dutch'], $config->getSupportedLocales()); } - /** @test */ - public function it_gets_the_omitted_locale() + #[Test] + public function it_gets_the_omitted_locale(): void { $config = new LocaleConfig(['omitted_locale' => null]); $this->assertEquals(null, $config->getOmittedLocale()); @@ -41,16 +42,16 @@ public function it_gets_the_omitted_locale() $this->assertEquals('en', $config->getOmittedLocale()); } - /** @test */ - public function it_sets_the_omitted_locale() + #[Test] + public function it_sets_the_omitted_locale(): void { $config = new LocaleConfig(['omitted_locale' => null]); $config->setOmittedLocale('en'); $this->assertEquals('en', $config->getOmittedLocale()); } - /** @test */ - public function it_gets_the_fallback_locale() + #[Test] + public function it_gets_the_fallback_locale(): void { $config = new LocaleConfig(['fallback_locale' => null]); $this->assertEquals(null, $config->getFallbackLocale()); @@ -59,16 +60,16 @@ public function it_gets_the_fallback_locale() $this->assertEquals('en', $config->getFallbackLocale()); } - /** @test */ - public function it_sets_the_fallback_locale() + #[Test] + public function it_sets_the_fallback_locale(): void { $config = new LocaleConfig(['fallback_locale' => null]); $config->setFallbackLocale('en'); $this->assertEquals('en', $config->getFallbackLocale()); } - /** @test */ - public function it_gets_the_route_action() + #[Test] + public function it_gets_the_route_action(): void { $config = new LocaleConfig(['route_action' => null]); $this->assertEquals(null, $config->getRouteAction()); @@ -77,16 +78,16 @@ public function it_gets_the_route_action() $this->assertEquals('locale', $config->getRouteAction()); } - /** @test */ - public function it_sets_the_route_action() + #[Test] + public function it_sets_the_route_action(): void { $config = new LocaleConfig(['route_action' => null]); $config->setRouteAction('locale'); $this->assertEquals('locale', $config->getRouteAction()); } - /** @test */ - public function it_gets_the_locales() + #[Test] + public function it_gets_the_locales(): void { $config = new LocaleConfig(['supported_locales' => null]); $this->assertEquals([], $config->getLocales()); @@ -101,8 +102,8 @@ public function it_gets_the_locales() $this->assertEquals(['en', 'nl'], $config->getLocales()); } - /** @test */ - public function it_finds_a_slug_by_its_locale() + #[Test] + public function it_finds_a_slug_by_its_locale(): void { $config = new LocaleConfig(['supported_locales' => null]); $this->assertEquals(null, $config->findSlugByLocale('en')); @@ -120,8 +121,8 @@ public function it_finds_a_slug_by_its_locale() $this->assertEquals(null, $config->findSlugByLocale('en')); } - /** @test */ - public function it_finds_a_domain_by_its_locale() + #[Test] + public function it_finds_a_domain_by_its_locale(): void { $config = new LocaleConfig(['supported_locales' => null]); $this->assertEquals(null, $config->findDomainByLocale('en')); @@ -137,8 +138,8 @@ public function it_finds_a_domain_by_its_locale() $this->assertEquals('english.test', $config->findDomainByLocale('en')); } - /** @test */ - public function it_finds_a_locale_by_its_slug() + #[Test] + public function it_finds_a_locale_by_its_slug(): void { $config = new LocaleConfig(['supported_locales' => null]); $this->assertEquals(null, $config->findLocaleBySlug('en')); @@ -156,8 +157,8 @@ public function it_finds_a_locale_by_its_slug() $this->assertEquals(null, $config->findLocaleBySlug('english.test')); } - /** @test */ - public function it_finds_a_locale_by_its_domain() + #[Test] + public function it_finds_a_locale_by_its_domain(): void { $config = new LocaleConfig(['supported_locales' => null]); $this->assertEquals(null, $config->findLocaleByDomain('english.test')); @@ -175,8 +176,8 @@ public function it_finds_a_locale_by_its_domain() $this->assertEquals('en', $config->findLocaleByDomain('english.test')); } - /** @test */ - public function it_checks_if_there_are_any_locales_configured() + #[Test] + public function it_checks_if_there_are_any_locales_configured(): void { $config = new LocaleConfig(['supported_locales' => null]); $this->assertFalse($config->hasLocales()); @@ -191,8 +192,8 @@ public function it_checks_if_there_are_any_locales_configured() $this->assertTrue($config->hasLocales()); } - /** @test */ - public function it_checks_if_custom_slugs_are_configured() + #[Test] + public function it_checks_if_custom_slugs_are_configured(): void { $config = new LocaleConfig(['supported_locales' => null]); $this->assertFalse($config->hasCustomSlugs()); @@ -207,8 +208,8 @@ public function it_checks_if_custom_slugs_are_configured() $this->assertFalse($config->hasCustomSlugs()); } - /** @test */ - public function it_checks_if_custom_domains_are_configured() + #[Test] + public function it_checks_if_custom_domains_are_configured(): void { $config = new LocaleConfig(['supported_locales' => null]); $this->assertFalse($config->hasCustomDomains()); @@ -223,8 +224,8 @@ public function it_checks_if_custom_domains_are_configured() $this->assertTrue($config->hasCustomDomains()); } - /** @test */ - public function it_checks_if_a_locale_is_supported() + #[Test] + public function it_checks_if_a_locale_is_supported(): void { $config = new LocaleConfig(['supported_locales' => null]); $this->assertFalse($config->isSupportedLocale(null)); diff --git a/tests/Unit/Middleware/LocaleHandlerTest.php b/tests/Unit/Middleware/LocaleHandlerTest.php index abcc91b..69b7bdc 100644 --- a/tests/Unit/Middleware/LocaleHandlerTest.php +++ b/tests/Unit/Middleware/LocaleHandlerTest.php @@ -2,6 +2,7 @@ namespace CodeZero\LocalizedRoutes\Tests\Unit\Middleware; +use PHPUnit\Framework\Attributes\Test; use CodeZero\LocalizedRoutes\Middleware\Detectors\Detector; use CodeZero\LocalizedRoutes\Middleware\LocaleHandler; use CodeZero\LocalizedRoutes\Middleware\Stores\Store; @@ -9,10 +10,10 @@ use Illuminate\Support\Facades\App; use Mockery; -class LocaleHandlerTest extends TestCase +final class LocaleHandlerTest extends TestCase { - /** @test */ - public function it_loops_through_the_detectors_and_returns_the_first_supported_locale() + #[Test] + public function it_loops_through_the_detectors_and_returns_the_first_supported_locale(): void { $supportedLocales = ['en', 'nl']; $detectors = [ @@ -26,8 +27,8 @@ public function it_loops_through_the_detectors_and_returns_the_first_supported_l $this->assertEquals('nl', $localeHandler->detect()); } - /** @test */ - public function it_returns_the_first_match_if_an_array_of_locales_is_detected() + #[Test] + public function it_returns_the_first_match_if_an_array_of_locales_is_detected(): void { $supportedLocales = ['en', 'nl']; $detectors = [ @@ -39,8 +40,8 @@ public function it_returns_the_first_match_if_an_array_of_locales_is_detected() $this->assertEquals('nl', $localeHandler->detect()); } - /** @test */ - public function trusted_detectors_ignore_supported_locales_and_may_set_any_locale() + #[Test] + public function trusted_detectors_ignore_supported_locales_and_may_set_any_locale(): void { $supportedLocales = ['en']; $detectors = [ @@ -55,8 +56,8 @@ public function trusted_detectors_ignore_supported_locales_and_may_set_any_local $this->assertEquals('nl', $localeHandler->detect()); } - /** @test */ - public function it_skips_null_and_false_and_empty_values() + #[Test] + public function it_skips_null_and_false_and_empty_values(): void { App::instance(Detector::class, Mockery::mock(Detector::class)->allows()->detect()->andReturns('')->getMock()); @@ -75,8 +76,8 @@ public function it_skips_null_and_false_and_empty_values() $this->assertEquals('nl', $localeHandler->detect()); } - /** @test */ - public function it_skips_null_and_false_and_empty_values_from_trusted_detectors() + #[Test] + public function it_skips_null_and_false_and_empty_values_from_trusted_detectors(): void { App::instance(Detector::class, Mockery::mock(Detector::class)->allows()->detect()->andReturns('')->getMock()); @@ -98,8 +99,8 @@ public function it_skips_null_and_false_and_empty_values_from_trusted_detectors( $this->assertEquals('nl', $localeHandler->detect()); } - /** @test */ - public function it_returns_false_if_no_supported_locale_could_be_detected() + #[Test] + public function it_returns_false_if_no_supported_locale_could_be_detected(): void { $supportedLocales = ['en']; $detectors = [ @@ -113,8 +114,8 @@ public function it_returns_false_if_no_supported_locale_could_be_detected() $this->assertNull($localeHandler->detect()); } - /** @test */ - public function it_loops_through_the_stores_and_calls_the_store_method_with_the_given_locale() + #[Test] + public function it_loops_through_the_stores_and_calls_the_store_method_with_the_given_locale(): void { $stores = [ Mockery::mock(Store::class)->expects()->store('nl')->once()->getMock(), @@ -127,8 +128,8 @@ public function it_loops_through_the_stores_and_calls_the_store_method_with_the_ $localeHandler->store('nl'); } - /** @test */ - public function it_accepts_class_names_instead_of_instances_in_the_constructor() + #[Test] + public function it_accepts_class_names_instead_of_instances_in_the_constructor(): void { App::instance(Store::class, Mockery::mock(Store::class)->expects()->store('nl')->once()->getMock()); App::instance(Detector::class, Mockery::mock(Detector::class)->expects()->detect()->once()->getMock()); From 28a5517cc425a9b4741889f3f051b5419309f78f Mon Sep 17 00:00:00 2001 From: Ivan Vermeyen Date: Wed, 13 Mar 2024 13:53:23 +0100 Subject: [PATCH 13/14] Fix middleware namespace in README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c304a1d..6156c2a 100644 --- a/README.md +++ b/README.md @@ -170,7 +170,7 @@ Add the middleware to the `web` middleware group in `bootstrap/app.php`. \Illuminate\Routing\Middleware\SubstituteBindings::class, ]); $middleware->web(append: [ - \CodeZero\Localizer\Middleware\SetLocale::class, + \CodeZero\LocalizedRoutes\Middleware\SetLocale::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, ]); }) @@ -187,7 +187,7 @@ protected $middlewareGroups = [ //... \Illuminate\Session\Middleware\StartSession::class, // <= after this //... - \CodeZero\Localizer\Middleware\SetLocale::class, + \CodeZero\LocalizedRoutes\Middleware\SetLocale::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this ], ]; From 3c8c8bdac753b6b75b4c6f3f7f7b39b962d507cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Garc=C3=A9s=20Rodr=C3=ADguez?= Date: Sun, 17 Mar 2024 22:41:22 +0000 Subject: [PATCH 14/14] Add Vary Accept-Language header (#108) --- src/Controllers/FallbackController.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Controllers/FallbackController.php b/src/Controllers/FallbackController.php index 0f44583..acd1732 100644 --- a/src/Controllers/FallbackController.php +++ b/src/Controllers/FallbackController.php @@ -41,7 +41,8 @@ protected function redirectResponse() } return Redirect::to($localizedUrl, $this->getRedirectStatusCode()) - ->header('Cache-Control', 'no-store, no-cache, must-revalidate'); + ->header('Cache-Control', 'no-store, no-cache, must-revalidate') + ->header('Vary', 'Accept-Language'); } /**