Symfony2 功能测试认证
Posted
技术标签:
【中文标题】Symfony2 功能测试认证【英文标题】:Symfony2 functional test authentication 【发布时间】:2012-07-18 00:58:57 【问题描述】:我正在尝试设置我的功能测试,但在获得身份验证时遇到了问题。我已经阅读了本指南:http://symfony.com/doc/current/cookbook/testing/http_authentication.html 并实现了他们所说的,但我仍然卡在重定向登录上。我确定这是微不足道的,但我不确定是什么。
测试控制器
namespace HvH\ClientsBundle\Tests\Controller;
use HvH\ClientsBundle\Controller\ClientsController;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\HeaderBag;
use Symfony\Component\HttpFoundation\Session;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ClientsControllerTest extends WebTestCase
public function testGetClientsAction()
$client = static::createClient();
$client->request(
'/clients/123456',
'GET',
array(), /* request params */
array(), /* files */
array('X-Requested-With' => "XMLHttpRequest", 'php_AUTH_USER' => 'testuser', 'PHP_AUTH_PW' => 'testpass')
);
print_r($client->getResponse());
die();
congif_test.yml
security:
firewalls:
secured_area:
http_basic:
请求结果
Symfony\Component\HttpFoundation\RedirectResponse Object
(
[headers] => Symfony\Component\HttpFoundation\ResponseHeaderBag Object
(
[computedCacheControl:protected] => Array
(
[no-cache] => 1
)
[cookies:protected] => Array
(
[] => Array
(
[/] => Array
(
[PHPSESSID] => Symfony\Component\HttpFoundation\Cookie Object
(
[name:protected] => PHPSESSID
[value:protected] => 7e3ece541918264de0003e2dcd251833
[domain:protected] =>
[expire:protected] => 1342616045
[path:protected] => /
[secure:protected] =>
[httpOnly:protected] =>
)
)
)
)
[headers:protected] => Array
(
[location] => Array
(
[0] => http://localhost/login
)
[cache-control] => Array
(
[0] => no-cache
)
[date] => Array
(
[0] => Wed, 18 Jul 2012 00:54:05 GMT
)
[content-type] => Array
(
[0] => text/html
)
[x-debug-token] => Array
(
[0] => 5006092d43848
)
)
[cacheControl:protected] => Array
(
)
)
[content:protected] => <!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="1;url=http://localhost/login" />
<title>Redirecting to http://localhost/login</title>
</head>
<body>
Redirecting to <a href="http://localhost/login">http://localhost/login</a>.
</body>
</html>
[version:protected] => 1.0
[statusCode:protected] => 302
[statusText:protected] => Found
[charset:protected] => UTF-8
)
关于如何解决这个问题的任何建议?
【问题讨论】:
如果您使用 FOSUserBundle,请查看***.com/questions/14957807/… 【参考方案1】:任意使用:
$crawler = $client->followRedirect();
或使该客户端始终重定向:
$client->followRedirects();
相关文档:http://symfony.com/doc/current/book/testing.html#redirecting
【讨论】:
谢谢。这似乎让我进入了登录页面,我该如何让它进行身份验证? 我想我可以为爬虫创建一个模拟登录的方法,但理想情况下它可以与 PHP_AUTH 变量一起使用 你试过了吗? symfony.com/doc/current/cookbook/testing/… 你传参数的顺序好像不对,看这里:api.symfony.com/2.0/Symfony/Component/HttpKernel/…【参考方案2】:您应该能够做到以下几点:
1) '浏览'到页面
$client = static::createClient();
$crawler = $client->request('GET', '/login');
2) 通过提交按钮选择表单
$buttonCrawlerNode = $crawler->selectButton('submit');
3) 将登录凭据作为数据传递并提交表单
$form = $buttonCrawlerNode->form();
$data = array('username' => 'u@u.com','password' => 'pass');
$client->submit($form,$data);
4) 跟随重定向
$crawler = $client->followRedirect();
5)此时您应该可以检查响应代码
$this->assertEquals(302, $client->getResponse()->getStatusCode());
或访问安全页面
$crawler = $client->request('GET', '/dashboard');
//do other stuff
【讨论】:
以上是关于Symfony2 功能测试认证的主要内容,如果未能解决你的问题,请参考以下文章
Symfony2 - 使用 FOSUserBundle 进行测试