Symfony 6.2 - EasyAdmin 4 phpunit test - Mandatory _locale

110 views Asked by At

Within an application that I am building I got a admin with an locale variable (en/nl)

#[Route('/{_locale<%app.supported_locales%>}/admin', name: 'admin')]
    public function index(): Response

Now everything is working fine but I am creating unit tests to test the application. Easyadmin has a own $this->generateIndexUrl and when I try to request the url:

$this->client->request("GET", $this->generateIndexUrl(null, $this->getDashboardFqcn(), $this->getControllerFqcn()), [
   "_locale" => "en",
   "_route_params" => [
        "_locale" => "en"
   ]
]);

It gives me an Exception: Symfony\Component\Routing\Exception\MissingMandatoryParametersException : Some mandatory parameters are missing ("_locale") to generate a URL for route "admin".

After searching and searching I can't find any method to set the 'mandatory' _locale. Anyone got any ideas?

I tried several things:

// Set the locale
        $this->client->setServerParameters([
            'HTTP_ACCEPT_LANGUAGE' => 'en',
            '_locale' => 'en'
        ]);
setlocale(LC_ALL, 'en_GB');
 $session = $this->createSession($this->client);

        $container = $this->client->getContainer();
        $sessionSavePath = $container->getParameter('session.save_path');
        $sessionStorage = new MockFileSessionStorage($sessionSavePath);

        $session = new Session($sessionStorage);
        $session->start();
        $session->set('_locale', 'en');
        $session->save();

All with not the desired effect. I got it working with an request in front of this one where I specify the route and _locale in an other way.

$url = $router->generate('admin', ['_locale' => 'en']);
        $this->client->request("GET", $url);

And strangely putting this one in front of the other, gives the desired effect because it takes the locale settings from this one.

I haven't found a method or variable yet to just set the locale with the EasyAdmin 4 method $this->generateIndexUrl.

I really hope someone has the answer how to give the route the required locale parameter within the Unit test and EasyAdmin

0

There are 0 answers