|
2 | 2 |
|
3 | 3 | namespace Illuminate\Tests\Integration\Console\Scheduling; |
4 | 4 |
|
| 5 | +use Illuminate\Console\Application; |
5 | 6 | use Illuminate\Console\Command; |
6 | 7 | use Illuminate\Console\Scheduling\Schedule; |
7 | 8 | use Illuminate\Console\Scheduling\ScheduleListCommand; |
@@ -31,6 +32,15 @@ public function testDisplayEmptySchedule() |
31 | 32 | ->expectsOutputToContain('No scheduled tasks have been defined.'); |
32 | 33 | } |
33 | 34 |
|
| 35 | + public function testDisplayEmptyScheduleAsJson() |
| 36 | + { |
| 37 | + $this->withoutMockingConsoleOutput()->artisan(ScheduleListCommand::class, ['--json' => true]); |
| 38 | + $output = Artisan::output(); |
| 39 | + |
| 40 | + $this->assertJson($output); |
| 41 | + $this->assertJsonStringEqualsJsonString('[]', $output); |
| 42 | + } |
| 43 | + |
34 | 44 | public function testDisplaySchedule() |
35 | 45 | { |
36 | 46 | $this->schedule->command(FooCommand::class)->quarterly(); |
@@ -65,6 +75,128 @@ public function testDisplaySchedule() |
65 | 75 | ->expectsOutput(' * * * * * Closure at: '.$closureFilePath.':'.$closureLineNumber.' Next Due: 1 minute from now'); |
66 | 76 | } |
67 | 77 |
|
| 78 | + public function testDisplayScheduleAsJson() |
| 79 | + { |
| 80 | + $this->schedule->command(FooCommand::class)->quarterly(); |
| 81 | + $this->schedule->command('inspire')->twiceDaily(14, 18); |
| 82 | + $this->schedule->command('foobar', ['a' => 'b'])->everyMinute(); |
| 83 | + $this->schedule->job(FooJob::class)->everyMinute(); |
| 84 | + $this->schedule->job(new FooParamJob('test'))->everyMinute(); |
| 85 | + $this->schedule->job(FooJob::class)->name('foo-named-job')->everyMinute(); |
| 86 | + $this->schedule->job(new FooParamJob('test'))->name('foo-named-param-job')->everyMinute(); |
| 87 | + $this->schedule->command('inspire')->cron('0 9,17 * * *'); |
| 88 | + $this->schedule->call(fn () => '')->everyMinute(); |
| 89 | + |
| 90 | + $this->withoutMockingConsoleOutput()->artisan(ScheduleListCommand::class, ['--json' => true]); |
| 91 | + $output = Artisan::output(); |
| 92 | + |
| 93 | + $this->assertJson($output); |
| 94 | + $data = json_decode($output, true); |
| 95 | + |
| 96 | + $this->assertIsArray($data); |
| 97 | + $this->assertCount(9, $data); |
| 98 | + |
| 99 | + $this->assertSame('0 0 1 1-12/3 *', $data[0]['expression']); |
| 100 | + $this->assertNull($data[0]['repeat_seconds']); |
| 101 | + $this->assertSame('php artisan foo:command', $data[0]['command']); |
| 102 | + $this->assertSame('This is the description of the command.', $data[0]['description']); |
| 103 | + $this->assertStringContainsString('2023-04-01 00:00:00', $data[0]['next_due_date']); |
| 104 | + $this->assertSame('3 months from now', $data[0]['next_due_date_human']); |
| 105 | + $this->assertFalse($data[0]['has_mutex']); |
| 106 | + |
| 107 | + $this->assertSame('* * * * *', $data[2]['expression']); |
| 108 | + $this->assertSame('php artisan foobar a='.ProcessUtils::escapeArgument('b'), $data[2]['command']); |
| 109 | + $this->assertNull($data[2]['description']); |
| 110 | + $this->assertSame('1 minute from now', $data[2]['next_due_date_human']); |
| 111 | + |
| 112 | + $this->assertSame('Illuminate\Tests\Integration\Console\Scheduling\FooJob', $data[3]['command']); |
| 113 | + |
| 114 | + $this->assertSame('foo-named-job', $data[5]['command']); |
| 115 | + |
| 116 | + $this->assertStringContainsString('Closure at:', $data[8]['command']); |
| 117 | + $this->assertStringContainsString('ScheduleListCommandTest.php', $data[8]['command']); |
| 118 | + } |
| 119 | + |
| 120 | + public function testDisplayScheduleWithSortAsJson() |
| 121 | + { |
| 122 | + $this->schedule->command(FooCommand::class)->quarterly(); |
| 123 | + $this->schedule->command('inspire')->twiceDaily(14, 18); |
| 124 | + $this->schedule->command('foobar', ['a' => 'b'])->everyMinute(); |
| 125 | + |
| 126 | + $this->withoutMockingConsoleOutput()->artisan(ScheduleListCommand::class, [ |
| 127 | + '--next' => true, |
| 128 | + '--json' => true, |
| 129 | + ]); |
| 130 | + $output = Artisan::output(); |
| 131 | + |
| 132 | + $this->assertJson($output); |
| 133 | + $data = json_decode($output, true); |
| 134 | + |
| 135 | + $this->assertIsArray($data); |
| 136 | + $this->assertCount(3, $data); |
| 137 | + |
| 138 | + $this->assertSame('* * * * *', $data[0]['expression']); |
| 139 | + $this->assertSame('1 minute from now', $data[0]['next_due_date_human']); |
| 140 | + $this->assertSame('php artisan foobar a='.ProcessUtils::escapeArgument('b'), $data[0]['command']); |
| 141 | + |
| 142 | + $this->assertSame('0 14,18 * * *', $data[1]['expression']); |
| 143 | + $this->assertSame('14 hours from now', $data[1]['next_due_date_human']); |
| 144 | + $this->assertSame('php artisan inspire', $data[1]['command']); |
| 145 | + |
| 146 | + $this->assertSame('0 0 1 1-12/3 *', $data[2]['expression']); |
| 147 | + $this->assertSame('3 months from now', $data[2]['next_due_date_human']); |
| 148 | + $this->assertSame('php artisan foo:command', $data[2]['command']); |
| 149 | + } |
| 150 | + |
| 151 | + public function testDisplayScheduleAsJsonWithTimezone() |
| 152 | + { |
| 153 | + $this->schedule->command('inspire')->daily(); |
| 154 | + |
| 155 | + $this->withoutMockingConsoleOutput()->artisan(ScheduleListCommand::class, [ |
| 156 | + '--timezone' => 'America/Chicago', |
| 157 | + '--json' => true, |
| 158 | + ]); |
| 159 | + $output = Artisan::output(); |
| 160 | + |
| 161 | + $this->assertJson($output); |
| 162 | + $data = json_decode($output, true); |
| 163 | + |
| 164 | + $this->assertIsArray($data); |
| 165 | + $this->assertCount(1, $data); |
| 166 | + $this->assertSame('America/Chicago', $data[0]['timezone']); |
| 167 | + $this->assertStringContainsString('-06:00', $data[0]['next_due_date']); |
| 168 | + $this->assertSame('php artisan inspire', $data[0]['command']); |
| 169 | + } |
| 170 | + |
| 171 | + public function testDisplayScheduleAsJsonInVerboseMode() |
| 172 | + { |
| 173 | + $this->schedule->command(FooCommand::class)->quarterly(); |
| 174 | + $this->schedule->command('inspire')->everyMinute(); |
| 175 | + $this->schedule->call(fn () => '')->everyMinute(); |
| 176 | + |
| 177 | + $this->withoutMockingConsoleOutput()->artisan(ScheduleListCommand::class, [ |
| 178 | + '--json' => true, |
| 179 | + '-v' => true, |
| 180 | + ]); |
| 181 | + $output = Artisan::output(); |
| 182 | + |
| 183 | + $this->assertJson($output); |
| 184 | + $data = json_decode($output, true); |
| 185 | + |
| 186 | + $this->assertIsArray($data); |
| 187 | + $this->assertCount(3, $data); |
| 188 | + |
| 189 | + $this->assertSame('0 0 1 1-12/3 *', $data[0]['expression']); |
| 190 | + $this->assertSame(Application::phpBinary().' '.Application::artisanBinary().' foo:command', $data[0]['command']); |
| 191 | + $this->assertSame('This is the description of the command.', $data[0]['description']); |
| 192 | + |
| 193 | + $this->assertSame('* * * * *', $data[1]['expression']); |
| 194 | + $this->assertSame(Application::phpBinary().' '.Application::artisanBinary().' inspire', $data[1]['command']); |
| 195 | + |
| 196 | + $this->assertStringContainsString('Closure at:', $data[2]['command']); |
| 197 | + $this->assertStringContainsString('ScheduleListCommandTest.php', $data[2]['command']); |
| 198 | + } |
| 199 | + |
68 | 200 | public function testDisplayScheduleWithSort() |
69 | 201 | { |
70 | 202 | $this->schedule->command(FooCommand::class)->quarterly(); |
|
0 commit comments