forked from codeigniter4/tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCronExpressionTest.php
More file actions
251 lines (211 loc) · 9.02 KB
/
CronExpressionTest.php
File metadata and controls
251 lines (211 loc) · 9.02 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Tasks.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
use CodeIgniter\I18n\Time;
use CodeIgniter\Tasks\CronExpression;
use CodeIgniter\Tasks\Exceptions\TasksException;
use CodeIgniter\Test\CIUnitTestCase as TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
/**
* @internal
*/
final class CronExpressionTest extends TestCase
{
private CronExpression $cron;
protected function setUp(): void
{
parent::setUp();
$this->cron = new CronExpression();
}
public function testMinutes()
{
$this->assertTrue($this->cron->shouldRun('* * * * *'));
$this->cron->testTime('2020-05-01 10:04 am');
$this->assertFalse($this->cron->shouldRun('10 * * * *'));
$this->assertTrue($this->cron->shouldRun('4 * * * *'));
$this->assertTrue($this->cron->shouldRun('04 * * * *'));
$this->assertTrue($this->cron->shouldRun('4,8 * * * *'));
$this->assertTrue($this->cron->shouldRun('1,2,4 * * * *'));
$this->assertFalse($this->cron->shouldRun('5-15 * * * *'));
$this->assertTrue($this->cron->shouldRun('1-5 * * * *'));
$this->assertTrue($this->cron->shouldRun('/4 * * * *'));
$this->assertTrue($this->cron->shouldRun('/2 * * * *'));
$this->assertFalse($this->cron->shouldRun('/5 * * * *'));
}
public function testMinutesInvalidNoNumber()
{
$this->cron->testTime('2020-05-01 10:04 am');
$this->expectException(TasksException::class);
$this->expectExceptionMessage('"/ * * * *" is not a valid cron expression.');
$this->assertFalse($this->cron->shouldRun('/ * * * *'));
}
public function testMinutesInvalidNotANumber()
{
$this->cron->testTime('2020-05-01 10:04 am');
$this->expectException(TasksException::class);
$this->expectExceptionMessage('"/a * * * *" is not a valid cron expression.');
$this->assertFalse($this->cron->shouldRun('/a * * * *'));
}
public function testHours()
{
$this->cron->testTime('2020-05-01 10:04 am');
$this->assertTrue($this->cron->shouldRun('* * * * *'));
$this->assertTrue($this->cron->shouldRun('* 10 * * *'));
$this->assertFalse($this->cron->shouldRun('* 20 * * *'));
$this->assertTrue($this->cron->shouldRun('4 10 * * *'));
$this->assertFalse($this->cron->shouldRun('10 10 * * *'));
$this->assertTrue($this->cron->shouldRun('* 10,11 * * *'));
$this->assertTrue($this->cron->shouldRun('* 9,11,10 * * *'));
$this->assertFalse($this->cron->shouldRun('* 9,11,12 * * *'));
$this->assertTrue($this->cron->shouldRun('* 8-11 * * *'));
$this->assertFalse($this->cron->shouldRun('* 7-9 * * *'));
$this->assertTrue($this->cron->shouldRun('* /2 * * *'));
$this->assertTrue($this->cron->shouldRun('* /5 * * *'));
$this->assertFalse($this->cron->shouldRun('* /3 * * *'));
}
public function testMonthDay()
{
$this->cron->testTime('2020-05-01 10:04 am');
$this->assertTrue($this->cron->shouldRun('* * 1 * *'));
$this->assertTrue($this->cron->shouldRun('* * 01 * *'));
$this->assertFalse($this->cron->shouldRun('* * 02 * *'));
$this->assertTrue($this->cron->shouldRun('04 10 1 * *'));
$this->assertFalse($this->cron->shouldRun('05 10 1 * *'));
$this->assertFalse($this->cron->shouldRun('04 11 1 * *'));
$this->assertTrue($this->cron->shouldRun('* * 1,2 * *'));
$this->assertFalse($this->cron->shouldRun('* * 3,2 * *'));
$this->assertTrue($this->cron->shouldRun('* * 1-3 * *'));
$this->assertFalse($this->cron->shouldRun('* * 3-5 * *'));
$this->assertTrue($this->cron->shouldRun('* * /1 * *'));
$this->assertFalse($this->cron->shouldRun('* * /2 * *'));
}
public function testMonth()
{
$this->cron->testTime('2020-05-01 10:04 am');
$this->assertTrue($this->cron->shouldRun('* * * 5 *'));
$this->assertFalse($this->cron->shouldRun('* * * 6 *'));
$this->assertTrue($this->cron->shouldRun('* * * 5,6 *'));
$this->assertFalse($this->cron->shouldRun('* * * 4,6 *'));
$this->assertTrue($this->cron->shouldRun('* * * 4-6 *'));
$this->assertFalse($this->cron->shouldRun('* * * 6-8 *'));
$this->assertTrue($this->cron->shouldRun('* * * /5 *'));
$this->assertFalse($this->cron->shouldRun('* * * /2 *'));
}
public function testWeekDay()
{
// May 1 is s Friday
$this->cron->testTime('2020-05-01 10:04 am');
$this->assertTrue($this->cron->shouldRun('* * * * 5'));
$this->assertFalse($this->cron->shouldRun('* * * * 6'));
$this->assertTrue($this->cron->shouldRun('* * * * 5,6'));
$this->assertFalse($this->cron->shouldRun('* * * * 4,6'));
$this->assertFalse($this->cron->shouldRun('* * * * 1-3'));
$this->assertTrue($this->cron->shouldRun('* * * * 4-6'));
$this->assertTrue($this->cron->shouldRun('* * * * /5'));
$this->assertFalse($this->cron->shouldRun('* * * * /2'));
}
public function testHoursAndMins()
{
$this->cron->testTime('6:30 PM');
$this->assertTrue($this->cron->shouldRun('30 18 * * *'));
}
/**
* @param mixed $hourTrue
* @param mixed $hourFalse
*/
#[DataProvider('provideEveryHour')]
public function testEveryHour($hourTrue, $hourFalse)
{
$this->cron->testTime($hourTrue);
$this->assertTrue($this->cron->shouldRun('00 * * * *'));
$this->cron->testTime($hourFalse);
$this->assertFalse($this->cron->shouldRun('00 * * * *'));
}
public static function provideEveryHour(): iterable
{
$hours24 = array_map(static fn ($h) => [
$h . ':00',
$h . ':10',
], range(0, 23));
$hoursAM = array_map(static fn ($h) => [
$h . ':00 AM',
$h . ':10 AM',
], range(1, 12));
$hoursPM = array_map(static fn ($h) => [
$h . ':00 PM',
$h . ':10 PM',
], range(1, 12));
return [
...$hours24,
...$hoursAM,
...$hoursPM,
];
}
public function testQuarterPastHour()
{
$this->cron->testTime('6:15 PM');
$this->assertTrue($this->cron->shouldRun('15 * * * *'));
$this->cron->testTime('6:30 PM');
$this->assertFalse($this->cron->shouldRun('15 * * * *'));
}
public function testSpecificDate()
{
$this->cron->testTime('January 1, 2020 12:30 am');
$this->assertTrue($this->cron->shouldRun('30 0 1 1,6,12 *'));
$this->cron->testTime('June 1, 2020 12:30 am');
$this->assertTrue($this->cron->shouldRun('30 0 1 1,6,12 *'));
$this->cron->testTime('December 1, 2020 12:30 am');
$this->assertTrue($this->cron->shouldRun('30 0 1 1,6,12 *'));
$this->cron->testTime('February 1, 2020 12:30 am');
$this->assertFalse($this->cron->shouldRun('30 0 1 1,6,12 *'));
}
public function testEveryWeekdayInMonth()
{
// October 5th is a Monday
$this->cron->testTime('October 5, 2020 8:00 pm');
$this->assertTrue($this->cron->shouldRun('0 20 * 10 1-5'));
$this->cron->testTime('October 4, 2020 8:00 pm');
$this->assertFalse($this->cron->shouldRun('0 20 * 10 1-5'));
// Another Monday
$this->cron->testTime('November 2, 2020 8:00 pm');
$this->assertFalse($this->cron->shouldRun('0 20 * 10 1-5'));
}
#[DataProvider('provideNextRun')]
public function testNextRun(string $exp, string $expected)
{
$this->cron->testTime('October 5, 2020 8:00 pm');
$next = $this->cron->nextRun($exp);
$this->assertInstanceOf(Time::class, $next);
$this->assertSame($expected, $next->format('F j, Y g:i a'));
}
public static function provideNextRun(): iterable
{
return [
['* * * * *', 'October 5, 2020 8:01 pm'],
['4 * * * *', 'October 5, 2020 8:04 pm'],
['5-10 * * * *', 'October 5, 2020 8:05 pm'],
['57,3,8,10 * * * *', 'October 5, 2020 8:03 pm'],
['*/5 * * * *', 'October 5, 2020 8:05 pm'],
['30/5 * * * *', 'October 5, 2020 8:30 pm'],
['* 6 * * *', 'October 6, 2020 6:00 am'],
['* 12-14 * * *', 'October 6, 2020 12:00 pm'],
['* 5,6 * * *', 'October 6, 2020 5:00 am'],
['* 2/4 * * *', 'October 5, 2020 10:00 pm'],
['5 10 * * *', 'October 6, 2020 10:05 am'],
['* * 10 * *', 'October 10, 2020 8:00 pm'],
['5 4 10 * *', 'October 10, 2020 4:05 am'],
['* * * 3 *', 'March 5, 2021 8:00 pm'],
['* 4/5 12 3 *', 'March 12, 2021 4:00 am'],
['* * * * Wed', 'October 6, 2020 8:00 pm'],
['* * * * 3', 'October 6, 2020 8:00 pm'],
['* * * * 6,0', 'October 9, 2020 8:00 pm'],
];
}
}