如何在调用请求之前更改Symfony5/PHPUnit中的区域设置?
我需要在功能测试中更改区域设置:如何在调用请求之前更改Symfony5/PHPUnit中的区域设置?,symfony,phpunit,Symfony,Phpunit,我需要在功能测试中更改区域设置: <?php namespace App\Tests\Controller; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class TranslationControllerTest extends WebTestCase { public function testIndex() { $client = static::createClient();
<?php
namespace App\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class TranslationControllerTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient();
$client->request('GET', '/translation.js');
$client->getRequest()->setLocale('en');
$content = $client->getResponse()->getContent();
$trans = json_decode(substr($content, strpos($content, "'{") + 1, strpos($content, "}'") - strpos($content, "'{")), true);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertSame($trans['test.case'], 'en');
}
}
我也已经搜索了很长时间了。我现在发现,它可以从以下方面推导:
我不确定这是否是定义的行为,因为在测试中更改两次区域设置的行为有点奇怪:
class MyTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient();
$session = self::$container->get(SessionInterface::class);
$session->set('_locale', 'de');
$client->request('GET', '/dashboard');
// The resulting page is in English (my default locale)
....
$session->set('_locale', 'nl');
$client->request('GET', '/dashboard');
// The resulting page is in German
....
}
}
我一直在寻找这个也有相当一段时间。我现在发现,它可以从以下方面推导:
我不确定这是否是定义的行为,因为在测试中更改两次区域设置的行为有点奇怪:
class MyTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient();
$session = self::$container->get(SessionInterface::class);
$session->set('_locale', 'de');
$client->request('GET', '/dashboard');
// The resulting page is in English (my default locale)
....
$session->set('_locale', 'nl');
$client->request('GET', '/dashboard');
// The resulting page is in German
....
}
}