Sébastien Sébastien - 3 months ago 14
PHP Question

symfony2 phpunit : how to get exception message when status code is not expected one

With my symfony 2 application, i am using phpunit to test if each controller's action response's status code is the expected one.

When not, how can I have phpunit display the error message that comes with the exception, or best, simulate a profiler exception page ?

The reason for this is I have an action returning a 500 code in phpunit but which loads just fine in my browser.

My code :

/**
* @dataProvider urlProvider
* @param $url
*/
public function testPageIsSuccessful($url)
{
$client = self::createClient(array(), array(
'PHP_AUTH_USER' => 'superadmin',
'PHP_AUTH_PW' => '587010',
));
$client->followRedirects();
$client->request('GET', $url);
var_dump($url);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}


Thanks !

A.L A.L
Answer

You can get the rendered HTML in your test with the following code:

die($client->getResponse()->getContent());

You have to put this just before the failing test and PHPUnit will display the HTML code. It's not convenient but it helped me many times.