Skip to content

Commit 1579b40

Browse files
committed
Add tests ComponentParser should pass
1 parent 262db2b commit 1579b40

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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\UX\TwigComponent\Tests\Integration\Twig;
13+
14+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
15+
use Twig\Environment;
16+
use Twig\Error\SyntaxError;
17+
use Twig\Loader\ArrayLoader;
18+
use Twig\TemplateWrapper;
19+
20+
/**
21+
* The "component" tag must accept any valid "name" argument (in Twig & HTML syntax).
22+
*
23+
* @author Simon André <[email protected]>
24+
*/
25+
final class ComponentParserTest extends KernelTestCase
26+
{
27+
/**
28+
* @dataProvider provideValidComponentNames
29+
*/
30+
public function testAcceptTwigComponentTagWithValidComponentName(string $name): void
31+
{
32+
$environment = self::createEnvironment();
33+
$source = str_replace('XXX', $name, "{% component 'XXX' %}{% endcomponent %}");
34+
35+
$template = $environment->createTemplate($source);
36+
37+
self::assertInstanceOf(TemplateWrapper::class, $template);
38+
}
39+
40+
/**
41+
* @dataProvider provideValidComponentNames
42+
*/
43+
public function testAcceptHtmlComponentTagWithValidComponentName(string $name): void
44+
{
45+
$environment = self::createEnvironment();
46+
$source = sprintf('<twig:%s></twig:%s>', $name, $name);
47+
48+
$template = $environment->createTemplate($source);
49+
50+
self::assertInstanceOf(TemplateWrapper::class, $template);
51+
}
52+
53+
/**
54+
* @dataProvider provideValidComponentNames
55+
*/
56+
public function testAcceptHtmlSelfClosingComponentTagWithValidComponentName(string $name): void
57+
{
58+
$environment = self::createEnvironment();
59+
$source = sprintf('<twig:%s />', $name);
60+
61+
$template = $environment->createTemplate($source);
62+
63+
self::assertInstanceOf(TemplateWrapper::class, $template);
64+
}
65+
66+
/**
67+
* @dataProvider provideInvalidComponentNames
68+
*/
69+
public function testRejectTwigComponentTagWithInvalidComponentName(string $name): void
70+
{
71+
$environment = self::createEnvironment();
72+
$source = str_replace('XXX', $name, "{% component 'XXX' %}{% endcomponent %}");
73+
74+
self::expectException(SyntaxError::class);
75+
76+
$template = $environment->createTemplate($source);
77+
}
78+
79+
/**
80+
* @dataProvider provideInvalidComponentNames
81+
*/
82+
public function testRejectHtmlComponentTagWithInvalidComponentName(string $name): void
83+
{
84+
$environment = self::createEnvironment();
85+
$source = sprintf('<twig:%s></twig:%s>', $name, $name);
86+
87+
self::expectException(SyntaxError::class);
88+
89+
$template = $environment->createTemplate($source);
90+
}
91+
92+
/**
93+
* @dataProvider provideInvalidComponentNames
94+
*/
95+
public function testRejectHtmlSelfClosingComponentTagWithInvalidComponentName(string $name): void
96+
{
97+
$environment = self::createEnvironment();
98+
$source = sprintf('<twig:%s />', $name);
99+
100+
self::expectException(SyntaxError::class);
101+
102+
$template = $environment->createTemplate($source);
103+
}
104+
105+
public static function provideValidComponentNames(): iterable
106+
{
107+
// List of components that do not exist
108+
$names = [
109+
'Nope',
110+
'NopeNope',
111+
'Nope:Nope',
112+
'Nope6',
113+
];
114+
115+
foreach ($names as $name) {
116+
yield $name => [$name];
117+
}
118+
}
119+
120+
public static function provideInvalidComponentNames(): iterable
121+
{
122+
// TODO add expression syntaxes (?)
123+
// @see Twig\TokenParser\UseTokenParser
124+
$names = [
125+
'',
126+
' ',
127+
];
128+
129+
foreach ($names as $name) {
130+
yield $name => [$name];
131+
}
132+
}
133+
134+
private function createEnvironment(): Environment
135+
{
136+
/** @var Environment $environment */
137+
$environment = self::getContainer()->get(Environment::class);
138+
$environment->setLoader(new ArrayLoader());
139+
140+
return $environment;
141+
}
142+
}

0 commit comments

Comments
 (0)