‪TYPO3CMS  10.4
CronCommandTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
19 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
20 
24 class ‪CronCommandTest extends UnitTestCase
25 {
29  const ‪TIMESTAMP = 1262304000;
30 
34  protected ‪$timezoneBackup = '';
35 
42  protected function ‪setUp(): void
43  {
44  parent::setUp();
45  $this->timezoneBackup = date_default_timezone_get();
46  date_default_timezone_set('UTC');
47  }
48 
49  protected function ‪tearDown(): void
50  {
51  date_default_timezone_set($this->timezoneBackup);
52  parent::tearDown();
53  }
54 
59  {
60  $instance = new ‪CronCommand('2-3 * * * *');
61  self::assertSame(['2,3', '*', '*', '*', '*'], $instance->getCronCommandSections());
62  }
63 
68  {
69  $this->expectException(\InvalidArgumentException::class);
70  $this->expectExceptionCode(1291470170);
71  new ‪CronCommand('61 * * * *');
72  }
73 
78  {
79  $instance = new ‪CronCommand('* * * * *');
80  $currentTime = time();
81  $expectedTime = $currentTime - ($currentTime % 60) + 60;
82  self::assertSame($expectedTime, $instance->getTimestamp());
83  }
84 
89  {
90  $instance = new ‪CronCommand('* * * * *', self::TIMESTAMP);
91  self::assertSame(self::TIMESTAMP + 60, $instance->getTimestamp());
92  }
93 
98  {
99  $instance = new CronCommand('* * * * *', self::TIMESTAMP + 1);
100  self::assertSame(self::TIMESTAMP + 60, $instance->getTimestamp());
101  }
102 
106  public static function ‪expectedTimestampDataProvider()
107  {
108  return [
109  'every minute' => [
110  '* * * * *',
112  self::TIMESTAMP + 60,
113  self::TIMESTAMP + 120
114  ],
115  'once an hour at 1' => [
116  '1 * * * *',
118  self::TIMESTAMP + 60,
119  self::TIMESTAMP + 60 + 60 * 60
120  ],
121  'once an hour at 0' => [
122  '0 * * * *',
124  self::TIMESTAMP + 60 * 60,
125  self::TIMESTAMP + 60 * 60 + 60 * 60
126  ],
127  'once a day at 1:00' => [
128  '0 1 * * *',
130  self::TIMESTAMP + 60 * 60,
131  self::TIMESTAMP + 60 * 60 + 60 * 60 * 24
132  ],
133  'once a day at 0:00' => [
134  '0 0 * * *',
136  self::TIMESTAMP + 60 * 60 * 24,
137  self::TIMESTAMP + 60 * 60 * 24 * 2
138  ],
139  'once a month' => [
140  '0 0 4 * *',
142  self::TIMESTAMP + 60 * 60 * 24 * 3,
143  self::TIMESTAMP + 60 * 60 * 24 * 3 + 60 * 60 * 24 * 31
144  ],
145  'once every Saturday' => [
146  '0 0 * * sat',
148  self::TIMESTAMP + 60 * 60 * 24,
149  self::TIMESTAMP + 60 * 60 * 24 + 60 * 60 * 24 * 7
150  ],
151  'once every day in February' => [
152  '0 0 * feb *',
154  self::TIMESTAMP + 60 * 60 * 24 * 31,
155  self::TIMESTAMP + 60 * 60 * 24 * 31 + 60 * 60 * 24
156  ],
157  'day of week and day of month restricted, next match in day of month field' => [
158  '0 0 2 * sun',
160  self::TIMESTAMP + 60 * 60 * 24,
161  self::TIMESTAMP + 60 * 60 * 24 + 60 * 60 * 24
162  ],
163  'day of week and day of month restricted, next match in day of week field' => [
164  '0 0 3 * sat',
166  self::TIMESTAMP + 60 * 60 * 24,
167  self::TIMESTAMP + 60 * 60 * 24 + 60 * 60 * 24
168  ],
169  'list of minutes' => [
170  '2,4 * * * *',
172  self::TIMESTAMP + 120,
173  self::TIMESTAMP + 240
174  ],
175  'list of hours' => [
176  '0 2,4 * * *',
178  self::TIMESTAMP + 60 * 60 * 2,
179  self::TIMESTAMP + 60 * 60 * 4
180  ],
181  ];
182  }
183 
187  public static function ‪expectedCalculatedTimestampDataProvider()
188  {
189  return [
190  'every first day of month' => [
191  '0 0 1 * *',
193  '01-02-2010',
194  '01-03-2010',
195  ],
196  'once every February' => [
197  '0 0 1 feb *',
199  '01-02-2010',
200  '01-02-2011',
201  ],
202  'once every Friday February' => [
203  '0 0 * feb fri',
205  '05-02-2010',
206  '12-02-2010',
207  ],
208  'first day in February and every Friday' => [
209  '0 0 1 feb fri',
211  '01-02-2010',
212  '05-02-2010',
213  ],
214  '29th February leap year' => [
215  '0 0 29 feb *',
217  '29-02-2012',
218  '29-02-2016',
219  ],
220  'list of days in month' => [
221  '0 0 2,4 * *',
223  '02-01-2010',
224  '04-01-2010',
225  ],
226  'list of month' => [
227  '0 0 1 2,3 *',
229  '01-02-2010',
230  '01-03-2010',
231  ],
232  'list of days of weeks' => [
233  '0 0 * * 2,4',
235  '05-01-2010',
236  '07-01-2010',
237  ]
238  ];
239  }
240 
248  public function ‪calculateNextValueDeterminesCorrectNextTimestamp($cronCommand, $startTimestamp, $expectedTimestamp)
249  {
250  $instance = new CronCommand($cronCommand, $startTimestamp);
251  $instance->calculateNextValue();
252  self::assertSame($expectedTimestamp, $instance->getTimestamp());
253  }
254 
262  public function ‪calculateNextValueDeterminesCorrectNextCalculatedTimestamp($cronCommand, $startTimestamp, $expectedTimestamp)
263  {
264  $instance = new CronCommand($cronCommand, $startTimestamp);
265  $instance->calculateNextValue();
266  self::assertSame(strtotime($expectedTimestamp), $instance->getTimestamp());
267  }
268 
277  public function ‪calculateNextValueDeterminesCorrectNextTimestampOnConsecutiveCall($cronCommand, $startTimestamp, $firstTimestamp, $secondTimestamp)
278  {
279  $instance = new CronCommand($cronCommand, $firstTimestamp);
280  $instance->calculateNextValue();
281  self::assertSame($secondTimestamp, $instance->getTimestamp());
282  }
283 
292  public function ‪calculateNextValueDeterminesCorrectNextCalculatedTimestampOnConsecutiveCall($cronCommand, $startTimestamp, $firstTimestamp, $secondTimestamp)
293  {
294  $instance = new CronCommand($cronCommand, strtotime($firstTimestamp));
295  $instance->calculateNextValue();
296  self::assertSame(strtotime($secondTimestamp), $instance->getTimestamp());
297  }
298 
303  {
304  $backupTimezone = date_default_timezone_get();
305  date_default_timezone_set('Europe/Berlin');
306  $instance = new CronCommand('* 3 28 mar *', self::TIMESTAMP);
307  $instance->calculateNextValue();
308  date_default_timezone_set($backupTimezone);
309  self::assertSame(1269741600, $instance->getTimestamp());
310  }
311 
316  {
317  $this->expectException(\RuntimeException::class);
318  $this->expectExceptionCode(1291501280);
319  $instance = new CronCommand('* * 31 apr *', self::TIMESTAMP);
320  $instance->calculateNextValue();
321  }
322 
326  public function ‪getTimestampReturnsInteger()
327  {
328  $instance = new CronCommand('* * * * *');
329  self::assertIsInt($instance->getTimestamp());
330  }
331 
335  public function ‪getCronCommandSectionsReturnsArray()
336  {
337  $instance = new CronCommand('* * * * *');
338  self::assertIsArray($instance->getCronCommandSections());
339  }
340 }
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\TIMESTAMP
‪const TIMESTAMP
Definition: CronCommandTest.php:29
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\constructorSetsTimestampToGiveTimestampRoundedDownToSixtySeconds
‪constructorSetsTimestampToGiveTimestampRoundedDownToSixtySeconds()
Definition: CronCommandTest.php:96
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\constructorSetsNormalizedCronCommandSections
‪constructorSetsNormalizedCronCommandSections()
Definition: CronCommandTest.php:57
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\calculateNextValueDeterminesCorrectNextCalculatedTimestamp
‪calculateNextValueDeterminesCorrectNextCalculatedTimestamp($cronCommand, $startTimestamp, $expectedTimestamp)
Definition: CronCommandTest.php:261
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\getCronCommandSectionsReturnsArray
‪getCronCommandSectionsReturnsArray()
Definition: CronCommandTest.php:334
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\constructorSetsTimestampToGivenTimestampPlusSixtySeconds
‪constructorSetsTimestampToGivenTimestampPlusSixtySeconds()
Definition: CronCommandTest.php:87
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\calculateNextValueDeterminesCorrectNextTimestampOnChangeToSummertime
‪calculateNextValueDeterminesCorrectNextTimestampOnChangeToSummertime()
Definition: CronCommandTest.php:301
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\calculateNextValueDeterminesCorrectNextTimestamp
‪calculateNextValueDeterminesCorrectNextTimestamp($cronCommand, $startTimestamp, $expectedTimestamp)
Definition: CronCommandTest.php:247
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\constructorSetsTimestampToNowPlusOneMinuteRoundedDownToSixtySeconds
‪constructorSetsTimestampToNowPlusOneMinuteRoundedDownToSixtySeconds()
Definition: CronCommandTest.php:76
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\tearDown
‪tearDown()
Definition: CronCommandTest.php:48
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\calculateNextValueDeterminesCorrectNextTimestampOnConsecutiveCall
‪calculateNextValueDeterminesCorrectNextTimestampOnConsecutiveCall($cronCommand, $startTimestamp, $firstTimestamp, $secondTimestamp)
Definition: CronCommandTest.php:276
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\getTimestampReturnsInteger
‪getTimestampReturnsInteger()
Definition: CronCommandTest.php:325
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\calculateNextValueDeterminesCorrectNextCalculatedTimestampOnConsecutiveCall
‪calculateNextValueDeterminesCorrectNextCalculatedTimestampOnConsecutiveCall($cronCommand, $startTimestamp, $firstTimestamp, $secondTimestamp)
Definition: CronCommandTest.php:291
‪TYPO3\CMS\Scheduler\CronCommand\CronCommand
Definition: CronCommand.php:24
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\expectedTimestampDataProvider
‪static array expectedTimestampDataProvider()
Definition: CronCommandTest.php:105
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\constructorThrowsExceptionForInvalidCronCommand
‪constructorThrowsExceptionForInvalidCronCommand()
Definition: CronCommandTest.php:66
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest
Definition: CronCommandTest.php:25
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\expectedCalculatedTimestampDataProvider
‪static array expectedCalculatedTimestampDataProvider()
Definition: CronCommandTest.php:186
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\calculateNextValueThrowsExceptionWithImpossibleCronCommand
‪calculateNextValueThrowsExceptionWithImpossibleCronCommand()
Definition: CronCommandTest.php:314
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\setUp
‪setUp()
Definition: CronCommandTest.php:41
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\$timezoneBackup
‪string $timezoneBackup
Definition: CronCommandTest.php:33