如何在 Symfony2 功能测试中使用经过身份验证的用户? [英] How to use an authenticated user in a Symfony2 functional test?

查看:38
本文介绍了如何在 Symfony2 功能测试中使用经过身份验证的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 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.

有人知道如何实现吗?

推荐答案

其实很简单.

  1. 为测试环境设置security.yml(这是来自Symfony 2.0-RC3的片段)

  1. Setup security.yml for test environment (it's a snippet from Symfony 2.0-RC3)

security:
    encoders:
        SymfonyComponentSecurityCoreUserUser: 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屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆