提问人:Yoran Schreuder Peters 提问时间:10/27/2023 更新时间:10/27/2023 访问量:30
Symfony 6.2 - EasyAdmin 4 phpunit test - 必修_locale
Symfony 6.2 - EasyAdmin 4 phpunit test - Mandatory _locale
问:
在我正在构建的应用程序中,我得到了一个带有区域设置变量 (en/nl) 的管理员
#[Route('/{_locale<%app.supported_locales%>}/admin', name: 'admin')]
public function index(): Response
现在一切正常,但我正在创建单元测试来测试应用程序。Easyadmin 有自己的 $this->generateIndexUrl,当我尝试请求 url 时:
$this->client->request("GET", $this->generateIndexUrl(null, $this->getDashboardFqcn(), $this->getControllerFqcn()), [
"_locale" => "en",
"_route_params" => [
"_locale" => "en"
]
]);
它给了我一个例外: Symfony\Component\Routing\Exception\MissingMandatoryParametersException:缺少一些必需参数 (“_locale”) 来生成路由“admin”的 URL。
搜索和搜索后,我找不到任何设置“强制”_locale的方法。 有人有什么想法吗?
我尝试了几件事:
// 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();
所有这些都没有达到预期的效果。我让它在这个请求前面有一个请求,我指定了路由并以另一种方式_locale。
$url = $router->generate('admin', ['_locale' => 'en']);
$this->client->request("GET", $url);
奇怪的是,将这个放在另一个前面,会产生所需的效果,因为它采用了这个区域设置。
我还没有找到一种方法或变量来使用 EasyAdmin 4 方法 $this->generateIndexUrl 设置语言环境。
我真的希望有人能回答如何在单元测试和 EasyAdmin 中为路由提供所需的语言环境参数
答: 暂无答案
评论
$this->adminUrlGenerator->set('_locale', 'en');