Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot feature test multilanguage routes #113

Open
jakobbuis opened this issue Nov 27, 2017 · 9 comments
Open

Cannot feature test multilanguage routes #113

jakobbuis opened this issue Nov 27, 2017 · 9 comments
Assignees

Comments

@jakobbuis
Copy link

  • 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:

  1. Create a new, empty Laravel project:
    composer create-project --prefer-dist laravel/laravel loctest
  2. Require the localization package
    composer require "arcanedev/localization:^2.1"
  3. Publish the configuration file
    php artisan vendor:publish --provider="Arcanedev\Localization\LocalizationServiceProvider"
  4. Change routes/web.php to:
    Route::localizedGroup(function () {
        Route::view('/', 'welcome');
    });
  5. Change the test method in tests/Feature/ExampleTest.php to:
    public function testBasicTest()
    {
        $response = $this->get('/en');
        $response->assertStatus(200);
    }
  6. 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
@jakobbuis
Copy link
Author

jakobbuis commented Nov 28, 2017

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.

@arcanedev-maroc
Copy link
Member

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:

@StanBarrows
Copy link

@arcanedev-maroc are there any updates on this?

@Morinohtar
Copy link

I just bumped into this also. Any news regarding this issue?

Tks

@Morinohtar
Copy link

Ok, i used this approach and it works :)

mcamara/laravel-localization#161 (comment)

@flexchar
Copy link

Came around the same.

@hrnicek
Copy link

hrnicek commented Jun 4, 2019

Did anyone solve it?

Expected status code 200 but received 302.
Failed asserting that false is true.

@jartaud
Copy link

jartaud commented Aug 5, 2019

//web.php

if (app()->environment('testing')) {
require '_web.php';
} else {
Route::localizedGroup(function () {
require '_web.php';
});
}

@Krato
Copy link

Krato commented Oct 29, 2021

Ok, i used this approach and it works :)

mcamara/laravel-localization#161 (comment)

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 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants