From 1ce27eca9a55553e57f05ccce7175f99911fa288 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 27 Jan 2022 15:21:30 -0500 Subject: [PATCH] [LiveComponent] fix `component_url` twig function Previously, it didn't work as intended. It required a component instance. It was supposed to work like `component()` except generate a url to the live component. --- .../src/Twig/LiveComponentRuntime.php | 6 ++-- .../Fixture/templates/component_url.html.twig | 1 + .../Twig/LiveComponentExtensionTest.php | 33 +++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 src/LiveComponent/tests/Fixture/templates/component_url.html.twig create mode 100644 src/LiveComponent/tests/Integration/Twig/LiveComponentExtensionTest.php diff --git a/src/LiveComponent/src/Twig/LiveComponentRuntime.php b/src/LiveComponent/src/Twig/LiveComponentRuntime.php index f858bdf8c1a..fed073f3131 100644 --- a/src/LiveComponent/src/Twig/LiveComponentRuntime.php +++ b/src/LiveComponent/src/Twig/LiveComponentRuntime.php @@ -59,10 +59,10 @@ public function renderLiveAttributes(Environment $env, object $component, string ); } - public function getComponentUrl(object $component, string $name = null): string + public function getComponentUrl(string $name, array $props = []): string { - $data = $this->hydrator->dehydrate($component); - $params = ['component' => $this->nameFor($component, $name)] + $data; + $component = $this->factory->create($name, $props); + $params = ['component' => $name] + $this->hydrator->dehydrate($component); return $this->urlGenerator->generate('live_component', $params); } diff --git a/src/LiveComponent/tests/Fixture/templates/component_url.html.twig b/src/LiveComponent/tests/Fixture/templates/component_url.html.twig new file mode 100644 index 00000000000..74b0d86588d --- /dev/null +++ b/src/LiveComponent/tests/Fixture/templates/component_url.html.twig @@ -0,0 +1 @@ +{{ component_url('component1', { prop1: null, prop2: date }) }} diff --git a/src/LiveComponent/tests/Integration/Twig/LiveComponentExtensionTest.php b/src/LiveComponent/tests/Integration/Twig/LiveComponentExtensionTest.php new file mode 100644 index 00000000000..50bc2a12cef --- /dev/null +++ b/src/LiveComponent/tests/Integration/Twig/LiveComponentExtensionTest.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\UX\LiveComponent\Tests\Integration; + +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Symfony\UX\LiveComponent\Tests\ContainerBC; + +/** + * @author Kevin Bond + */ +final class LiveComponentExtensionTest extends KernelTestCase +{ + use ContainerBC; + + public function testGetComponentUrl(): void + { + $rendered = self::getContainer()->get('twig')->render('component_url.html.twig', [ + 'date' => new \DateTime('2022-10-06-0'), + ]); + + $this->assertStringContainsString('/_components/component1?prop2=2022-10-06T00:00:00', $rendered); + $this->assertStringContainsString('_checksum=', $rendered); + } +}