diff --git a/.gitignore b/.gitignore index 73d8e98..4832772 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /composer.phar /composer.lock /.phpunit.result.cache +.vscode diff --git a/src/Router.php b/src/Router.php index 6024f80..707fb50 100644 --- a/src/Router.php +++ b/src/Router.php @@ -23,10 +23,11 @@ public static function fromServerlessConfig(array $serverlessConfig): self continue; } - if (is_array($pattern)) { - $pattern = self::patternToString($pattern); + if (is_string($pattern)) { + $pattern = array_combine(['method', 'path'], array_pad(explode(' ', $pattern, 2), 2, '*')); } + $pattern = self::patternToString($pattern); $routes[$pattern] = $function['handler']; } @@ -39,7 +40,7 @@ private static function patternToString(array $pattern): string $path = $pattern['path'] ?? '*'; // Special "any" method MUST be converted to star. - if ($method === 'any') { + if (strtolower($method) === 'any') { $method = '*'; } diff --git a/tests/RouterTest.php b/tests/RouterTest.php index e7c690b..62895d7 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -50,11 +50,33 @@ public function test routing path parameters(): void } /** + * Test routing against function YAML configs + * + * @param string $config YAML config string + * @param array{ + * request: \Nyholm\Psr7\ServerRequest, + * expected: ?string + * }[] $requests Array of requests to test + * * @test + * @dataProvider expandedConfigProvider */ - public function test_routing_expanded_config(): void + public function test_routing_expanded_config(string $config, array $requests): void { - $config = <<match($request['request'])[0]); + } + } + + /** + * Data provider for test_routing_expanded_config + */ + public function expandedConfigProvider(): array + { + $wildcard_array_config = << $wildcard_array_config, + 'requests' => [ + [ + 'request' => $this->request('GET', '/'), + 'expected' => 'api.php', + ], + [ + 'request' => $this->request('POST', '/'), + 'expected' => 'api.php', + ], + [ + 'request' => $this->request('GET', '/tests/1'), + 'expected' => 'api.php', + ], + [ + 'request' => $this->request('PUT', '/tests/1'), + 'expected' => 'api.php', + ], + ], + ]; + + $string_upper_any_config = << $string_upper_any_config, + 'requests' => [ + [ + 'request' => $this->request('POST', '/'), + 'expected' => 'api.php', + ], + ], + ]; + + $string_lower_any_config = << $string_lower_any_config, + 'requests' => [ + [ + 'request' => $this->request('POST', '/'), + 'expected' => 'api.php', + ], + ], + ]; + + $string_wildcard_config = << $string_wildcard_config, + 'requests' => [ + [ + 'request' => $this->request('POST', '/'), + 'expected' => 'api.php', + ], + ], + ]; - self::assertSame('api.php', $router->match($this->request('GET', '/'))[0]); - self::assertSame('api.php', $router->match($this->request('POST', '/'))[0]); - self::assertSame('api.php', $router->match($this->request('GET', '/tests/1'))[0]); - self::assertSame('api.php', $router->match($this->request('PUT', '/tests/1'))[0]); + return compact( + 'wildcard_config_as_array', + 'string_config_upper_any', + 'string_config_lower_any', + 'string_config_wildcard', + ); } private function request(string $method, string $path): ServerRequestInterface