Skip to content

Commit df97b2f

Browse files
committed
Update and extends functional tests
1 parent 810ae21 commit df97b2f

File tree

5 files changed

+116
-0
lines changed

5 files changed

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

0 commit comments

Comments
 (0)