Localization
Localization copied to clipboard
Cannot feature test multilanguage routes
- Localization Version: 2.1.0
- Laravel Version: 5.5.22
- PHP Version: 7.1.8-1ubuntu1
Description:
I cannot test multilanguage translated routes. Created routes work in the browser, but never in a testcase. Tried it using a empty Laravel-project, which doesn't work either. I'd like to start a PR, but I have no clue where to start at this point in time.
Steps To Reproduce:
- Create a new, empty Laravel project:
composer create-project --prefer-dist laravel/laravel loctest - Require the localization package
composer require "arcanedev/localization:^2.1" - Publish the configuration file
php artisan vendor:publish --provider="Arcanedev\Localization\LocalizationServiceProvider" - Change
routes/web.phpto:Route::localizedGroup(function () { Route::view('/', 'welcome'); }); - Change the test method in
tests/Feature/ExampleTest.phpto:public function testBasicTest() { $response = $this->get('/en'); $response->assertStatus(200); } - Run the tests:
vendor/bin/phpunit.
Expected
No changes in the testcase
Happened
PHPUnit 6.4.4 by Sebastian Bergmann and contributors.
F. 2 / 2 (100%)
Time: 73 ms, Memory: 12.00MB
There was 1 failure:
1) Tests\Feature\ExampleTest::testBasicTest
Expected status code 200 but received 404.
Failed asserting that false is true.
/home/jakob/code/loctest/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:78
/home/jakob/code/loctest/tests/Feature/ExampleTest.php:19
Additional: I've noticed that
$this->get('/')
will result in a 302 redirect (as expected), but both
$this->followingsRedirects()->get('/')
$this->get('/en')
result in a 404.
This package needs a rewrite because the localized routes are registered dynamically based on different factors.
I'm aware about this issue and i need time to rewrite it to make it easy to test (with trait helpers). Also make the localized routes cachable.
These are some related issues:
- https://github.com/mcamara/laravel-localization/issues/435
- https://github.com/mcamara/laravel-localization/issues/308
- https://github.com/mcamara/laravel-localization/issues/289
- https://github.com/mcamara/laravel-localization/issues/161
@arcanedev-maroc are there any updates on this?
I just bumped into this also. Any news regarding this issue?
Tks
Ok, i used this approach and it works :)
https://github.com/mcamara/laravel-localization/issues/161#issuecomment-381367191
Came around the same.
Did anyone solve it?
Expected status code 200 but received 302. Failed asserting that false is true.
//web.php
if (app()->environment('testing')) {
require '_web.php';
} else {
Route::localizedGroup(function () {
require '_web.php';
});
}
Ok, i used this approach and it works :)
This sould be the accepted answer. Following that answer, this is how I resolved.
First modifiy config/localization.php and change hide-default-in-urlto:
'hide-default-in-url' => env('LOCALIZATION_HIDE_DEFAULT_LOCALE', false),
If you don't use English as your default language you need to override locale option on config/app.php. For example if you use spanish as your default locale change to:
'locale' => env('DEFAULT_LOCALE', 'es'),
Then add to your phpunit.xml this valus inside the <php>:
<env name="DEFAULT_LOCALE" value="en"/>
<env name="LOCALIZATION_HIDE_DEFAULT_LOCALE" value="true"/>
Now remember to use always english for your routes in tests.
Your tests should work 😄