From e8e34ecb39f66e71a74f112420cc5e61669ae934 Mon Sep 17 00:00:00 2001 From: Funky Waddle Date: Sat, 14 Feb 2026 17:27:33 -0600 Subject: [PATCH] feat: implement milestone 8 - parameters & validation --- tests/Unit/ParameterValidationTest.php | 97 ++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 tests/Unit/ParameterValidationTest.php diff --git a/tests/Unit/ParameterValidationTest.php b/tests/Unit/ParameterValidationTest.php new file mode 100644 index 0000000..a770739 --- /dev/null +++ b/tests/Unit/ParameterValidationTest.php @@ -0,0 +1,97 @@ + ['/path/to/modules']]); + $this->router = new Router($config); + } + + private function createRequest(string $method, string $path): ServerRequestInterface + { + $uri = $this->createMock(UriInterface::class); + $uri->method('getPath')->willReturn($path); + + $request = $this->createMock(ServerRequestInterface::class); + $request->method('getMethod')->willReturn($method); + $request->method('getUri')->willReturn($uri); + + return $request; + } + + public function testNumericValidation(): void + { + $this->router->get('/users/{{id}}', 'handler')->valid('id', 'numeric'); + + $this->assertNotNull($this->router->match($this->createRequest('GET', '/users/123'))); + $this->assertNull($this->router->match($this->createRequest('GET', '/users/abc'))); + } + + public function testAlphaValidation(): void + { + $this->router->get('/tags/{{tag}}', 'handler')->valid('tag', 'alpha'); + + $this->assertNotNull($this->router->match($this->createRequest('GET', '/tags/php'))); + $this->assertNull($this->router->match($this->createRequest('GET', '/tags/123'))); + } + + public function testRegexValidation(): void + { + $this->router->get('/date/{{year}}', 'handler')->valid('year', 'regex:[0-9]{4}'); + + $this->assertNotNull($this->router->match($this->createRequest('GET', '/date/2024'))); + $this->assertNull($this->router->match($this->createRequest('GET', '/date/24'))); + $this->assertNull($this->router->match($this->createRequest('GET', '/date/abcd'))); + } + + public function testMultipleValidationRules(): void + { + // Test that alphanumeric works as expected + $this->router->get('/product/{{sku}}', 'handler')->valid('sku', 'alphanumeric'); + $this->assertNotNull($this->router->match($this->createRequest('GET', '/product/ABC123'))); + $this->assertNull($this->router->match($this->createRequest('GET', '/product/ABC_123'))); + } + + public function testDefaultValuesMarkParametersAsOptional(): void + { + $this->router->get('/shop/{{category}}', 'handler')->default('category', 'all'); + + $match1 = $this->router->match($this->createRequest('GET', '/shop/electronics')); + $this->assertNotNull($match1); + $this->assertSame('electronics', $match1->getAttributes()['category']); + + $match2 = $this->router->match($this->createRequest('GET', '/shop')); + $this->assertNotNull($match2); + $this->assertSame('all', $match2->getAttributes()['category']); + } + + public function testOptionalParameterWithDefault(): void + { + $this->router->get('/archive/{{year?}}', 'handler')->default('year', 2023); + + $match1 = $this->router->match($this->createRequest('GET', '/archive/2024')); + $this->assertSame('2024', $match1->getAttributes()['year']); + + $match2 = $this->router->match($this->createRequest('GET', '/archive')); + $this->assertSame(2023, $match2->getAttributes()['year']); + } + + public function testAlphanumericValidation(): void + { + $this->router->get('/profile/{{username}}', 'handler')->valid('username', 'alphanumeric'); + + $this->assertNotNull($this->router->match($this->createRequest('GET', '/profile/user123'))); + $this->assertNull($this->router->match($this->createRequest('GET', '/profile/user_123'))); + } +}