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

Unit testing localized routes #161

Closed
leonardoalifraco opened this issue Jan 3, 2015 · 24 comments
Closed

Unit testing localized routes #161

leonardoalifraco opened this issue Jan 3, 2015 · 24 comments

Comments

@leonardoalifraco
Copy link

@leonardoalifraco leonardoalifraco commented Jan 3, 2015

Hello,

I'm trying to unit test calling to localized routes with a very simple test, but it is not working. I guess it is because the routes are loaded with the default prefix just one time on the testing flow.

On a browser, when the routes.php is excecuted for every request, everything works fine.

Is this an expected behaviour?
Am i doing something wrong?
Am i supposed to test this?

Here is the code of the test:

class ExampleTest extends TestCase {

    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testBasicExample()
    {
        $crawler = $this->client->request('GET', '/');

        $this->assertTrue($this->client->getResponse()->isOk());
    }

    /**
     * A basic functional test example with language.
     *
     * @return void
     */
    public function testBasicExampleWithLanguage()
    {
        $crawler = $this->client->request('GET', '/es');

        $this->assertTrue($this->client->getResponse()->isOk());
    }

}

And here is the error i'm getting when excecuting phpunit:

image

My routes.php is:

Route::group(array('prefix' => LaravelLocalization::setLocale(), 
                   'before' => 'LaravelLocalizationRedirectFilter'), function()
{
    Route::get('/', function() {
        return 'home';
    });

    Route::get('test',function(){
        return 'test';
    });
});
@cariboufute

This comment has been minimized.

Copy link

@cariboufute cariboufute commented Jan 4, 2015

Had the same problem too. Maybe I'm wrong but what I see in the vendor tests is that you have to reproduce all the route mapping in the test class to make it work. With this, you don't test the actual route mapping but a replica, which is not what you need to do. We need to test the actual route map.

Wonder how we could do this though...

@acaron

This comment has been minimized.

Copy link

@acaron acaron commented Feb 18, 2015

+1 same problem with Behat.

@acaron

This comment has been minimized.

Copy link

@acaron acaron commented Feb 18, 2015

After looking into the code, the function LaravelLocalization::setLocale() can return null or a locale. In the test case, it return null. If you force a value to always be return, all tests are ok but the redirection middleware is never hit since a null prefix can't happen anymore.

So, I looked in the request object and the test case was returning:

pathInfo: "/"
requestUri: "/"

instead of:

pathInfo: "/en/login"
requestUri: "/en/login"

So, the segment can never be fetch. We need a solution for testing! This way, we have to copy the routes like said previously.

This is my temporary fix until a better solution comes up:

'prefix' => (env('APP_ENV') === 'acceptance' ? 'en' : LaravelLocalization::setLocale())
@cariboufute

This comment has been minimized.

Copy link

@cariboufute cariboufute commented Feb 18, 2015

+1 for testing solution. I abandoned this package for the moment and made my own solution, which is less elegant, but can be tested.

@osaris

This comment has been minimized.

Copy link

@osaris osaris commented May 18, 2015

+1 this is really annoying to have all tests broken due to this package :-(

@okaufmann

This comment has been minimized.

Copy link
Contributor

@okaufmann okaufmann commented Jun 30, 2015

+1 have the same problem here. Is there already a fix for this ?

@dsamojlenko

This comment has been minimized.

Copy link

@dsamojlenko dsamojlenko commented Oct 15, 2015

Anyone have any further thoughts/suggestions on this? Same problem here...

@cariboufute

This comment has been minimized.

Copy link

@cariboufute cariboufute commented Oct 15, 2015

I made an alternate solution to this package but I did not package it yet. I may try to publish an alpha version of a package soon. The routes of my solution are unit testable.

@jaumeavila

This comment has been minimized.

Copy link

@jaumeavila jaumeavila commented Feb 17, 2016

+1 I have the same problem. No solution yet?

@zachleigh

This comment has been minimized.

Copy link

@zachleigh zachleigh commented Jul 3, 2016

+1 Anybody found a solution to this yet? This package seems great, but if its going to break all my tests, its pretty worthless.

@da-n

This comment has been minimized.

Copy link

@da-n da-n commented Sep 19, 2016

+1 Shame that there is no solution to this, has anybody found a workaround? This is an awesome package but totally breaks all tests.

@wtom

This comment has been minimized.

Copy link

@wtom wtom commented Sep 28, 2016

wtom@452a1c2

is this a possible solution?

I call it in my createApplication() method in my TestCase.php.

public function createApplication()
    {
        $app = require __DIR__.'/../bootstrap/app.php';

        $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
        app('laravellocalization')->setUnitTestingRoutes();

        return $app;
    }

I my test it's like this:

app('laravellocalization')->setLocale('en');
$url = app('laravellocalization')->getLocalizedURL('en', 'api/v1/test');
$this->json('GET', $url)
             ->seeJson([
                 'msg' => 'Hello World!',
             ]);

Route::group(['prefix' => LaravelLocalization::setLocale() ? LaravelLocalization::setLocale() : '/' , 'middleware' => [ 'localeSessionRedirect', 'localizationRedirect' ]], function() {

@cariboufute

This comment has been minimized.

Copy link

@cariboufute cariboufute commented Oct 1, 2016

I am making my own localization routing project for Laravel here : https://github.com/cariboufute/locale-route. Their routes will be fully testable. It should be ready in a few weeks.

@JensJI

This comment has been minimized.

Copy link

@JensJI JensJI commented Nov 2, 2016

This is a major problem. Using the localization plugin just broke our test suite for all http tests, and no of the proposed solutions seems applicable.

Maybe you, @mcamara , could take a look at this issue as you can see the big picture of the code?

@cariboufute

This comment has been minimized.

Copy link

@cariboufute cariboufute commented Nov 9, 2016

I have mostly finished making my own localization routing package for Laravel : https://github.com/cariboufute/locale-route. The version is an alpha, but is already functional and should be set to beta really soon. Please check it... and this one is fully testable with the usual testing methods.

@aurawindsurfing

This comment has been minimized.

Copy link

@aurawindsurfing aurawindsurfing commented Apr 14, 2018

For me different solution works:

Set in app/config:

'hideDefaultLocaleInURL' => env('LOCALIZATION_HIDE_DEFAULT_LOCALE', false)

And then in phpunit.xml:

<env name="LOCALIZATION_HIDE_DEFAULT_LOCALE" value="true"/>

Hope it helps someone!

@zedee

This comment has been minimized.

Copy link

@zedee zedee commented Jan 25, 2019

For me different solution works:

Set in app/config:

'hideDefaultLocaleInURL' => env('LOCALIZATION_HIDE_DEFAULT_LOCALE', false)

And then in phpunit.xml:

<env name="LOCALIZATION_HIDE_DEFAULT_LOCALE" value="true"/>

Hope it helps someone!

That workaround is not working for me :(

I guess there's still no updates on this?

@marufmax

This comment has been minimized.

Copy link

@marufmax marufmax commented Apr 5, 2019

+1 Having the same issue

@bertheyman

This comment has been minimized.

Copy link
Contributor

@bertheyman bertheyman commented Jun 28, 2019

Would love to find out if/when someone has a fix or workaround for this.
Tried the one above with no luck.

@cariboufute

This comment has been minimized.

Copy link

@cariboufute cariboufute commented Jun 28, 2019

Try my package : https://github.com/cariboufute/locale-route

It offers a testable alternate to this package and works well.

@V35CH

This comment has been minimized.

Copy link

@V35CH V35CH commented Aug 1, 2019

Does anyone have a solution for this issue?

@mgralikowski

This comment has been minimized.

Copy link

@mgralikowski mgralikowski commented Aug 24, 2019

For me (useAcceptLanguageHeader = true, hideDefaultLocaleInURL = false) it's works:

'prefix' => 
    !App::runningUnitTests() ? 
    LaravelLocalization::setLocale(null) : 
    config('app.locale'),

Of course, we need to use route helper in our tests:

$this->get(route('login'));`
@V35CH

This comment has been minimized.

Copy link

@V35CH V35CH commented Aug 30, 2019

I have found a solution:
The route prefix is only called once when the test case is setup. In this point the route doesnt know of the requested url (It is empty). So I added an function to my test cases: $this->refreshApplicationWithLocale($locale); in this function I set the env variable for the route key to the assumed language. Now this seems to work. Or do I have an misconception?

protected function refreshApplicationWithLocale($locale)
    {
        self::tearDown();
        putenv(LaravelLocalization::ENV_ROUTE_KEY . '=' . $locale);
        self::setUp();
    }
protected function tearDown()
    {
        putenv(LaravelLocalization::ENV_ROUTE_KEY);
        parent::tearDown();
    }
@bertheyman

This comment has been minimized.

Copy link
Contributor

@bertheyman bertheyman commented Sep 19, 2019

Works for me, thanks @V35CH !

I've made a pull request to add this to the docs:
#655

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

Successfully merging a pull request may close this issue.

None yet
You can’t perform that action at this time.