From 31b3fd8c03a0389a012f96a840d8f4d325ffa326 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Mon, 17 Jun 2024 11:10:21 +0300 Subject: [PATCH] Added `localizedGroup` macro for `Route` facade --- composer.json | 7 +++ src/ServiceProvider.php | 18 ++++++ tests/Concerns/Routes.php | 11 +++- tests/Feature/Facades/RouteTest.php | 23 ++++++-- .../ParameterPrefixForFacadeTest.php | 58 +++++++++++++++++++ ...st.php => ParameterPrefixForMacroTest.php} | 14 ++--- tests/TestCase.php | 2 + 7 files changed, 119 insertions(+), 14 deletions(-) create mode 100644 src/ServiceProvider.php create mode 100644 tests/Feature/Middlewares/ParameterPrefixForFacadeTest.php rename tests/Feature/Middlewares/{ParameterPrefixTest.php => ParameterPrefixForMacroTest.php} (68%) diff --git a/composer.json b/composer.json index 5e9ed45..1759a76 100644 --- a/composer.json +++ b/composer.json @@ -66,6 +66,13 @@ "preferred-install": "dist", "sort-packages": true }, + "extra": { + "laravel": { + "providers": [ + "LaravelLang\\Routes\\ServiceProvider" + ] + } + }, "scripts": { "test": "vendor/bin/pest --parallel" } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php new file mode 100644 index 0000000..76feab7 --- /dev/null +++ b/src/ServiceProvider.php @@ -0,0 +1,18 @@ + LocalizationRoute::group($callback)); + } +} diff --git a/tests/Concerns/Routes.php b/tests/Concerns/Routes.php index 92ab183..d553cd2 100644 --- a/tests/Concerns/Routes.php +++ b/tests/Concerns/Routes.php @@ -65,11 +65,18 @@ public function setUpRoutes(): void ->name('clean'); }); + app('router')->localizedGroup(function () { + app('router') + ->middleware('web') + ->get('group/macro/{foo}', $this->jsonResponse()) + ->name('via.group.macro'); + }); + LocalizationRoute::group(function () { app('router') ->middleware('web') - ->get('group/{foo}', $this->jsonResponse()) - ->name('via.group'); + ->get('group/facade/{foo}', $this->jsonResponse()) + ->name('via.group.facade'); }); } diff --git a/tests/Feature/Facades/RouteTest.php b/tests/Feature/Facades/RouteTest.php index c1c13f9..310c083 100644 --- a/tests/Feature/Facades/RouteTest.php +++ b/tests/Feature/Facades/RouteTest.php @@ -5,15 +5,28 @@ use LaravelLang\Routes\Helpers\Route as RouteName; use Tests\Constants\LocaleValue; -test('group', function () { +test('group: facade', function () { $foo = 'test'; - expect(route('via.group', compact('foo'))) + expect(route('via.group.facade', compact('foo'))) ->toBeString() - ->toBe('http://localhost/group/test'); + ->toBe('http://localhost/group/facade/test'); - expect(route(RouteName::prefix() . 'via.group', [ + expect(route(RouteName::prefix() . 'via.group.facade', [ 'locale' => LocaleValue::LocaleMain, 'foo' => $foo, - ]))->toBeString()->toBe('http://localhost/fr/group/test'); + ]))->toBeString()->toBe('http://localhost/fr/group/facade/test'); +}); + +test('group: macro', function () { + $foo = 'test'; + + expect(route('via.group.macro', compact('foo'))) + ->toBeString() + ->toBe('http://localhost/group/macro/test'); + + expect(route(RouteName::prefix() . 'via.group.macro', [ + 'locale' => LocaleValue::LocaleMain, + 'foo' => $foo, + ]))->toBeString()->toBe('http://localhost/fr/group/macro/test'); }); diff --git a/tests/Feature/Middlewares/ParameterPrefixForFacadeTest.php b/tests/Feature/Middlewares/ParameterPrefixForFacadeTest.php new file mode 100644 index 0000000..908c5e0 --- /dev/null +++ b/tests/Feature/Middlewares/ParameterPrefixForFacadeTest.php @@ -0,0 +1,58 @@ +assertSuccessful() + ->assertJsonPath($foo, LocaleValue::TranslationFrench); +}); + +test('main locale', function (string $locale) { + $foo = 'test'; + + getJson(route(RouteName::prefix() . 'via.group.facade', compact('foo', 'locale'))) + ->assertSuccessful() + ->assertJsonPath($foo, LocaleValue::TranslationFrench); + + assertEventDispatched(); +})->with('main-locales'); + +test('aliased locale', function (string $locale) { + $foo = 'test'; + + getJson(route(RouteName::prefix() . 'via.group.facade', compact('foo', 'locale'))) + ->assertSuccessful() + ->assertJsonPath($foo, LocaleValue::TranslationGerman); + + assertEventDispatched(); +})->with('aliased-locales'); + +test('uninstalled locale', function (string $locale) { + $foo = 'test'; + + getJson(route(RouteName::prefix() . 'via.group.facade', compact('foo', 'locale'))) + ->assertRedirectToRoute('via.group.facade', [ + 'foo' => $foo, + ]); + + assertEventNotDispatched(); +})->with('uninstalled-locales'); + +test('unknown locale', function (int|string $locale) { + $foo = 'test'; + + getJson(route(RouteName::prefix() . 'via.group.facade', compact('foo', 'locale'))) + ->assertRedirectToRoute('via.group.facade', [ + 'foo' => $foo, + ]); + + assertEventNotDispatched(); +})->with('unknown-locales'); diff --git a/tests/Feature/Middlewares/ParameterPrefixTest.php b/tests/Feature/Middlewares/ParameterPrefixForMacroTest.php similarity index 68% rename from tests/Feature/Middlewares/ParameterPrefixTest.php rename to tests/Feature/Middlewares/ParameterPrefixForMacroTest.php index 25000ec..93890e4 100644 --- a/tests/Feature/Middlewares/ParameterPrefixTest.php +++ b/tests/Feature/Middlewares/ParameterPrefixForMacroTest.php @@ -10,7 +10,7 @@ test('main without prefix', function () { $foo = 'test'; - getJson(route('via.group', compact('foo'))) + getJson(route('via.group.macro', compact('foo'))) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationFrench); }); @@ -18,7 +18,7 @@ test('main locale', function (string $locale) { $foo = 'test'; - getJson(route(RouteName::prefix() . 'via.group', compact('foo', 'locale'))) + getJson(route(RouteName::prefix() . 'via.group.macro', compact('foo', 'locale'))) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationFrench); @@ -28,7 +28,7 @@ test('aliased locale', function (string $locale) { $foo = 'test'; - getJson(route(RouteName::prefix() . 'via.group', compact('foo', 'locale'))) + getJson(route(RouteName::prefix() . 'via.group.macro', compact('foo', 'locale'))) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationGerman); @@ -38,8 +38,8 @@ test('uninstalled locale', function (string $locale) { $foo = 'test'; - getJson(route(RouteName::prefix() . 'via.group', compact('foo', 'locale'))) - ->assertRedirectToRoute('via.group', [ + getJson(route(RouteName::prefix() . 'via.group.macro', compact('foo', 'locale'))) + ->assertRedirectToRoute('via.group.macro', [ 'foo' => $foo, ]); @@ -49,8 +49,8 @@ test('unknown locale', function (int|string $locale) { $foo = 'test'; - getJson(route(RouteName::prefix() . 'via.group', compact('foo', 'locale'))) - ->assertRedirectToRoute('via.group', [ + getJson(route(RouteName::prefix() . 'via.group.macro', compact('foo', 'locale'))) + ->assertRedirectToRoute('via.group.macro', [ 'foo' => $foo, ]); diff --git a/tests/TestCase.php b/tests/TestCase.php index fdfca10..1a407f4 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,6 +5,7 @@ use LaravelLang\Config\Enums\Name; use LaravelLang\Config\ServiceProvider as ConfigServiceProvider; use LaravelLang\Locales\ServiceProvider as LocalesServiceProvider; +use LaravelLang\Routes\ServiceProvider; use Orchestra\Testbench\TestCase as BaseTestCase; use Tests\Concerns\Locales; use Tests\Concerns\Routes; @@ -20,6 +21,7 @@ protected function getPackageProviders($app): array return [ LocalesServiceProvider::class, ConfigServiceProvider::class, + ServiceProvider::class, ]; }