|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Endroid\QrCodeBundle\Tests\Routing; |
| 6 | + |
| 7 | +use Endroid\QrCodeBundle\Routing\QrCodeRouteLoader; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Symfony\Component\Config\Loader\LoaderInterface; |
| 11 | +use Symfony\Component\Routing\Matcher\UrlMatcher; |
| 12 | +use Symfony\Component\Routing\RequestContext; |
| 13 | +use Symfony\Component\Routing\Route; |
| 14 | +use Symfony\Component\Routing\RouteCollection; |
| 15 | + |
| 16 | +final class QrCodeRouteLoaderTest extends TestCase |
| 17 | +{ |
| 18 | + #[DataProvider('providePriorities')] |
| 19 | + public function testRoutePriority(?int $priority, int $catchAllPriority, string $expectedRoute): void |
| 20 | + { |
| 21 | + $routes = new RouteCollection(); |
| 22 | + $routes->add('catch_all', new Route('/{slug}', [], ['slug' => '.*']), $catchAllPriority); |
| 23 | + |
| 24 | + $loader = $this->createStub(LoaderInterface::class); |
| 25 | + $loader->method('load')->willReturn($routes); |
| 26 | + |
| 27 | + $routeLoader = $priority === null |
| 28 | + ? new QrCodeRouteLoader('app', '/2fa/qr') |
| 29 | + : new QrCodeRouteLoader('app', '/2fa/qr', $priority); |
| 30 | + $matcher = new UrlMatcher($routeLoader($loader, null), new RequestContext()); |
| 31 | + |
| 32 | + self::assertSame($expectedRoute, $matcher->match('/2fa/qr/default/test')['_route']); |
| 33 | + self::assertSame('catch_all', $matcher->match('/other/page')['_route']); |
| 34 | + } |
| 35 | + |
| 36 | + public static function providePriorities(): iterable |
| 37 | + { |
| 38 | + yield 'omitted priority preserves application precedence' => [null, 0, 'catch_all']; |
| 39 | + yield 'equal priority preserves application precedence' => [0, 0, 'catch_all']; |
| 40 | + yield 'higher priority beats catch-all' => [1, 0, 'qr_code_generate']; |
| 41 | + yield 'higher application priority still wins' => [1, 2, 'catch_all']; |
| 42 | + yield 'negative priorities are respected' => [-2, -1, 'catch_all']; |
| 43 | + } |
| 44 | + |
| 45 | + public function testExplicitRouteIsPreserved(): void |
| 46 | + { |
| 47 | + $routes = new RouteCollection(); |
| 48 | + $routes->add('catch_all', new Route('/{slug}', [], ['slug' => '.*'])); |
| 49 | + $routes->add('qr_code_generate', new Route('/manual/{data}'), 1); |
| 50 | + |
| 51 | + $loader = $this->createStub(LoaderInterface::class); |
| 52 | + $loader->method('load')->willReturn($routes); |
| 53 | + |
| 54 | + $routeLoader = new QrCodeRouteLoader('app', '/2fa/qr', -1); |
| 55 | + $matcher = new UrlMatcher($routeLoader($loader, null), new RequestContext()); |
| 56 | + |
| 57 | + self::assertSame('qr_code_generate', $matcher->match('/manual/test')['_route']); |
| 58 | + self::assertSame('catch_all', $matcher->match('/2fa/qr/default/test')['_route']); |
| 59 | + } |
| 60 | +} |
0 commit comments