Skip to content

Commit

Permalink
[spiral/router] Fix issue with default parameter values (#933)
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed Apr 26, 2023
1 parent 80464b8 commit 47f56e1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
35 changes: 35 additions & 0 deletions tests/ControllerTest.php
Expand Up @@ -7,6 +7,7 @@
use Spiral\Http\Exception\ClientException\NotFoundException;
use Spiral\Router\Exception\UndefinedRouteException;
use Spiral\Router\Route;
use Spiral\Router\Target\Action;
use Spiral\Router\Target\Controller;
use Spiral\Tests\Router\Fixtures\TestController;
use Nyholm\Psr7\ServerRequest;
Expand Down Expand Up @@ -48,6 +49,40 @@ public function testRoute(): void
$this->assertSame('888', (string)$response->getBody());
}

public function testOptionalParam(): void
{
$router = $this->makeRouter();
$router->setRoute(
'action',
new Route('/default[/<id>]', new Action(TestController::class, 'default'))
);

$response = $router->handle(new ServerRequest('GET', new Uri('/default')));
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('default', (string)$response->getBody());

$response = $router->handle(new ServerRequest('GET', new Uri('/default/123')));
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('123', (string)$response->getBody());
}

public function testOptionalParamWithDefaultInt(): void
{
$router = $this->makeRouter();
$router->setRoute(
'action',
new Route('/defaultInt[/<id>]', new Action(TestController::class, 'defaultInt'))
);

$response = $router->handle(new ServerRequest('GET', new Uri('/defaultInt')));
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('int: 1', (string)$response->getBody());

$response = $router->handle(new ServerRequest('GET', new Uri('/defaultInt/123')));
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('string: 123', (string)$response->getBody());
}

public function testUriGeneration(): void
{
$router = $this->makeRouter();
Expand Down
26 changes: 19 additions & 7 deletions tests/Fixtures/TestController.php
Expand Up @@ -9,21 +9,33 @@

class TestController
{
public function index()
public function index(): string
{
return 'hello world';
}

public function test()
public function test(): string
{
return 'hello world';
}

public function id(string $id)
public function id(string $id): string
{
return $id;
}

public function default(string $id = 'default'): string
{
return $id;
}

public function defaultInt(string|int $id = 1): string
{
$result = \is_int($id) ? 'int: ' : 'string: ';

return $result . $id;
}

public function echo(): void
{
ob_start();
Expand All @@ -35,7 +47,7 @@ public function err(): void
throw new \Error('error.controller');
}

public function rsp()
public function rsp(): Response
{
$r = new Response();
$r->getBody()->write('rsp');
Expand All @@ -45,7 +57,7 @@ public function rsp()
return $r;
}

public function json()
public function json(): array
{
return [
'status' => 301,
Expand All @@ -68,12 +80,12 @@ public function weird(): void
throw new ControllerException('', 99);
}

public function postTarget()
public function postTarget(): string
{
return 'POST';
}

public function deleteTarget()
public function deleteTarget(): string
{
return 'DELETE';
}
Expand Down

0 comments on commit 47f56e1

Please sign in to comment.