Skip to content

Commit d650287

Browse files
committed
UrlFactoryTest
1 parent 04dc91a commit d650287

File tree

2 files changed

+151
-2
lines changed

2 files changed

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

0 commit comments

Comments
 (0)