Completed
Push — master ( 30af4a...256b3e )
by Łukasz
18:25 queued 05:11
created

UrlAliasRouterTest::getRouter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 5
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the UrlAliasRouterTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\MVC\Symfony\Routing\Tests;
10
11
use eZ\Publish\API\Repository\ContentService;
12
use eZ\Publish\API\Repository\LocationService;
13
use eZ\Publish\API\Repository\URLAliasService;
14
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
15
use eZ\Publish\API\Repository\Values\Content\URLAlias;
16
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
17
use eZ\Publish\Core\MVC\ConfigResolverInterface;
18
use eZ\Publish\Core\MVC\Symfony\Routing\Generator\UrlAliasGenerator;
19
use eZ\Publish\Core\MVC\Symfony\Routing\UrlAliasRouter;
20
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
21
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher;
22
use eZ\Publish\Core\MVC\Symfony\View\Manager as ViewManager;
23
use eZ\Publish\Core\Repository\Repository;
24
use eZ\Publish\Core\Repository\Values\Content\Location;
25
use PHPUnit\Framework\TestCase;
26
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
29
use Symfony\Component\Routing\RequestContext;
30
use Symfony\Component\Routing\RouteCollection;
31
use Symfony\Component\Routing\RouterInterface;
32
33
class UrlAliasRouterTest extends TestCase
34
{
35
    /** @var \PHPUnit\Framework\MockObject\MockObject */
36
    protected $repository;
37
38
    /** @var \PHPUnit\Framework\MockObject\MockObject */
39
    protected $urlAliasService;
40
41
    /** @var \PHPUnit\Framework\MockObject\MockObject */
42
    protected $locationService;
43
44
    /** @var \PHPUnit\Framework\MockObject\MockObject */
45
    protected $contentService;
46
47
    /** @var \PHPUnit\Framework\MockObject\MockObject */
48
    protected $urlALiasGenerator;
49
50
    protected $requestContext;
51
52
    /** @var UrlAliasRouter */
53
    protected $router;
54
55
    protected function setUp(): void
56
    {
57
        parent::setUp();
58
        $repositoryClass = Repository::class;
59
        $this->repository = $repository = $this
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/pull/3687

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
60
            ->getMockBuilder($repositoryClass)
61
            ->disableOriginalConstructor()
62
            ->setMethods(
63
                array_diff(
64
                    get_class_methods($repositoryClass),
65
                    ['sudo']
66
                )
67
            )
68
            ->getMock();
69
        $this->urlAliasService = $this->createMock(URLAliasService::class);
70
        $this->locationService = $this->createMock(LocationService::class);
71
        $this->contentService = $this->createMock(ContentService::class);
72
        $this->urlALiasGenerator = $this
73
            ->getMockBuilder(UrlAliasGenerator::class)
74
            ->setConstructorArgs(
75
                [
76
                    $repository,
77
                    $this->createMock(RouterInterface::class),
78
                    $this->createMock(ConfigResolverInterface::class),
79
                ]
80
            )
81
            ->getMock();
82
        $this->requestContext = new RequestContext();
83
84
        $this->router = $this->getRouter($this->locationService, $this->urlAliasService, $this->contentService, $this->urlALiasGenerator, $this->requestContext);
85
    }
86
87
    /**
88
     * @param \eZ\Publish\API\Repository\LocationService $locationService
89
     * @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
90
     * @param \eZ\Publish\API\Repository\ContentService $contentService
91
     * @param UrlAliasGenerator $urlAliasGenerator
92
     * @param RequestContext $requestContext
93
     *
94
     * @return UrlAliasRouter
95
     */
96
    protected function getRouter(LocationService $locationService, URLAliasService $urlAliasService, ContentService $contentService, UrlAliasGenerator $urlAliasGenerator, RequestContext $requestContext)
97
    {
98
        return new UrlAliasRouter($locationService, $urlAliasService, $contentService, $urlAliasGenerator, $requestContext);
99
    }
100
101
    public function testRequestContext()
102
    {
103
        $this->assertSame($this->requestContext, $this->router->getContext());
104
        $newContext = new RequestContext();
105
        $this->urlALiasGenerator
106
            ->expects($this->once())
107
            ->method('setRequestContext')
108
            ->with($newContext);
109
        $this->router->setContext($newContext);
110
        $this->assertSame($newContext, $this->router->getContext());
111
    }
112
113
    public function testMatch()
114
    {
115
        $this->expectException(\RuntimeException::class);
116
117
        $this->router->match('/foo');
118
    }
119
120
    /**
121
     * @dataProvider providerTestSupports
122
     */
123
    public function testSupports($routeReference, $isSupported)
124
    {
125
        $this->assertSame($isSupported, $this->router->supports($routeReference));
126
    }
127
128
    public function providerTestSupports()
129
    {
130
        return [
131
            [new Location(), true],
132
            [new \stdClass(), false],
133
            [UrlAliasRouter::URL_ALIAS_ROUTE_NAME, true],
134
            ['some_route_name', false],
135
        ];
136
    }
137
138
    public function testGetRouteCollection()
139
    {
140
        $this->assertInstanceOf(RouteCollection::class, $this->router->getRouteCollection());
141
    }
142
143
    /**
144
     * @param $pathInfo
145
     *
146
     * @return Request
147
     */
148
    protected function getRequestByPathInfo($pathInfo)
149
    {
150
        $request = Request::create($pathInfo);
151
        $request->attributes->set('semanticPathinfo', $pathInfo);
152
        $request->attributes->set(
153
            'siteaccess',
154
            new SiteAccess(
155
                'test',
156
                'fake',
157
                $this->createMock(Matcher::class)
158
            )
159
        );
160
161
        return $request;
162
    }
163
164
    public function testMatchRequestLocation()
165
    {
166
        $pathInfo = '/foo/bar';
167
        $destinationId = 123;
168
        $urlAlias = new URLAlias(
169
            [
170
                'path' => $pathInfo,
171
                'type' => UrlAlias::LOCATION,
172
                'destination' => $destinationId,
173
                'isHistory' => false,
174
            ]
175
        );
176
        $request = $this->getRequestByPathInfo($pathInfo);
177
        $this->urlAliasService
178
            ->expects($this->once())
179
            ->method('lookup')
180
            ->with($pathInfo)
181
            ->will($this->returnValue($urlAlias));
182
        $this->urlALiasGenerator
183
            ->expects($this->once())
184
            ->method('loadLocation')
185
            ->will($this->returnValue(new Location(['contentInfo' => new ContentInfo(['id' => 456])])));
186
187
        $expected = [
188
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
189
            '_controller' => UrlAliasRouter::VIEW_ACTION,
190
            'locationId' => $destinationId,
191
            'contentId' => 456,
192
            'viewType' => ViewManager::VIEW_TYPE_FULL,
193
            'layout' => true,
194
        ];
195
        $this->assertEquals($expected, $this->router->matchRequest($request));
196
    }
197
198 View Code Duplication
    public function testMatchRequestLocationWithCaseRedirect()
199
    {
200
        $pathInfo = '/Foo/bAR';
201
        $urlAliasPath = '/foo/bar';
202
        $destinationId = 123;
203
        $urlAlias = new URLAlias(
204
            [
205
                'path' => $urlAliasPath,
206
                'type' => UrlAlias::LOCATION,
207
                'destination' => $destinationId,
208
                'isHistory' => false,
209
            ]
210
        );
211
        $request = $this->getRequestByPathInfo($pathInfo);
212
        $this->urlALiasGenerator
213
            ->expects($this->once())
214
            ->method('isUriPrefixExcluded')
215
            ->with($pathInfo)
216
            ->will($this->returnValue(false));
217
        $this->urlAliasService
218
            ->expects($this->once())
219
            ->method('lookup')
220
            ->with($pathInfo)
221
            ->will($this->returnValue($urlAlias));
222
        $this->urlALiasGenerator
223
            ->expects($this->once())
224
            ->method('loadLocation')
225
            ->will($this->returnValue(new Location(['contentInfo' => new ContentInfo(['id' => 456])])));
226
227
        $expected = [
228
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
229
            '_controller' => UrlAliasRouter::VIEW_ACTION,
230
            'locationId' => $destinationId,
231
            'contentId' => 456,
232
            'viewType' => ViewManager::VIEW_TYPE_FULL,
233
            'layout' => true,
234
            'needsRedirect' => true,
235
            'semanticPathinfo' => $urlAliasPath,
236
        ];
237
        $this->assertEquals($expected, $this->router->matchRequest($request));
238
    }
239
240 View Code Duplication
    public function testMatchRequestLocationWrongCaseUriPrefixExcluded()
241
    {
242
        $pathInfo = '/Foo/bAR';
243
        $urlAliasPath = '/foo/bar';
244
        $destinationId = 123;
245
        $urlAlias = new URLAlias(
246
            [
247
                'path' => $urlAliasPath,
248
                'type' => UrlAlias::LOCATION,
249
                'destination' => $destinationId,
250
                'isHistory' => false,
251
            ]
252
        );
253
        $request = $this->getRequestByPathInfo($pathInfo);
254
        $this->urlALiasGenerator
255
            ->expects($this->once())
256
            ->method('isUriPrefixExcluded')
257
            ->with($pathInfo)
258
            ->will($this->returnValue(true));
259
        $this->urlAliasService
260
            ->expects($this->once())
261
            ->method('lookup')
262
            ->with($pathInfo)
263
            ->will($this->returnValue($urlAlias));
264
        $this->urlALiasGenerator
265
            ->expects($this->once())
266
            ->method('loadLocation')
267
            ->will($this->returnValue(new Location(['contentInfo' => new ContentInfo(['id' => 456])])));
268
269
        $expected = [
270
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
271
            '_controller' => UrlAliasRouter::VIEW_ACTION,
272
            'contentId' => 456,
273
            'locationId' => $destinationId,
274
            'viewType' => ViewManager::VIEW_TYPE_FULL,
275
            'layout' => true,
276
            'needsRedirect' => true,
277
            'semanticPathinfo' => $urlAliasPath,
278
        ];
279
        $this->assertEquals($expected, $this->router->matchRequest($request));
280
    }
281
282
    public function testMatchRequestLocationCorrectCaseUriPrefixExcluded()
283
    {
284
        $pathInfo = $urlAliasPath = '/foo/bar';
285
        $destinationId = 123;
286
        $urlAlias = new URLAlias(
287
            [
288
                'path' => $urlAliasPath,
289
                'type' => UrlAlias::LOCATION,
290
                'destination' => $destinationId,
291
                'isHistory' => false,
292
            ]
293
        );
294
        $request = $this->getRequestByPathInfo($pathInfo);
295
        $this->urlALiasGenerator
296
            ->expects($this->once())
297
            ->method('isUriPrefixExcluded')
298
            ->with($pathInfo)
299
            ->will($this->returnValue(true));
300
        $this->urlAliasService
301
            ->expects($this->once())
302
            ->method('lookup')
303
            ->with($pathInfo)
304
            ->will($this->returnValue($urlAlias));
305
        $this->urlALiasGenerator
306
            ->expects($this->once())
307
            ->method('loadLocation')
308
            ->will($this->returnValue(new Location(['contentInfo' => new ContentInfo(['id' => 456])])));
309
310
        $expected = [
311
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
312
            '_controller' => UrlAliasRouter::VIEW_ACTION,
313
            'locationId' => $destinationId,
314
            'contentId' => 456,
315
            'viewType' => ViewManager::VIEW_TYPE_FULL,
316
            'layout' => true,
317
        ];
318
        $this->assertEquals($expected, $this->router->matchRequest($request));
319
        $this->assertFalse($request->attributes->has('needsRedirect'));
320
        $this->assertSame($pathInfo, $request->attributes->get('semanticPathinfo'));
321
    }
322
323
    public function testMatchRequestLocationHistory()
324
    {
325
        $pathInfo = '/foo/bar';
326
        $newPathInfo = '/foo/bar-new';
327
        $destinationId = 123;
328
        $destinationLocation = new Location([
329
            'id' => $destinationId,
330
            'contentInfo' => new ContentInfo(['id' => 456]),
331
        ]);
332
        $urlAlias = new URLAlias(
333
            [
334
                'path' => $pathInfo,
335
                'type' => UrlAlias::LOCATION,
336
                'destination' => $destinationId,
337
                'isHistory' => true,
338
            ]
339
        );
340
        $request = $this->getRequestByPathInfo($pathInfo);
341
        $this->urlAliasService
342
            ->expects($this->once())
343
            ->method('lookup')
344
            ->with($pathInfo)
345
            ->will($this->returnValue($urlAlias));
346
        $this->urlALiasGenerator
347
            ->expects($this->once())
348
            ->method('generate')
349
            ->with($destinationLocation)
350
            ->will($this->returnValue($newPathInfo));
351
        $this->urlALiasGenerator
352
            ->expects($this->once())
353
            ->method('loadLocation')
354
            ->will($this->returnValue($destinationLocation));
355
356
        $expected = [
357
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
358
            '_controller' => UrlAliasRouter::VIEW_ACTION,
359
            'locationId' => $destinationId,
360
            'contentId' => 456,
361
            'viewType' => ViewManager::VIEW_TYPE_FULL,
362
            'layout' => true,
363
            'needsRedirect' => true,
364
            'semanticPathinfo' => $newPathInfo,
365
            'prependSiteaccessOnRedirect' => false,
366
        ];
367
        $this->assertEquals($expected, $this->router->matchRequest($request));
368
    }
369
370
    public function testMatchRequestLocationCustom()
371
    {
372
        $pathInfo = '/foo/bar';
373
        $destinationId = 123;
374
        $urlAlias = new URLAlias(
375
            [
376
                'path' => $pathInfo,
377
                'type' => UrlAlias::LOCATION,
378
                'destination' => $destinationId,
379
                'isHistory' => false,
380
                'isCustom' => true,
381
                'forward' => false,
382
            ]
383
        );
384
        $request = $this->getRequestByPathInfo($pathInfo);
385
        $this->urlAliasService
386
            ->expects($this->once())
387
            ->method('lookup')
388
            ->with($pathInfo)
389
            ->will($this->returnValue($urlAlias));
390
        $this->urlALiasGenerator
391
            ->expects($this->once())
392
            ->method('loadLocation')
393
            ->will($this->returnValue(new Location(['contentInfo' => new ContentInfo(['id' => 456])])));
394
395
        $expected = [
396
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
397
            '_controller' => UrlAliasRouter::VIEW_ACTION,
398
            'locationId' => $destinationId,
399
            'contentId' => 456,
400
            'viewType' => ViewManager::VIEW_TYPE_FULL,
401
            'layout' => true,
402
        ];
403
        $this->assertEquals($expected, $this->router->matchRequest($request));
404
    }
405
406
    public function testMatchRequestLocationCustomForward()
407
    {
408
        $pathInfo = '/foo/bar';
409
        $newPathInfo = '/foo/bar-new';
410
        $destinationId = 123;
411
        $destinationLocation = new Location([
412
            'id' => $destinationId,
413
            'contentInfo' => new ContentInfo(['id' => 456]),
414
        ]);
415
        $urlAlias = new URLAlias(
416
            [
417
                'path' => $pathInfo,
418
                'type' => UrlAlias::LOCATION,
419
                'destination' => $destinationId,
420
                'isHistory' => false,
421
                'isCustom' => true,
422
                'forward' => true,
423
            ]
424
        );
425
        $request = $this->getRequestByPathInfo($pathInfo);
426
        $this->urlAliasService
427
            ->expects($this->once())
428
            ->method('lookup')
429
            ->with($pathInfo)
430
            ->will($this->returnValue($urlAlias));
431
        $this->urlALiasGenerator
432
            ->expects($this->once())
433
            ->method('loadLocation')
434
            ->with($destinationId)
435
            ->will(
436
                $this->returnValue($destinationLocation)
437
            );
438
        $this->urlALiasGenerator
439
            ->expects($this->once())
440
            ->method('generate')
441
            ->with($destinationLocation)
442
            ->will($this->returnValue($newPathInfo));
443
        $this->urlALiasGenerator
444
            ->expects($this->once())
445
            ->method('loadLocation')
446
            ->will($this->returnValue($destinationLocation));
447
448
        $expected = [
449
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
450
            '_controller' => UrlAliasRouter::VIEW_ACTION,
451
            'locationId' => $destinationId,
452
            'contentId' => 456,
453
            'viewType' => ViewManager::VIEW_TYPE_FULL,
454
            'layout' => true,
455
            'needsRedirect' => true,
456
            'semanticPathinfo' => $newPathInfo,
457
            'prependSiteaccessOnRedirect' => false,
458
        ];
459
        $this->assertEquals($expected, $this->router->matchRequest($request));
460
    }
461
462
    public function testMatchRequestFail()
463
    {
464
        $this->expectException(\Symfony\Component\Routing\Exception\ResourceNotFoundException::class);
465
466
        $pathInfo = '/foo/bar';
467
        $request = $this->getRequestByPathInfo($pathInfo);
468
        $this->urlAliasService
469
            ->expects($this->once())
470
            ->method('lookup')
471
            ->with($pathInfo)
472
            ->will($this->throwException(new NotFoundException('URLAlias', $pathInfo)));
473
        $this->router->matchRequest($request);
474
    }
475
476 View Code Duplication
    public function testMatchRequestResource()
477
    {
478
        $pathInfo = '/hello_content/hello_search';
479
        $destination = '/content/search';
480
        $urlAlias = new URLAlias(
481
            [
482
                'destination' => $destination,
483
                'path' => $pathInfo,
484
                'type' => UrlAlias::RESOURCE,
485
            ]
486
        );
487
        $request = $this->getRequestByPathInfo($pathInfo);
488
        $this->urlAliasService
489
            ->expects($this->once())
490
            ->method('lookup')
491
            ->with($pathInfo)
492
            ->will($this->returnValue($urlAlias));
493
494
        $expected = [
495
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
496
            'semanticPathinfo' => $destination,
497
            'needsForward' => true,
498
        ];
499
        $this->assertEquals($expected, $this->router->matchRequest($request));
500
    }
501
502 View Code Duplication
    public function testMatchRequestResourceWithRedirect()
503
    {
504
        $pathInfo = '/hello_content/hello_search';
505
        $destination = '/content/search';
506
        $urlAlias = new URLAlias(
507
            [
508
                'destination' => $destination,
509
                'path' => $pathInfo,
510
                'type' => UrlAlias::RESOURCE,
511
                'forward' => true,
512
            ]
513
        );
514
        $request = $this->getRequestByPathInfo($pathInfo);
515
        $this->urlAliasService
516
            ->expects($this->once())
517
            ->method('lookup')
518
            ->with($pathInfo)
519
            ->will($this->returnValue($urlAlias));
520
521
        $expected = [
522
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
523
            'needsRedirect' => true,
524
            'semanticPathinfo' => $destination,
525
        ];
526
        $this->assertEquals($expected, $this->router->matchRequest($request));
527
    }
528
529
    public function testMatchRequestResourceWithCaseRedirect()
530
    {
531
        $pathInfo = '/heLLo_contEnt/hEllo_SEarch';
532
        $urlAliasPath = '/hello_content/hello_search';
533
        $destination = '/content/search';
534
        $urlAlias = new URLAlias(
535
            [
536
                'destination' => $destination,
537
                'path' => $urlAliasPath,
538
                'type' => UrlAlias::RESOURCE,
539
                'forward' => false,
540
            ]
541
        );
542
        $request = $this->getRequestByPathInfo($pathInfo);
543
        $this->urlALiasGenerator
544
            ->expects($this->once())
545
            ->method('isUriPrefixExcluded')
546
            ->with($pathInfo)
547
            ->will($this->returnValue(false));
548
        $this->urlAliasService
549
            ->expects($this->once())
550
            ->method('lookup')
551
            ->with($pathInfo)
552
            ->will($this->returnValue($urlAlias));
553
554
        $expected = [
555
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
556
            'semanticPathinfo' => $urlAliasPath,
557
            'needsRedirect' => true,
558
        ];
559
        $this->assertEquals($expected, $this->router->matchRequest($request));
560
    }
561
562
    /**
563
     * Tests that forwarding custom alias will redirect to the resource destination rather than
564
     * to the case-corrected alias.
565
     */
566 View Code Duplication
    public function testMatchRequestResourceCaseIncorrectWithForwardRedirect()
567
    {
568
        $pathInfo = '/heLLo_contEnt/hEllo_SEarch';
569
        $urlAliasPath = '/hello_content/hello_search';
570
        $destination = '/content/search';
571
        $urlAlias = new URLAlias(
572
            [
573
                'destination' => $destination,
574
                'path' => $urlAliasPath,
575
                'type' => UrlAlias::RESOURCE,
576
                'forward' => true,
577
            ]
578
        );
579
        $request = $this->getRequestByPathInfo($pathInfo);
580
        $this->urlAliasService
581
            ->expects($this->once())
582
            ->method('lookup')
583
            ->with($pathInfo)
584
            ->will($this->returnValue($urlAlias));
585
586
        $expected = [
587
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
588
            'semanticPathinfo' => $destination,
589
            'needsRedirect' => true,
590
        ];
591
        $this->assertEquals($expected, $this->router->matchRequest($request));
592
    }
593
594
    public function testMatchRequestVirtual()
595
    {
596
        $pathInfo = '/foo/bar';
597
        $urlAlias = new URLAlias(
598
            [
599
                'path' => $pathInfo,
600
                'type' => UrlAlias::VIRTUAL,
601
            ]
602
        );
603
        $request = $this->getRequestByPathInfo($pathInfo);
604
        $this->urlAliasService
605
            ->expects($this->once())
606
            ->method('lookup')
607
            ->with($pathInfo)
608
            ->will($this->returnValue($urlAlias));
609
610
        $expected = [
611
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
612
            'semanticPathinfo' => '/',
613
            'needsForward' => true,
614
        ];
615
        $this->assertEquals($expected, $this->router->matchRequest($request));
616
    }
617
618
    public function testMatchRequestVirtualWithCaseRedirect()
619
    {
620
        $pathInfo = '/Foo/bAR';
621
        $urlAliasPath = '/foo/bar';
622
        $urlAlias = new URLAlias(
623
            [
624
                'path' => $urlAliasPath,
625
                'type' => UrlAlias::VIRTUAL,
626
            ]
627
        );
628
        $request = $this->getRequestByPathInfo($pathInfo);
629
        $this->urlALiasGenerator
630
            ->expects($this->once())
631
            ->method('isUriPrefixExcluded')
632
            ->with($pathInfo)
633
            ->will($this->returnValue(false));
634
        $this->urlAliasService
635
            ->expects($this->once())
636
            ->method('lookup')
637
            ->with($pathInfo)
638
            ->will($this->returnValue($urlAlias));
639
640
        $expected = [
641
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
642
            'semanticPathinfo' => $urlAliasPath,
643
            'needsRedirect' => true,
644
        ];
645
        $this->assertEquals($expected, $this->router->matchRequest($request));
646
    }
647
648
    public function testGenerateFail()
649
    {
650
        $this->expectException(\Symfony\Component\Routing\Exception\RouteNotFoundException::class);
651
652
        $this->router->generate('invalidRoute');
653
    }
654
655 View Code Duplication
    public function testGenerateWithRouteObject(): void
656
    {
657
        $location = new Location(['id' => 54]);
658
        $parameters = [
659
            'some' => 'thing',
660
        ];
661
662
        $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH;
663
        $generatedLink = '/foo/bar';
664
665
        $this->urlALiasGenerator
666
            ->expects($this->once())
667
            ->method('generate')
668
            ->with($location, $parameters, $referenceType)
669
            ->will($this->returnValue($generatedLink));
670
671
        $this->assertSame(
672
            $generatedLink,
673
            $this->router->generate(
674
                '',
675
                $parameters + [
676
                    RouteObjectInterface::ROUTE_OBJECT => $location,
677
                ],
678
                $referenceType
679
            )
680
        );
681
    }
682
683
    public function testGenerateNoLocation()
684
    {
685
        $this->expectException(\InvalidArgumentException::class);
686
687
        $this->router->generate(UrlAliasRouter::URL_ALIAS_ROUTE_NAME, ['foo' => 'bar']);
688
    }
689
690
    public function testGenerateInvalidLocation()
691
    {
692
        $this->expectException(\LogicException::class);
693
694
        $this->router->generate(UrlAliasRouter::URL_ALIAS_ROUTE_NAME, ['location' => new \stdClass()]);
695
    }
696
697
    public function testGenerateWithLocationId()
698
    {
699
        $locationId = 123;
700
        $location = new Location(['id' => $locationId]);
701
        $parameters = ['some' => 'thing'];
702
        $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH;
703
        $generatedLink = '/foo/bar';
704
        $this->locationService
705
            ->expects($this->once())
706
            ->method('loadLocation')
707
            ->with($locationId)
708
            ->will($this->returnValue($location));
709
        $this->urlALiasGenerator
710
            ->expects($this->once())
711
            ->method('generate')
712
            ->with($location, $parameters, $referenceType)
713
            ->will($this->returnValue($generatedLink));
714
        $this->assertSame(
715
            $generatedLink,
716
            $this->router->generate(
717
                UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
718
                $parameters + ['locationId' => $locationId],
719
                $referenceType
720
            )
721
        );
722
    }
723
724 View Code Duplication
    public function testGenerateWithLocationAsParameter()
725
    {
726
        $locationId = 123;
727
        $location = new Location(['id' => $locationId]);
728
        $parameters = ['some' => 'thing'];
729
        $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH;
730
        $generatedLink = '/foo/bar';
731
        $this->urlALiasGenerator
732
            ->expects($this->once())
733
            ->method('generate')
734
            ->with($location, $parameters, $referenceType)
735
            ->will($this->returnValue($generatedLink));
736
        $this->assertSame(
737
            $generatedLink,
738
            $this->router->generate(
739
                UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
740
                $parameters + ['location' => $location],
741
                $referenceType
742
            )
743
        );
744
    }
745
746
    public function testGenerateWithContentId()
747
    {
748
        $locationId = 123;
749
        $contentId = 456;
750
        $location = new Location(['id' => $locationId]);
751
        $contentInfo = new ContentInfo(['id' => $contentId, 'mainLocationId' => $locationId]);
752
        $parameters = ['some' => 'thing'];
753
        $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH;
754
        $generatedLink = '/foo/bar';
755
        $this->contentService
756
            ->expects($this->once())
757
            ->method('loadContentInfo')
758
            ->with($contentId)
759
            ->will($this->returnValue($contentInfo));
760
        $this->locationService
761
            ->expects($this->once())
762
            ->method('loadLocation')
763
            ->with($contentInfo->mainLocationId)
764
            ->will($this->returnValue($location));
765
        $this->urlALiasGenerator
766
            ->expects($this->once())
767
            ->method('generate')
768
            ->with($location, $parameters, $referenceType)
769
            ->will($this->returnValue($generatedLink));
770
        $this->assertSame(
771
            $generatedLink,
772
            $this->router->generate(
773
                UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
774
                $parameters + ['contentId' => $contentId],
775
                $referenceType
776
            )
777
        );
778
    }
779
780 View Code Duplication
    public function testGenerateWithContentIdWithMissingMainLocation()
781
    {
782
        $this->expectException(\LogicException::class);
783
784
        $contentId = 456;
785
        $contentInfo = new ContentInfo(['id' => $contentId, 'mainLocationId' => null]);
786
        $parameters = ['some' => 'thing'];
787
        $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH;
788
        $this->contentService
789
            ->expects($this->once())
790
            ->method('loadContentInfo')
791
            ->with($contentId)
792
            ->will($this->returnValue($contentInfo));
793
794
        $this->router->generate(
795
            UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
796
            $parameters + ['contentId' => $contentId],
797
            $referenceType
798
        );
799
    }
800
}
801