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
Changes according to reviews
  • Loading branch information
javiereguiluz committed Oct 9, 2024
commit a043bf79708e023c492e0820d259e05e03792093
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(
?ErrorRendererInterface $fallbackErrorRenderer = null,
bool|callable $debug = false,
) {
$this->fallbackErrorRenderer = \in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) ? new CliErrorRenderer() : ($fallbackErrorRenderer ?? new HtmlErrorRenderer());
$this->fallbackErrorRenderer = $fallbackErrorRenderer ?? new HtmlErrorRenderer();
$this->debug = \is_bool($debug) ? $debug : $debug(...);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@
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;
use Twig\Environment;
use Twig\Loader\ArrayLoader;

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

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

(new TwigErrorRenderer($twig, $nativeRenderer, true))->render(new \Exception());
$customRenderer = new class implements ErrorRendererInterface {
public function render(\Throwable $exception): FlattenException
{
return 'This is a custom error renderer.';
}
};

$this->assertSame('This is a custom error renderer.', (new TwigErrorRenderer($twig, $customRenderer, true))->render(new \Exception()));
}

public function testCliRenderer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function load(array $configs, ContainerBuilder $container): void
{
$loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('twig.php');
$loader->load('twig_test.php');

if (method_exists(Environment::class, 'resetGlobals')) {
$container->getDefinition('twig')->addTag('kernel.reset', ['method' => 'resetGlobals']);
Expand Down
29 changes: 29 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Bridge\Twig\ErrorRenderer\TwigErrorRenderer;
use Symfony\Component\ErrorHandler\ErrorRenderer\CliErrorRenderer;

return static function (ContainerConfigurator $container) {
$container->services()
->set('twig.error_renderer.html', TwigErrorRenderer::class)
->decorate('error_renderer.html')
->args([
service('twig'),
inline_service(CliErrorRenderer::class),
inline_service('bool')
->factory([TwigErrorRenderer::class, 'isDebug'])
->args([service('request_stack'), param('kernel.debug')]),
])
;
};
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