Question

The problem is that in the tests the langauge wont switch, on the website is does well. So the tests fail.

I have this routing:

$app->before(function () use ($app) {

        if ($locale = $app['request']->get('locale')) {
            $app['locale'] = $locale;
        }
    ...
    }); 
$app->get('/{locale}/index', function () use ($app) {
            return $app['twig']->render('index.twig');
    })->assert('locale',implode('|', $app['languages']));

$app->get('/{locale}', function () use ($app) {
            return  $app->redirect("/".$app['locale']."/index");
    })->assert('locale',implode('|', $app['languages']));

The test is so:

public function testLanguageRoutes(){
    $client = $this->createClient();
$client->followRedirects();
    $crawler = $client->request('GET', '/de');
    $this->assertTrue($client->getResponse()->isOk());
    $this->assertTrue($crawler->filter('html:contains("Kopf")')->count() > 0);
$crawler = $client->request('GET', '/de/index');
    $this->assertTrue($client->getResponse()->isOk());
    $this->assertTrue($crawler->filter('html:contains("Kopf")')->count() > 0);
    $crawler = $client->request('GET', '/fr');
    $this->assertTrue($client->getResponse()->isOk());
    //TODO not working, still returns de should return fr
    var_dump($crawler->text());
    $this->assertTrue($crawler->filter('html:contains("Pied")')->count() > 0);
$crawler = $client->request('GET', '/aa');
    $this->assertTrue($client->getResponse()->isNotFound());
}

Here the switch to french wont work, $crawler->text() still shows the german text.

Do I have to create one Test for each language? That wont test the langauge switch really, in my opinion.

What is common way to test a scenario like that?

Was it helpful?

Solution

I suspect this is due to the way your translation code works. If the language that is used for translation is tied to your translator, then it may not change on the second request.

This is one of the limitations of pimple, you cannot reset the container.

You could try working around it by insulating the requests. You do this by loading the Symfony Process component (just make sure the autoloader can find it) and calling insulate(true) on your BrowserKit Client instance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top