DavidBuchukuri's avatar

How to test localization with phpunit

I have implemented localization in my laravel app with sessions and a global middleware. It works like this, when user clicks on for example 'french' button. I catch that request and put french in my session. Then global middleware is ran for every request and it sets the current language in a session as a locale. I tried to test it like this:

$response = $this->get('/language/ka')->assertStatus(302)->assertSessionHas('lang', 'ka');

Those 2 assertions pass, however when i add 1 more assertion on the next line $this->assertTrue(app()->getLocale() === 'ka');. it fails. It seems like middleware isn't working inside tests. here is my middleware:

public function handle(Request $request, Closure $next){
		$lang = session('lang', 'en');
		if (isset($lang) && in_array($lang, config('app.available_locales')))
		{
			app()->setLocale($lang);
		}
		else
		{
			app()->setLocale('en');
		}
		return $next($request);
	}
0 likes
0 replies

Please or to participate in this conversation.