From d06f3a9d745e4d52f721a85604d1732aa63aa5b2 Mon Sep 17 00:00:00 2001
From: Liam <liamk15@gmail.com>
Date: Wed, 7 Jun 2023 14:33:10 +0100
Subject: [PATCH] [10.x] Fix `schedule:list` to display named Jobs (#47367)

* Fix `schedule:list` to display named jobs

* Extra test coverage

* use in array

* Rename

* cs

* cs
---
 .../Scheduling/ScheduleListCommand.php        |  7 +++---
 .../Scheduling/ScheduleListCommandTest.php    | 23 +++++++++++++++++--
 2 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/src/Illuminate/Console/Scheduling/ScheduleListCommand.php b/src/Illuminate/Console/Scheduling/ScheduleListCommand.php
index e92b0112e696..6a1128827183 100644
--- a/src/Illuminate/Console/Scheduling/ScheduleListCommand.php
+++ b/src/Illuminate/Console/Scheduling/ScheduleListCommand.php
@@ -81,10 +81,9 @@ public function handle(Schedule $schedule)
             }
 
             if ($event instanceof CallbackEvent) {
-                if (class_exists($description)) {
-                    $command = $description;
-                    $description = '';
-                } else {
+                $command = $event->getSummaryForDisplay();
+
+                if (in_array($command, ['Closure', 'Callback'])) {
                     $command = 'Closure at: '.$this->getClosureLocation($event);
                 }
             }
diff --git a/tests/Integration/Console/Scheduling/ScheduleListCommandTest.php b/tests/Integration/Console/Scheduling/ScheduleListCommandTest.php
index 4c0d6bcee70f..dae9de4a375a 100644
--- a/tests/Integration/Console/Scheduling/ScheduleListCommandTest.php
+++ b/tests/Integration/Console/Scheduling/ScheduleListCommandTest.php
@@ -36,6 +36,9 @@ public function testDisplaySchedule()
         $this->schedule->command('inspire')->twiceDaily(14, 18);
         $this->schedule->command('foobar', ['a' => 'b'])->everyMinute();
         $this->schedule->job(FooJob::class)->everyMinute();
+        $this->schedule->job(new FooParamJob('test'))->everyMinute();
+        $this->schedule->job(FooJob::class)->name('foo-named-job')->everyMinute();
+        $this->schedule->job(new FooParamJob('test'))->name('foo-named-param-job')->everyMinute();
         $this->schedule->command('inspire')->cron('0 9,17 * * *');
         $this->schedule->command('inspire')->cron("0 10\t* * *");
         $this->schedule->call(FooCall::class)->everyMinute();
@@ -51,9 +54,12 @@ public function testDisplaySchedule()
             ->expectsOutput('  0 14,18 * *      *  php artisan inspire ........ Next Due: 14 hours from now')
             ->expectsOutput('  * *     * *      *  php artisan foobar a='.ProcessUtils::escapeArgument('b').' ... Next Due: 1 minute from now')
             ->expectsOutput('  * *     * *      *  Illuminate\Tests\Integration\Console\Scheduling\FooJob  Next Due: 1 minute from now')
+            ->expectsOutput('  * *     * *      *  Illuminate\Tests\Integration\Console\Scheduling\FooParamJob  Next Due: 1 minute from now')
+            ->expectsOutput('  * *     * *      *  foo-named-job .............. Next Due: 1 minute from now')
+            ->expectsOutput('  * *     * *      *  foo-named-param-job ........ Next Due: 1 minute from now')
             ->expectsOutput('  0 9,17  * *      *  php artisan inspire ......... Next Due: 9 hours from now')
             ->expectsOutput('  0 10    * *      *  php artisan inspire ........ Next Due: 10 hours from now')
-            ->expectsOutput('  * *     * *      *  Closure at: Illuminate\Tests\Integration\Console\Scheduling\FooCall  Next Due: 1 minute from now')
+            ->expectsOutput('  * *     * *      *  Illuminate\Tests\Integration\Console\Scheduling\FooCall  Next Due: 1 minute from now')
             ->expectsOutput('  * *     * *      *  Closure at: Illuminate\Tests\Integration\Console\Scheduling\FooCall::fooFunction  Next Due: 1 minute from now')
             ->expectsOutput('  * *     * *      *  Closure at: '.$closureFilePath.':'.$closureLineNumber.'  Next Due: 1 minute from now');
     }
@@ -64,6 +70,9 @@ public function testDisplayScheduleWithSort()
         $this->schedule->command('inspire')->twiceDaily(14, 18);
         $this->schedule->command('foobar', ['a' => 'b'])->everyMinute();
         $this->schedule->job(FooJob::class)->everyMinute();
+        $this->schedule->job(new FooParamJob('test'))->everyMinute();
+        $this->schedule->job(FooJob::class)->name('foo-named-job')->everyMinute();
+        $this->schedule->job(new FooParamJob('test'))->name('foo-named-param-job')->everyMinute();
         $this->schedule->command('inspire')->cron('0 9,17 * * *');
         $this->schedule->command('inspire')->cron("0 10\t* * *");
         $this->schedule->call(FooCall::class)->everyMinute();
@@ -77,7 +86,10 @@ public function testDisplayScheduleWithSort()
             ->assertSuccessful()
             ->expectsOutput('  * *     * *      *  php artisan foobar a='.ProcessUtils::escapeArgument('b').' ... Next Due: 1 minute from now')
             ->expectsOutput('  * *     * *      *  Illuminate\Tests\Integration\Console\Scheduling\FooJob  Next Due: 1 minute from now')
-            ->expectsOutput('  * *     * *      *  Closure at: Illuminate\Tests\Integration\Console\Scheduling\FooCall  Next Due: 1 minute from now')
+            ->expectsOutput('  * *     * *      *  Illuminate\Tests\Integration\Console\Scheduling\FooParamJob  Next Due: 1 minute from now')
+            ->expectsOutput('  * *     * *      *  foo-named-job .............. Next Due: 1 minute from now')
+            ->expectsOutput('  * *     * *      *  foo-named-param-job ........ Next Due: 1 minute from now')
+            ->expectsOutput('  * *     * *      *  Illuminate\Tests\Integration\Console\Scheduling\FooCall  Next Due: 1 minute from now')
             ->expectsOutput('  * *     * *      *  Closure at: Illuminate\Tests\Integration\Console\Scheduling\FooCall::fooFunction  Next Due: 1 minute from now')
             ->expectsOutput('  * *     * *      *  Closure at: '.$closureFilePath.':'.$closureLineNumber.'  Next Due: 1 minute from now')
             ->expectsOutput('  0 9,17  * *      *  php artisan inspire ......... Next Due: 9 hours from now')
@@ -117,6 +129,13 @@ class FooJob
 {
 }
 
+class FooParamJob
+{
+    public function __construct($param)
+    {
+    }
+}
+
 class FooCall
 {
     public function __invoke(): void