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
Shows invalid routes with `x` to let devs know there are methods.
  • Loading branch information
kenjis committed Apr 10, 2023
1 parent 7e0631c commit 1df8a1b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ public function read(string $class, string $defaultController = 'Home', string $
$params[$param->getName()] = $required;
}

// If it is the default controller, the method will not be
// routed.
if ($classShortname === $defaultController) {
$route = 'x ' . $route;
}

$output[] = [
'method' => $httpVerb,
'route' => $route,
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 1df8a1b

Please sign in to comment.