Answer the question
In order to leave comments, you need to log in
How to test the page where the browser receives cookies?
Good afternoon, there is a simple localization logic on the site:
A controller with a route, to which the user goes to set the language:
Route::get('lang/{lang}', ['uses'=>'[email protected]'])->name('lang.set');
public function setLang($lang)
{
if (array_key_exists($lang, config('languages'))) {
setcookie ('applocale', $lang, 0, '/', NULL, 0 );
}
return redirect()->back();
}
public function handle($request, Closure $next)
{
$requestedLocale = $request->cookie('applocale');
$applocale = config('app.locale'); // default locale
if( $requestedLocale !== null && array_key_exists($requestedLocale, config('languages'))) {
$applocale = $requestedLocale;
}
app()->setLocale($applocale);
session()->put('applocale', $applocale);
Carbon::setLocale($applocale);
return $next($request);
}
/**
* Language can be changed.
*
* @return void
*
* @runInSeparateProcess
*/
public function testChangeLanguage()
{
print 'Locale before: ' . app()->getLocale() . "\n";
$this->get('/lang/en')->assertStatus(302);
print 'Locale after: ' . app()->getLocale();
$this->get('/')->assertCookie('applocale');
}
Cookie [applocale] not present on response.
Failed asserting that null is not null.
C:\...\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestResponse.php:154
C:\...\tests\Feature\LanguageTest.php:24
Locale before: ru
Locale after: ru
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question