gpt4 book ai didi

php - Symfony2 phpunit 测试认证

转载 作者:行者123 更新时间:2023-12-04 18:01:20 27 4
gpt4 key购买 nike

谁能帮助我在 php 功能测试中进行身份验证。我进行了授权并且我所有的 Controller 测试都失败了,因为它们在 localhost/之后没有得到任何东西,因为需要角色 ROLE_USER。

我不知道如何在 Controller 测试中做到这一点。我的授权步骤看起来像这样放入输入表单电子邮件并提交输入,如果该电子邮件存在于 db create session 与用户实体和重定向。

但是如何在测试中模拟呢?

例如我的测试的一部分。

public function testUnlinkWatcher()
{
$client = static::createClient();
$client->request('POST', 'project/client/unlink_watcher');


$this->assertTrue($client->getResponse()->isSuccessful()); // this false because access denied

}

我尝试使用 2 个示例,但我不工作。能做什么?
private $client = null;

public function setUp()
{
$this->client = static::createClient();
}

private function logIn()
{
$session = $this->client->getContainer()->get('session');

$firewall = 'secured_area';
$token = new UsernamePasswordToken('login', null, $firewall, array('ROLE_USER'));
$session->set('_security_'.$firewall, serialize($token));
$session->save(); // It's here object session with roles
}

public function testUnlinkWatcher()
{

$this->logIn();
$this->client->request('POST', 'testProject/client/unlink_watcher');

var_dump($client->getResponse()->isSuccessful());die; //false

我的安全
security:

providers:
in_memory:
memory: ~

hwi:
entity: { class: Test\testProjectBundle\Entity\User }

firewalls:
dev:
pattern: ^/(_(profiler|wdt|error)|css|images|js)/
security: false

secured_area:

anonymous: ~

oauth:
resource_owners:
google: "/login/check-google"
facebook: "/login/check-facebook"
login_path: /
failure_path: /


oauth_user_provider:
service: testProjectbundle.oauth_provider

logout:
path: /logout


access_control:
- { path: ^/testProject(.+), roles: ROLE_USER }

登录方法中的 session 转储:
    Symfony\Component\HttpFoundation\Session\Session {#377
#storage: Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage {#376
-savePath: "/var/www/integra/app/cache/test/sessions"
#id: "e3723c3056f23247565a4abdafd2c3cc964cc6d425e6d51dfd70bc588b4ad78b"
#name: "MOCKSESSID"
#started: false
#closed: false
#data: array:3 [
"_sf2_attributes" => &1 array:1 [
"_security_secured_area" => "C:74:"Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken":227:{a:3:{i:0;N;i:1;s:12:"secured_area";i:2;s:178:"a:4:{i:0;s:5:"login";i:1;b:1;i:2;a:1:{i:0;O:41:"Symfony\Component\Security\Core\Role\Role":1:{s:47:"\x00Symfony\Component\Security\Core\Role\Role\x00role";s:9:"ROLE_USER";}}i:3;a:0:{}}";}}"
]
"_sf2_flashes" => &2 []
"_sf2_meta" => &3 array:3 [
"u" => 1453705239
"c" => 1453705239
"l" => "0"
]
]

最佳答案

关于php - Symfony2 phpunit 测试认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34947740/

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