Skip to content

Commit

Permalink
Merge pull request #15 from Laravel-Lang/1.x
Browse files Browse the repository at this point in the history
Added `localizedGroup` macro for `Route` facade
  • Loading branch information
andrey-helldar authored Jun 17, 2024
2 parents 5496696 + 31b3fd8 commit 9d6219d
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 14 deletions.
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@
"preferred-install": "dist",
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"LaravelLang\\Routes\\ServiceProvider"
]
}
},
"scripts": {
"test": "vendor/bin/pest --parallel"
}
Expand Down
18 changes: 18 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Routes;

use Closure;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use LaravelLang\Routes\Facades\LocalizationRoute;

class ServiceProvider extends BaseServiceProvider
{
public function boot(): void
{
Route::macro('localizedGroup', fn (Closure $callback) => LocalizationRoute::group($callback));
}
}
11 changes: 9 additions & 2 deletions tests/Concerns/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}

Expand Down
23 changes: 18 additions & 5 deletions tests/Feature/Facades/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
58 changes: 58 additions & 0 deletions tests/Feature/Middlewares/ParameterPrefixForFacadeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

use LaravelLang\Routes\Helpers\Route as RouteName;
use Tests\Constants\LocaleValue;

use function Pest\Laravel\getJson;

test('main without prefix', function () {
$foo = 'test';

getJson(route('via.group.facade', compact('foo')))
->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');
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
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);
});

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);

Expand All @@ -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);

Expand All @@ -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,
]);

Expand All @@ -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,
]);

Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,6 +21,7 @@ protected function getPackageProviders($app): array
return [
LocalesServiceProvider::class,
ConfigServiceProvider::class,
ServiceProvider::class,
];
}

Expand Down

0 comments on commit 9d6219d

Please sign in to comment.