‪TYPO3CMS  ‪main
PageRouterTest.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 
20 use PHPUnit\Framework\Attributes\Test;
31 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
32 
33 final class ‪PageRouterTest extends UnitTestCase
34 {
35  protected bool ‪$resetSingletonInstances = true;
36 
37  protected function ‪setUp(): void
38  {
39  $requestContextFactory = new ‪RequestContextFactory(new ‪BackendEntryPointResolver());
40  GeneralUtility::addInstance(RequestContextFactory::class, $requestContextFactory);
41  parent::setUp(); // TODO: Change the autogenerated stub
42  }
43 
44  #[Test]
46  {
47  $this->expectException(RouteNotFoundException::class);
48  $this->expectExceptionCode(1555303496);
49  $incomingUrl = 'https://king.com/lotus-flower/en/mr-magpie/bloom';
50  $request = new ‪ServerRequest($incomingUrl, 'GET');
51  $subject = new ‪PageRouter(new ‪Site('lotus-flower', 13, []));
52  $subject->matchRequest($request);
53  }
54 
55  #[Test]
56  public function ‪properSiteConfigurationFindsRoute(): void
57  {
58  $incomingUrl = 'https://king.com/lotus-flower/en/mr-magpie/bloom';
59  $pageRecord = ['uid' => 13, 'l10n_parent' => 0, 'slug' => '/mr-magpie/bloom', 'sys_language_uid' => 0];
60  $site = new ‪Site('lotus-flower', 13, [
61  'base' => '/lotus-flower/',
62  'languages' => [
63  0 => [
64  'languageId' => 0,
65  'locale' => 'en_US.UTF-8',
66  'base' => '/en/',
67  ],
68  ],
69  ]);
70  $language = $site->getDefaultLanguage();
71 
72  $pageSlugCandidateProvider = $this->createMock(PageSlugCandidateProvider::class);
73  $pageSlugCandidateProvider->method('getCandidatesForPath')->with('/mr-magpie/bloom', $language)->willReturn([$pageRecord]);
74  GeneralUtility::addInstance(PageSlugCandidateProvider::class, $pageSlugCandidateProvider);
75 
76  $request = new ‪ServerRequest($incomingUrl, 'GET');
77  $previousResult = new ‪SiteRouteResult($request->getUri(), $site, $language, '/mr-magpie/bloom');
78  $routeResult = (new ‪PageRouter($site))->matchRequest($request, $previousResult);
79 
80  $expectedRouteResult = new ‪PageArguments(13, '0', [], [], []);
81  self::assertEquals($expectedRouteResult, $routeResult);
82  }
83 
87  #[Test]
89  {
90  $incomingUrl = 'https://king.com/lotus-flower/en/mr-magpie/bloom';
91  $pageRecord = ['uid' => 13, 'l10n_parent' => 0, 'slug' => '/mr-magpie/bloom', 'sys_language_uid' => 0];
92  $site = new ‪Site('lotus-flower', 13, [
93  'base' => '/lotus-flower/',
94  'languages' => [
95  0 => [
96  'languageId' => 0,
97  'locale' => 'en_US.UTF-8',
98  'base' => '/en',
99  ],
100  ],
101  ]);
102  $language = $site->getDefaultLanguage();
103  $pageSlugCandidateProvider = $this->createMock(PageSlugCandidateProvider::class);
104  $pageSlugCandidateProvider->method('getCandidatesForPath')->with(self::anything())->willReturn([$pageRecord]);
105  GeneralUtility::addInstance(PageSlugCandidateProvider::class, $pageSlugCandidateProvider);
106 
107  $request = new ‪ServerRequest($incomingUrl, 'GET');
108  $previousResult = new ‪SiteRouteResult($request->getUri(), $site, $language, '/mr-magpie/bloom/');
109  $routeResult = (new ‪PageRouter($site))->matchRequest($request, $previousResult);
110 
111  $expectedRouteResult = new ‪PageArguments((int)$pageRecord['uid'], '0', []);
112  self::assertEquals($expectedRouteResult, $routeResult);
113  }
114 }
‪TYPO3\CMS\Core\Routing\PageArguments
Definition: PageArguments.php:26
‪TYPO3\CMS\Core\Routing\PageSlugCandidateProvider
Definition: PageSlugCandidateProvider.php:43
‪TYPO3\CMS\Core\Tests\Unit\Routing
‪TYPO3\CMS\Core\Tests\Unit\Routing\PageRouterTest\setUp
‪setUp()
Definition: PageRouterTest.php:37
‪TYPO3\CMS\Core\Routing\PageRouter
Definition: PageRouter.php:71
‪TYPO3\CMS\Core\Tests\Unit\Routing\PageRouterTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: PageRouterTest.php:35
‪TYPO3\CMS\Core\Routing\RouteNotFoundException
Definition: RouteNotFoundException.php:25
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Core\Tests\Unit\Routing\PageRouterTest\properSiteConfigurationFindsRoute
‪properSiteConfigurationFindsRoute()
Definition: PageRouterTest.php:56
‪TYPO3\CMS\Core\Routing\SiteRouteResult
Definition: SiteRouteResult.php:30
‪TYPO3\CMS\Core\Tests\Unit\Routing\PageRouterTest
Definition: PageRouterTest.php:34
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Core\Tests\Unit\Routing\PageRouterTest\matchRequestThrowsExceptionIfNoPreviousResultGiven
‪matchRequestThrowsExceptionIfNoPreviousResultGiven()
Definition: PageRouterTest.php:45
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Routing\BackendEntryPointResolver
Definition: BackendEntryPointResolver.php:29
‪TYPO3\CMS\Core\Tests\Unit\Routing\PageRouterTest\properSiteConfigurationWithoutTrailingSlashFindsRoute
‪properSiteConfigurationWithoutTrailingSlashFindsRoute()
Definition: PageRouterTest.php:88
‪TYPO3\CMS\Core\Routing\RequestContextFactory
Definition: RequestContextFactory.php:30