diff --git a/composer.json b/composer.json index b4ab9ef..a12abe6 100644 --- a/composer.json +++ b/composer.json @@ -15,5 +15,8 @@ "email": "email@sharkk.net" } ], - "minimum-stability": "stable" + "minimum-stability": "stable", + "require-dev": { + "phpunit/phpunit": "^11.5" + } } diff --git a/tests/SegmentRouterTest.php b/tests/SegmentRouterTest.php new file mode 100644 index 0000000..e430bde --- /dev/null +++ b/tests/SegmentRouterTest.php @@ -0,0 +1,84 @@ +router = new SegmentRouter(); + } + + public function testAddAndLookupRoute(): void + { + $routes = []; + SegmentRouter::add($routes, 'GET', '/blog/:id', function($id) { + return "Blog post $id"; + }); + + $result = SegmentRouter::lookup($routes, 'GET', '/blog/123'); + $this->assertEquals(200, $result['code']); + $this->assertIsCallable($result['handler']); + $this->assertEquals(['123'], $result['params']); + $this->assertEquals("Blog post 123", $result['handler'](...$result['params'])); + } + + public function testLookupNotFound(): void + { + $routes = []; + SegmentRouter::add($routes, 'GET', '/blog/:id', function($id) { + return "Blog post $id"; + }); + + $result = SegmentRouter::lookup($routes, 'GET', '/nonexistent'); + $this->assertEquals(404, $result['code']); + $this->assertNull($result['handler']); + $this->assertEmpty($result['params']); + } + + public function testMethodNotAllowed(): void + { + $routes = []; + SegmentRouter::add($routes, 'GET', '/blog/:id', function($id) { + return "Blog post $id"; + }); + + $result = SegmentRouter::lookup($routes, 'POST', '/blog/123'); + $this->assertEquals(405, $result['code']); + $this->assertNull($result['handler']); + $this->assertEmpty($result['params']); + } + + public function testClearRoutes(): void + { + $routes = []; + SegmentRouter::add($routes, 'GET', '/blog/:id', function($id) { + return "Blog post $id"; + }); + + SegmentRouter::clear($routes); + $this->assertEmpty($routes); + } + + public function testAddMultipleMethods(): void + { + $routes = []; + SegmentRouter::add($routes, 'GET', '/blog/:id', function($id) { + return "GET Blog post $id"; + }); + SegmentRouter::add($routes, 'POST', '/blog/:id', function($id) { + return "POST Blog post $id"; + }); + + $resultGet = SegmentRouter::lookup($routes, 'GET', '/blog/123'); + $this->assertEquals(200, $resultGet['code']); + $this->assertEquals("GET Blog post 123", $resultGet['handler'](...$resultGet['params'])); + + $resultPost = SegmentRouter::lookup($routes, 'POST', '/blog/123'); + $this->assertEquals(200, $resultPost['code']); + $this->assertEquals("POST Blog post 123", $resultPost['handler'](...$resultPost['params'])); + } +} \ No newline at end of file diff --git a/tests/test.php b/tests/test.php index f6c08c0..3c0f945 100644 --- a/tests/test.php +++ b/tests/test.php @@ -70,7 +70,11 @@ $testRoutes = [ }], ]; -array_walk($testRoutes, fn($route) => SegmentRouter::add($routes, $route[0], $route[1], $route[2])); +$routes = array_map(function($route) use (&$routes) { + [$method, $path, $handler] = $route; + SegmentRouter::add($routes, $method, $path, $handler); + return [$method, $path, $handler]; +}, $testRoutes); for ($i = 0; $i < 10; $i++) {