diff options
| author | Brett Parson <brett@brett-parson.com> | 2026-08-21 13:55:37 -0500 |
|---|---|---|
| committer | Brett Parson <brett@brett-parson.com> | 2026-08-21 14:00:11 -0500 |
| commit | d039a5d62a9ac6ababae70d9d96422ca59f2b2d4 (patch) | |
| tree | f0db4750a571e8040a5da02cc7190079c0d62914 /tests/RouteMatcherTest.php | |
| parent | 0de77a6334cee1f6e15f7e0fd35602514560be33 (diff) | |
| download | miniroute-d039a5d62a9ac6ababae70d9d96422ca59f2b2d4.tar.gz miniroute-d039a5d62a9ac6ababae70d9d96422ca59f2b2d4.zip | |
Scaffold miniroute routing/middleware kernel
Diffstat (limited to 'tests/RouteMatcherTest.php')
| -rw-r--r-- | tests/RouteMatcherTest.php | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/tests/RouteMatcherTest.php b/tests/RouteMatcherTest.php new file mode 100644 index 0000000..5181c50 --- /dev/null +++ b/tests/RouteMatcherTest.php @@ -0,0 +1,68 @@ +<?php + +declare(strict_types=1); + +namespace BrettParson\MiniRoute\Tests; + +use BrettParson\MiniRoute\Routing\RouteDef; +use BrettParson\MiniRoute\Routing\RouteMatcher; +use PHPUnit\Framework\TestCase; + +final class RouteMatcherTest extends TestCase +{ + private RouteMatcher $matcher; + + protected function setUp(): void + { + $this->matcher = new RouteMatcher(); + } + + private function route(string $method, string $path): RouteDef + { + return new RouteDef($method, $path, 'C', 'm'); + } + + public function testMatchesExactPath(): void + { + $route = $this->route('GET', '/entries'); + + $this->assertTrue($this->matcher->matches($route, 'get', '/entries')); + $this->assertFalse($this->matcher->matches($route, 'POST', '/entries')); + $this->assertFalse($this->matcher->matches($route, 'GET', '/entries/1')); + } + + public function testMatchesParameterizedPath(): void + { + $route = $this->route('GET', '/entries/{slug}'); + + $this->assertTrue($this->matcher->matches($route, 'GET', '/entries/hello-world')); + $this->assertFalse($this->matcher->matches($route, 'GET', '/entries/hello/world')); + } + + public function testExtractsNamedParameters(): void + { + $route = $this->route('GET', '/entries/{slug}'); + + $this->assertSame( + ['slug' => 'hello-world'], + $this->matcher->extractParams($route, '/entries/hello-world'), + ); + } + + public function testLiteralBeatsParameter(): void + { + $literal = $this->route('GET', '/entries/archive'); + $param = $this->route('GET', '/entries/{slug}'); + + $this->assertLessThan(0, $this->matcher->compare($literal, $param)); + $this->assertGreaterThan(0, $this->matcher->compare($param, $literal)); + } + + public function testExactBeatsParameterAtSamePosition(): void + { + $exact = $this->route('POST', '/admin/entries/preview'); + $param = $this->route('POST', '/admin/entries/{id}'); + + $this->assertLessThan(0, $this->matcher->compare($exact, $param)); + } +} |
