Skip to content

Instantly share code, notes, and snippets.

@mrself
Last active March 29, 2018 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrself/5a9e5b5ed35e4b6178f15d86058a74e3 to your computer and use it in GitHub Desktop.
Save mrself/5a9e5b5ed35e4b6178f15d86058a74e3 to your computer and use it in GitHub Desktop.
Track errors in symfony when testing with phpunit when using WebTestCase
public function testSmth() {
$this->client = static::createClient();
$crawler = $this->client->request('GET', '/my-test-page');
if ($this->client->getResponse()->getStatusCode() !== 200) {
dump($crawler->filter('.text-exception')->first()->text());
dump($crawler->filter('.traces > li')->eq(0)->text());
dump($crawler->filter('.traces > li')->eq(1)->text());
throw new \Exception('Request to /my-test-page was not successfull');
}
public function testSmth() {
$this->client = static::createClient();
$crawler = $this->client->request('GET', '/my-test-page');
if ($this->client->getResponse()->getStatusCode() !== 200) {
$profile = $this->client->getProfile();
$exceptionProfile = $profile->getCollector('exception');
$trace = $exceptionProfile->getTrace();
$trace = array_slice($trace, 0, 5);
$trace = array_map(function($i) {
unset($i['short_class']);
unset($i['namespace']);
if ($i['file']) {
$root = $this->container->getParameter('kernel.root_dir');
$root = str_replace('app', '', $root);
$i['file'] = str_replace($root, '', $i['file']);
unset($i['class']);
$i['file'] .= $i['type'] . $i['function'] . ':' . $i['line'];
unset($i['line']);
unset($i['type']);
unset($i['function']);
}
return $i;
}, $trace);
dump($trace);
throw new \Exception($exceptionProfile->getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment