8

I suspect PHPUnit is showing that 1 line of code is not covered by unit tests because of exceptions thats thrown (but I caught)

I have unit test that should cover that line

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

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

}

Why is my code coverage still reflecting that is not covered?

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

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

Is PHPUnit skipping all execution once an exception is thrown?

UPDATE: I got a feeling that I am using @expectedException wrong tho ...

2
  • Is your "login" method inside a namespace? Commented Dec 19, 2010 at 10:01
  • @Anti Veeranna, Yes it is in Application\Models. Does it being in a namespace change anything? Commented Dec 19, 2010 at 11:54

2 Answers 2

4

Your code samples are just the tip of the iceberg, its hard to pinpoint the exact problem.

But one detail seems suspicious to me: given that your login method is in the Application\Models then, the following code

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

will not catch any exception, it would catch \Application\Models\Exception - if you even have such a class defined.

Maybe that is the reason why your exception handler does not run.

Sign up to request clarification or add additional context in comments.

1 Comment

Unless I'm mistaken, the solution is to change the exception class to \Exception so it catches all exception types.
2

The @expectedException Annotation is similar to this testcode:

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

so you can not (should not) test two things at once in that Testcase. (If the exception is thrown AND the return value)

If you want to test that User::login throws an exception you are good to go for that testcase and the assertion isn't needed (that code isn't executed anyways :) )

To get the red line covered you'd need to write code so $query->getSingleResult() throws an exception. That could be tricky but since i don't see enough of the source (like where the query object is coming from) i can't be specific here.

If the $query object is a mock let it throw an exception on ->getSingleResult and write testcase checking for "null"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.