Skip to content

Commit bba954a

Browse files
committed
feat(twig): using FQCN with test helpers
1 parent d5df312 commit bba954a

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

src/TwigComponent/doc/index.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ You can test how your component is mounted and rendered using the
948948
public function testComponentMount(): void
949949
{
950950
$component = $this->mountTwigComponent(
951-
name: 'MyComponent',
951+
name: 'MyComponent', // can also use FQCN (MyComponent::class)
952952
data: ['foo' => 'bar'],
953953
);
954954

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

@@ -969,7 +969,7 @@ You can test how your component is mounted and rendered using the
969969
public function testEmbeddedComponentRenders(): void
970970
{
971971
$rendered = $this->renderTwigComponent(
972-
name: 'MyComponent',
972+
name: 'MyComponent', // can also use FQCN (MyComponent::class)
973973
data: ['foo' => 'bar'],
974974
content: '<div>My content</div>', // "content" (default) block
975975
blocks: [

src/TwigComponent/src/Test/InteractsWithTwigComponents.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ protected function renderTwigComponent(string $name, array $data = [], ?string $
3636
throw new \LogicException(sprintf('The "%s" trait can only be used on "%s" classes.', __TRAIT__, KernelTestCase::class));
3737
}
3838

39+
if (class_exists($name)) {
40+
$name = static::getContainer()->get('ux.twig_component.component_factory')->metadataFor($name)->getName();
41+
}
42+
3943
$blocks = array_filter(array_merge($blocks, ['content' => $content]));
4044

4145
if (!$blocks) {

src/TwigComponent/tests/Integration/Test/InteractsWithTwigComponentsTest.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@
1414
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1515
use Symfony\UX\TwigComponent\Test\InteractsWithTwigComponents;
1616
use Symfony\UX\TwigComponent\Tests\Fixtures\Component\ComponentA;
17+
use Symfony\UX\TwigComponent\Tests\Fixtures\Component\WithSlots;
1718
use Symfony\UX\TwigComponent\Tests\Fixtures\Service\ServiceA;
1819

1920
final class InteractsWithTwigComponentsTest extends KernelTestCase
2021
{
2122
use InteractsWithTwigComponents;
2223

23-
public function testCanMountComponent(): void
24+
/**
25+
* @dataProvider componentANameProvider
26+
*/
27+
public function testCanMountComponent(string $name): void
2428
{
25-
$component = $this->mountTwigComponent('component_a', [
29+
$component = $this->mountTwigComponent($name, [
2630
'propA' => 'prop a value',
2731
'propB' => 'prop b value',
2832
]);
@@ -33,9 +37,12 @@ public function testCanMountComponent(): void
3337
$this->assertSame('prop b value', $component->getPropB());
3438
}
3539

36-
public function testCanRenderComponent(): void
40+
/**
41+
* @dataProvider componentANameProvider
42+
*/
43+
public function testCanRenderComponent(string $name): void
3744
{
38-
$rendered = $this->renderTwigComponent('component_a', [
45+
$rendered = $this->renderTwigComponent($name, [
3946
'propA' => 'prop a value',
4047
'propB' => 'prop b value',
4148
]);
@@ -45,10 +52,13 @@ public function testCanRenderComponent(): void
4552
$this->assertStringContainsString('service: service a value', $rendered);
4653
}
4754

48-
public function testCanRenderComponentWithSlots(): void
55+
/**
56+
* @dataProvider withSlotsNameProvider
57+
*/
58+
public function testCanRenderComponentWithSlots(string $name): void
4959
{
5060
$rendered = $this->renderTwigComponent(
51-
name: 'WithSlots',
61+
name: $name,
5262
content: '<p>some content</p>',
5363
blocks: [
5464
'slot1' => '<p>some slot1 content</p>',
@@ -65,4 +75,16 @@ public function testCanRenderComponentWithSlots(): void
6575
$this->assertStringContainsString('propB: prop b value', $rendered);
6676
$this->assertStringContainsString('service: service a value', $rendered);
6777
}
78+
79+
public static function componentANameProvider(): iterable
80+
{
81+
yield ['component_a'];
82+
yield [ComponentA::class];
83+
}
84+
85+
public static function withSlotsNameProvider(): iterable
86+
{
87+
yield ['WithSlots'];
88+
yield [WithSlots::class];
89+
}
6890
}

0 commit comments

Comments
 (0)