2012-07-18 10 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 –

答えて

8

のいずれかを使用:

$crawler = $client->followRedirect(); 

またはそのクライアントは常にリダイレクトします

$client->followRedirects(); 

関連ドキュメント:

1:http://symfony.com/doc/current/book/testing.html#redirecting

+0

ありがとうございます。それがログインページに表示されるようですが、どのように認証するのですか? – greg

+0

私はログインをシミュレートするクローラーのためのメソッドを作成することができたと思いますが、理想的にはPHP_AUTH varsで動作することができます – greg

+0

これを試しましたか? http://symfony.com/doc/current/cookbook/testing/http_authentication.html –

8

あなたは次の操作を行うことができるはずです)ページへの '参照'

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

2)に従って)、リダイレクト

$crawler = $client->followRedirect(); 
をフォーム

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

4をデータとしてログイン資格情報を渡して送信ボタン

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

3)を介してフォームを選択し、提出します

5)この時点で、応答cを確認できるはずです頌歌

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

または保護されたページにアクセス

$crawler = $client->request('GET', '/dashboard'); 
//do other stuff 
関連する問題