‪TYPO3CMS  ‪main
StaticRouteResolverTest.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;
21 use Psr\Http\Message\ResponseInterface;
22 use Psr\Http\Message\ServerRequestInterface;
23 use Psr\Http\Server\RequestHandlerInterface;
31 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
32 
33 final class ‪StaticRouteResolverTest extends UnitTestCase
34 {
35  protected RequestHandlerInterface ‪$requestHandler;
36 
37  protected function ‪setUp(): void
38  {
39  parent::setUp();
40  // A request handler which expects a site to be found.
41  $this->requestHandler = new class () implements RequestHandlerInterface {
42  public function handle(ServerRequestInterface $request): ResponseInterface
43  {
44  return new ‪NullResponse();
45  }
46  };
47  }
48 
49  #[Test]
50  public function ‪invalidStaticRouteDoesNotWork(): void
51  {
52  $requestFactoryMock = $this->getMockBuilder(RequestFactory::class)->disableOriginalConstructor()->getMock();
53  $linkServiceMock = $this->getMockBuilder(LinkService::class)->disableOriginalConstructor()->getMock();
54  $subject = new ‪StaticRouteResolver($requestFactoryMock, $linkServiceMock);
55  $site = new ‪Site('lotus-flower', 13, [
56  'base' => 'https://example.com/',
57  'languages' => [
58  0 => [
59  'languageId' => 0,
60  'locale' => 'en_US.UTF-8',
61  'base' => '/en/',
62  ],
63  ],
64  'routes' => [
65  [
66  'route' => '/lotus/',
67  'type' => 'staticText',
68  'content' => 'nice',
69  ],
70  [
71  'route' => null,
72  'type' => 'staticText',
73  'content' => 'no-route',
74  ],
75  [
76  'route' => '',
77  'type' => 'staticText',
78  'content' => 'empty-route',
79  ],
80  [
81  'route' => '/empty-type',
82  'type' => '',
83  ],
84  [
85  'route' => '/no-type',
86  ],
87  [
88  'route' => '',
89  'type' => '',
90  ],
91  ],
92  ]);
93 
94  $request = new ‪ServerRequest('https://example.com/lotus/');
95  $request = $request->withAttribute('site', $site);
96  $response = $subject->process($request, $this->requestHandler);
97  self::assertInstanceOf(HtmlResponse::class, $response);
98  self::assertEquals('nice', $response->getBody()->getContents());
99 
100  $request = new ‪ServerRequest('https://example.com/nothing');
101  $request = $request->withAttribute('site', $site);
102  $response = $subject->process($request, $this->requestHandler);
103  self::assertInstanceOf(NullResponse::class, $response);
104 
105  $request = new ‪ServerRequest('https://example.com/no-type');
106  $request = $request->withAttribute('site', $site);
107  $response = $subject->process($request, $this->requestHandler);
108  self::assertInstanceOf(NullResponse::class, $response);
109  }
110 }
‪TYPO3\CMS\Frontend\Middleware\StaticRouteResolver
Definition: StaticRouteResolver.php:37
‪TYPO3\CMS\Core\Http\NullResponse
Definition: NullResponse.php:26
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\StaticRouteResolverTest\$requestHandler
‪RequestHandlerInterface $requestHandler
Definition: StaticRouteResolverTest.php:35
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\StaticRouteResolverTest\invalidStaticRouteDoesNotWork
‪invalidStaticRouteDoesNotWork()
Definition: StaticRouteResolverTest.php:50
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware
Definition: PageArgumentValidatorTest.php:18
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\StaticRouteResolverTest
Definition: StaticRouteResolverTest.php:34
‪TYPO3\CMS\Core\Http\RequestFactory
Definition: RequestFactory.php:30
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\StaticRouteResolverTest\setUp
‪setUp()
Definition: StaticRouteResolverTest.php:37
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:28