ShinDarth ShinDarth - 22 days ago 5
Twig Question

Phpunit marks test as "risky" when calling showAction method of ExceptionController

When trying to test a class which calls the

showAction
method of
ExceptionController
(class located at
vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/Tests/Controller/ExceptionControllerTest.php
) it marks as "risky" the test, saying:


Test code or tested code did not (only) close its own output buffers


example code:

$request = Request::create('whatever', 'GET');
$request->attributes->set('showException', false);

$exception = FlattenException::create(new \Exception(), 404);

$controller = new ExceptionController($twig, true);

$response = $controller->showAction($request, $exception, null);

// some assertions....

Answer

in order to solve the problem, the header X-Php-Ob-Level of the request should be set to 1:

$request->headers->set('X-Php-Ob-Level', 1);

example code:

$request = Request::create('whatever', 'GET');
$request->headers->set('X-Php-Ob-Level', 1);
$request->attributes->set('showException', false);

$exception = FlattenException::create(new \Exception(), 404);

$controller = new ExceptionController($twig, true);

$response = $controller->showAction($request, $exception, null);

// some assertions....