Skip to content

Commit

Permalink
Merge pull request #5712 from kenjis/fix-spark-routes
Browse files Browse the repository at this point in the history
fix: auto routes incorrectly display route filters with GET method
  • Loading branch information
kenjis committed Feb 21, 2022
2 parents e1b4f76 + 5c2995c commit 77c5ba9
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 11 deletions.
4 changes: 3 additions & 1 deletion system/Commands/Utilities/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ public function run(array $params)
$autoRoutes = $autoRouteCollector->get();

foreach ($autoRoutes as &$routes) {
$filters = $filterCollector->get('get', $uriGenerator->get($routes[1]));
// There is no `auto` method, but it is intentional not to get route filters.
$filters = $filterCollector->get('auto', $uriGenerator->get($routes[1]));

$routes[] = implode(' ', array_map('class_basename', $filters['before']));
$routes[] = implode(' ', array_map('class_basename', $filters['after']));
}
Expand Down
10 changes: 0 additions & 10 deletions tests/system/Commands/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,6 @@ public function testNamespacesCommand()
$this->assertStringContainsString('| Yes', $this->getBuffer());
}

public function testRoutesCommand()
{
command('routes');

$this->assertStringContainsString('| (Closure)', $this->getBuffer());
$this->assertStringContainsString('| Route', $this->getBuffer());
$this->assertStringContainsString('| testing', $this->getBuffer());
$this->assertStringContainsString('\\TestController::index', $this->getBuffer());
}

public function testInexistentCommandWithNoAlternatives()
{
command('app:oops');
Expand Down
73 changes: 73 additions & 0 deletions tests/system/Commands/RoutesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

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

namespace CodeIgniter\Commands;

use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Filters\CITestStreamFilter;
use Config\Services;

/**
* @internal
*/
final class RoutesTest extends CIUnitTestCase
{
private $streamFilter;
protected $logger;
protected $commands;

protected function setUp(): void
{
parent::setUp();

CITestStreamFilter::$buffer = '';

$this->streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter');
$this->streamFilter = stream_filter_append(STDERR, 'CITestStreamFilter');

$this->logger = Services::logger();
$this->commands = Services::commands();
}

protected function tearDown(): void
{
stream_filter_remove($this->streamFilter);
}

protected function getBuffer()
{
return CITestStreamFilter::$buffer;
}

public function testRoutesCommand()
{
command('routes');

$this->assertStringContainsString('| (Closure)', $this->getBuffer());
$this->assertStringContainsString('| Route', $this->getBuffer());
$this->assertStringContainsString('| testing', $this->getBuffer());
$this->assertStringContainsString('\\TestController::index', $this->getBuffer());
}

public function testRoutesCommandRouteFilterAndAutoRoute()
{
$routes = Services::routes();
$routes->resetRoutes();
$routes->get('/', 'Home::index', ['filter' => 'csrf']);

command('routes');

$this->assertStringContainsString(
'|auto|/|\App\Controllers\Home::index||toolbar|',
str_replace(' ', '', $this->getBuffer())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Commands\Utilities\Routes;

use CodeIgniter\Test\CIUnitTestCase;
use Config\Services;

/**
* @internal
Expand All @@ -20,6 +21,9 @@ final class FilterCollectorTest extends CIUnitTestCase
{
public function testGet()
{
$routes = Services::routes();
$routes->resetRoutes();

$collector = new FilterCollector();

$filters = $collector->get('get', '/');
Expand Down

0 comments on commit 77c5ba9

Please sign in to comment.