/
ScheduleListCommandTest.php
194 lines (165 loc) · 9.67 KB
/
ScheduleListCommandTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace Illuminate\Tests\Integration\Console\Scheduling;
use Illuminate\Console\Command;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Console\Scheduling\ScheduleListCommand;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\ProcessUtils;
use Orchestra\Testbench\TestCase;
class ScheduleListCommandTest extends TestCase
{
public $schedule;
protected function setUp(): void
{
parent::setUp();
Carbon::setTestNow('2023-01-01');
ScheduleListCommand::resolveTerminalWidthUsing(fn () => 80);
$this->schedule = $this->app->make(Schedule::class);
}
public function testDisplayEmptySchedule()
{
$this->artisan(ScheduleListCommand::class)
->assertSuccessful()
->expectsOutputToContain('No scheduled tasks have been defined.');
}
public function testDisplaySchedule()
{
$this->schedule->command(FooCommand::class)->quarterly();
$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();
$this->schedule->call([FooCall::class, 'fooFunction'])->everyMinute();
$this->schedule->call(fn () => '')->everyMinute();
$closureLineNumber = __LINE__ - 1;
$closureFilePath = __FILE__;
$this->artisan(ScheduleListCommand::class)
->assertSuccessful()
->expectsOutput(' 0 0 1 1-12/3 * php artisan foo:command .... Next Due: 3 months from now')
->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(' * * * * * 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');
}
public function testDisplayScheduleWithSort()
{
$this->schedule->command(FooCommand::class)->quarterly();
$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();
$this->schedule->call([FooCall::class, 'fooFunction'])->everyMinute();
$this->schedule->call(fn () => '')->everyMinute();
$closureLineNumber = __LINE__ - 1;
$closureFilePath = __FILE__;
$this->artisan(ScheduleListCommand::class, ['--next' => true])
->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(' * * * * * 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')
->expectsOutput(' 0 10 * * * php artisan inspire ........ Next Due: 10 hours from now')
->expectsOutput(' 0 14,18 * * * php artisan inspire ........ Next Due: 14 hours from now')
->expectsOutput(' 0 0 1 1-12/3 * php artisan foo:command .... Next Due: 3 months from now');
}
public function testDisplayScheduleInVerboseMode()
{
$this->schedule->command(FooCommand::class)->everyMinute();
$this->artisan(ScheduleListCommand::class, ['-v' => true])
->assertSuccessful()
->expectsOutputToContain('Next Due: '.now()->setMinutes(1)->format('Y-m-d H:i:s P'))
->expectsOutput(' ⇁ This is the description of the command.');
}
public function testDisplayScheduleSubMinute()
{
$this->schedule->command('inspire')->weekly()->everySecond();
$this->schedule->command('inspire')->everyTwoSeconds();
$this->schedule->command('inspire')->everyFiveSeconds();
$this->schedule->command('inspire')->everyTenSeconds();
$this->schedule->command('inspire')->everyFifteenSeconds();
$this->schedule->command('inspire')->everyTwentySeconds();
$this->schedule->command('inspire')->everyThirtySeconds();
$this->artisan(ScheduleListCommand::class)
->assertSuccessful()
->expectsOutput(' * 0 * * 0 1s php artisan inspire ............. Next Due: 1 second from now')
->expectsOutput(' * * * * * 2s php artisan inspire ............ Next Due: 2 seconds from now')
->expectsOutput(' * * * * * 5s php artisan inspire ............ Next Due: 5 seconds from now')
->expectsOutput(' * * * * * 10s php artisan inspire ........... Next Due: 10 seconds from now')
->expectsOutput(' * * * * * 15s php artisan inspire ........... Next Due: 15 seconds from now')
->expectsOutput(' * * * * * 20s php artisan inspire ........... Next Due: 20 seconds from now')
->expectsOutput(' * * * * * 30s php artisan inspire ........... Next Due: 30 seconds from now');
}
public function testClosureCommandsMayBeScheduled()
{
$closure = function () {
};
Artisan::command('one', $closure)->weekly()->everySecond();
Artisan::command('two', $closure)->everyTwoSeconds();
Artisan::command('three', $closure)->everyFiveSeconds();
Artisan::command('four', $closure)->everyTenSeconds();
Artisan::command('five', $closure)->everyFifteenSeconds();
Artisan::command('six', $closure)->everyTwentySeconds()->hourly();
Artisan::command('six', $closure)->everyThreeHours()->everySecond();
$this->artisan(ScheduleListCommand::class)
->assertSuccessful()
->expectsOutput(' * 0 * * 0 1s php artisan one ............... Next Due: 1 second from now')
->expectsOutput(' * * * * * 2s php artisan two .............. Next Due: 2 seconds from now')
->expectsOutput(' * * * * * 5s php artisan three ............ Next Due: 5 seconds from now')
->expectsOutput(' * * * * * 10s php artisan four ............ Next Due: 10 seconds from now')
->expectsOutput(' * * * * * 15s php artisan five ............ Next Due: 15 seconds from now')
->expectsOutput(' 0 * * * * 20s php artisan six ............. Next Due: 20 seconds from now')
->expectsOutput(' * */3 * * * 1s php artisan six ............... Next Due: 1 second from now');
}
protected function tearDown(): void
{
parent::tearDown();
putenv('SHELL_VERBOSITY');
ScheduleListCommand::resolveTerminalWidthUsing(null);
}
}
class FooCommand extends Command
{
protected $signature = 'foo:command';
protected $description = 'This is the description of the command.';
}
class FooJob
{
}
class FooParamJob
{
public function __construct($param)
{
}
}
class FooCall
{
public function __invoke(): void
{
}
public function fooFunction(): void
{
}
}