Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zero params should be passed through when routing. Fixes #2032 #2043

Merged
merged 2 commits into from
Jun 7, 2019
Merged
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
4 changes: 3 additions & 1 deletion system/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,9 @@ public function autoRoute(string $uri)
*/
protected function validateRequest(array $segments): array
{
$segments = array_filter($segments);
$segments = array_filter($segments, function ($segment) {
return ! empty($segment) || ($segment !== '0' || $segment !== 0);
});
$segments = array_values($segments);

$c = count($segments);
Expand Down
4 changes: 2 additions & 2 deletions system/Test/FeatureTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ public function skipEvents()
public function call(string $method, string $path, array $params = null)
{
// Simulate having a blank session
$_SESSION = [];
$_SESSION = [];
$_SERVER['REQUEST_METHOD'] = $method;

$request = $this->setupRequest($method, $path, $params);
$request = $this->populateGlobals($method, $request, $params);
Expand All @@ -165,7 +166,6 @@ public function call(string $method, string $path, array $params = null)
// Make sure any other classes that might call the request
// instance get the right one.
Services::injectMock('request', $request);
$_SERVER['REQUEST_METHOD'] = $method;

$response = $this->app
->setRequest($request)
Expand Down
19 changes: 19 additions & 0 deletions tests/system/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,23 @@ public function testTranslateURIDashesForAutoRoute()
}

//--------------------------------------------------------------------

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/2032
*/
public function testAutoRouteMatchesZeroParams()
{
$router = new Router($this->collection, $this->request);

$router->autoRoute('myController/someMethod/0/abc');

$this->assertEquals('MyController', $router->controllerName());
$this->assertEquals('someMethod', $router->methodName());

$expected = [
'0',
'abc',
];
$this->assertEquals($expected, $router->params());
}
}
16 changes: 12 additions & 4 deletions tests/system/Test/FeatureTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,18 @@ function () {

public function testSession()
{
$response = $this->withSession([
'fruit' => 'apple',
'greeting' => 'hello',
])->get('home');
$response = $this->withRoutes([
[
'get',
'home',
function () {
return 'Home';
},
],
])->withSession([
'fruit' => 'apple',
'greeting' => 'hello',
])->get('home');

$response->assertSessionHas('fruit', 'apple');
$response->assertSessionMissing('popcorn');
Expand Down