Skip to content

Commit f154958

Browse files
committed
Update and extends functional tests
1 parent cb2b104 commit f154958

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ public function modifyMaybeBoundProp(LiveProp $prop): LiveProp
6868
#[LiveProp]
6969
public ?string $customAlias = null;
7070

71+
#[LiveProp(writable: true, url: new UrlMapping(mapPath: true))]
72+
public ?string $pathProp = null;
73+
74+
#[LiveProp(writable: true, url: new UrlMapping(as: 'pathAlias', mapPath: true))]
75+
public ?string $pathPropWithAlias = null;
76+
7177
public function modifyBoundPropWithCustomAlias(LiveProp $liveProp): LiveProp
7278
{
7379
if ($this->customAlias) {

src/LiveComponent/tests/Fixtures/Kernel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,5 +216,7 @@ protected function configureRoutes(RoutingConfigurator $routes): void
216216
$routes->add('homepage', '/')->controller('kernel::index');
217217
$routes->add('alternate_live_route', '/alt/{_live_component}/{_live_action}')->defaults(['_live_action' => 'get']);
218218
$routes->add('localized_route', '/locale/{_locale}/{_live_component}/{_live_action}')->defaults(['_live_action' => 'get']);
219+
$routes->add('route_with_prop', '/route_with_prop/{pathProp}');
220+
$routes->add('route_with_alias_prop', '/route_with_alias_prop/{pathAlias}');
219221
}
220222
}

src/LiveComponent/tests/Fixtures/templates/components/component_with_url_bound_props.html.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@
99
MaybeBoundProp: {{ maybeBoundProp }}
1010
BoundPropWithAlias: {{ boundPropWithAlias }}
1111
BoundPropWithCustomAlias: {{ boundPropWithCustomAlias }}
12+
PathProp: {{ pathProp }}
13+
PathPropWithAlias: {{ pathPropWithAlias }}
1214
</div>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ public function testQueryStringMappingAttribute()
139139
'maybeBoundProp' => ['name' => 'maybeBoundProp'],
140140
'boundPropWithAlias' => ['name' => 'q'],
141141
'boundPropWithCustomAlias' => ['name' => 'customAlias'],
142+
'pathProp' => ['name' => 'pathProp'],
143+
'pathPropWithAlias' => ['name' => 'pathAlias'],
142144
];
143145

144146
$this->assertEquals($expected, $queryMapping);
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\UX\LiveComponent\Tests\Functional\EventListener;
6+
7+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
8+
use Symfony\UX\LiveComponent\Tests\LiveComponentTestHelper;
9+
use Zenstruck\Browser\Test\HasBrowser;
10+
11+
class LiveUrlSubscriberTest extends KernelTestCase
12+
{
13+
use HasBrowser;
14+
use LiveComponentTestHelper;
15+
16+
public function getTestData(): iterable
17+
{
18+
yield 'missing_header' => [
19+
'previousLocation' => null,
20+
'expectedLocation' => null,
21+
'props' => [],
22+
];
23+
yield 'unknown_previous_location' => [
24+
'previousLocation' => 'foo/bar',
25+
'expectedLocation' => null,
26+
'props' => [],
27+
];
28+
29+
yield 'no_prop' => [
30+
'previousLocation' => '/route_with_prop/foo',
31+
'expectedLocation' => null,
32+
'props' => [],
33+
];
34+
35+
yield 'no_change' => [
36+
'previousLocation' => '/route_with_prop/foo',
37+
'expectedLocation' => '/route_with_prop/foo',
38+
'props' => [
39+
'pathProp' => 'foo',
40+
],
41+
];
42+
43+
yield 'prop_changed' => [
44+
'previousLocation' => '/route_with_prop/foo',
45+
'expectedLocation' => '/route_with_prop/bar',
46+
'props' => [
47+
'pathProp' => 'foo',
48+
],
49+
'updated' => [
50+
'pathProp' => 'bar',
51+
],
52+
];
53+
54+
yield 'alias_prop_changed' => [
55+
'previousLocation' => '/route_with_alias_prop/foo',
56+
'expectedLocation' => '/route_with_alias_prop/bar',
57+
'props' => [
58+
'pathPropWithAlias' => 'foo',
59+
],
60+
'updated' => [
61+
'pathPropWithAlias' => 'bar',
62+
],
63+
];
64+
}
65+
66+
/**
67+
* @dataProvider getTestData
68+
*/
69+
public function testNoHeader(
70+
?string $previousLocation,
71+
?string $expectedLocation,
72+
array $props,
73+
array $updated = []
74+
): void {
75+
$component = $this->mountComponent('component_with_url_bound_props', $props);
76+
$dehydrated = $this->dehydrateComponent($component);
77+
78+
$this->browser()
79+
->throwExceptions()
80+
->post(
81+
'/_components/component_with_url_bound_props',
82+
[
83+
'body' => [
84+
'data' => json_encode([
85+
'props' => $dehydrated->getProps(),
86+
'updated' => $updated
87+
]),
88+
],
89+
'headers' => [
90+
'X-Live-Url' => $previousLocation,
91+
],
92+
]
93+
)
94+
->assertSuccessful()
95+
->assertHeaderEquals('X-Live-Url', $expectedLocation);
96+
}
97+
}

0 commit comments

Comments
 (0)