Localization icon indicating copy to clipboard operation
Localization copied to clipboard

Cannot feature test multilanguage routes

Open jakobbuis opened this issue 8 years ago • 9 comments

  • 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 avatar Nov 27 '17 19:11 jakobbuis

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.

jakobbuis avatar Nov 28 '17 22:11 jakobbuis

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 avatar Nov 29 '17 11:11 arcanedev-maroc

@arcanedev-maroc are there any updates on this?

StanBarrows avatar Mar 26 '18 10:03 StanBarrows

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

Tks

Morinohtar avatar Dec 21 '18 18:12 Morinohtar

Ok, i used this approach and it works :)

https://github.com/mcamara/laravel-localization/issues/161#issuecomment-381367191

Morinohtar avatar Jan 05 '19 14:01 Morinohtar

Came around the same.

flexchar avatar Jan 28 '19 20:01 flexchar

Did anyone solve it?

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

hrnicek avatar Jun 04 '19 08:06 hrnicek

//web.php

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

jartaud avatar Aug 05 '19 00:08 jartaud

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 😄

Krato avatar Oct 29 '21 07:10 Krato