|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\UX\TwigComponent\Tests\Integration\Twig; |
| 13 | + |
| 14 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 15 | +use Twig\Environment; |
| 16 | +use Twig\Loader\ArrayLoader; |
| 17 | +use Twig\TemplateWrapper; |
| 18 | + |
| 19 | +/** |
| 20 | + * The "component" tag must accept any valid "name" argument (in Twig & HTML syntax). |
| 21 | + * |
| 22 | + * @author Simon André <smn.andre@gmail.com> |
| 23 | + */ |
| 24 | +final class ComponentParserTest extends KernelTestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @dataProvider provideValidComponentNames |
| 28 | + */ |
| 29 | + public function testAcceptTwigComponentTagWithValidComponentName(string $name): void |
| 30 | + { |
| 31 | + $environment = self::createEnvironment(); |
| 32 | + $source = str_replace('XXX', $name, "{% component 'XXX' %}{% endcomponent %}"); |
| 33 | + |
| 34 | + $template = $environment->createTemplate($source); |
| 35 | + |
| 36 | + self::assertInstanceOf(TemplateWrapper::class, $template); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @dataProvider provideValidComponentNames |
| 41 | + */ |
| 42 | + public function testAcceptHtmlComponentTagWithValidComponentName(string $name): void |
| 43 | + { |
| 44 | + $environment = self::createEnvironment(); |
| 45 | + $source = sprintf('<twig:%s></twig:%s>', $name, $name); |
| 46 | + |
| 47 | + $template = $environment->createTemplate($source); |
| 48 | + |
| 49 | + self::assertInstanceOf(TemplateWrapper::class, $template); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @dataProvider provideValidComponentNames |
| 54 | + */ |
| 55 | + public function testAcceptHtmlSelfClosingComponentTagWithValidComponentName(string $name): void |
| 56 | + { |
| 57 | + $environment = self::createEnvironment(); |
| 58 | + $source = sprintf('<twig:%s />', $name); |
| 59 | + |
| 60 | + $template = $environment->createTemplate($source); |
| 61 | + |
| 62 | + self::assertInstanceOf(TemplateWrapper::class, $template); |
| 63 | + } |
| 64 | + |
| 65 | + public static function provideValidComponentNames(): iterable |
| 66 | + { |
| 67 | + // Those names are all syntactically valid even if |
| 68 | + // they do not match any component class or template |
| 69 | + $names = [ |
| 70 | + 'Nope', |
| 71 | + 'NopeNope', |
| 72 | + 'Nope:Nope', |
| 73 | + 'Nope6', |
| 74 | + ]; |
| 75 | + |
| 76 | + foreach ($names as $name) { |
| 77 | + yield $name => [$name]; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private function createEnvironment(): Environment |
| 82 | + { |
| 83 | + /** @var Environment $environment */ |
| 84 | + $environment = self::getContainer()->get(Environment::class); |
| 85 | + $environment->setLoader(new ArrayLoader()); |
| 86 | + |
| 87 | + return $environment; |
| 88 | + } |
| 89 | +} |
0 commit comments