2012-07-18 94 views
7

我試圖設置我的功能測試,並且遇到了通過身份驗證的問題。我已閱讀了本指南:http://symfony.com/doc/current/cookbook/testing/http_authentication.html並實施了他們所說的操作,但仍然陷於重定向登錄。我確信這是件小事,但我不確定是什麼。Symfony2功能測試認證

測試控制器

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 
) 

的Ÿ如何解決這個問題的建議?

+1

如果您正在使用FOSUserBundle,請看看http://stackoverflow.com/questions/14957807/symfony2-tests-with-fosuserbundle/27223293# 27223293 – 2014-12-01 07:24:22

回答

8

要麼使用:

$crawler = $client->followRedirect(); 

或使該客戶端始終重定向:

$client->followRedirects(); 

相關文件:http://symfony.com/doc/current/book/testing.html#redirecting

+0

謝謝。這似乎讓我到登錄頁面,我如何得到它的身份驗證? – greg 2012-07-18 16:32:05

+0

我想我可以爲抓取工具創建一個模擬登錄的方法,但理想情況下它可能只適用於PHP_AUTH變量 – greg 2012-07-18 16:39:55

+0

您是否嘗試過? http://symfony.com/doc/cookbook/testing/http_authentication.html – 2012-07-18 17:02:17

8

你應該能夠做到以下幾點:

1 )'瀏覽'至頁面

$client = static::createClient(); 
$crawler = $client->request('GET', '/login'); 

2)經由提交按鈕

$buttonCrawlerNode = $crawler->selectButton('submit'); 

3)選擇的形式傳遞登錄憑證,如數據並提交表單

$form = $buttonCrawlerNode->form(); 
$data = array('username' => '[email protected]','password' => 'pass'); 
$client->submit($form,$data); 

4)按照重定向

$crawler = $client->followRedirect(); 

5)此時你應該能夠檢查響應c頌

$this->assertEquals(302, $client->getResponse()->getStatusCode()); 

或訪問受保護的頁面

$crawler = $client->request('GET', '/dashboard'); 
//do other stuff