Php 如何在Symfony2功能测试中通过HWIOAuth模拟身份验证?
HWIOAuthBundle是一个composer捆绑包,支持Symfony2项目中的社交登录: Symfony2文档给出了如何在功能测试中模拟身份验证的示例: 不过,在我的测试中,我并没有成功地让这两个角色一起工作。Symfony2安全系统指示存在非匿名用户,但我的控制器中的getUser()返回null 有人在这方面取得了成功,或者对如何最好地调试或实现有一些建议吗 使用:Php 如何在Symfony2功能测试中通过HWIOAuth模拟身份验证?,php,symfony,testing,hwioauthbundle,Php,Symfony,Testing,Hwioauthbundle,HWIOAuthBundle是一个composer捆绑包,支持Symfony2项目中的社交登录: Symfony2文档给出了如何在功能测试中模拟身份验证的示例: 不过,在我的测试中,我并没有成功地让这两个角色一起工作。Symfony2安全系统指示存在非匿名用户,但我的控制器中的getUser()返回null 有人在这方面取得了成功,或者对如何最好地调试或实现有一些建议吗 使用: Symfony2-2.6.3 HWIOAuthBundle-开发主机-309d802f8de9f39e30851
- Symfony2-2.6.3
- HWIOAuthBundle-开发主机-309d802f8de9f39e30851a145a7fc0de69b94076
use Symfony\Component\BrowserKit\Cookie;
use Liip\FunctionalTestBundle\Test\WebTestCase;
use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;
use HWI\Bundle\OAuthBundle\Security\Core\User\OAuthUser;
class ControllerTest extends WebTestCase
{
public function testSecurity()
{
$client = static::createClient();
$token = new OAuthToken('test', array( 'ROLE_USER', 'ROLE_OAUTH_USER' ));
$user = new OAuthUser('test@test.com');
$token->setUser($user);
$session = $client->getContainer()->get('session');
// Name of your firewall has to be prefixed by "_security_"
$session->set('_security_name_of_your_firewall', serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$client->getCookieJar()->set($cookie);
// $client now acts as authenticated client