/
githubmirror
/
framework
Обзор
Документация
Войти
/
githubmirror
/
framework
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
13.x
tests/Foundation/Console/RouteListCommandTest.php
289 строк
10 KB
Jason McCreary
[13.x] Mockery cleanup (#61117)
10 авг 2026, 20:23
Не верифицирован
10 авг 2026, 20:23
2ee40ee
Код
Авторство
О чём код?
<?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; use PHPUnit\Framework\TestCase; class RouteListCommandTest extends TestCase { protected Application $app; protected function setUp(): void { $this->app = new Application( $laravel = new \Illuminate\Foundation\Application(__DIR__), Mockery::mock(Dispatcher::class, ['dispatch' => null, 'fire' => null]), 'testing', ); $router = new Router(Mockery::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->instance(Kernel::class, $kernel); $router->get('/example', function () { return 'Hello World'; })->middleware('exampleMiddleware'); $router->get('/sub-example', function () { return 'Hello World'; })->domain('sub') ->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 testSortRouteListAsc() { $this->app->call('route:list', ['--json' => true, '--sort' => 'domain,uri']); $output = $this->app->output(); $routes = json_decode($output, true); $this->assertCount(3, $routes); $this->assertSame('example', $routes[0]['uri']); $this->assertSame('example-group', $routes[1]['uri']); $this->assertSame('sub-example', $routes[2]['uri']); foreach ($routes as $route) { $this->assertArrayHasKey('path', $route); $this->assertStringContainsString('RouteListCommandTest.php:', $route['path']); } } public function testSortRouteListDesc() { $this->app->call('route:list', ['--json' => true, '--sort' => 'domain,uri', '--reverse' => true]); $output = $this->app->output(); $routes = json_decode($output, true); $this->assertCount(3, $routes); $this->assertSame('sub-example', $routes[0]['uri']); $this->assertSame('example-group', $routes[1]['uri']); $this->assertSame('example', $routes[2]['uri']); foreach ($routes as $route) { $this->assertArrayHasKey('path', $route); $this->assertStringContainsString('RouteListCommandTest.php:', $route['path']); } } public function testSortRouteListDefault() { $this->app->call('route:list', ['--json' => true]); $output = $this->app->output(); $routes = json_decode($output, true); $this->assertCount(3, $routes); $this->assertSame('example', $routes[0]['uri']); $this->assertSame('example-group', $routes[1]['uri']); $this->assertSame('sub-example', $routes[2]['uri']); foreach ($routes as $route) { $this->assertArrayHasKey('path', $route); $this->assertStringContainsString('RouteListCommandTest.php:', $route['path']); } } public function testSortRouteListPrecedence() { $this->app->call('route:list', ['--json' => true, '--sort' => 'definition']); $output = $this->app->output(); $routes = json_decode($output, true); $this->assertCount(3, $routes); $this->assertSame('example', $routes[0]['uri']); $this->assertSame('sub-example', $routes[1]['uri']); $this->assertSame('example-group', $routes[2]['uri']); foreach ($routes as $route) { $this->assertArrayHasKey('path', $route); $this->assertStringContainsString('RouteListCommandTest.php:', $route['path']); } } 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(); $routes = json_decode($output, true); $this->assertCount(3, $routes); $this->assertSame('example', $routes[0]['uri']); $this->assertEquals(['exampleMiddleware'], $routes[0]['middleware']); $this->assertSame('example-group', $routes[1]['uri']); $this->assertEquals(['Middleware 5', 'Middleware 1', 'Middleware 4', 'Middleware 2', 'Middleware 3'], $routes[1]['middleware']); $this->assertSame('sub-example', $routes[2]['uri']); $this->assertEquals(['exampleMiddleware'], $routes[2]['middleware']); } public function testFilterByMiddleware() { $this->app->call('route:list', ['--json' => true, '-v' => true, '--middleware' => 'auth']); $output = $this->app->output(); $routes = json_decode($output, true); $this->assertCount(1, $routes); $this->assertSame('example-group', $routes[0]['uri']); $this->assertEquals(['web', 'auth'], $routes[0]['middleware']); $this->assertStringContainsString('RouteListCommandTest.php:', $routes[0]['path']); } public function testClosureRouteShowsPathInCli() { RouteListCommand::resolveTerminalWidthUsing(fn () => 200); $this->app->call('route:list'); $output = $this->app->output(); $this->assertStringContainsString('RouteListCommandTest.php:', $output); RouteListCommand::resolveTerminalWidthUsing(null); } public function testControllerRoutePathIsNull() { $laravel = new \Illuminate\Foundation\Application(__DIR__); $router = new Router(Mockery::mock('Illuminate\Events\Dispatcher')); $kernel = new class($laravel, $router) extends Kernel { protected $middlewareGroups = []; }; $laravel->instance(Kernel::class, $kernel); $router->get('/controller-route', [RouteListCommandTestController::class, 'index']); $command = new RouteListCommand($router); $command->setLaravel($laravel); $app = new Application( $laravel, Mockery::mock(Dispatcher::class, ['dispatch' => null, 'fire' => null]), 'testing', ); $app->addCommands([$command]); $app->call('route:list', ['--json' => true]); $output = $app->output(); $routes = json_decode($output, true); $this->assertCount(1, $routes); $this->assertNull($routes[0]['path']); } } class RouteListCommandTestController { public function index() { return 'Hello World'; } }