Symfony2功能测试认证
发布时间:2020-12-13 18:21:38 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试设置我的功能测试,我遇到了经过身份验证的问题.我已经阅读了本指南: http://symfony.com/doc/current/cookbook/testing/http_authentication.html并实现了他们所说的要做但我仍然坚持重定向登录.我确信这是微不足道的,但我不确定是什么. 测试控制
我正在尝试设置我的功能测试,我遇到了经过身份验证的问题.我已经阅读了本指南:
http://symfony.com/doc/current/cookbook/testing/http_authentication.html并实现了他们所说的要做但我仍然坚持重定向登录.我确信这是微不足道的,但我不确定是什么.
测试控制器 namespace HvHClientsBundleTestsController; use HvHClientsBundleControllerClientsController; use SymfonyBundleFrameworkBundleTestWebTestCase; use SymfonyComponentHttpFoundationRequest; use SymfonyComponentHttpFoundationHeaderBag; use SymfonyComponentHttpFoundationSession; use SymfonyBundleFrameworkBundleControllerController; 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: 请求的结果 SymfonyComponentHttpFoundationRedirectResponse Object ( [headers] => SymfonyComponentHttpFoundationResponseHeaderBag Object ( [computedCacheControl:protected] => Array ( [no-cache] => 1 ) [cookies:protected] => Array ( [] => Array ( [/] => Array ( [PHPSESSID] => SymfonyComponentHttpFoundationCookie 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 ) 关于如何解决这个问题的任何建议?
您应该能够执行以下操作:
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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |