我正在尝试对防火墙后面的路由进行功能测试。我不确定我做错了什么,但是路由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: ~`发布于 2015-12-01 17:50:22
该路由不是公共路由
失败的测试位于可能受身份验证保护的/admin/dashboard路由上,因此服务器响应不会成功(200 OK),而是(403访问拒绝或302重定向)
因此您必须以不同方式测试您的路由:该路由是受保护的,因此请检查403或重定向到登录页面
查看有关How to Simulate Authentication with a Token in a Functional Test的文档
并测试通过身份验证的用户是否正确地看到该页面
https://stackoverflow.com/questions/34017065
复制相似问题