HtmlErrorRendererTest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\ErrorHandler\Tests\ErrorRenderer;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
  13. class HtmlErrorRendererTest extends TestCase
  14. {
  15. /**
  16. * @dataProvider getRenderData
  17. */
  18. public function testRender(\Throwable $exception, HtmlErrorRenderer $errorRenderer, string $expected)
  19. {
  20. $this->assertStringMatchesFormat($expected, $errorRenderer->render($exception)->getAsString());
  21. }
  22. public static function getRenderData(): iterable
  23. {
  24. $expectedDebug = <<<HTML
  25. <!-- Foo (500 Internal Server Error) -->
  26. <!DOCTYPE html>
  27. <html lang="en">
  28. %A<title>Foo (500 Internal Server Error)</title>
  29. %A<div class="trace trace-as-html" id="trace-box-1">%A
  30. <!-- Foo (500 Internal Server Error) -->
  31. HTML;
  32. $expectedNonDebug = <<<HTML
  33. <!DOCTYPE html>
  34. <html lang="en">
  35. %A<title>An Error Occurred: Internal Server Error</title>
  36. %A<h2>The server returned a "500 Internal Server Error".</h2>%A
  37. HTML;
  38. yield '->render() returns the HTML content WITH stack traces in debug mode' => [
  39. new \RuntimeException('Foo'),
  40. new HtmlErrorRenderer(true),
  41. $expectedDebug,
  42. ];
  43. yield '->render() returns the HTML content WITHOUT stack traces in non-debug mode' => [
  44. new \RuntimeException('Foo'),
  45. new HtmlErrorRenderer(false),
  46. $expectedNonDebug,
  47. ];
  48. }
  49. public function testRendersStackWithoutBinaryStrings()
  50. {
  51. if (\PHP_VERSION_ID >= 70400) {
  52. // make sure method arguments are available in stack traces (see https://www.php.net/manual/en/ini.core.php)
  53. ini_set('zend.exception_ignore_args', false);
  54. }
  55. $binaryData = file_get_contents(__DIR__.'/../Fixtures/pixel.png');
  56. $exception = $this->getRuntimeException($binaryData);
  57. $rendered = (new HtmlErrorRenderer(true))->render($exception)->getAsString();
  58. $this->assertStringContainsString(
  59. "buildRuntimeException('FooException')",
  60. $rendered,
  61. '->render() contains the method call with "FooException"'
  62. );
  63. $this->assertStringContainsString(
  64. 'getRuntimeException(binary string)',
  65. $rendered,
  66. '->render() contains the method call with "binary string" replacement'
  67. );
  68. $this->assertStringContainsString(
  69. '<em>binary string</em>',
  70. $rendered,
  71. '->render() returns the HTML content with "binary string" replacement'
  72. );
  73. }
  74. private function getRuntimeException(string $unusedArgument): \RuntimeException
  75. {
  76. return $this->buildRuntimeException('FooException');
  77. }
  78. private function buildRuntimeException(string $message): \RuntimeException
  79. {
  80. return new \RuntimeException($message);
  81. }
  82. }