Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Dec 20, 2023
1 parent 9468413 commit 59937ca
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions tests/system/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Router;

use CodeIgniter\Config\Factories;
use CodeIgniter\Config\Services;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\Exceptions\RedirectException;
Expand All @@ -38,13 +39,21 @@ protected function setUp(): void
{
parent::setUp();

$this->createRouteCollection();

$this->request = Services::request();
$this->request->setMethod(Method::GET);
}

private function createRouteCollection(?Routing $routingConfig = null): void
{
$moduleConfig = new Modules();
$moduleConfig->enabled = false;

$routerConfig = new Routing();
$routerConfig->defaultNamespace = '\\';
$routingConfig ??= new Routing();
$routingConfig->defaultNamespace = '\\';

$this->collection = new RouteCollection(Services::locator(), $moduleConfig, $routerConfig);
$this->collection = new RouteCollection(Services::locator(), $moduleConfig, $routingConfig);

$routes = [
'/' => 'Home::index',
Expand All @@ -66,9 +75,6 @@ protected function setUp(): void
'(:segment)/(:segment)/(:segment)' => '$2::$3/$1',
];
$this->collection->map($routes);

$this->request = Services::request();
$this->request->setMethod(Method::GET);
}

public function testEmptyURIMatchesRoot(): void
Expand Down Expand Up @@ -925,4 +931,34 @@ public static function provideRedirectRoute(): iterable
['post/(:segment)/comment/(:any)', 'article/(:num)/(:any)', 'alias', '/article/1/2', 'post/1/comment/2', 'alias'],
];
}

public function testRoutePlaceholderAnyWithMultipleSegmentsParamFalse(): void
{
$this->createRouteCollection();
$this->collection->get('product/(:any)', 'Catalog::productLookup/$1');
$router = new Router($this->collection, $this->request);

$router->handle('product/123/456');

$this->assertSame('\Catalog', $router->controllerName());
$this->assertSame('productLookup', $router->methodName());
$this->assertSame(['123', '456'], $router->params());
}

public function testRoutePlaceholderAnyWithMultipleSegmentsParamTrue(): void
{
$routingConfig = new Routing();
$routingConfig->multipleSegmentsOneParam = true;
Factories::injectMock('config', Routing::class, $routingConfig);

$this->createRouteCollection($routingConfig);
$this->collection->get('product/(:any)', 'Catalog::productLookup/$1');
$router = new Router($this->collection, $this->request);

$router->handle('product/123/456');

$this->assertSame('\Catalog', $router->controllerName());
$this->assertSame('productLookup', $router->methodName());
$this->assertSame(['123/456'], $router->params());
}
}

0 comments on commit 59937ca

Please sign in to comment.