2015-12-01 157 views
2

我正在尝试对防火墙后的路由进行功能测试。我不知道我在做什么错,但路线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); 
    } 
} 

// UPDATE

这里的错误,我得到

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

这里是$令牌转储变量

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: ~` 
+0

你得到了什么错误? –

+0

显然没有这样的路线。 'app/console debug:router'显示与'/ admin/dashboard'相关的任何内容吗? –

+0

路由存在。它在浏览器中加载得很好。这里是调试的输出:router 'app_admin_dashboard任何任何/ admin /仪表板' – Vodokan

回答

0

的路线是不公开

失败的测试是,可能是通过认证保护的/admin/dashboard路线上,因此服务器响应未成功(200 OK),但(403访问被拒绝或302重定向)

所以你必须以不同测试路线:路线被保护,以便检查403或重定向到登录页面

检查有关的文档

和测试验证的用户看到正确的页面

+0

是的,路由受防火墙保护。这就是为什么我使用方法logIn()。可能我错过了那里的东西...... – Vodokan

+0

嗨@Vodokan你需要做登录,检查[doc here](http://symfony.com/doc/current/cookbook/testing/simulating_authentication.html)以获得更多信息详细了解如何在功能测试中实施认证 – Matteo

+0

请再次查看我发布的代码。我做了从您推荐的网页中描述的所有内容。 – Vodokan

相关问题