-
-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #821 [Twig] add test helper (kbond)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Twig] add test helper | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Tickets | Fix #818 | License | MIT _This works with Live components out of the box (not with slots though as they aren't supported)._ ```php use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\UX\TwigComponent\Test\InteractsWithTwigComponents; class MyComponentTest extends KernelTestCase { use InteractsWithTwigComponents; public function testComponentMount(): void { $component = $this->mountTwigComponent( name: 'MyComponent', // can also use FQCN (MyComponent::class) data: ['foo' => 'bar'], ); $this->assertInstanceOf(MyComponent::class, $component); $this->assertSame('bar', $component->foo); } public function testComponentRenders(): void { $rendered = $this->renderTwigComponent( name: 'MyComponent', // can also use FQCN (MyComponent::class) data: ['foo' => 'bar'], ); $this->assertStringContainsString('bar', $rendered); } public function testEmbeddedComponentRenders(): void { $rendered = $this->renderTwigComponent( name: 'MyComponent', // can also use FQCN (MyComponent::class) data: ['foo' => 'bar'], content: '<div>My content</div>', // "content" (default) block blocks: [ 'header' => '<div>My header</div>', 'menu' => $this->renderTwigComponent('Menu'), // can embed other components ], ); $this->assertStringContainsString('bar', $rendered); } } ``` Commits ------- 0b27ea9 [Twig] add test helper
- Loading branch information
Showing
10 changed files
with
305 additions
and
5 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
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
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
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
67 changes: 67 additions & 0 deletions
67
src/TwigComponent/src/Test/InteractsWithTwigComponents.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,67 @@ | ||
<?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\Test; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
trait InteractsWithTwigComponents | ||
{ | ||
protected function mountTwigComponent(string $name, array $data = []): object | ||
{ | ||
if (!$this instanceof KernelTestCase) { | ||
throw new \LogicException(sprintf('The "%s" trait can only be used on "%s" classes.', __TRAIT__, KernelTestCase::class)); | ||
} | ||
|
||
return static::getContainer()->get('ux.twig_component.component_factory')->create($name, $data)->getComponent(); | ||
} | ||
|
||
/** | ||
* @param array<string,string> $blocks | ||
*/ | ||
protected function renderTwigComponent(string $name, array $data = [], ?string $content = null, array $blocks = []): RenderedComponent | ||
{ | ||
if (!$this instanceof KernelTestCase) { | ||
throw new \LogicException(sprintf('The "%s" trait can only be used on "%s" classes.', __TRAIT__, KernelTestCase::class)); | ||
} | ||
|
||
$blocks = array_filter(array_merge($blocks, ['content' => $content])); | ||
|
||
if (!$blocks) { | ||
return new RenderedComponent(self::getContainer()->get('twig') | ||
->createTemplate('{{ component(name, data) }}') | ||
->render([ | ||
'name' => $name, | ||
'data' => $data, | ||
]) | ||
); | ||
} | ||
|
||
$template = sprintf('{%% component "%s" with data %%}', addslashes($name)); | ||
|
||
foreach (array_keys($blocks) as $blockName) { | ||
$template .= sprintf('{%% block %1$s %%}{{ blocks.%1$s|raw }}{%% endblock %%}', $blockName); | ||
} | ||
|
||
$template .= '{% endcomponent %}'; | ||
|
||
return new RenderedComponent(self::getContainer()->get('twig') | ||
->createTemplate($template) | ||
->render([ | ||
'data' => $data, | ||
'blocks' => $blocks, | ||
]) | ||
); | ||
} | ||
} |
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,30 @@ | ||
<?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\Test; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
final class RenderedComponent implements \Stringable | ||
{ | ||
/** | ||
* @internal | ||
*/ | ||
public function __construct(private string $html) | ||
{ | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->html; | ||
} | ||
} |
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; | ||
|
||
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
#[AsTwigComponent] | ||
final class WithSlots | ||
{ | ||
} |
5 changes: 5 additions & 0 deletions
5
src/TwigComponent/tests/Fixtures/templates/components/WithSlots.html.twig
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,5 @@ | ||
<div> | ||
{% block content %}{% endblock %} | ||
{% block slot1 %}{% endblock %} | ||
{% block slot2 %}{% endblock %} | ||
</div> |
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 |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
use Symfony\UX\TwigComponent\Tests\Fixtures\Component\ComponentA; | ||
use Symfony\UX\TwigComponent\Tests\Fixtures\Component\ComponentB; | ||
use Symfony\UX\TwigComponent\Tests\Fixtures\Component\ComponentC; | ||
use Symfony\UX\TwigComponent\Tests\Fixtures\Component\WithSlots; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
|
@@ -170,6 +171,18 @@ public function testInputPropsStoredOnMountedComponent(): void | |
$this->assertSame(['propA' => 'A', 'propB' => 'B'], $mountedComponent->getInputProps()); | ||
} | ||
|
||
/** | ||
* @doesNotPerformAssertions | ||
*/ | ||
public function testGetComponentWithClassName(): void | ||
{ | ||
$factory = $this->factory(); | ||
|
||
$factory->create(WithSlots::class); | ||
$factory->get(WithSlots::class); | ||
$factory->metadataFor(WithSlots::class); | ||
} | ||
|
||
private function factory(): ComponentFactory | ||
{ | ||
return self::getContainer()->get('ux.twig_component.component_factory'); | ||
|
90 changes: 90 additions & 0 deletions
90
src/TwigComponent/tests/Integration/Test/InteractsWithTwigComponentsTest.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,90 @@ | ||
<?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\Test; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\UX\TwigComponent\Test\InteractsWithTwigComponents; | ||
use Symfony\UX\TwigComponent\Tests\Fixtures\Component\ComponentA; | ||
use Symfony\UX\TwigComponent\Tests\Fixtures\Component\WithSlots; | ||
use Symfony\UX\TwigComponent\Tests\Fixtures\Service\ServiceA; | ||
|
||
final class InteractsWithTwigComponentsTest extends KernelTestCase | ||
{ | ||
use InteractsWithTwigComponents; | ||
|
||
/** | ||
* @dataProvider componentANameProvider | ||
*/ | ||
public function testCanMountComponent(string $name): void | ||
{ | ||
$component = $this->mountTwigComponent($name, [ | ||
'propA' => 'prop a value', | ||
'propB' => 'prop b value', | ||
]); | ||
|
||
$this->assertInstanceof(ComponentA::class, $component); | ||
$this->assertInstanceOf(ServiceA::class, $component->getService()); | ||
$this->assertSame('prop a value', $component->propA); | ||
$this->assertSame('prop b value', $component->getPropB()); | ||
} | ||
|
||
/** | ||
* @dataProvider componentANameProvider | ||
*/ | ||
public function testCanRenderComponent(string $name): void | ||
{ | ||
$rendered = $this->renderTwigComponent($name, [ | ||
'propA' => 'prop a value', | ||
'propB' => 'prop b value', | ||
]); | ||
|
||
$this->assertStringContainsString('propA: prop a value', $rendered); | ||
$this->assertStringContainsString('propB: prop b value', $rendered); | ||
$this->assertStringContainsString('service: service a value', $rendered); | ||
} | ||
|
||
/** | ||
* @dataProvider withSlotsNameProvider | ||
*/ | ||
public function testCanRenderComponentWithSlots(string $name): void | ||
{ | ||
$rendered = $this->renderTwigComponent( | ||
name: $name, | ||
content: '<p>some content</p>', | ||
blocks: [ | ||
'slot1' => '<p>some slot1 content</p>', | ||
'slot2' => $this->renderTwigComponent('component_a', [ | ||
'propA' => 'prop a value', | ||
'propB' => 'prop b value', | ||
]), | ||
], | ||
); | ||
|
||
$this->assertStringContainsString('<p>some content</p>', $rendered); | ||
$this->assertStringContainsString('<p>some slot1 content</p>', $rendered); | ||
$this->assertStringContainsString('propA: prop a value', $rendered); | ||
$this->assertStringContainsString('propB: prop b value', $rendered); | ||
$this->assertStringContainsString('service: service a value', $rendered); | ||
} | ||
|
||
public static function componentANameProvider(): iterable | ||
{ | ||
yield ['component_a']; | ||
yield [ComponentA::class]; | ||
} | ||
|
||
public static function withSlotsNameProvider(): iterable | ||
{ | ||
yield ['WithSlots']; | ||
yield [WithSlots::class]; | ||
} | ||
} |