Skip to content

Commit c60e5bf

Browse files
committed
Add a test to verify live query props works with absolute URL
1 parent 2d371af commit c60e5bf

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/LiveComponent/tests/Fixtures/Component/WithAbsoluteUrl.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
66
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
7+
use Symfony\UX\LiveComponent\Attribute\LiveAction;
8+
use Symfony\UX\LiveComponent\Attribute\LiveProp;
79
use Symfony\UX\LiveComponent\DefaultActionTrait;
810

911
/**
@@ -13,4 +15,13 @@
1315
final class WithAbsoluteUrl
1416
{
1517
use DefaultActionTrait;
18+
19+
#[LiveProp(writable: true, url: true)]
20+
public int $count = 0;
21+
22+
#[LiveAction]
23+
public function increase(): void
24+
{
25+
++$this->count;
26+
}
1627
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<div{{ attributes }}>
2-
From absolute url.
2+
From absolute url. Count: {{ count }}
33
</div>

src/LiveComponent/tests/Functional/EventListener/AddLiveAttributesSubscriberTest.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\UX\LiveComponent\Tests\Functional\EventListener;
1313

1414
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
15+
use Symfony\Component\DomCrawler\Crawler;
1516
use Symfony\UX\LiveComponent\Tests\LiveComponentTestHelper;
1617
use Zenstruck\Browser\Test\HasBrowser;
1718

@@ -160,6 +161,7 @@ public function testAbsoluteUrl(): void
160161
$div = $this->browser()
161162
->visit('/render-template/render_with_absolute_url')
162163
->assertSuccessful()
164+
->assertContains('Count: 0')
163165
->crawler()
164166
->filter('div')
165167
;
@@ -169,9 +171,46 @@ public function testAbsoluteUrl(): void
169171
$this->assertSame('live', $div->attr('data-controller'));
170172
$this->assertSame('http://localhost/_components/with_absolute_url', $div->attr('data-live-url-value'));
171173
$this->assertNotNull($div->attr('data-live-csrf-value'));
172-
$this->assertCount(2, $props);
174+
$this->assertCount(3, $props);
173175
$this->assertArrayHasKey('@checksum', $props);
174176
$this->assertArrayHasKey('@attributes', $props);
175177
$this->assertArrayHasKey('data-live-id', $props['@attributes']);
178+
$this->assertArrayHasKey('count', $props);
179+
$this->assertSame($props['count'], 0);
180+
}
181+
182+
public function testAbsoluteUrlWithLiveQueryProp()
183+
{
184+
$token = null;
185+
$props = [];
186+
$div = $this->browser()
187+
->get('/render-template/render_with_absolute_url?count=1')
188+
->assertSuccessful()
189+
->assertContains('Count: 1')
190+
->use(function (Crawler $crawler) use (&$token, &$props) {
191+
$div = $crawler->filter('div')->first();
192+
$token = $div->attr('data-live-csrf-value');
193+
$props = json_decode($div->attr('data-live-props-value'), true);
194+
})
195+
->post('http://localhost/_components/with_absolute_url/increase', [
196+
'headers' => ['X-CSRF-TOKEN' => $token],
197+
'body' => ['data' => json_encode(['props' => $props])],
198+
])
199+
->assertContains('Count: 2')
200+
->crawler()
201+
->filter('div')
202+
;
203+
204+
$props = json_decode($div->attr('data-live-props-value'), true);
205+
206+
$this->assertSame('live', $div->attr('data-controller'));
207+
$this->assertSame('http://localhost/_components/with_absolute_url', $div->attr('data-live-url-value'));
208+
$this->assertNotNull($div->attr('data-live-csrf-value'));
209+
$this->assertCount(3, $props);
210+
$this->assertArrayHasKey('@checksum', $props);
211+
$this->assertArrayHasKey('@attributes', $props);
212+
$this->assertArrayHasKey('data-live-id', $props['@attributes']);
213+
$this->assertArrayHasKey('count', $props);
214+
$this->assertSame($props['count'], 2);
176215
}
177216
}

0 commit comments

Comments
 (0)