Skip to content

Commit 3ffae76

Browse files
smnandreweaverryan
authored andcommitted
[TwigComponent] Add tests ComponentRenderer should pass
1 parent 5dce994 commit 3ffae76

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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\Fixtures\Component\FooBar;
13+
14+
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
15+
16+
/**
17+
* @author Simon André <[email protected]>
18+
*/
19+
#[AsTwigComponent('FooBar:Baz')]
20+
final class Baz
21+
{
22+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\Fixtures\EventListener;
13+
14+
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
15+
use Symfony\UX\TwigComponent\Event\PreRenderEvent;
16+
17+
/**
18+
* Change the template of "FooBarBaz:" components during PrePrenderEvent.
19+
* Before:
20+
* FooBar:Baz -> FooBar/Baz.html.twig
21+
* After:
22+
* FooBar:Baz -> FooBar/Baz.foo_bar.html.twig
23+
*
24+
* @see PreRenderEvent
25+
*/
26+
class FooBarComponentTemplateListener
27+
{
28+
#[AsEventListener]
29+
public function onPreRender(PreRenderEvent $event): void
30+
{
31+
$metadata = $event->getMetadata();
32+
if (!str_starts_with($metadata->getName(), 'FooBar:')) {
33+
return;
34+
}
35+
36+
if (!str_ends_with($event->getTemplate(), '.html.twig')) {
37+
return;
38+
}
39+
40+
$template = substr($event->getTemplate(), 0, -strlen('.html.twig')).'.foo_bar.html.twig';
41+
$event->setTemplate($template);
42+
}
43+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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;
13+
14+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
15+
use Twig\Environment;
16+
use Twig\Loader\ArrayLoader;
17+
18+
/**
19+
* The template can be updated by PreRender event listeners.
20+
*
21+
* @author Simon André <[email protected]>
22+
*/
23+
final class ComponentEventTest extends KernelTestCase
24+
{
25+
/**
26+
* @dataProvider provideFooBarSyntaxes
27+
*/
28+
public function testTemplateIsUpdatedByEventListener(string $syntax): void
29+
{
30+
/** @var Environment $environment */
31+
$environment = self::getContainer()->get(Environment::class);
32+
$environment->setLoader(new ArrayLoader([
33+
'components/FooBar/Baz.foo_bar.html.twig' => 'updated',
34+
'components/FooBar/Baz.html.twig' => 'original',
35+
]));
36+
37+
$component = $environment->createTemplate($syntax);
38+
$result = $component->render();
39+
40+
self::assertSame('updated', $result);
41+
}
42+
43+
public static function provideFooBarSyntaxes(): iterable
44+
{
45+
yield 'TWIG component tag' => ['{% component "FooBar:Baz" %}{% endcomponent %}'];
46+
yield 'TWIG component function' => ['{{ component("FooBar:Baz") }}'];
47+
yield 'HTML self-closing tag' => ['<twig:FooBar:Baz />'];
48+
yield 'HTML open-close tag' => ['<twig:FooBar:Baz></twig:FooBar:Baz>'];
49+
}
50+
}

0 commit comments

Comments
 (0)