Exception not checked with phpUnit?

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:

/**
 * @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 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.

+3
1

? PC_Translate_MySQL::getInstance()...

Exception. , . changePassword, . , InvalidArgumentException RuntimeException. .

.

try {
} catch (DatabaseQueryException $e) {
    // Handle database error
} catch (DatabaseConnectionException $e) {
    // We never even connected...
} catch (InvalidArgumentException $e) {
    //...
}

, , catch (Exception $e) . , . ( , , , 500 )...

+4

Source: https://habr.com/ru/post/1759384/


All Articles