Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make symfony display exceptions in functional testing with phpunit

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?

like image 257
FreeLightman Avatar asked Oct 17 '25 07:10

FreeLightman


1 Answers

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);
}
like image 64
Aleksander Wons Avatar answered Oct 21 '25 17:10

Aleksander Wons