|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\UX\LiveComponent\Test; |
| 13 | + |
| 14 | +use Symfony\Bundle\FrameworkBundle\KernelBrowser; |
| 15 | +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 16 | +use Symfony\UX\LiveComponent\LiveComponentHydrator; |
| 17 | +use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadataFactory; |
| 18 | +use Symfony\UX\TwigComponent\ComponentFactory; |
| 19 | +use Symfony\UX\TwigComponent\ComponentMetadata; |
| 20 | +use Symfony\UX\TwigComponent\MountedComponent; |
| 21 | +use Symfony\UX\TwigComponent\Test\RenderedComponent; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author Kevin Bond <kevinbond@gmail.com> |
| 25 | + */ |
| 26 | +final class TestLiveComponent |
| 27 | +{ |
| 28 | + private string $rendered; |
| 29 | + private array $props; |
| 30 | + private string $csrfToken; |
| 31 | + private object $component; |
| 32 | + |
| 33 | + /** |
| 34 | + * @internal |
| 35 | + */ |
| 36 | + public function __construct( |
| 37 | + private ComponentMetadata $metadata, |
| 38 | + array $data, |
| 39 | + private ComponentFactory $factory, |
| 40 | + private KernelBrowser $client, |
| 41 | + private LiveComponentHydrator $hydrator, |
| 42 | + private LiveComponentMetadataFactory $metadataFactory, |
| 43 | + private UrlGeneratorInterface $router, |
| 44 | + ) { |
| 45 | + $this->client->catchExceptions(false); |
| 46 | + |
| 47 | + $mounted = $this->factory->create($this->metadata->getName(), $data); |
| 48 | + $props = $this->hydrator->dehydrate( |
| 49 | + $mounted->getComponent(), |
| 50 | + $mounted->getAttributes(), |
| 51 | + $this->metadataFactory->getMetadata($mounted->getName()) |
| 52 | + ); |
| 53 | + |
| 54 | + $this->client->request('GET', $this->router->generate( |
| 55 | + $this->metadata->get('route'), |
| 56 | + [ |
| 57 | + '_live_component' => $this->metadata->getName(), |
| 58 | + 'props' => json_encode($props->getProps()), |
| 59 | + ] |
| 60 | + )); |
| 61 | + |
| 62 | + $this->updateState(); |
| 63 | + } |
| 64 | + |
| 65 | + public function render(): RenderedComponent |
| 66 | + { |
| 67 | + return new RenderedComponent($this->rendered); |
| 68 | + } |
| 69 | + |
| 70 | + public function component(): object |
| 71 | + { |
| 72 | + if (isset($this->component)) { |
| 73 | + return $this->component; |
| 74 | + } |
| 75 | + |
| 76 | + $component = $this->factory->get($this->metadata->getName()); |
| 77 | + $componentAttributes = $this->hydrator->hydrate( |
| 78 | + $component, |
| 79 | + $this->props, |
| 80 | + [], |
| 81 | + $this->metadataFactory->getMetadata($this->metadata->getName()), |
| 82 | + ); |
| 83 | + |
| 84 | + return $this->component = (new MountedComponent($this->metadata->getName(), $component, $componentAttributes))->getComponent(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @param array<string,mixed> $arguments |
| 89 | + */ |
| 90 | + public function call(string $action, array $arguments = []): self |
| 91 | + { |
| 92 | + return $this->request(['args' => $arguments], $action); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @param array<string,mixed> $arguments |
| 97 | + */ |
| 98 | + public function emit(string $event, array $arguments = []): self |
| 99 | + { |
| 100 | + return $this->call($event, $arguments); |
| 101 | + } |
| 102 | + |
| 103 | + public function set(string $prop, mixed $value): self |
| 104 | + { |
| 105 | + return $this->request(['updated' => [$prop => $value]]); |
| 106 | + } |
| 107 | + |
| 108 | + private function request(array $content = [], ?string $action = null): self |
| 109 | + { |
| 110 | + $this->client->request( |
| 111 | + 'POST', |
| 112 | + $this->router->generate( |
| 113 | + $this->metadata->get('route'), |
| 114 | + array_filter([ |
| 115 | + '_live_component' => $this->metadata->getName(), |
| 116 | + '_live_action' => $action, |
| 117 | + ]) |
| 118 | + ), |
| 119 | + parameters: ['data' => json_encode(array_merge($content, ['props' => $this->props]))], |
| 120 | + server: ['HTTP_X_CSRF_TOKEN' => $this->csrfToken], |
| 121 | + ); |
| 122 | + |
| 123 | + return $this->updateState(); |
| 124 | + } |
| 125 | + |
| 126 | + private function updateState(): self |
| 127 | + { |
| 128 | + $crawler = $this->client->getCrawler(); |
| 129 | + |
| 130 | + $this->props = json_decode($crawler->filter('[data-live-props-value]')->attr('data-live-props-value'), true, flags: \JSON_THROW_ON_ERROR); |
| 131 | + $this->csrfToken = $crawler->filter('[data-live-csrf-value]')->attr('data-live-csrf-value'); |
| 132 | + $this->rendered = $this->client->getResponse()->getContent(); |
| 133 | + |
| 134 | + unset($this->component); |
| 135 | + |
| 136 | + return $this; |
| 137 | + } |
| 138 | +} |
0 commit comments