-
-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minor #1237 [TwigComponent] Add tests ComponentRenderer should pass (…
…smnandre) This PR was squashed before being merged into the 2.x branch. Discussion ---------- [TwigComponent] Add tests ComponentRenderer should pass As written in the documentation, it is possible to modify the template during the PreRenderEvent, so those tests should pass. Commits ------- 3ffae76 [TwigComponent] Add tests ComponentRenderer should pass
- Loading branch information
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\TwigComponent\Tests\Fixtures\Component\FooBar; | ||
|
||
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent; | ||
|
||
/** | ||
* @author Simon André <[email protected]> | ||
*/ | ||
#[AsTwigComponent('FooBar:Baz')] | ||
final class Baz | ||
{ | ||
} |
43 changes: 43 additions & 0 deletions
43
src/TwigComponent/tests/Fixtures/EventListener/FooBarComponentTemplateListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\TwigComponent\Tests\Fixtures\EventListener; | ||
|
||
use Symfony\Component\EventDispatcher\Attribute\AsEventListener; | ||
use Symfony\UX\TwigComponent\Event\PreRenderEvent; | ||
|
||
/** | ||
* Change the template of "FooBarBaz:" components during PrePrenderEvent. | ||
* Before: | ||
* FooBar:Baz -> FooBar/Baz.html.twig | ||
* After: | ||
* FooBar:Baz -> FooBar/Baz.foo_bar.html.twig | ||
* | ||
* @see PreRenderEvent | ||
*/ | ||
class FooBarComponentTemplateListener | ||
{ | ||
#[AsEventListener] | ||
public function onPreRender(PreRenderEvent $event): void | ||
{ | ||
$metadata = $event->getMetadata(); | ||
if (!str_starts_with($metadata->getName(), 'FooBar:')) { | ||
return; | ||
} | ||
|
||
if (!str_ends_with($event->getTemplate(), '.html.twig')) { | ||
return; | ||
} | ||
|
||
$template = substr($event->getTemplate(), 0, -strlen('.html.twig')).'.foo_bar.html.twig'; | ||
$event->setTemplate($template); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/TwigComponent/tests/Integration/ComponentEventTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\TwigComponent\Tests\Integration; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Twig\Environment; | ||
use Twig\Loader\ArrayLoader; | ||
|
||
/** | ||
* The template can be updated by PreRender event listeners. | ||
* | ||
* @author Simon André <[email protected]> | ||
*/ | ||
final class ComponentEventTest extends KernelTestCase | ||
{ | ||
/** | ||
* @dataProvider provideFooBarSyntaxes | ||
*/ | ||
public function testTemplateIsUpdatedByEventListener(string $syntax): void | ||
{ | ||
/** @var Environment $environment */ | ||
$environment = self::getContainer()->get(Environment::class); | ||
$environment->setLoader(new ArrayLoader([ | ||
'components/FooBar/Baz.foo_bar.html.twig' => 'updated', | ||
'components/FooBar/Baz.html.twig' => 'original', | ||
])); | ||
|
||
$component = $environment->createTemplate($syntax); | ||
$result = $component->render(); | ||
|
||
self::assertSame('updated', $result); | ||
} | ||
|
||
public static function provideFooBarSyntaxes(): iterable | ||
{ | ||
yield 'TWIG component tag' => ['{% component "FooBar:Baz" %}{% endcomponent %}']; | ||
yield 'TWIG component function' => ['{{ component("FooBar:Baz") }}']; | ||
yield 'HTML self-closing tag' => ['<twig:FooBar:Baz />']; | ||
yield 'HTML open-close tag' => ['<twig:FooBar:Baz></twig:FooBar:Baz>']; | ||
} | ||
} |