Skip to content

[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 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/TwigComponent/tests/Fixtures/Component/FooBar/Baz.php
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
{
}
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
Copy link
Member

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?

Copy link
Member Author

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

{
#[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 src/TwigComponent/tests/Integration/ComponentEventTest.php
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>'];
}
}