【问题标题】:Function test fails功能测试失败
【发布时间】:2016-03-05 04:17:19
【问题描述】:

我正在尝试对防火墙后面的路由进行功能测试。我不确定我做错了什么,但路由 admin/dashboard 的测试失败了。有什么想法吗?

<?php

namespace AppBundle\Tests;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;

class ApplicationAvailabilityFunctionalTest extends WebTestCase
{

    private $client;

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

    /**
     * @dataProvider urlProvider
     */
    public function testPageIsSuccessful($url)
    {

        $this->client->request('GET', $url);

        $this->assertTrue($this->client->getResponse()->isSuccessful());
    }

    public function urlProvider()
    {
        $this->logIn();

        return array(
            array('/'),
            array('/admin/login'),
            array('/admin/dashboard'),
        );
    }

    public function logIn()
    {

        $this->client = self::createClient();
        $session = $this->client->getContainer()->get('session');

        $firewall = 'our_db_provider';
        $token = new UsernamePasswordToken('admin', 'admin', $firewall, array('ROLE_ADMIN'));
        $session->set('_security_'.$firewall, serialize($token));
        $session->save();

        $cookie = new Cookie($session->getName(), $session->getId());
        $this->client->getCookieJar()->set($cookie);
    }
}

//更新

这是我得到的错误

1) AppBundle\Tests\ApplicationAvailabilityFunctionalTest::testPageIsSuccessful with data set #2 ('/admin/dashboard')
Failed asserting that false is true.

/Users/me/Projects/cms/src/AppBundle/Tests/ApplicationAvailabilityFunctionalTest.php:27

//更新2

这是 $token 变量的转储

Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken {#488 -credentials: null -providerKey: "security" -user: "admin" -roles: array:1 [ 0 => Symfony\Component\Security\Core\Role\Role {#487 -role: "ROLE_ADMIN" } ] -authenticated: true -attributes: [] }

//更新3

`security:
    encoders:
        AppBundle\Entity\Admin\User:
            algorithm: bcrypt
    providers:
        our_db_provider:
            entity:
               class: AppBundle\Entity\Admin\User
               property: username
    access_control:
        - { path: ^/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, roles: ROLE_ADMIN }
    firewalls:
        default:
            anonymous: ~
            http_basic: ~
            form_login:
               login_path: /admin/login
               check_path: /admin/login_check
               csrf_provider: security.csrf.token_manager
            logout:
               path:   /admin/logout
               target: /admin/login
            provider: our_db_provider
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        main:
            anonymous: ~`

【问题讨论】:

  • 你得到什么错误?
  • 显然没有这样的路线。 app/console debug:router 是否显示与 /admin/dashboard 相关的任何内容?
  • 路由存在。它在浏览器中加载良好。这是调试的输出:路由器app_admin_dashboard ANY ANY ANY /admin/dashboard
  • $url 声明在哪里?

标签: php symfony phpunit functional-testing


【解决方案1】:

路线不公开

失败的测试是在/admin/dashboard 路由上,该路由可能受身份验证保护,因此服务器响应不成功(200 OK)而是(403 访问被拒绝或 302 重定向)

所以你必须以不同的方式测试你的路由:路由受到保护,所以检查 403 或重定向到登录页面

查看有关How to Simulate Authentication with a Token in a Functional Test的文档

并测试经过身份验证的用户是否正确地看到了页面

【讨论】:

  • 是的,路由受防火墙保护。这就是我使用方法 logIn() 的原因。可能我在那里遗漏了一些东西......
  • 嗨@Vodokan,您需要登录,请查看doc here,了解有关如何在功能测试中实施身份验证的更多详细信息
  • 请再次查看我发布的代码。我做了你引用的页面中描述的所有内容。
  • 嗨@Vodokan 很奇怪,检查app/log/test.log 以调查问题
  • 我不知道 test.log 文件的存在。无论如何,我发现这一行`security.DEBUG: Access denied, the user is not fully authenticated;重定向到身份验证入口点`
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-23
  • 1970-01-01
  • 1970-01-01
  • 2013-02-26
  • 2020-10-08
  • 2017-02-07
相关资源
最近更新 更多