如何在Symfony2功能测试中使用经过身份验证的用户? [英] How to use an authenticated user in a Symfony2 functional test?
问题描述
我使用FacebookBundle
对我的Symfony2应用程序中的用户进行身份验证.但是,我想使用phpunit使用经过身份验证的用户来创建功能测试.
I use the FacebookBundle
to authenticate users in my Symfony2 application. However, I would like to create functional tests with phpunit which uses an authenticated user.
此外,我不想为此使用Facebook用户,但将其固定住了.
Moreover, I don't want to use a facebook user for this, but harcoded one.
有人知道如何实现吗?
推荐答案
这实际上很简单.
-
用于测试环境的设置
security.yml
(这是Symfony 2.0-RC3的摘录)
Setup
security.yml
for test environment (it's a snippet from Symfony 2.0-RC3)
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
providers:
in_memory:
users:
user: { password: userpass, roles: [ 'ROLE_USER' ] }
admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
firewalls:
secured-area:
pattern: ^/demo/secured/
http_basic:
realm: "Secured Demo Area"
您可以看到定义了HTTP身份验证.所以现在,您要做的就是在功能测试中设置客户端:
As you can see there is HTTP Authentication defined. So now, all you have to do is to setup a client in functional test:
$client = static::createClient(array(), array(
'PHP_AUTH_USER' => 'user',
'PHP_AUTH_PW' => 'userpass',
));
此链接可能有用: http://symfony.com/doc/current/testing .html
这篇关于如何在Symfony2功能测试中使用经过身份验证的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!