|
| 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\Exception\MissingMandatoryParametersException; |
| 16 | +use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
| 17 | +use Symfony\Component\Routing\RouterInterface; |
| 18 | +use Symfony\UX\LiveComponent\Util\UrlFactory; |
| 19 | + |
| 20 | +class UrlFactoryTest extends TestCase |
| 21 | +{ |
| 22 | + public function getData(): \Generator |
| 23 | + { |
| 24 | + yield 'keep_default_url' => []; |
| 25 | + |
| 26 | + yield 'keep_relative_url' => [ |
| 27 | + 'input' => ['previousUrl' => '/foo/bar'], |
| 28 | + 'expectedUrl' => '/foo/bar', |
| 29 | + ]; |
| 30 | + |
| 31 | + yield 'keep_absolute_url' => [ |
| 32 | + 'input' => ['previousUrl' => 'https://symfony.com/foo/bar'], |
| 33 | + 'expectedUrl' => '/foo/bar', |
| 34 | + 'routerStubData' => [ |
| 35 | + 'previousUrl' => '/foo/bar', |
| 36 | + 'newUrl' => '/foo/bar', |
| 37 | + ], |
| 38 | + ]; |
| 39 | + |
| 40 | + yield 'keep_url_with_query_parameters' => [ |
| 41 | + 'input' => ['previousUrl' => 'https://symfony.com/foo/bar?prop1=val1&prop2=val2'], |
| 42 | + '/foo/bar?prop1=val1&prop2=val2', |
| 43 | + 'routerStubData' => [ |
| 44 | + 'previousUrl' => '/foo/bar?prop1=val1&prop2=val2', |
| 45 | + 'newUrl' => '/foo/bar?prop1=val1&prop2=val2', |
| 46 | + ], |
| 47 | + ]; |
| 48 | + |
| 49 | + yield 'add_query_parameters' => [ |
| 50 | + 'input' => [ |
| 51 | + 'previousUrl' => '/foo/bar', |
| 52 | + 'queryMappedProps' => ['prop1' => 'val1', 'prop2' => 'val2'], |
| 53 | + ], |
| 54 | + 'expectedUrl' => '/foo/bar?prop1=val1&prop2=val2', |
| 55 | + ]; |
| 56 | + |
| 57 | + yield 'override_previous_matching_query_parameters' => [ |
| 58 | + 'input' => [ |
| 59 | + 'previousUrl' => '/foo/bar?prop1=oldValue&prop3=oldValue', |
| 60 | + 'queryMappedProps' => ['prop1' => 'val1', 'prop2' => 'val2'], |
| 61 | + ], |
| 62 | + 'expectedUrl' => '/foo/bar?prop1=val1&prop3=oldValue&prop2=val2', |
| 63 | + ]; |
| 64 | + |
| 65 | + yield 'add_path_parameters' => [ |
| 66 | + 'input' => [ |
| 67 | + 'previousUrl' => '/foo/bar', |
| 68 | + 'pathMappedProps' => ['value' => 'baz'], |
| 69 | + ], |
| 70 | + 'expectedUrl' => '/foo/baz', |
| 71 | + 'routerStubData' => [ |
| 72 | + 'previousUrl' => '/foo/bar', |
| 73 | + 'newUrl' => '/foo/baz', |
| 74 | + 'props' => ['value' => 'baz'], |
| 75 | + ], |
| 76 | + ]; |
| 77 | + |
| 78 | + yield 'add_both_parameters' => [ |
| 79 | + 'input' => [ |
| 80 | + 'previousUrl' => '/foo/bar', |
| 81 | + 'pathMappedProps' => ['value' => 'baz'], |
| 82 | + 'queryMappedProps' => ['filter' => 'all'], |
| 83 | + ], |
| 84 | + 'expectedUrl' => '/foo/baz?filter=all', |
| 85 | + 'routerStubData' => [ |
| 86 | + 'previousUrl' => '/foo/bar', |
| 87 | + 'newUrl' => '/foo/baz', |
| 88 | + 'props' => ['value' => 'baz'], |
| 89 | + ], |
| 90 | + ]; |
| 91 | + |
| 92 | + yield 'handle_path_parameter_not_recognized' => [ |
| 93 | + 'input' => [ |
| 94 | + 'previousUrl' => '/foo/bar', |
| 95 | + 'pathMappedProps' => ['value' => 'baz'], |
| 96 | + ], |
| 97 | + 'expectedUrl' => '/foo/bar?value=baz', |
| 98 | + 'routerStubData' => [ |
| 99 | + 'previousUrl' => '/foo/bar', |
| 100 | + 'newUrl' => '/foo/bar?value=baz', |
| 101 | + 'props' => ['value' => 'baz'], |
| 102 | + ], |
| 103 | + ]; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @dataProvider getData |
| 108 | + */ |
| 109 | + public function testCreate( |
| 110 | + array $input = [], |
| 111 | + string $expectedUrl = '', |
| 112 | + array $routerStubData = [], |
| 113 | + ): void { |
| 114 | + $previousUrl = $input['previousUrl'] ?? ''; |
| 115 | + $router = $this->createRouterStub( |
| 116 | + $routerStubData['previousUrl'] ?? $previousUrl, |
| 117 | + $routerStubData['newUrl'] ?? $previousUrl, |
| 118 | + $routerStubData['props'] ?? [], |
| 119 | + ); |
| 120 | + $factory = new UrlFactory($router); |
| 121 | + $newUrl = $factory->createFromPreviousAndProps( |
| 122 | + $previousUrl, |
| 123 | + $input['pathMappedProps'] ?? [], |
| 124 | + $input['queryMappedProps'] ?? [] |
| 125 | + ); |
| 126 | + |
| 127 | + $this->assertEquals($expectedUrl, $newUrl); |
| 128 | + } |
| 129 | + |
| 130 | + public function testResourceNotFoundException(): void |
| 131 | + { |
| 132 | + $previousUrl = '/foo/bar'; |
| 133 | + $router = $this->createMock(RouterInterface::class); |
| 134 | + $router->expects(self::once()) |
| 135 | + ->method('match') |
| 136 | + ->with($previousUrl) |
| 137 | + ->willThrowException(new ResourceNotFoundException()); |
| 138 | + $factory = new UrlFactory($router); |
| 139 | + |
| 140 | + $this->assertNull($factory->createFromPreviousAndProps($previousUrl, [], [])); |
| 141 | + } |
| 142 | + |
| 143 | + public function testMissingMandatoryParametersException(): void |
| 144 | + { |
| 145 | + $previousUrl = '/foo/bar'; |
| 146 | + $matchedRouteName = 'foo_bar'; |
| 147 | + $router = $this->createMock(RouterInterface::class); |
| 148 | + $router->expects(self::once()) |
| 149 | + ->method('match') |
| 150 | + ->with($previousUrl) |
| 151 | + ->willReturn(['_route' => $matchedRouteName]); |
| 152 | + $router->expects(self::once()) |
| 153 | + ->method('generate') |
| 154 | + ->with($matchedRouteName, []) |
| 155 | + ->willThrowException(new MissingMandatoryParametersException($matchedRouteName, ['baz'])); |
| 156 | + $factory = new UrlFactory($router); |
| 157 | + |
| 158 | + $this->assertNull($factory->createFromPreviousAndProps($previousUrl, [], [])); |
| 159 | + } |
| 160 | + |
| 161 | + private function createRouterStub( |
| 162 | + string $previousUrl, |
| 163 | + string $newUrl, |
| 164 | + array $props = [], |
| 165 | + ): RouterInterface { |
| 166 | + $matchedRoute = 'default'; |
| 167 | + $router = $this->createMock(RouterInterface::class); |
| 168 | + $router->expects(self::once()) |
| 169 | + ->method('match') |
| 170 | + ->with($previousUrl) |
| 171 | + ->willReturn(['_route' => $matchedRoute]); |
| 172 | + $router->expects(self::once()) |
| 173 | + ->method('generate') |
| 174 | + ->with($matchedRoute, $props) |
| 175 | + ->willReturn($newUrl); |
| 176 | + |
| 177 | + return $router; |
| 178 | + } |
| 179 | +} |
0 commit comments