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

fix: auto routes incorrectly display route filters with GET method #5712

Merged
merged 2 commits into from
Feb 21, 2022
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/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