Skip to content

Commit

Permalink
Processing of sessions and cookies is prepared
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Jun 9, 2024
1 parent b0cd5d6 commit 846c27a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 14 deletions.
42 changes: 28 additions & 14 deletions tests/Concerns/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,37 @@ trait Routes
public function setUpRoutes(): void
{
app('router')
->middleware(ParameterLocale::class)
->get('path/{locale?}', $this->jsonResponse())
->name('via.parameter');
->middleware('web')
->group(function () {
app('router')
->middleware(ParameterLocale::class)
->get('path/{locale?}', $this->jsonResponse())
->name('via.parameter');

app('router')
->middleware(ParameterRedirectLocale::class)
->get('redirect/{locale?}', $this->jsonResponse())
->name('via.parameter.redirect');
app('router')
->middleware(ParameterRedirectLocale::class)
->get('redirect/{locale?}', $this->jsonResponse())
->name('via.parameter.redirect');

app('router')
->middleware(ParameterRedirectLocale::class)
->get('not-named/redirect/{locale?}', $this->jsonResponse());
app('router')
->middleware(ParameterRedirectLocale::class)
->get('not-named/redirect/{locale?}', $this->jsonResponse());

app('router')
->middleware(HeaderLocale::class)
->get('header', $this->jsonResponse())
->name('via.header');
app('router')
->middleware(HeaderLocale::class)
->get('header', $this->jsonResponse())
->name('via.header');

app('router')
->middleware(CookiesLocale::class)
->get('cookie', $this->jsonResponse())
->name('via.cookie');

app('router')
->middleware(SessionLocale::class)
->get('session', $this->jsonResponse())
->name('via.session');
});
}

protected function jsonResponse(): Closure
Expand Down
29 changes: 29 additions & 0 deletions tests/Feature/CookieTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use LaravelLang\Config\Facades\Config;
use Tests\Constants\LocaleValue;

use function Pest\Laravel\withCookie;

test('main locale', function (string $locale) {
withCookie(Config::shared()->routes->names->cookie, $locale)
->getJson(route('via.cookie'))
->assertSuccessful()
->assertJsonPath('message', LocaleValue::TranslationFrench);
})->with('main-locales');

test('aliased locale', function (string $locale) {
withCookie(Config::shared()->routes->names->cookie, $locale)
->getJson(route('via.cookie'))
->assertSuccessful()
->assertJsonPath('message', LocaleValue::TranslationGerman);
})->with('aliased-locales');

test('empty locale', function (int|string|null $locale) {
withCookie(Config::shared()->routes->names->cookie, $locale)
->getJson(route('via.cookie'))
->assertSuccessful()
->assertJsonPath('message', LocaleValue::TranslationFrench);
})->with('empty-locales');
29 changes: 29 additions & 0 deletions tests/Feature/SessionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use LaravelLang\Config\Facades\Config;
use Tests\Constants\LocaleValue;

use function Pest\Laravel\withSession;

test('main locale', function (string $locale) {
withSession([Config::shared()->routes->names->session => $locale])
->getJson(route('via.session'))
->assertSuccessful()
->assertJsonPath('message', LocaleValue::TranslationFrench);
})->with('main-locales');

test('aliased locale', function (string $locale) {
withSession([Config::shared()->routes->names->session => $locale])
->getJson(route('via.session'))
->assertSuccessful()
->assertJsonPath('message', LocaleValue::TranslationGerman);
})->with('aliased-locales');

test('empty locale', function (int|string|null $locale) {
withSession([Config::shared()->routes->names->session => $locale])
->getJson(route('via.session'))
->assertSuccessful()
->assertJsonPath('message', LocaleValue::TranslationFrench);
})->with('empty-locales');

0 comments on commit 846c27a

Please sign in to comment.