如何使用共享上下文防火墙测试symfony身份验证?

时间:2015-01-12 21:58:48

标签: php security unit-testing symfony phpunit

我正在尝试测试一个通过上下文使用共享防火墙的应用程序(symfony 2.6 + phpunit),但它无法正常工作。 我关注了http://symfony.com/doc/current/cookbook/testing/simulating_authentication.html

的文档

问题是我的测试是返回匿名令牌而不是UsernamePasswordToken。为什么??

这是我的security.yml文件

// app/config/security.yml
security:
    encoders:
        Marcoshoya\MarquejogoBundle\Entity\AdmUser:
            algorithm:        sha1
            encode_as_base64: false
            iterations:       1

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH ]

    providers:
        customer:
            entity: { class: MarcoshoyaMarquejogoBundle:Customer, property: email }

    firewalls:
        site:
            context: website
            pattern: ^/(?!(cliente)).*$
            anonymous: true

        customer:
            anonymous: ~
            context: website
            pattern:    ^/cliente/.*
            form_login:
                provider: customer
                check_path: /cliente/login_check
                login_path: /cliente/login
                default_target_path: /cliente

    access_control:
        - { path: ^/cliente/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/cliente, roles: [ROLE_CUSTOMER] }

我的测试文件:

// ControllerTest.php
public function logIn()
{
    // this function is implemented as documentation
}

public function testDashboard()
{
    $this->logIn();
    $crawler = $this->client->request('GET', '/cliente/');

    //
    var_dump($this->client->getContainer()->get('security.context')->getToken());

}

转储问题:

.object(Symfony\Component\Security\Core\Authentication\Token\AnonymousToken)#5855 (5) {
  ["key":"Symfony\Component\Security\Core\Authentication\Token\AnonymousToken":private]=>
  string(13) "54b43b41cbe2a"
  ["user":"Symfony\Component\Security\Core\Authentication\Token\AbstractToken":private]=>
  string(5) "anon."
  ["roles":"Symfony\Component\Security\Core\Authentication\Token\AbstractToken":private]=>
  array(0) {
  }
  ["authenticated":"Symfony\Component\Security\Core\Authentication\Token\AbstractToken":private]=>
  bool(true)
  ["attributes":"Symfony\Component\Security\Core\Authentication\Token\AbstractToken":private]=>
  array(0) {
  }
}

从security.yml文件中删除选项“context”,我得到了我期望的响应:

.object(Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken)#882 (6) {
  ["credentials":"Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken":private]=>
  NULL
  ["providerKey":"Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken":private]=>
  string(8) "customer"
  ["user":"Symfony\Component\Security\Core\Authentication\Token\AbstractToken":private]=>
  string(8) "customer"
  ["roles":"Symfony\Component\Security\Core\Authentication\Token\AbstractToken":private]=>
  array(1) {
    [0]=>
    object(Symfony\Component\Security\Core\Role\Role)#823 (1) {
      ["role":"Symfony\Component\Security\Core\Role\Role":private]=>
      string(13) "ROLE_CUSTOMER"
    }
  }
  ["authenticated":"Symfony\Component\Security\Core\Authentication\Token\AbstractToken":private]=>
  bool(true)
  ["attributes":"Symfony\Component\Security\Core\Authentication\Token\AbstractToken":private]=>
  array(0) {
  }
}

0 个答案:

没有答案