-
-
Notifications
You must be signed in to change notification settings - Fork 364
[TwigComponent] Add tests ComponentRenderer should pass #1237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 <fabien@symfony.com> | ||
* | ||
* 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é <smn.andre@gmail.com> | ||
*/ | ||
#[AsTwigComponent('FooBar:Baz')] | ||
final class Baz | ||
{ | ||
} |
43 changes: 43 additions & 0 deletions
43
src/TwigComponent/tests/Fixtures/EventListener/FooBarComponentTemplateListener.php
This file contains hidden or 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 <fabien@symfony.com> | ||
* | ||
* 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 hidden or 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 <fabien@symfony.com> | ||
* | ||
* 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é <smn.andre@gmail.com> | ||
*/ | ||
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>']; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this registered in the test kernel and being used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is registered (the test kernel has autowire+autoconfigure for the entire namespace) and it is used
There are 4 data provided / tested in src/TwigComponent/tests/Integration/ComponentEventTest.php and only two fail because the FooBarComponentTemplateListener is called