Skip to content

Commit

Permalink
fix: spark routes shows invalid routes with Auto Routing Improved
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Apr 8, 2023
1 parent e8cf06f commit 5a527d4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public function read(string $class, string $defaultController = 'Home', string $
continue;
}

// Skip the default controller.
if (class_basename($class) === $defaultController) {
continue;
}

$route = $classInUri . '/' . $methodInUri;

$params = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved;

use CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Home;
use CodeIgniter\Test\CIUnitTestCase;
use Tests\Support\Controllers\Newautorouting;
use Tests\Support\Controllers\Remap;
Expand All @@ -22,13 +23,13 @@
*/
final class ControllerMethodReaderTest extends CIUnitTestCase
{
private function createControllerMethodReader(): ControllerMethodReader
{
private function createControllerMethodReader(
string $namespace = 'Tests\Support\Controllers'
): ControllerMethodReader {
$methods = [
'get',
'post',
];
$namespace = 'Tests\Support\Controllers';

return new ControllerMethodReader($namespace, $methods);
}
Expand Down Expand Up @@ -63,6 +64,34 @@ public function testRead()
$this->assertSame($expected, $routes);
}

public function testReadDefaultController()
{
$reader = $this->createControllerMethodReader(
'CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers'
);

$routes = $reader->read(Home::class);

$expected = [
0 => [
'method' => 'get',
'route' => '/',
'route_params' => '',
'handler' => '\CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Home::getIndex',
'params' => [],
],
[
'method' => 'post',
'route' => '/',
'route_params' => '',
'handler' => '\CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Home::postIndex',
'params' => [],
],
];

$this->assertSame($expected, $routes);
}

public function testReadControllerWithRemap()
{
$reader = $this->createControllerMethodReader();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers;

use CodeIgniter\Controller;

/**
* The default controller for Auto Routing (Improved)
*/
class Home extends Controller
{
public function getIndex()
{
}

public function postIndex()
{
}

/**
* This method cannot be accessible.
*/
public function getFoo()
{
}
}

0 comments on commit 5a527d4

Please sign in to comment.