‪TYPO3CMS  11.5
RouteSorterTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23 
24 class ‪RouteSorterTest extends UnitTestCase
25 {
27  {
28  return [
29  'default route only' => [
30  // routes
31  [
32  $this->‪createDefaultRoute('/default'),
33  ],
34  // given parameters
35  [],
36  // expected route paths order
37  [
38  '/default',
39  ],
40  ],
41  'static, default route' => [
42  [
43  $this->‪createDefaultRoute('/default-1'),
44  $this->‪createRoute('/list'),
45  $this->‪createDefaultRoute('/default-2'),
46  ],
47  [],
48  [
49  '/list',
50  '/default-1',
51  '/default-2',
52  ],
53  ],
54  'mandatory, static, default route' => [
55  [
56  $this->‪createDefaultRoute('/default'),
57  $this->‪createRoute('/list'),
58  $this->‪createRoute('/list/{page}', ['page' => 0]),
59  ],
60  [],
61  [
62  '/list/{page}',
63  '/list',
64  '/default',
65  ],
66  ],
67  // not really important, since missing mandatory
68  // variables would have been skipped during generation
69  'ambiguous routes, no parameters, most probable' => [
70  [
71  $this->‪createRoute('/list'),
72  $this->‪createRoute('/list/{uid}'),
73  $this->‪createRoute('/list/{uid}/{category}', ['category' => 0]),
74  $this->‪createRoute('/list/{page}', ['page' => 0]),
75  $this->‪createRoute('/list/{category}', ['category' => 0]),
76  ],
77  [],
78  [
79  '/list/{page}', // no parameters given -> defaults take precedence
80  '/list/{category}', // no parameters given -> defaults take precedence
81  '/list/{uid}',
82  '/list/{uid}/{category}',
83  '/list',
84  ],
85  ],
86  'mandatory first, ambiguous parameters' => [
87  [
88  $this->‪createRoute('/list'),
89  $this->‪createRoute('/list/{uid}'),
90  $this->‪createRoute('/list/{uid}/{category}', ['category' => 0]),
91  $this->‪createRoute('/list/{page}', ['page' => 0]),
92  $this->‪createRoute('/list/{category}', ['category' => 0]),
93  ],
94  [
95  'uid' => 123,
96  'page' => 1,
97  ],
98  [
99  '/list/{uid}', // value for {uid} given, complete mandatory first -> takes precedence
100  '/list/{page}', // value for {page} given, complete first -> takes precedence
101  '/list/{uid}/{category}',
102  '/list/{category}',
103  '/list',
104  ],
105  ],
106  'complete first, ambiguous parameters #1' => [
107  [
108  $this->‪createRoute('/list'),
109  $this->‪createRoute('/list/{uid}'),
110  $this->‪createRoute('/list/{uid}/{category}', ['category' => 0]),
111  $this->‪createRoute('/list/{page}', ['page' => 0]),
112  $this->‪createRoute('/list/{category}', ['category' => 0]),
113  ],
114  [
115  'category' => 1,
116  'page' => 1,
117  ],
118  [
119  '/list/{page}', // value for default {page} given, complete first -> takes precedence
120  '/list/{category}',
121  '/list/{uid}',
122  '/list/{uid}/{category}',
123  '/list',
124  ],
125  ],
126  'complete first, ambiguous parameters #2' => [
127  [
128  $this->‪createRoute('/list'),
129  $this->‪createRoute('/list/{uid}'),
130  $this->‪createRoute('/list/{uid}/{category}', ['category' => 0]),
131  $this->‪createRoute('/list/{page}', ['page' => 0]),
132  $this->‪createRoute('/list/{category}', ['category' => 0]),
133  ],
134  [
135  'uid' => 123,
136  'page' => 1,
137  'category' => 2,
138  ],
139  [
140  '/list/{uid}/{category}', // values for {uid} and {category} given, complete first -> takes precedence
141  '/list/{uid}',
142  '/list/{page}',
143  '/list/{category}',
144  '/list',
145  ],
146  ],
147  // not really important, just to show order is kept
148  'defaults only, no parameters given #1' => [
149  [
150  $this->‪createRoute('/list/{defA}/{defB}/{defC}', ['defA' => 0, 'defB' => 0, 'defC' => 0]),
151  $this->‪createRoute('/list/{defD}/{defE}/{defF}', ['defD' => 0, 'defE' => 0, 'defF' => 0]),
152  ],
153  [
154  ],
155  [
156  '/list/{defA}/{defB}/{defC}',
157  '/list/{defD}/{defE}/{defF}',
158  ],
159  ],
160  // not really important, just to show order is kept
161  'defaults only, no parameters given #2' => [
162  [
163  $this->‪createRoute('/list/{defD}/{defE}/{defF}', ['defD' => 0, 'defE' => 0, 'defF' => 0]),
164  $this->‪createRoute('/list/{defA}/{defB}/{defC}', ['defA' => 0, 'defB' => 0, 'defC' => 0]),
165  ],
166  [
167  ],
168  [
169  '/list/{defD}/{defE}/{defF}',
170  '/list/{defA}/{defB}/{defC}',
171  ],
172  ],
173  'defaults only, {defF} given, best match' => [
174  [
175  $this->‪createRoute('/list/{defA}/{defB}/{defC}', ['defA' => 0, 'defB' => 0, 'defC' => 0]),
176  $this->‪createRoute('/list/{defD}/{defE}/{defF}', ['defD' => 0, 'defE' => 0, 'defF' => 0]),
177  ],
178  [
179  'defF' => 1,
180  ],
181  [
182  '/list/{defD}/{defE}/{defF}', // {defF} given, best match -> takes precedence
183  '/list/{defA}/{defB}/{defC}',
184  ],
185  ],
186  'mixed variables, ambiguous parameters, complete mandatory first #1' => [
187  [
188  $this->‪createRoute('/list/{d}/{e}/{defF}', ['defF' => 0]),
189  $this->‪createRoute('/list/{a}/{defB}/{defC}', ['defB' => 0, 'defC' => 0]),
190  ],
191  [
192  'a' => 1,
193  'd' => 1,
194  'defF' => 1,
195  ],
196  [
197  '/list/{a}/{defB}/{defC}', // mandatory {a} given, complete mandatory first -> takes precedence
198  '/list/{d}/{e}/{defF}',
199  ],
200  ],
201  'mixed variables, ambiguous parameters, complete mandatory first #2' => [
202  [
203  $this->‪createRoute('/list/{a}/{defB}/{defC}', ['defB' => 0, 'defC' => 0]),
204  $this->‪createRoute('/list/{d}/{e}/{defF}', ['defF' => 0]),
205  ],
206  [
207  'd' => 1,
208  'e' => 1,
209  'defB' => 1,
210  'defC' => 1,
211  ],
212  [
213  '/list/{d}/{e}/{defF}', // mandatory {d} and {e} given, complete mandatory first -> takes precedence
214  '/list/{a}/{defB}/{defC}',
215  ],
216  ],
217  'mixed variables, ambiguous parameters, complete first' => [
218  [
219  $this->‪createRoute('/list/{d}/{e}/{defF}', ['defF' => 0]),
220  $this->‪createRoute('/list/{a}/{defB}/{defC}', ['defB' => 0, 'defC' => 0]),
221  ],
222  [
223  'd' => 1,
224  'e' => 1,
225  'a' => 1,
226  'defB' => 1,
227  'defC' => 1,
228  ],
229  [
230  '/list/{a}/{defB}/{defC}', // all parameters given, complete first -> takes precedence
231  '/list/{d}/{e}/{defF}',
232  ],
233  ],
234  ];
235  }
236 
245  public function ‪routesAreSortedForGeneration(array $givenRoutes, array $givenParameters, array $expectation): void
246  {
247  $sorter = (new ‪RouteSorter())
248  ->withRoutes($givenRoutes)
249  ->withOriginalParameters($givenParameters);
250  $routes = $sorter->sortRoutesForGeneration()->getRoutes();
251  $routePaths = array_map([$this, 'getRoutePath'], array_values($routes));
252  self::assertSame($expectation, $routePaths);
253  }
254 
255  private function ‪getRoutePath(‪Route $route): string
256  {
257  return $route->getPath();
258  }
259 
260  private function ‪createRoute(string $path, array $defaults = []): ‪Route
261  {
262  $route = new ‪Route($path);
263  $route->setDefaults($defaults);
264  return $route;
265  }
266 
267  private function ‪createDefaultRoute(string $path): ‪Route
268  {
269  $route = new ‪Route($path);
270  $route->setOption('_isDefault', true);
271  return $route;
272  }
273 }
‪TYPO3\CMS\Core\Tests\Unit\Routing\RouteSorterTest\createRoute
‪createRoute(string $path, array $defaults=[])
Definition: RouteSorterTest.php:260
‪TYPO3\CMS\Core\Tests\Unit\Routing
‪TYPO3\CMS\Core\Tests\Unit\Routing\RouteSorterTest\routesAreSortedForGenerationDataProvider
‪routesAreSortedForGenerationDataProvider()
Definition: RouteSorterTest.php:26
‪TYPO3\CMS\Core\Tests\Unit\Routing\RouteSorterTest\getRoutePath
‪getRoutePath(Route $route)
Definition: RouteSorterTest.php:255
‪TYPO3\CMS\Core\Tests\Unit\Routing\RouteSorterTest
Definition: RouteSorterTest.php:25
‪TYPO3\CMS\Core\Tests\Unit\Routing\RouteSorterTest\routesAreSortedForGeneration
‪routesAreSortedForGeneration(array $givenRoutes, array $givenParameters, array $expectation)
Definition: RouteSorterTest.php:245
‪TYPO3\CMS\Core\Routing\Route
Definition: Route.php:32
‪TYPO3\CMS\Core\Routing\RouteSorter
Definition: RouteSorter.php:25
‪TYPO3\CMS\Core\Tests\Unit\Routing\RouteSorterTest\createDefaultRoute
‪createDefaultRoute(string $path)
Definition: RouteSorterTest.php:267