在我的控制器中,我使用security.token_storage服务将用户连接起来:
$client = $this->container->get('security.token_storage')->getToken()->getUser();
$username = $client->getNom()." ".$client->getPrenom();
在功能测试中,我尝试登录用户以在控制器中检索它,但是如果在内部调试,则表明我是匿名的:
private function logIn()
{
$client = static::createClient();
$firewallName = 'main';
$token = new UsernamePasswordToken("admin", "admin", "main", array());
static::$kernel->getContainer()->get('security.token_storage')->setToken($token);
$session = static::$kernel->getContainer()->get('session');
$session->set('_security_'.$firewallName, serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$client->getCookieJar()->set($cookie);
return $client;
}
public function testIndex()
{
$client = $this->logIn();
$crawler = $client->request('GET', '/');
$login = $crawler->filter("#username_index")->text();
$this->assertEquals($login, 'ADMIN NOM Admin Prenom');
}
我如何将我的测试用户传递给我的主要代码?