‪TYPO3CMS  11.5
CronCommandTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
21 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
22 
26 class ‪CronCommandTest extends UnitTestCase
27 {
31  private const ‪TIMESTAMP = 1262304000;
32 
36  protected string ‪$timezoneBackup = '';
37 
44  protected function ‪setUp(): void
45  {
46  parent::setUp();
47  $this->timezoneBackup = date_default_timezone_get();
48  date_default_timezone_set('UTC');
49  }
50 
51  protected function ‪tearDown(): void
52  {
53  date_default_timezone_set($this->timezoneBackup);
54  parent::tearDown();
55  }
56 
61  {
62  $instance = new ‪CronCommand('2-3 * * * *');
63  self::assertSame(['2,3', '*', '*', '*', '*'], $instance->getCronCommandSections());
64  }
65 
70  {
71  $this->expectException(\InvalidArgumentException::class);
72  $this->expectExceptionCode(1291470170);
73  new ‪CronCommand('61 * * * *');
74  }
75 
80  {
81  $instance = new ‪CronCommand('* * * * *');
82  $currentTime = time();
83  $expectedTime = $currentTime - ($currentTime % 60) + 60;
84  self::assertSame($expectedTime, $instance->getTimestamp());
85  }
86 
91  {
92  $instance = new ‪CronCommand('* * * * *', self::TIMESTAMP);
93  self::assertSame(self::TIMESTAMP + 60, $instance->getTimestamp());
94  }
95 
100  {
101  $instance = new ‪CronCommand('* * * * *', self::TIMESTAMP + 1);
102  self::assertSame(self::TIMESTAMP + 60, $instance->getTimestamp());
103  }
104 
108  public static function ‪expectedTimestampDataProvider(): array
109  {
110  return [
111  'every minute' => [
112  '* * * * *',
114  self::TIMESTAMP + 60,
115  self::TIMESTAMP + 120,
116  ],
117  'once an hour at 1' => [
118  '1 * * * *',
120  self::TIMESTAMP + 60,
121  self::TIMESTAMP + 60 + 60 * 60,
122  ],
123  'once an hour at 0' => [
124  '0 * * * *',
126  self::TIMESTAMP + 60 * 60,
127  self::TIMESTAMP + 60 * 60 + 60 * 60,
128  ],
129  'once a day at 1:00' => [
130  '0 1 * * *',
132  self::TIMESTAMP + 60 * 60,
133  self::TIMESTAMP + 60 * 60 + 60 * 60 * 24,
134  ],
135  'once a day at 0:00' => [
136  '0 0 * * *',
138  self::TIMESTAMP + 60 * 60 * 24,
139  self::TIMESTAMP + 60 * 60 * 24 * 2,
140  ],
141  'once a month' => [
142  '0 0 4 * *',
144  self::TIMESTAMP + 60 * 60 * 24 * 3,
145  self::TIMESTAMP + 60 * 60 * 24 * 3 + 60 * 60 * 24 * 31,
146  ],
147  'once every Saturday' => [
148  '0 0 * * sat',
150  self::TIMESTAMP + 60 * 60 * 24,
151  self::TIMESTAMP + 60 * 60 * 24 + 60 * 60 * 24 * 7,
152  ],
153  'once every day in February' => [
154  '0 0 * feb *',
156  self::TIMESTAMP + 60 * 60 * 24 * 31,
157  self::TIMESTAMP + 60 * 60 * 24 * 31 + 60 * 60 * 24,
158  ],
159  'day of week and day of month restricted, next match in day of month field' => [
160  '0 0 2 * sun',
162  self::TIMESTAMP + 60 * 60 * 24,
163  self::TIMESTAMP + 60 * 60 * 24 + 60 * 60 * 24,
164  ],
165  'day of week and day of month restricted, next match in day of week field' => [
166  '0 0 3 * sat',
168  self::TIMESTAMP + 60 * 60 * 24,
169  self::TIMESTAMP + 60 * 60 * 24 + 60 * 60 * 24,
170  ],
171  'list of minutes' => [
172  '2,4 * * * *',
174  self::TIMESTAMP + 120,
175  self::TIMESTAMP + 240,
176  ],
177  'list of hours' => [
178  '0 2,4 * * *',
180  self::TIMESTAMP + 60 * 60 * 2,
181  self::TIMESTAMP + 60 * 60 * 4,
182  ],
183  ];
184  }
185 
189  public static function ‪expectedCalculatedTimestampDataProvider(): array
190  {
191  return [
192  'every first day of month' => [
193  '0 0 1 * *',
195  '01-02-2010',
196  '01-03-2010',
197  ],
198  'once every February' => [
199  '0 0 1 feb *',
201  '01-02-2010',
202  '01-02-2011',
203  ],
204  'once every Friday February' => [
205  '0 0 * feb fri',
207  '05-02-2010',
208  '12-02-2010',
209  ],
210  'first day in February and every Friday' => [
211  '0 0 1 feb fri',
213  '01-02-2010',
214  '05-02-2010',
215  ],
216  '29th February leap year' => [
217  '0 0 29 feb *',
219  '29-02-2012',
220  '29-02-2016',
221  ],
222  'list of days in month' => [
223  '0 0 2,4 * *',
225  '02-01-2010',
226  '04-01-2010',
227  ],
228  'list of month' => [
229  '0 0 1 2,3 *',
231  '01-02-2010',
232  '01-03-2010',
233  ],
234  'list of days of weeks' => [
235  '0 0 * * 2,4',
237  '05-01-2010',
238  '07-01-2010',
239  ],
240  ];
241  }
242 
250  public function ‪calculateNextValueDeterminesCorrectNextTimestamp(string $cronCommand, int $startTimestamp, int $expectedTimestamp): void
251  {
252  $instance = new ‪CronCommand($cronCommand, $startTimestamp);
253  $instance->calculateNextValue();
254  self::assertSame($expectedTimestamp, $instance->getTimestamp());
255  }
256 
264  public function ‪calculateNextValueDeterminesCorrectNextCalculatedTimestamp(string $cronCommand, int $startTimestamp, string $expectedTimestamp): void
265  {
266  $instance = new ‪CronCommand($cronCommand, $startTimestamp);
267  $instance->calculateNextValue();
268  self::assertSame(strtotime($expectedTimestamp), $instance->getTimestamp());
269  }
270 
279  public function ‪calculateNextValueDeterminesCorrectNextTimestampOnConsecutiveCall(string $cronCommand, int $startTimestamp, int $firstTimestamp, int $secondTimestamp): void
280  {
281  $instance = new ‪CronCommand($cronCommand, $firstTimestamp);
282  $instance->calculateNextValue();
283  self::assertSame($secondTimestamp, $instance->getTimestamp());
284  }
285 
294  public function ‪calculateNextValueDeterminesCorrectNextCalculatedTimestampOnConsecutiveCall(string $cronCommand, int $startTimestamp, string $firstTimestamp, string $secondTimestamp): void
295  {
296  $instance = new ‪CronCommand($cronCommand, strtotime($firstTimestamp));
297  $instance->calculateNextValue();
298  self::assertSame(strtotime($secondTimestamp), $instance->getTimestamp());
299  }
300 
305  {
306  $backupTimezone = date_default_timezone_get();
307  date_default_timezone_set('Europe/Berlin');
308  $instance = new ‪CronCommand('* 3 28 mar *', self::TIMESTAMP);
309  $instance->calculateNextValue();
310  date_default_timezone_set($backupTimezone);
311  self::assertSame(1269741600, $instance->getTimestamp());
312  }
313 
318  {
319  $this->expectException(\RuntimeException::class);
320  $this->expectExceptionCode(1291501280);
321  $instance = new ‪CronCommand('* * 31 apr *', self::TIMESTAMP);
322  $instance->calculateNextValue();
323  }
324 
328  public function ‪getTimestampReturnsInteger(): void
329  {
330  $instance = new ‪CronCommand('* * * * *');
331  self::assertIsInt($instance->getTimestamp());
332  }
333 
338  {
339  $instance = new ‪CronCommand('* * * * *');
340  self::assertIsArray($instance->getCronCommandSections());
341  }
342 }
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\TIMESTAMP
‪const TIMESTAMP
Definition: CronCommandTest.php:31
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\constructorSetsTimestampToGiveTimestampRoundedDownToSixtySeconds
‪constructorSetsTimestampToGiveTimestampRoundedDownToSixtySeconds()
Definition: CronCommandTest.php:99
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\calculateNextValueDeterminesCorrectNextTimestampOnConsecutiveCall
‪calculateNextValueDeterminesCorrectNextTimestampOnConsecutiveCall(string $cronCommand, int $startTimestamp, int $firstTimestamp, int $secondTimestamp)
Definition: CronCommandTest.php:279
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\constructorSetsNormalizedCronCommandSections
‪constructorSetsNormalizedCronCommandSections()
Definition: CronCommandTest.php:60
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\getCronCommandSectionsReturnsArray
‪getCronCommandSectionsReturnsArray()
Definition: CronCommandTest.php:337
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\constructorSetsTimestampToGivenTimestampPlusSixtySeconds
‪constructorSetsTimestampToGivenTimestampPlusSixtySeconds()
Definition: CronCommandTest.php:90
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\calculateNextValueDeterminesCorrectNextTimestampOnChangeToSummertime
‪calculateNextValueDeterminesCorrectNextTimestampOnChangeToSummertime()
Definition: CronCommandTest.php:304
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\constructorSetsTimestampToNowPlusOneMinuteRoundedDownToSixtySeconds
‪constructorSetsTimestampToNowPlusOneMinuteRoundedDownToSixtySeconds()
Definition: CronCommandTest.php:79
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\tearDown
‪tearDown()
Definition: CronCommandTest.php:51
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\getTimestampReturnsInteger
‪getTimestampReturnsInteger()
Definition: CronCommandTest.php:328
‪TYPO3\CMS\Scheduler\CronCommand\CronCommand
Definition: CronCommand.php:24
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\expectedTimestampDataProvider
‪static array expectedTimestampDataProvider()
Definition: CronCommandTest.php:108
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\calculateNextValueDeterminesCorrectNextTimestamp
‪calculateNextValueDeterminesCorrectNextTimestamp(string $cronCommand, int $startTimestamp, int $expectedTimestamp)
Definition: CronCommandTest.php:250
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\calculateNextValueDeterminesCorrectNextCalculatedTimestamp
‪calculateNextValueDeterminesCorrectNextCalculatedTimestamp(string $cronCommand, int $startTimestamp, string $expectedTimestamp)
Definition: CronCommandTest.php:264
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\constructorThrowsExceptionForInvalidCronCommand
‪constructorThrowsExceptionForInvalidCronCommand()
Definition: CronCommandTest.php:69
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\calculateNextValueDeterminesCorrectNextCalculatedTimestampOnConsecutiveCall
‪calculateNextValueDeterminesCorrectNextCalculatedTimestampOnConsecutiveCall(string $cronCommand, int $startTimestamp, string $firstTimestamp, string $secondTimestamp)
Definition: CronCommandTest.php:294
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest
Definition: CronCommandTest.php:27
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\expectedCalculatedTimestampDataProvider
‪static array expectedCalculatedTimestampDataProvider()
Definition: CronCommandTest.php:189
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\calculateNextValueThrowsExceptionWithImpossibleCronCommand
‪calculateNextValueThrowsExceptionWithImpossibleCronCommand()
Definition: CronCommandTest.php:317
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\setUp
‪setUp()
Definition: CronCommandTest.php:44
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\$timezoneBackup
‪string $timezoneBackup
Definition: CronCommandTest.php:36