2011-12-06 60 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-> getValue('username')造成的,因爲表單實際上沒有s ubmitted,而不是PHPunit調度了該操作並設置了POST變量。

我該如何得到這個工作?

回答

4

對不起大家。我曾評論了這條線,試圖研究我的問題:

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

和事實證明,我的輸入測試輸入無效,你不能使用$形式 - >的getValue得到一個無效的值形成。

我沒有得到任何答案,因爲這條線沒有在我的文章中註釋掉,並會工作。拍頭............ MODS隨時刪除這篇文章,如果你認爲這對任何人都沒有幫助。