Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/composer.phar
/composer.lock
/.phpunit.result.cache
.vscode
7 changes: 4 additions & 3 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

Expand All @@ -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 = '*';
}

Expand Down
113 changes: 105 additions & 8 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<YAML
$parsed_config = Yaml::parse($config);
$router = Router::fromServerlessConfig($parsed_config);

foreach ($requests as $request) {
self::assertSame($request['expected'], $router->match($request['request'])[0]);
}
}

/**
* Data provider for test_routing_expanded_config
*/
public function expandedConfigProvider(): array
{
$wildcard_array_config = <<<YAML
functions:
api:
handler: api.php
Expand All @@ -64,13 +86,88 @@ functions:
path: '*'
YAML;

$config = Yaml::parse($config);
$router = Router::fromServerlessConfig($config);
$wildcard_config_as_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 = <<<YAML
functions:
api:
handler: api.php
events:
- httpApi: 'ANY /'
YAML;

$string_config_upper_any = [
'config' => $string_upper_any_config,
'requests' => [
[
'request' => $this->request('POST', '/'),
'expected' => 'api.php',
],
],
];

$string_lower_any_config = <<<YAML
functions:
api:
handler: api.php
events:
- httpApi: 'any /'
YAML;

$string_config_lower_any = [
'config' => $string_lower_any_config,
'requests' => [
[
'request' => $this->request('POST', '/'),
'expected' => 'api.php',
],
],
];

$string_wildcard_config = <<<YAML
functions:
api:
handler: api.php
events:
- httpApi: '*'
YAML;

$string_config_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
Expand Down