|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace B13\JustInCase\Tests\Unit\Middleware\Fixtures; |
| 6 | + |
| 7 | +/* |
| 8 | + * This file is part of TYPO3 CMS-based extension "JustInCase" by b13. |
| 9 | + * |
| 10 | + * It is free software; you can redistribute it and/or modify it under |
| 11 | + * the terms of the GNU General Public License, either version 2 |
| 12 | + * of the License, or any later version. |
| 13 | + */ |
| 14 | + |
| 15 | +use Psr\Http\Message\ServerRequestInterface; |
| 16 | +use Psr\Http\Message\UriInterface; |
| 17 | +use TYPO3\CMS\Core\Context\Context; |
| 18 | +use TYPO3\CMS\Core\Routing\RouteNotFoundException; |
| 19 | +use TYPO3\CMS\Core\Routing\RouteResultInterface; |
| 20 | +use TYPO3\CMS\Core\Routing\RouterInterface; |
| 21 | +use TYPO3\CMS\Core\Routing\SiteRouteResult; |
| 22 | +use TYPO3\CMS\Core\Site\Entity\Site; |
| 23 | + |
| 24 | +/** |
| 25 | + * A Site whose router only matches an explicit list of paths. |
| 26 | + * |
| 27 | + * Site::getRouter() builds a PageRouter via GeneralUtility::makeInstance and needs |
| 28 | + * a database, so it is replaced here to keep these tests pure unit tests. |
| 29 | + */ |
| 30 | +final class RoutableSite extends Site |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @param string[] $matchingPaths paths the router resolves; everything else throws |
| 34 | + */ |
| 35 | + public function __construct( |
| 36 | + string $identifier, |
| 37 | + int $rootPageId, |
| 38 | + array $configuration, |
| 39 | + private readonly array $matchingPaths = [] |
| 40 | + ) { |
| 41 | + parent::__construct($identifier, $rootPageId, $configuration); |
| 42 | + } |
| 43 | + |
| 44 | + public function getRouter(?Context $context = null): RouterInterface |
| 45 | + { |
| 46 | + return new class($this, $this->matchingPaths) implements RouterInterface { |
| 47 | + /** |
| 48 | + * @param string[] $matchingPaths |
| 49 | + */ |
| 50 | + public function __construct( |
| 51 | + private readonly Site $site, |
| 52 | + private readonly array $matchingPaths |
| 53 | + ) { |
| 54 | + } |
| 55 | + |
| 56 | + public function matchRequest( |
| 57 | + ServerRequestInterface $request, |
| 58 | + ?RouteResultInterface $previousResult = null |
| 59 | + ): RouteResultInterface { |
| 60 | + if (!in_array($request->getUri()->getPath(), $this->matchingPaths, true)) { |
| 61 | + throw new RouteNotFoundException('No route found', 1579000000); |
| 62 | + } |
| 63 | + return new SiteRouteResult($request->getUri(), $this->site, null); |
| 64 | + } |
| 65 | + |
| 66 | + public function generateUri( |
| 67 | + $route, |
| 68 | + array $parameters = [], |
| 69 | + string $fragment = '', |
| 70 | + string $type = self::ABSOLUTE_URL |
| 71 | + ): UriInterface { |
| 72 | + throw new \BadMethodCallException('Not needed in these tests', 1579000001); |
| 73 | + } |
| 74 | + }; |
| 75 | + } |
| 76 | +} |
0 commit comments