我正在尝试为用 ZF2 编写的表单帖子编写 PHPunit 测试。我要测试的动作如下,
public function resetAction() {
try {
$resetForm = $this->getServiceLocator()->get('Login\Model\ResetForm');
$message = $errmessage = null;
if ($this->getRequest()->isPost()) {
$resetForm->setInputFilter($resetForm->getInputFilter());
$resetForm->setData($this->getRequest()->getPost());
if ($resetForm->isValid()) {
$signInService = $this->getServiceLocator()->get('Login\Model\SignInService');
/* Genereate a unique token to put to the tAccounts table to reset the password */
$serverUrl = $this->getRequest()->getUri()->getScheme() . '://' . $this->getRequest()->getUri()->getHost();
/* Complete the URL, this need to change */
$serverUrl = $serverUrl . '/basezf2/public';
/* Initiate the reset password send email notification process */
if ($signInService->sendPasswordResetNotification($resetForm->getData(), $serverUrl)) {
$message = 'Email has been send with the password reset link';
} else {
$errmessage = 'No account found for given email address';
}
}
}
return new ViewModel(array('resetForm' => $resetForm, 'message' => $message, 'errmessage' => $errmessage));
} catch (\Exception $e) {
$log = $this->getServiceLocator()->get('Error\Log');
$log->log(Logger::ERR, $e->getMessage());
}
}
我的单元测试如下
public function testresetActionPost() {
date_default_timezone_set('US/Eastern');
$this->dispatch('/login/reset', 'POST', array('email' => 'notEmail'));
$this->assertResponseStatusCode(200);
}
如您所见,此测试我没有发送正确的电子邮件地址,当我通过网络执行此操作时,表单验证失败并执行它应该做的所有事情。我怎样才能在我的测试中完成这项工作。
我正在使用Zf2
+ PHPunit 3.7
。