Skip to content

Commit

Permalink
Merge pull request #6271 from kenjis/feat-spark-routes-route-name
Browse files Browse the repository at this point in the history
feat: `spark routes` shows route name
  • Loading branch information
kenjis authored Aug 1, 2022
2 parents f0405ad + 8300dce commit cef5e53
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 115 deletions.
10 changes: 9 additions & 1 deletion system/Commands/Utilities/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,17 @@ public function run(array $params)
$sampleUri = $uriGenerator->get($route);
$filters = $filterCollector->get($method, $sampleUri);

if ($handler instanceof Closure) {
$handler = '(Closure)';
}

$routeName = $collection->getRoutesOptions($route)['as'] ?? '»';

$tbody[] = [
strtoupper($method),
$route,
is_string($handler) ? $handler : '(Closure)',
$routeName,
$handler,
implode(' ', array_map('class_basename', $filters['before'])),
implode(' ', array_map('class_basename', $filters['after'])),
];
Expand Down Expand Up @@ -149,6 +156,7 @@ public function run(array $params)
$thead = [
'Method',
'Route',
'Name',
'Handler',
'Before Filters',
'After Filters',
Expand Down
1 change: 1 addition & 0 deletions system/Commands/Utilities/Routes/AutoRouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function get(): array
$tbody[] = [
'auto',
$item['route'],
'',
$item['handler'],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public function get(): array
$tbody[] = [
strtoupper($item['method']) . '(auto)',
$item['route'] . $item['route_params'],
'',
$item['handler'],
$item['before'],
$item['after'],
Expand Down
118 changes: 105 additions & 13 deletions tests/system/Commands/RoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CodeIgniter\Commands;

use CodeIgniter\Router\RouteCollection;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\StreamFilterTrait;
use Config\Services;
Expand Down Expand Up @@ -39,29 +40,120 @@ protected function getBuffer()
return $this->getStreamFilterBuffer();
}

private function getCleanRoutes(): RouteCollection
{
$routes = Services::routes();
$routes->resetRoutes();
$routes->loadRoutes();

return $routes;
}

public function testRoutesCommand()
{
$this->getCleanRoutes();

command('routes');

$this->assertStringContainsString('| (Closure)', $this->getBuffer());
$this->assertStringContainsString('| Route', $this->getBuffer());
$this->assertStringContainsString('| testing', $this->getBuffer());
$this->assertStringContainsString('\\TestController::index', $this->getBuffer());
$expected = <<<'EOL'
+---------+---------+---------------+----------------------------------------+----------------+---------------+
| Method | Route | Name | Handler | Before Filters | After Filters |
+---------+---------+---------------+----------------------------------------+----------------+---------------+
| GET | / | » | \App\Controllers\Home::index | | toolbar |
| GET | closure | » | (Closure) | | toolbar |
| GET | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| HEAD | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| POST | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| PUT | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| DELETE | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| OPTIONS | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| TRACE | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| CONNECT | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| CLI | testing | testing-index | \App\Controllers\TestController::index | | |
+---------+---------+---------------+----------------------------------------+----------------+---------------+
EOL;
$this->assertStringContainsString($expected, $this->getBuffer());
}

public function testRoutesCommandRouteFilterAndAutoRoute()
public function testRoutesCommandAutoRouteImproved()
{
$routes = Services::routes();
$routes->setDefaultNamespace('App\Controllers');
$routes->resetRoutes();
$routes->get('/', 'Home::index', ['filter' => 'csrf']);
$routes = $this->getCleanRoutes();

$routes->setAutoRoute(true);
config('Feature')->autoRoutesImproved = true;
$namespace = 'Tests\Support\Controllers';
$routes->setDefaultNamespace($namespace);

command('routes');

$expected = <<<'EOL'
+------------+--------------------------------+---------------+-----------------------------------------------------+----------------+---------------+
| Method | Route | Name | Handler | Before Filters | After Filters |
+------------+--------------------------------+---------------+-----------------------------------------------------+----------------+---------------+
| GET | / | » | \App\Controllers\Home::index | | toolbar |
| GET | closure | » | (Closure) | | toolbar |
| GET | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| HEAD | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| POST | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| PUT | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| DELETE | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| OPTIONS | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| TRACE | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| CONNECT | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| CLI | testing | testing-index | \App\Controllers\TestController::index | | |
| GET(auto) | newautorouting | | \Tests\Support\Controllers\Newautorouting::getIndex | | toolbar |
| POST(auto) | newautorouting/save/../..[/..] | | \Tests\Support\Controllers\Newautorouting::postSave | | toolbar |
+------------+--------------------------------+---------------+-----------------------------------------------------+----------------+---------------+
EOL;
$this->assertStringContainsString($expected, $this->getBuffer());
}

public function testRoutesCommandRouteLegacy()
{
$routes = $this->getCleanRoutes();

$routes->setAutoRoute(true);
$namespace = 'Tests\Support\Controllers';
$routes->setDefaultNamespace($namespace);

command('routes');

$this->assertStringContainsString(
'|auto|/|\App\Controllers\Home::index||toolbar|',
str_replace(' ', '', $this->getBuffer())
);
$expected = <<<'EOL'
+---------+-------------------------------+---------------+-----------------------------------------------------+----------------+---------------+
| Method | Route | Name | Handler | Before Filters | After Filters |
+---------+-------------------------------+---------------+-----------------------------------------------------+----------------+---------------+
| GET | / | » | \App\Controllers\Home::index | | toolbar |
| GET | closure | » | (Closure) | | toolbar |
| GET | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| HEAD | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| POST | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| PUT | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| DELETE | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| OPTIONS | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| TRACE | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| CONNECT | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
| CLI | testing | testing-index | \App\Controllers\TestController::index | | |
| auto | hello | | \Tests\Support\Controllers\Hello::index | | toolbar |
| auto | hello/index[/...] | | \Tests\Support\Controllers\Hello::index | | toolbar |
| auto | newautorouting/getIndex[/...] | | \Tests\Support\Controllers\Newautorouting::getIndex | | toolbar |
| auto | newautorouting/postSave[/...] | | \Tests\Support\Controllers\Newautorouting::postSave | | toolbar |
| auto | popcorn | | \Tests\Support\Controllers\Popcorn::index | | toolbar |
| auto | popcorn/index[/...] | | \Tests\Support\Controllers\Popcorn::index | | toolbar |
| auto | popcorn/pop[/...] | | \Tests\Support\Controllers\Popcorn::pop | | toolbar |
| auto | popcorn/popper[/...] | | \Tests\Support\Controllers\Popcorn::popper | | toolbar |
| auto | popcorn/weasel[/...] | | \Tests\Support\Controllers\Popcorn::weasel | | toolbar |
| auto | popcorn/oops[/...] | | \Tests\Support\Controllers\Popcorn::oops | | toolbar |
| auto | popcorn/goaway[/...] | | \Tests\Support\Controllers\Popcorn::goaway | | toolbar |
| auto | popcorn/index3[/...] | | \Tests\Support\Controllers\Popcorn::index3 | | toolbar |
| auto | popcorn/canyon[/...] | | \Tests\Support\Controllers\Popcorn::canyon | | toolbar |
| auto | popcorn/cat[/...] | | \Tests\Support\Controllers\Popcorn::cat | | toolbar |
| auto | popcorn/json[/...] | | \Tests\Support\Controllers\Popcorn::json | | toolbar |
| auto | popcorn/xml[/...] | | \Tests\Support\Controllers\Popcorn::xml | | toolbar |
| auto | popcorn/toindex[/...] | | \Tests\Support\Controllers\Popcorn::toindex | | toolbar |
| auto | popcorn/echoJson[/...] | | \Tests\Support\Controllers\Popcorn::echoJson | | toolbar |
| auto | remap[/...] | | \Tests\Support\Controllers\Remap::_remap | | toolbar |
+---------+-------------------------------+---------------+-----------------------------------------------------+----------------+---------------+
EOL;
$this->assertStringContainsString($expected, $this->getBuffer());
}
}
Loading

0 comments on commit cef5e53

Please sign in to comment.