2011-12-06 8 views
2

私はこのようなPHPUnitのテストがあります。PHPUnitの派遣コントローラのPOSTでのアクションとフォーム

public function testUsersCanRegisterWhenUsingValidData() 
    { 

    $this->request->setMethod('POST') 
     ->setPost(array(
      'username'   => 'user123', 
      'zip_code'   => '43215', 
      'email'   => '[email protected]', 
      'password'   => 'secret', 
      'confirm_pswd'  => 'secret', 
      )); 

    $this->dispatch('/account/register'); 

    $this->assertRedirectTo('/account/login'); 

    } 

と、このようなレジスタと呼ばれるユーザーのコントローラのアクション:

 public function registerAction() 
     { 

      // Instantiate the registration form model 
      $form = new Application_Model_FormRegister(); 

      // Has the form been submitted? 
      if ($this->getRequest()->isPost()) { 

      // If the form data is valid, process it 
      if ($form->isValid($this->_request->getPost())) { 

       // Does an account associated with this username already exist? 
       $account = $this->em->getRepository('Entities\Account') 
           ->findOneByUsernameOrEmail($form->getValue('username'), $form->getValue('email'));   

       if (! $account) 
       { // do something 
    ............. 
    .............. 

} else { 

      $this->view->errors = array(
       array("The desired username {$form->getValue('username')} has already been taken, or 
       the provided e-mail address is already associated with a registered user.") 
      ); 

      } 

     } else { 
      $this->view->errors = $form->getErrors(); 
     } 

     } 

     $this->view->form = $form; 

    } 

を私は、この行でエラーが出ます:

$account = $this->em->getRepository('Entities\Account') 
            ->findOneByUsernameOrEmail($form->getValue('username'), $form->getValue('email')); 

は、それがます$ form-によって引き起こされています> NULLであることのgetValue( 'ユーザ名')の形式は、実際のされていないので、代わりに、PHPunitはアクションをディスパッチしてPOST変数を設定しました。

どうすればこの問題を解決できますか?

答えて

4

ごめんなさい。無効の値を取得する>のgetValueを

// If the form data is valid, process it 
     if ($form->isValid($this->_request->getPost())) { 

をし、それは私の入力テスト入力が有効ではなかったし、あなたがます$ form-を使用できないことが判明:私は試してみて、私の問題を研究するために、この行をコメントアウトしていました形。

この行は私のポストでコメントアウトされておらず、働いていたので、私は答えを得られませんでした。あなたは誰にも助けとは思わない場合は、MODSはこの投稿を削除してください。

関連する問題