|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\WebProfilerBundle\Tests\Twig; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Bundle\WebProfilerBundle\Twig\WebProfilerExtension; |
| 16 | +use Symfony\Component\VarDumper\Cloner\VarCloner; |
| 17 | +use Twig\Environment; |
| 18 | +use Twig\Extension\CoreExtension; |
| 19 | +use Twig\Extension\EscaperExtension; |
| 20 | + |
| 21 | +class WebProfilerExtensionTest extends TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @dataProvider provideMessages |
| 25 | + */ |
| 26 | + public function testDumpHeaderIsDisplayed(string $message, array $context, bool $dump1HasHeader, bool $dump2HasHeader) |
| 27 | + { |
| 28 | + class_exists(CoreExtension::class); // Load twig_convert_encoding() |
| 29 | + class_exists(EscaperExtension::class); // Load twig_escape_filter() |
| 30 | + |
| 31 | + $twigEnvironment = $this->mockTwigEnvironment(); |
| 32 | + $varCloner = new VarCloner(); |
| 33 | + |
| 34 | + $webProfilerExtension = new WebProfilerExtension(); |
| 35 | + |
| 36 | + $needle = 'window.Sfdump'; |
| 37 | + |
| 38 | + $dump1 = $webProfilerExtension->dumpLog($twigEnvironment, $message, $varCloner->cloneVar($context)); |
| 39 | + self::assertSame($dump1HasHeader, str_contains($dump1, $needle)); |
| 40 | + |
| 41 | + $dump2 = $webProfilerExtension->dumpData($twigEnvironment, $varCloner->cloneVar([])); |
| 42 | + self::assertSame($dump2HasHeader, str_contains($dump2, $needle)); |
| 43 | + } |
| 44 | + |
| 45 | + public function provideMessages(): iterable |
| 46 | + { |
| 47 | + yield ['Some message', ['foo' => 'foo', 'bar' => 'bar'], false, true]; |
| 48 | + yield ['Some message {@see some text}', ['foo' => 'foo', 'bar' => 'bar'], false, true]; |
| 49 | + yield ['Some message {foo}', ['foo' => 'foo', 'bar' => 'bar'], true, false]; |
| 50 | + yield ['Some message {foo}', ['bar' => 'bar'], false, true]; |
| 51 | + } |
| 52 | + |
| 53 | + private function mockTwigEnvironment() |
| 54 | + { |
| 55 | + $twigEnvironment = $this->createMock(Environment::class); |
| 56 | + |
| 57 | + $twigEnvironment->expects($this->any())->method('getCharset')->willReturn('UTF-8'); |
| 58 | + |
| 59 | + return $twigEnvironment; |
| 60 | + } |
| 61 | +} |
0 commit comments