gpt4 book ai didi

php - 如何在 Symfony2 功能测试中通过 HWIOAuth 模拟身份验证?

转载 作者:可可西里 更新时间:2023-10-31 22:15:15 26 4
gpt4 key购买 nike

HWIOAuthBundle 是一个在 Symfony2 项目中支持社交登录的组合包:

https://github.com/hwi/HWIOAuthBundle

Symfony2 文档给出了如何在功能测试中模拟身份验证的示例:

http://symfony.com/doc/current/cookbook/testing/simulating_authentication.html

不过,在我的测试中,我没有成功地让这两个一起工作。 Symfony2 安全系统指示有一个非匿名用户,但我的 Controller 中的 getUser() 返回 null。

有人在这方面取得了成功,或者对如何最好地调试或实现有一些建议吗?

使用:

  • 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

关于php - 如何在 Symfony2 功能测试中通过 HWIOAuth 模拟身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27927454/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com