PHPUnit code coverage and exceptions

I suspect PHPUnit shows that 1 line of code does not apply to unit tests due to exceptions that were thrown (but I caught)

I have a unit test that should cover this line

/**
 * @expectedException Doctrine\ORM\NoResultException
 */
public function testCannotLoginInvalidUser() {

  $user = User::login($this->em, 'nonExistant', 'password');
  $this->assertNull($user);

}

Why does my code coverage still reflect what is not covered?

I did a test ... added echo b4, returning null ... I found that this line really is not covered ...

try {
  $user = $query->getSingleResult();
} catch (Exception $e) {
  echo 'caught exception';  <-- this does not get executed. 
  return null;
}

Is PHPUnit skipping all execution after throwing an exception?

The UPDATE . I got the feeling that I'm using the @expectedExceptionwrong tho ...

+5
source share
2 answers

- , .

: , \,

try {
  $user = $query->getSingleResult();
} catch (Exception $e) {

- , catch\Application\Models\Exception - .

, .

+4

@expectedException :

public function testDoStuff() {
    try { 
        doStuff();
    } catch(Exception $e) {
        // Test passed
        return;
    }
    $this->fail("Exception not thrown, test failed !");
}

( ) . ( )

, User::login , , ( :))

get the red line covered , $query- > getSingleResult() . , (, ), .

$query , → getSingleResult "null"

+2

All Articles