Skip to content

[TwigBridge] Improve error rendering when running tests #58456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Prev Previous commit
Next Next commit
Update tests
  • Loading branch information
javiereguiluz committed Oct 9, 2024
commit fc8cbe15795fc2374976c28279b54455287c4959
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\ErrorRenderer\TwigErrorRenderer;
use Symfony\Component\ErrorHandler\ErrorRenderer\CliErrorRenderer;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Expand All @@ -21,36 +22,30 @@

class TwigErrorRendererTest extends TestCase
{
public function testFallbackToNativeRendererIfDebugOn()
public function testDontUseNativeRenderInCliContext()
{
$exception = new \Exception();

$twig = $this->createMock(Environment::class);
$nativeRenderer = $this->createMock(HtmlErrorRenderer::class);
$nativeRenderer = $this->createMock(CliErrorRenderer::class);
$nativeRenderer
->expects($this->once())
->expects($this->never())
->method('render')
->with($exception)
;

(new TwigErrorRenderer($twig, $nativeRenderer, true))->render(new \Exception());
}

public function testFallbackToNativeRendererIfCustomTemplateNotFound()
public function testCliRenderer()
{
$exception = new NotFoundHttpException();

$twig = new Environment(new ArrayLoader([]));

$nativeRenderer = $this->createMock(HtmlErrorRenderer::class);
$nativeRenderer
->expects($this->once())
->method('render')
->with($exception)
->willReturn(FlattenException::createFromThrowable($exception))
;
$exception = (new TwigErrorRenderer($twig, null, false))->render($exception);

(new TwigErrorRenderer($twig, $nativeRenderer, false))->render($exception);
$this->assertSame('text/plain', $exception->getHeaders()['Content-Type'], 'The exception does not return HTML contents (to prevent potential XSS vulnerabilities)');
$this->assertStringContainsString('0;38;5;208m', $exception->getAsString(), 'The exception includes the escape sequence for CLI colorized output');
}

public function testRenderCustomErrorTemplate()
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy