-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[10.x] Allow route:list to expand middleware groups in 'VeryVerbose' …
…mode (#48703) * Allow middleware groups to be expanded with -vv * Keep things simple and do not use group nesting * Moved test to correct folder * Removed useless tmp vars * Style Changes * Added test to ensure expanded middleware are correctly sorted * Improved tests once again to ensure ordering via $kernel->prependToMiddlewarePriority() works as well, and that middleware is not displayed in non very verbose cases
- Loading branch information
Showing
2 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Foundation\Console; | ||
|
||
use Illuminate\Console\Application; | ||
use Illuminate\Contracts\Events\Dispatcher; | ||
use Illuminate\Foundation\Console\RouteListCommand; | ||
use Illuminate\Foundation\Http\Kernel; | ||
use Illuminate\Routing\Router; | ||
use Mockery as m; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RouteListCommandTest extends TestCase | ||
{ | ||
protected Application $app; | ||
|
||
protected function tearDown(): void | ||
{ | ||
m::close(); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->app = new Application( | ||
$laravel = new \Illuminate\Foundation\Application(__DIR__), | ||
m::mock(Dispatcher::class, ['dispatch' => null, 'fire' => null]), | ||
'testing', | ||
); | ||
|
||
$router = new Router(m::mock('Illuminate\Events\Dispatcher')); | ||
|
||
$kernel = new class($laravel, $router) extends Kernel | ||
{ | ||
protected $middlewareGroups = [ | ||
'web' => ['Middleware 1', 'Middleware 2', 'Middleware 5'], | ||
'auth' => ['Middleware 3', 'Middleware 4'], | ||
]; | ||
|
||
protected $middlewarePriority = [ | ||
'Middleware 1', | ||
'Middleware 4', | ||
'Middleware 2', | ||
'Middleware 3', | ||
]; | ||
}; | ||
|
||
$kernel->prependToMiddlewarePriority('Middleware 5'); | ||
|
||
$laravel->singleton(Kernel::class, function () use ($kernel) { | ||
return $kernel; | ||
}); | ||
|
||
$router->get('/example', function () { | ||
return 'Hello World'; | ||
})->middleware('exampleMiddleware'); | ||
|
||
$router->get('/example-group', function () { | ||
return 'Hello Group'; | ||
})->middleware(['web', 'auth']); | ||
|
||
$command = new RouteListCommand($router); | ||
$command->setLaravel($laravel); | ||
|
||
$this->app->addCommands([$command]); | ||
} | ||
|
||
public function testNoMiddlewareIfNotVerbose() | ||
{ | ||
$this->app->call('route:list'); | ||
$output = $this->app->output(); | ||
|
||
$this->assertStringNotContainsString('exampleMiddleware', $output); | ||
} | ||
|
||
public function testMiddlewareGroupsAssignmentInCli() | ||
{ | ||
$this->app->call('route:list', ['-v' => true]); | ||
$output = $this->app->output(); | ||
|
||
$this->assertStringContainsString('exampleMiddleware', $output); | ||
$this->assertStringContainsString('web', $output); | ||
$this->assertStringContainsString('auth', $output); | ||
|
||
$this->assertStringNotContainsString('Middleware 1', $output); | ||
$this->assertStringNotContainsString('Middleware 2', $output); | ||
$this->assertStringNotContainsString('Middleware 3', $output); | ||
$this->assertStringNotContainsString('Middleware 4', $output); | ||
$this->assertStringNotContainsString('Middleware 5', $output); | ||
} | ||
|
||
public function testMiddlewareGroupsExpandInCliIfVeryVerbose() | ||
{ | ||
$this->app->call('route:list', ['-vv' => true]); | ||
$output = $this->app->output(); | ||
|
||
$this->assertStringContainsString('exampleMiddleware', $output); | ||
$this->assertStringContainsString('Middleware 1', $output); | ||
$this->assertStringContainsString('Middleware 2', $output); | ||
$this->assertStringContainsString('Middleware 3', $output); | ||
$this->assertStringContainsString('Middleware 4', $output); | ||
$this->assertStringContainsString('Middleware 5', $output); | ||
|
||
$this->assertStringNotContainsString('web', $output); | ||
$this->assertStringNotContainsString('auth', $output); | ||
} | ||
|
||
public function testMiddlewareGroupsAssignmentInJson() | ||
{ | ||
$this->app->call('route:list', ['--json' => true, '-v' => true]); | ||
$output = $this->app->output(); | ||
|
||
$this->assertStringContainsString('exampleMiddleware', $output); | ||
$this->assertStringContainsString('web', $output); | ||
$this->assertStringContainsString('auth', $output); | ||
|
||
$this->assertStringNotContainsString('Middleware 1', $output); | ||
$this->assertStringNotContainsString('Middleware 2', $output); | ||
$this->assertStringNotContainsString('Middleware 3', $output); | ||
$this->assertStringNotContainsString('Middleware 4', $output); | ||
$this->assertStringNotContainsString('Middleware 5', $output); | ||
} | ||
|
||
public function testMiddlewareGroupsExpandInJsonIfVeryVerbose() | ||
{ | ||
$this->app->call('route:list', ['--json' => true, '-vv' => true]); | ||
$output = $this->app->output(); | ||
|
||
$this->assertStringContainsString('exampleMiddleware', $output); | ||
$this->assertStringContainsString('Middleware 1', $output); | ||
$this->assertStringContainsString('Middleware 2', $output); | ||
$this->assertStringContainsString('Middleware 3', $output); | ||
$this->assertStringContainsString('Middleware 4', $output); | ||
$this->assertStringContainsString('Middleware 5', $output); | ||
|
||
$this->assertStringNotContainsString('web', $output); | ||
$this->assertStringNotContainsString('auth', $output); | ||
} | ||
|
||
public function testMiddlewareGroupsExpandCorrectlySortedIfVeryVerbose() | ||
{ | ||
$this->app->call('route:list', ['--json' => true, '-vv' => true]); | ||
$output = $this->app->output(); | ||
|
||
$expectedOrder = '[{"domain":null,"method":"GET|HEAD","uri":"example","name":null,"action":"Closure","middleware":["exampleMiddleware"]},{"domain":null,"method":"GET|HEAD","uri":"example-group","name":null,"action":"Closure","middleware":["Middleware 5","Middleware 1","Middleware 4","Middleware 2","Middleware 3"]}]'; | ||
|
||
$this->assertJsonStringEqualsJsonString($expectedOrder, $output); | ||
} | ||
} |