How to test the page where the browser receives the cookie?

Good day, there is a simple logic of localization on the website:
The controller rout on which the user clicks to set the language:
Route::get('lang/{lang}', ['uses'=>'LanguageController@setLang'])->name('lang.set');

public function setLang($lang)
{
 if (array_key_exists($lang, config('languages'))) {
 setcookie ('applocale', $lang, 0, '/', NULL, 0 );
}
 return redirect()->back();
 }


Also there is a global middleware, which hangs on any request and the cookie determines the display language:
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);
 }


I'm trying to write a test to verify the installation of this cookie:
/**
 * 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');
 }


But as a result, phpunit gives an error:
Cookie [applocale] not present on the 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: EN
Locale after: EN


How to test it?
March 12th 20 at 07:59
2 answers
March 12th 20 at 08:01
Solution
cookies are not in console, so test them makes no sense.
You mean that this functionality should be tested using Selenium/Dusk (for example), or that I'm doing something wrong? - Louie_Botsford commented on March 12th 20 at 08:04
March 12th 20 at 08:03
Solution
cookies - only a browser installed
And why then do we need a method assertCookie(), if it is not possible to use it? - Louie_Botsford commented on March 12th 20 at 08:06
@Louie_Botsford, it receives information from the headers of the client (browser). And in the console - they are not. - alva_Gorcza commented on March 12th 20 at 08:09
@alva_Gorcza, i.e. in fact to use this method only with libraries for testing in the browser? Not quite catch up then it's in the standard package, if out of the box it is useless. - Louie_Botsford commented on March 12th 20 at 08:12
@Louie_Botsford, although in the Dusk is his method assertHasCookie(). Something I'm very blunt, i.e. this method is generally not suitable for use? Why did he then have? - Louie_Botsford commented on March 12th 20 at 08:15
@Louie_Botsford,
it receives information from the headers of the client (browser). And in the console - they are not.
- alva_Gorcza commented on March 12th 20 at 08:18
@alva_Gorcza, I understand, and then to call it not from under the console, and from under the browser? - Louie_Botsford commented on March 12th 20 at 08:21
@Louie_Botsford, a page request (via GET): normal transition. - alva_Gorcza commented on March 12th 20 at 08:24
@alva_Gorcza, and in the context of automated testing? It was initially about him going. - Louie_Botsford commented on March 12th 20 at 08:27
@Louie_Botsford, need to pass cookies through curl directly from PHP. - alva_Gorcza commented on March 12th 20 at 08:30
@Louie_Botsford, or use parsing using the headless browser, like nightmare and the like... - alva_Gorcza commented on March 12th 20 at 08:33

Find more questions by tags LaravelPHPUnit