I'm writing some unit test with phpUnit to test a Zend Framework application and I've got some issues with testing an exception in the changePassword function. The test doesn't fail, but in the coverage tool which generates html the "throw new Exception($tr->translate('userOldPasswordIncorrect'));" line isn't 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();
}
The unit test which should test the 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());
}
I'll hope somebody can tell me what I'm doing wrong.
Update
The problem was inside the PC_Translate_MySQL::getInstance() method. There was thrown an exception. And as I was testing on getting a general exception this ofcourse passed. Solution don't use a general Exception in the changePassword method.