[英]Symfony functional test - controller method getUser returns null
I have method in in my functional test for user log in: 我在用户登录的功能测试中有方法:
/**
* @param User $user
*/
private function logIn(User $user)
{
$firewallName = 'site';
$session = $this->client->getContainer()->get('session');
$securityContext = $this->client->getContainer()->get('security.context');
$token = new UsernamePasswordToken($user, null, 'main', array('ROLE_USER'));
$securityContext->setToken($token);
$session->set('_security_'.$firewallName, serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$this->client->getCookieJar()->set($cookie);
}
When I try to get user in controller, then it returns null. 当我尝试在控制器中获取用户时,它返回null。
public function editProfile(Request $request)
{
$user = $this->getUser(); //returns null
...
}
Is logIn method wrong? logIn方法错了吗?
Update 更新
Test method: 测试方法:
public function testProfile() {
...
$user = $this->createUser();
$this->logIn($user);
$this->client->request('POST', '/profile/edit', $data);
...
}
Try change: 尝试更改:
$token = new UsernamePasswordToken($user, null, 'main', array('ROLE_USER'));
to 至
$token = new UsernamePasswordToken($user, null, $firewallName, array('ROLE_USER'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.