From 728102ac07f54aa37e5efab02477ebad8c048a04 Mon Sep 17 00:00:00 2001 From: Ash Allen Date: Sat, 28 May 2022 02:00:35 +0100 Subject: [PATCH 1/2] Added route counts to the route:list command. --- .../Foundation/Console/RouteListCommand.php | 26 ++++++++++++++++++- .../Testing/Console/RouteListCommandTest.php | 20 ++++++-------- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/Illuminate/Foundation/Console/RouteListCommand.php b/src/Illuminate/Foundation/Console/RouteListCommand.php index 7d8ffbbe5a6a..120e4701d15c 100644 --- a/src/Illuminate/Foundation/Console/RouteListCommand.php +++ b/src/Illuminate/Foundation/Console/RouteListCommand.php @@ -360,6 +360,8 @@ protected function forCli($routes) $terminalWidth = $this->getTerminalWidth(); + $routeCount = $this->determineRouteCountOutput($routes, $terminalWidth); + return $routes->map(function ($route) use ($maxMethod, $terminalWidth) { [ 'action' => $action, @@ -399,7 +401,29 @@ protected function forCli($routes) $dots, str_replace(' ', ' › ', $action), ), $this->output->isVerbose() && ! empty($middleware) ? "$middleware" : null]; - })->flatten()->filter()->prepend('')->push('')->toArray(); + }) + ->flatten() + ->filter() + ->prepend('')->prepend($routeCount)->prepend('') + ->push('')->push($routeCount)->push('') + ->toArray(); + } + + /** + * Determine and return the output for displaying the number of routes in the CLI output. + * + * @param \Illuminate\Support\Collection $routes + * @param int $terminalWidth + * @return string + */ + protected function determineRouteCountOutput($routes, $terminalWidth) + { + $routeCountText = 'Showing ['.$routes->count().'] routes'; + + $offset = $terminalWidth - mb_strlen($routeCountText) - 2; + $spaces = str_repeat(' ', $offset); + + return $spaces.'Showing ['.$routes->count().'] routes'; } /** diff --git a/tests/Testing/Console/RouteListCommandTest.php b/tests/Testing/Console/RouteListCommandTest.php index ea822a217dd9..893dc879af65 100644 --- a/tests/Testing/Console/RouteListCommandTest.php +++ b/tests/Testing/Console/RouteListCommandTest.php @@ -60,14 +60,13 @@ public function testDisplayRoutesForCli() $this->artisan(RouteListCommand::class) ->assertSuccessful() - ->expectsOutput('') + ->expectsOutputToContain('Showing [6] routes') ->expectsOutput(' GET|HEAD / ..................................................... ') ->expectsOutput(' GET|HEAD {account}.example.com/ ................................ ') ->expectsOutput(' GET|HEAD closure ............................................... ') ->expectsOutput(' POST controller-invokable Illuminate\Tests\Testing\Console\…') ->expectsOutput(' GET|HEAD controller-method/{user} Illuminate\Tests\Testing\Cons…') - ->expectsOutput(' GET|HEAD {account}.example.com/user/{id} ............. user.show') - ->expectsOutput(''); + ->expectsOutput(' GET|HEAD {account}.example.com/user/{id} ............. user.show'); } public function testDisplayRoutesForCliInVerboseMode() @@ -86,13 +85,12 @@ public function testDisplayRoutesForCliInVerboseMode() $this->artisan(RouteListCommand::class, ['-v' => true]) ->assertSuccessful() - ->expectsOutput('') + ->expectsOutputToContain('Showing [4] routes') ->expectsOutput(' GET|HEAD closure ............................................... ') ->expectsOutput(' POST controller-invokable Illuminate\\Tests\\Testing\\Console\\FooController') ->expectsOutput(' GET|HEAD controller-method/{user} Illuminate\\Tests\\Testing\\Console\\FooController@show') ->expectsOutput(' GET|HEAD {account}.example.com/user/{id} ............. user.show') - ->expectsOutput(' ⇂ web') - ->expectsOutput(''); + ->expectsOutput(' ⇂ web'); } public function testRouteCanBeFilteredByName() @@ -108,9 +106,8 @@ public function testRouteCanBeFilteredByName() $this->artisan(RouteListCommand::class, ['--name' => 'foo']) ->assertSuccessful() - ->expectsOutput('') - ->expectsOutput(' GET|HEAD foo ...................................... foo.show') - ->expectsOutput(''); + ->expectsOutputToContain('Showing [1] route') + ->expectsOutput(' GET|HEAD foo ...................................... foo.show'); } public function testDisplayRoutesExceptVendor() @@ -121,11 +118,10 @@ public function testDisplayRoutesExceptVendor() $this->artisan(RouteListCommand::class, ['-v' => true, '--except-vendor' => true]) ->assertSuccessful() - ->expectsOutput('') + ->expectsOutputToContain('Showing [3] routes') ->expectsOutput(' GET|HEAD foo/{user} Illuminate\Tests\Testing\Console\FooController@show') ->expectsOutput(' ANY redirect .... Illuminate\Routing\RedirectController') - ->expectsOutput(' GET|HEAD view .............................................. ') - ->expectsOutput(''); + ->expectsOutput(' GET|HEAD view .............................................. '); } protected function tearDown(): void From 7fdff3b84159799c3cc7b4a12fa3150ff16cdecf Mon Sep 17 00:00:00 2001 From: Ash Allen Date: Mon, 30 May 2022 21:22:32 +0100 Subject: [PATCH 2/2] Only display count at the bottom of the CLI output. --- .../Foundation/Console/RouteListCommand.php | 2 +- .../Testing/Console/RouteListCommandTest.php | 28 +++++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Foundation/Console/RouteListCommand.php b/src/Illuminate/Foundation/Console/RouteListCommand.php index d0b64c62e685..1fd2d470f960 100644 --- a/src/Illuminate/Foundation/Console/RouteListCommand.php +++ b/src/Illuminate/Foundation/Console/RouteListCommand.php @@ -405,7 +405,7 @@ protected function forCli($routes) }) ->flatten() ->filter() - ->prepend('')->prepend($routeCount)->prepend('') + ->prepend('') ->push('')->push($routeCount)->push('') ->toArray(); } diff --git a/tests/Testing/Console/RouteListCommandTest.php b/tests/Testing/Console/RouteListCommandTest.php index 893dc879af65..e75072f7f20d 100644 --- a/tests/Testing/Console/RouteListCommandTest.php +++ b/tests/Testing/Console/RouteListCommandTest.php @@ -60,13 +60,16 @@ public function testDisplayRoutesForCli() $this->artisan(RouteListCommand::class) ->assertSuccessful() - ->expectsOutputToContain('Showing [6] routes') + ->expectsOutput('') ->expectsOutput(' GET|HEAD / ..................................................... ') ->expectsOutput(' GET|HEAD {account}.example.com/ ................................ ') ->expectsOutput(' GET|HEAD closure ............................................... ') ->expectsOutput(' POST controller-invokable Illuminate\Tests\Testing\Console\…') ->expectsOutput(' GET|HEAD controller-method/{user} Illuminate\Tests\Testing\Cons…') - ->expectsOutput(' GET|HEAD {account}.example.com/user/{id} ............. user.show'); + ->expectsOutput(' GET|HEAD {account}.example.com/user/{id} ............. user.show') + ->expectsOutput('') + ->expectsOutput(' Showing [6] routes') + ->expectsOutput(''); } public function testDisplayRoutesForCliInVerboseMode() @@ -85,12 +88,15 @@ public function testDisplayRoutesForCliInVerboseMode() $this->artisan(RouteListCommand::class, ['-v' => true]) ->assertSuccessful() - ->expectsOutputToContain('Showing [4] routes') + ->expectsOutput('') ->expectsOutput(' GET|HEAD closure ............................................... ') ->expectsOutput(' POST controller-invokable Illuminate\\Tests\\Testing\\Console\\FooController') ->expectsOutput(' GET|HEAD controller-method/{user} Illuminate\\Tests\\Testing\\Console\\FooController@show') ->expectsOutput(' GET|HEAD {account}.example.com/user/{id} ............. user.show') - ->expectsOutput(' ⇂ web'); + ->expectsOutput(' ⇂ web') + ->expectsOutput('') + ->expectsOutput(' Showing [4] routes') + ->expectsOutput(''); } public function testRouteCanBeFilteredByName() @@ -106,8 +112,11 @@ public function testRouteCanBeFilteredByName() $this->artisan(RouteListCommand::class, ['--name' => 'foo']) ->assertSuccessful() - ->expectsOutputToContain('Showing [1] route') - ->expectsOutput(' GET|HEAD foo ...................................... foo.show'); + ->expectsOutput('') + ->expectsOutput(' GET|HEAD foo ...................................... foo.show') + ->expectsOutput('') + ->expectsOutput(' Showing [1] routes') + ->expectsOutput(''); } public function testDisplayRoutesExceptVendor() @@ -118,10 +127,13 @@ public function testDisplayRoutesExceptVendor() $this->artisan(RouteListCommand::class, ['-v' => true, '--except-vendor' => true]) ->assertSuccessful() - ->expectsOutputToContain('Showing [3] routes') + ->expectsOutput('') ->expectsOutput(' GET|HEAD foo/{user} Illuminate\Tests\Testing\Console\FooController@show') ->expectsOutput(' ANY redirect .... Illuminate\Routing\RedirectController') - ->expectsOutput(' GET|HEAD view .............................................. '); + ->expectsOutput(' GET|HEAD view .............................................. ') + ->expectsOutput('') + ->expectsOutput(' Showing [3] routes') + ->expectsOutput(''); } protected function tearDown(): void