4

When I run unit tests with PhpUnit in console all exceptions are display immediately there. However when I moved to functional testing I cannot see exceptions which occur.

First I tried symfony WebTestCase as it was said here. So I called $client with needed parameters and I have a response. But it took time to me to understand that this behaviour is just like I manually open a page I want to test. And a response from $client->request contains text (html). Yes errors are show there but there are pretty much html and it is really hard to find the exception. Yes, I can use $crawler->filter('.text-exception')->first()->text() but I want so that exception was visible like in unit test. When I tested commands exceptions are shown by PhpUnit as well as normal.

I tried to copy code from web/app_dev.php to test case. But it is the same. I have only html response.

So how can I make it so that PhpUnit display exceptions in functional testing like in unit testing?

1 Answer 1

6

You can use profiler to do it:

$client->enableProfiler();
$client->request('GET', '/');
$profile = $client->getProfile();
$exceptionProfile = $profile->getCollector('exception');

if ($exceptionProfile->hasException()) {
    $message = sprintf(
        "No exception was expected but got '%s' with message '%s'. Trace:\n%s",
        get_class($exceptionProfile->getException()),
        $exceptionProfile->getMessage(),
        $exceptionProfile->getException()->getTraceAsString()
    );
    throw new AssertionFailedError($message);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Added. Please see HttpKernel/DataCollector/ExceptionDataCollector.php for more details.

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.