2016-04-27 7 views
0

私が機能を持っているsymfonyの3コントローラファイルで:3 behatミンクテストのリダイレクト

/** 
    * @Route("/user/registration", name="post_registration") 
    * @Method("POST") 
    * @return mixed 
    */ 
    public function postRegistration() 
    { 
     $post = $this->getAllPost(); 

     $this->curl = $this->get('ApiClient'); 

     $responseArray = $this->curl->post(
      $this->container->getParameter('api_url') . '/users', 
      $post 
     ); 

     if (isset($responseArray['api_key'])) { 
      return $this->redirectResponseWithCookie($responseArray['api_key']); 
     } else { 

      return $this->render(
       static::REGISTRATION_TEMPLATE, 
       ['errors' => $responseArray['errors']] 
      ); 
     } 
    } 

1つの部分では、ページをリダイレクトする必要がある関数

redirectResponseWithCookie() 

を呼び出します。

私はリダイレクトヘッダーをテストしたいです - 正しいLocation値を持っていますか?

私は

class UserRegistrationContext extends BaseContext implements Context, SnippetAcceptingContext 
{ 

/** 
* @When I register successfully into the system 
* @return null 
*/ 
public function iRegisterSuccessfullyIntoTheSystem() 
{ 

    $this->canIntercept(); 
    $this->session->getDriver()->getClient()->followRedirects(false); 

    // Enters his data 
    // Posts to www.notification.guru . 
    $this->session->getDriver()->getClient() 
     ->request('POST', $this->baseUrl . '/user/registration', $this->getFormArray()); 

    echo 'test'; 

} 
} 

BaseContextクラスだけで、セッションのINITそのコンストラクタをいくつかのヘルパー関数を持っているUserRegistrationContextクラスの機能を持っている:

$driver = new GoutteDriver(); 
$this->session = new Session($driver); 

この部分は間違っているかもしれない:

$this->session->getDriver()->getClient()->followRedirects(false); 

私はちょうど2.5からコードを取り出し、3で動作させるように調整しようとしましたが、それが正しいかどうかわからないが、少なくともエラーを投げない。

それは、リダイレクトを停止する必要がありますので、私は

getResponseHeaders() 

でレスポンスヘッダを得ることができる。しかし、それはリダイレクトどこ実際のサイトがまだlauchedされていないため、問題は、それがリダイレクトしようとすることで、コードが失敗しました。また、実際のリダイレクト後に推測するヘッダーをテストすることもできません。

したがって、リダイレクトを停止する必要があります。

どうすればよいですか?私は情報を見つけることができません。私は、コンソールでよく見ていなかった -

テストは、私がコメントで言ったように私の質問のコードは、間違っていないライン

$this->session->getDriver()->getClient() 
     ->request('POST', $this->baseUrl . '/user/registration', $this->getFormArray()); 
+0

実は私はそのコードに誤りがない見て、私はテストを実行した後、コンソールにも表示されませんでした。 –

答えて

1

で失敗します。私がSOに投稿するときに、その質問を書くことを終わらせないこともよくあります。詳細を他の人に書いて、答えを見るだけです。

$this->session->getDriver()->getClient()->followRedirects(false); 

がうまくいきます。だから、チェックする方法を終了する

- このような機能のものを作り、それを呼び出す:

/** 
* @param Behat\Mink\Session $session - mink session 
* @throws \Exception 
* @return null 
*/ 
public function isLoggedIntoApi($session) 
{ 
    $headers = $session->getResponseHeaders(); 

    if ($headers['Location'][0] != $this->appUrl) { 
     throw new \Exception(
      'User is not redirected to ' . $this->appUrl 
     ); 
    } 
} 
関連する問題