I am writing a few unit test with phpUnit to test a Zend Framework application, and I have some problems checking exception in changePassword function. The test is not interrupted, but in the coverage tool that generates html, "throw new Exception ($ tr-> translate ('userOldPasswordIncorrect')); the line is not tested.
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();
}
unit test, which should test the exception:
public function testChangePasswordWrongOldPassword()
{
$this->_dummyUser = $this->_user->findByAuth($this->_dummyEmail, $this->_dummyPassword, $this->_reseller);
$data['oldPassword'] = 'wrongOldPassword';
$data['password'] = $this->_dummyNewPassword;
$this->_user->changePassword($data, $this->_dummyUser->getId());
}
I hope someone can tell me what I'm doing wrong.
Update
The problem was with the PC_Translate_MySQL :: getInstance () method. The exception has disappeared. And when I tested getting a general exception, this course went through. The solution does not use the general exception in the changePassword method.