Skip to content

Commit

Permalink
feat(twig): using FQCN with test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Apr 28, 2023
1 parent d5df312 commit b4f7084
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/TwigComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ You can test how your component is mounted and rendered using the
public function testComponentMount(): void
{
$component = $this->mountTwigComponent(
name: 'MyComponent',
name: 'MyComponent', // can also use FQCN (MyComponent::class)
data: ['foo' => 'bar'],
);

Expand All @@ -959,7 +959,7 @@ You can test how your component is mounted and rendered using the
public function testComponentRenders(): void
{
$rendered = $this->renderTwigComponent(
name: 'MyComponent',
name: 'MyComponent', // can also use FQCN (MyComponent::class)
data: ['foo' => 'bar'],
);

Expand All @@ -969,7 +969,7 @@ You can test how your component is mounted and rendered using the
public function testEmbeddedComponentRenders(): void
{
$rendered = $this->renderTwigComponent(
name: 'MyComponent',
name: 'MyComponent', // can also use FQCN (MyComponent::class)
data: ['foo' => 'bar'],
content: '<div>My content</div>', // "content" (default) block
blocks: [
Expand Down
2 changes: 1 addition & 1 deletion src/TwigComponent/src/Test/InteractsWithTwigComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function renderTwigComponent(string $name, array $data = [], ?string $
);
}

$template = sprintf('{%% component %s with data %%}', $name);
$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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
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;

public function testCanMountComponent(): void
/**
* @dataProvider componentANameProvider
*/
public function testCanMountComponent(string $name): void
{
$component = $this->mountTwigComponent('component_a', [
$component = $this->mountTwigComponent($name, [
'propA' => 'prop a value',
'propB' => 'prop b value',
]);
Expand All @@ -33,9 +37,12 @@ public function testCanMountComponent(): void
$this->assertSame('prop b value', $component->getPropB());
}

public function testCanRenderComponent(): void
/**
* @dataProvider componentANameProvider
*/
public function testCanRenderComponent(string $name): void
{
$rendered = $this->renderTwigComponent('component_a', [
$rendered = $this->renderTwigComponent($name, [
'propA' => 'prop a value',
'propB' => 'prop b value',
]);
Expand All @@ -45,10 +52,13 @@ public function testCanRenderComponent(): void
$this->assertStringContainsString('service: service a value', $rendered);
}

public function testCanRenderComponentWithSlots(): void
/**
* @dataProvider withSlotsNameProvider
*/
public function testCanRenderComponentWithSlots(string $name): void
{
$rendered = $this->renderTwigComponent(
name: 'WithSlots',
name: $name,
content: '<p>some content</p>',
blocks: [
'slot1' => '<p>some slot1 content</p>',
Expand All @@ -65,4 +75,16 @@ public function testCanRenderComponentWithSlots(): void
$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];
}
}

0 comments on commit b4f7084

Please sign in to comment.