J'écris un test unitaire avec phpUnit pour tester une application Zend Framework et j'ai quelques problèmes avec le test d'une exception dans la fonction changePassword. Le test n'échoue pas, mais dans l'outil de couverture qui génère html le "throw new Exception ($ tr-> translate ('userOldPasswordIncorrect'));" la ligne n'est pas testée.Exception non testée avec phpUnit?
public function changePassword(array $data, $id)
{
$user = $this->_em->find('Entities\User', (int) $id);
$oldPassword = sha1(self::$_salt . $data['oldPassword']);
if ($user->getPassword() !== $oldPassword) {
$tr = PC_Translate_MySQL::getInstance();
throw new Exception($tr->translate('userOldPasswordIncorrect'));
}
$user->setPassword(sha1(self::$_salt . $data['password']));
$this->_em->persist($user);
$this->_em->flush();
}
Le test unitaire qui devrait tester l'exception:
/**
* @depends testFindByAuth
* @expectedException Exception
*/
public function testChangePasswordWrongOldPassword()
{
$this->_dummyUser = $this->_user->findByAuth($this->_dummyEmail, $this->_dummyPassword, $this->_reseller);
// Try to change the password with a wrong oldPassword
$data['oldPassword'] = 'wrongOldPassword';
$data['password'] = $this->_dummyNewPassword;
$this->_user->changePassword($data, $this->_dummyUser->getId());
}
Je vais espère que quelqu'un peut me dire ce que je fais mal.
Mise à jour
Le problème était à l'intérieur de la méthode PC_Translate_MySQL :: getInstance(). Il y a eu une exception. Et pendant que j'essayais d'obtenir une exception générale, cela a bien sûr passé. Solution n'utilisez pas une exception générale dans la méthode changePassword.
est testFindByAuth passant? Si ce n'est pas le cas, cela ne permet même pas d'exécuter testChangePasswordWrongOldPassword. –
testFindByAuth passe. – tom