Skip to content

Commit 47808ea

Browse files
committed
UrlFactoryTest
1 parent 04dc91a commit 47808ea

File tree

2 files changed

+144
-2
lines changed

2 files changed

+144
-2
lines changed

src/LiveComponent/src/Util/UrlFactory.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ public function createFromPreviousAndProps(
2727
string $previousUrl,
2828
array $pathMappedProps,
2929
array $queryMappedProps,
30-
): string {
30+
): ?string {
3131
$parsed = parse_url($previousUrl);
32+
if (false === $parsed) {
33+
return null;
34+
}
3235

3336
// Make sure to handle only path and query
3437
$previousUrl = $parsed['path'] ?? '';
@@ -51,7 +54,7 @@ public function createFromPreviousAndProps(
5154
private function createPath(string $previousUrl, array $props): string
5255
{
5356
return $this->router->generate(
54-
$this->router->match($previousUrl)['_route'],
57+
$this->router->match($previousUrl)['_route'] ?? '',
5558
$props
5659
);
5760
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\UX\LiveComponent\Tests\Unit\Util;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\Routing\RouterInterface;
9+
use Symfony\UX\LiveComponent\Util\UrlFactory;
10+
11+
class UrlFactoryTest extends TestCase
12+
{
13+
public function getData(): \Generator
14+
{
15+
yield 'keep_default_url' => [];
16+
17+
yield 'keep_relative_url' => [
18+
'input' => ['previousUrl' => '/foo/bar'],
19+
'expectedUrl' => '/foo/bar',
20+
];
21+
22+
yield 'keep_absolute_url' => [
23+
'input' => ['previousUrl' => 'https://symfony.com/foo/bar'],
24+
'expectedUrl' => '/foo/bar',
25+
'routerStubData' => [
26+
'previousUrl' => '/foo/bar',
27+
'newUrl' => '/foo/bar',
28+
],
29+
];
30+
31+
yield 'keep_url_with_query_parameters' => [
32+
'input' => ['previousUrl' => 'https://symfony.com/foo/bar?prop1=val1&prop2=val2'],
33+
'/foo/bar?prop1=val1&prop2=val2',
34+
'routerStubData' => [
35+
'previousUrl' => '/foo/bar?prop1=val1&prop2=val2',
36+
'newUrl' => '/foo/bar?prop1=val1&prop2=val2',
37+
],
38+
];
39+
40+
yield 'add_query_parameters' => [
41+
'input' => [
42+
'previousUrl' => '/foo/bar',
43+
'queryMappedProps' => ['prop1' => 'val1', 'prop2' => 'val2'],
44+
],
45+
'expectedUrl' => '/foo/bar?prop1=val1&prop2=val2',
46+
];
47+
48+
yield 'override_previous_matching_query_parameters' => [
49+
'input' => [
50+
'previousUrl' => '/foo/bar?prop1=oldValue&prop3=oldValue',
51+
'queryMappedProps' => ['prop1' => 'val1', 'prop2' => 'val2'],
52+
],
53+
'expectedUrl' => '/foo/bar?prop1=val1&prop3=oldValue&prop2=val2',
54+
];
55+
56+
yield 'add_path_parameters' => [
57+
'input' => [
58+
'previousUrl' => '/foo/bar',
59+
'pathMappedProps' => ['value' => 'baz'],
60+
],
61+
'expectedUrl' => '/foo/baz',
62+
'routerStubData' => [
63+
'previousUrl' => '/foo/bar',
64+
'newUrl' => '/foo/baz',
65+
'props' => ['value' => 'baz'],
66+
],
67+
];
68+
69+
yield 'add_both_parameters' => [
70+
'input' => [
71+
'previousUrl' => '/foo/bar',
72+
'pathMappedProps' => ['value' => 'baz'],
73+
'queryMappedProps' => ['filter' => 'all']
74+
],
75+
'expectedUrl' => '/foo/baz?filter=all',
76+
'routerStubData' => [
77+
'previousUrl' => '/foo/bar',
78+
'newUrl' => '/foo/baz',
79+
'props' => ['value' => 'baz'],
80+
],
81+
];
82+
83+
yield 'handle_path_parameter_not_recognized' => [
84+
'input' => [
85+
'previousUrl' => '/foo/bar',
86+
'pathMappedProps' => ['value' => 'baz'],
87+
],
88+
'expectedUrl' => '/foo/bar?value=baz',
89+
'routerStubData' => [
90+
'previousUrl' => '/foo/bar',
91+
'newUrl' => '/foo/bar?value=baz',
92+
'props' => ['value' => 'baz'],
93+
],
94+
];
95+
}
96+
97+
/**
98+
* @dataProvider getData
99+
*/
100+
public function testCreate(
101+
array $input = [],
102+
string $expectedUrl = '',
103+
array $routerStubData = [],
104+
): void {
105+
$previousUrl = $input['previousUrl'] ?? '';
106+
$router = $this->createRouterStub(
107+
$routerStubData['previousUrl'] ?? $previousUrl,
108+
$routerStubData['newUrl'] ?? $previousUrl,
109+
$routerStubData['props'] ?? [],
110+
);
111+
$factory = new UrlFactory($router);
112+
$newUrl = $factory->createFromPreviousAndProps(
113+
$previousUrl,
114+
$input['pathMappedProps'] ?? [],
115+
$input['queryMappedProps'] ?? []
116+
);
117+
118+
$this->assertEquals($expectedUrl, $newUrl);
119+
}
120+
121+
private function createRouterStub(
122+
string $previousUrl,
123+
string $newUrl,
124+
array $props = [],
125+
): RouterInterface {
126+
$matchedRoute = 'default';
127+
$router = $this->createMock(RouterInterface::class);
128+
$router->expects(self::once())
129+
->method('match')
130+
->with($previousUrl)
131+
->willReturn(['_route' => $matchedRoute]);
132+
$router->expects(self::once())
133+
->method('generate')
134+
->with($matchedRoute, $props)
135+
->willReturn($newUrl);
136+
137+
return $router;
138+
}
139+
}

0 commit comments

Comments
 (0)