-
-
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
1 parent
b2c00e6
commit faa1518
Showing
13 changed files
with
442 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# CHANGELOG | ||
|
||
## 2.11.0 | ||
|
||
- Add helper for testing live components. | ||
|
||
## 2.9.0 | ||
|
||
- Add support for symfony/asset-mapper | ||
|
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
46 changes: 46 additions & 0 deletions
46
src/LiveComponent/src/Test/InteractsWithLiveComponents.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,46 @@ | ||
<?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\LiveComponent\Test; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\UX\TwigComponent\ComponentFactory; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
trait InteractsWithLiveComponents | ||
{ | ||
protected function createLiveComponent(string $name, array $data = []): TestLiveComponent | ||
{ | ||
if (!$this instanceof KernelTestCase) { | ||
throw new \LogicException(sprintf('The "%s" trait can only be used on "%s" classes.', __TRAIT__, KernelTestCase::class)); | ||
} | ||
|
||
/** @var ComponentFactory $factory */ | ||
$factory = self::getContainer()->get('ux.twig_component.component_factory'); | ||
$metadata = $factory->metadataFor($name); | ||
|
||
if (!$metadata->get('live')) { | ||
throw new \LogicException(sprintf('The "%s" component is not a live component.', $name)); | ||
} | ||
|
||
return new TestLiveComponent( | ||
$metadata, | ||
$data, | ||
$factory, | ||
self::getContainer()->get('test.client'), | ||
self::getContainer()->get('ux.live_component.component_hydrator'), | ||
self::getContainer()->get('ux.live_component.metadata_factory'), | ||
self::getContainer()->get('router'), | ||
); | ||
} | ||
} |
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,149 @@ | ||
<?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\LiveComponent\Test; | ||
|
||
use Symfony\Bundle\FrameworkBundle\KernelBrowser; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\UX\LiveComponent\LiveComponentHydrator; | ||
use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadataFactory; | ||
use Symfony\UX\TwigComponent\ComponentFactory; | ||
use Symfony\UX\TwigComponent\ComponentMetadata; | ||
use Symfony\UX\TwigComponent\MountedComponent; | ||
use Symfony\UX\TwigComponent\Test\RenderedComponent; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
final class TestLiveComponent | ||
{ | ||
/** | ||
* @internal | ||
*/ | ||
public function __construct( | ||
private ComponentMetadata $metadata, | ||
array $data, | ||
private ComponentFactory $factory, | ||
private KernelBrowser $client, | ||
private LiveComponentHydrator $hydrator, | ||
private LiveComponentMetadataFactory $metadataFactory, | ||
private UrlGeneratorInterface $router, | ||
) { | ||
$this->client->catchExceptions(false); | ||
|
||
$mounted = $this->factory->create($this->metadata->getName(), $data); | ||
$props = $this->hydrator->dehydrate( | ||
$mounted->getComponent(), | ||
$mounted->getAttributes(), | ||
$this->metadataFactory->getMetadata($mounted->getName()) | ||
); | ||
|
||
$this->client->request('GET', $this->router->generate( | ||
$this->metadata->get('route'), | ||
[ | ||
'_live_component' => $this->metadata->getName(), | ||
'props' => json_encode($props->getProps(), flags: \JSON_THROW_ON_ERROR), | ||
] | ||
)); | ||
} | ||
|
||
public function render(): RenderedComponent | ||
{ | ||
return new RenderedComponent($this->response()->getContent()); | ||
} | ||
|
||
public function component(): object | ||
{ | ||
$component = $this->factory->get($this->metadata->getName()); | ||
$componentAttributes = $this->hydrator->hydrate( | ||
$component, | ||
$this->props(), | ||
[], | ||
$this->metadataFactory->getMetadata($this->metadata->getName()), | ||
); | ||
|
||
return (new MountedComponent($this->metadata->getName(), $component, $componentAttributes))->getComponent(); | ||
} | ||
|
||
/** | ||
* @param array<string,mixed> $arguments | ||
*/ | ||
public function call(string $action, array $arguments = []): self | ||
{ | ||
return $this->request(['args' => $arguments], $action); | ||
} | ||
|
||
/** | ||
* @param array<string,mixed> $arguments | ||
*/ | ||
public function emit(string $event, array $arguments = []): self | ||
{ | ||
return $this->call($event, $arguments); | ||
} | ||
|
||
public function set(string $prop, mixed $value): self | ||
{ | ||
return $this->request(['updated' => [$prop => $value]]); | ||
} | ||
|
||
public function refresh(): self | ||
{ | ||
return $this->request(); | ||
} | ||
|
||
public function response(): Response | ||
{ | ||
return $this->client->getResponse(); | ||
} | ||
|
||
private function request(array $content = [], string $action = null): self | ||
{ | ||
$csrfToken = $this->csrfToken(); | ||
|
||
$this->client->request( | ||
'POST', | ||
$this->router->generate( | ||
$this->metadata->get('route'), | ||
array_filter([ | ||
'_live_component' => $this->metadata->getName(), | ||
'_live_action' => $action, | ||
]) | ||
), | ||
parameters: ['data' => json_encode(array_merge($content, ['props' => $this->props()]))], | ||
server: $csrfToken ? ['HTTP_X_CSRF_TOKEN' => $csrfToken] : [], | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
private function props(): array | ||
{ | ||
$crawler = $this->client->getCrawler(); | ||
|
||
if (!\count($node = $crawler->filter('[data-live-props-value]'))) { | ||
throw new \LogicException('A live component action has redirected and you can no longer access the component.'); | ||
} | ||
|
||
return json_decode($node->attr('data-live-props-value'), true, flags: \JSON_THROW_ON_ERROR); | ||
} | ||
|
||
private function csrfToken(): ?string | ||
{ | ||
$crawler = $this->client->getCrawler(); | ||
|
||
if (!\count($node = $crawler->filter('[data-live-csrf-value]'))) { | ||
return null; | ||
} | ||
|
||
return $node->attr('data-live-csrf-value'); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/LiveComponent/tests/Fixtures/Component/TrackRenders.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,32 @@ | ||
<?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\LiveComponent\Tests\Fixtures\Component; | ||
|
||
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; | ||
use Symfony\UX\LiveComponent\Attribute\LiveProp; | ||
use Symfony\UX\LiveComponent\Attribute\PreReRender; | ||
use Symfony\UX\LiveComponent\DefaultActionTrait; | ||
|
||
#[AsLiveComponent('track_renders')] | ||
final class TrackRenders | ||
{ | ||
use DefaultActionTrait; | ||
|
||
#[LiveProp] | ||
public int $reRenders = 0; | ||
|
||
#[PreReRender] | ||
public function preReRender(): void | ||
{ | ||
++$this->reRenders; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/LiveComponent/tests/Fixtures/templates/components/track_renders.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,3 @@ | ||
<div{{ attributes }}> | ||
Re-Render Count: {{ reRenders }} | ||
</div> |
Oops, something went wrong.