-
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
246 additions
and
3 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
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 %%}', $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,27 @@ | ||
<?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 | ||
{ | ||
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,13 @@ | ||
<?php | ||
|
||
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> |
68 changes: 68 additions & 0 deletions
68
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,68 @@ | ||
<?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\Service\ServiceA; | ||
|
||
final class InteractsWithTwigComponentsTest extends KernelTestCase | ||
{ | ||
use InteractsWithTwigComponents; | ||
|
||
public function testCanMountComponent(): void | ||
{ | ||
$component = $this->mountTwigComponent('component_a', [ | ||
'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()); | ||
} | ||
|
||
public function testCanRenderComponent(): void | ||
{ | ||
$rendered = $this->renderTwigComponent('component_a', [ | ||
'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); | ||
} | ||
|
||
public function testCanRenderComponentWithSlots(): void | ||
{ | ||
$rendered = $this->renderTwigComponent( | ||
name: 'WithSlots', | ||
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); | ||
} | ||
} |