‪TYPO3CMS  9.5
CronCommandTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
19 
23 class ‪CronCommandTest extends UnitTestCase
24 {
28  const ‪TIMESTAMP = 1262304000;
29 
33  protected ‪$timezoneBackup = '';
34 
41  protected function ‪setUp()
42  {
43  $this->timezoneBackup = date_default_timezone_get();
44  date_default_timezone_set('UTC');
45  }
46 
47  protected function ‪tearDown()
48  {
49  date_default_timezone_set($this->timezoneBackup);
50  parent::tearDown();
51  }
52 
57  {
58  $instance = new ‪CronCommand('2-3 * * * *');
59  $this->assertSame(['2,3', '*', '*', '*', '*'], $instance->getCronCommandSections());
60  }
61 
66  {
67  $this->expectException(\InvalidArgumentException::class);
68  $this->expectExceptionCode(1291470170);
69  new CronCommand('61 * * * *');
70  }
71 
76  {
77  $instance = new CronCommand('* * * * *');
78  $currentTime = time();
79  $expectedTime = $currentTime - ($currentTime % 60) + 60;
80  $this->assertSame($expectedTime, $instance->getTimestamp());
81  }
82 
87  {
88  $instance = new CronCommand('* * * * *', self::TIMESTAMP);
89  $this->assertSame(self::TIMESTAMP + 60, $instance->getTimestamp());
90  }
91 
96  {
97  $instance = new CronCommand('* * * * *', self::TIMESTAMP + 1);
98  $this->assertSame(self::TIMESTAMP + 60, $instance->getTimestamp());
99  }
100 
104  public static function ‪expectedTimestampDataProvider()
105  {
106  return [
107  'every minute' => [
108  '* * * * *',
110  self::TIMESTAMP + 60,
111  self::TIMESTAMP + 120
112  ],
113  'once an hour at 1' => [
114  '1 * * * *',
116  self::TIMESTAMP + 60,
117  self::TIMESTAMP + 60 + 60 * 60
118  ],
119  'once an hour at 0' => [
120  '0 * * * *',
122  self::TIMESTAMP + 60 * 60,
123  self::TIMESTAMP + 60 * 60 + 60 * 60
124  ],
125  'once a day at 1:00' => [
126  '0 1 * * *',
128  self::TIMESTAMP + 60 * 60,
129  self::TIMESTAMP + 60 * 60 + 60 * 60 * 24
130  ],
131  'once a day at 0:00' => [
132  '0 0 * * *',
134  self::TIMESTAMP + 60 * 60 * 24,
135  self::TIMESTAMP + 60 * 60 * 24 * 2
136  ],
137  'once a month' => [
138  '0 0 4 * *',
140  self::TIMESTAMP + 60 * 60 * 24 * 3,
141  self::TIMESTAMP + 60 * 60 * 24 * 3 + 60 * 60 * 24 * 31
142  ],
143  'once every Saturday' => [
144  '0 0 * * sat',
146  self::TIMESTAMP + 60 * 60 * 24,
147  self::TIMESTAMP + 60 * 60 * 24 + 60 * 60 * 24 * 7
148  ],
149  'once every day in February' => [
150  '0 0 * feb *',
152  self::TIMESTAMP + 60 * 60 * 24 * 31,
153  self::TIMESTAMP + 60 * 60 * 24 * 31 + 60 * 60 * 24
154  ],
155  'day of week and day of month restricted, next match in day of month field' => [
156  '0 0 2 * sun',
158  self::TIMESTAMP + 60 * 60 * 24,
159  self::TIMESTAMP + 60 * 60 * 24 + 60 * 60 * 24
160  ],
161  'day of week and day of month restricted, next match in day of week field' => [
162  '0 0 3 * sat',
164  self::TIMESTAMP + 60 * 60 * 24,
165  self::TIMESTAMP + 60 * 60 * 24 + 60 * 60 * 24
166  ],
167  'list of minutes' => [
168  '2,4 * * * *',
170  self::TIMESTAMP + 120,
171  self::TIMESTAMP + 240
172  ],
173  'list of hours' => [
174  '0 2,4 * * *',
176  self::TIMESTAMP + 60 * 60 * 2,
177  self::TIMESTAMP + 60 * 60 * 4
178  ],
179  ];
180  }
181 
185  public static function ‪expectedCalculatedTimestampDataProvider()
186  {
187  return [
188  'every first day of month' => [
189  '0 0 1 * *',
191  '01-02-2010',
192  '01-03-2010',
193  ],
194  'once every February' => [
195  '0 0 1 feb *',
197  '01-02-2010',
198  '01-02-2011',
199  ],
200  'once every Friday February' => [
201  '0 0 * feb fri',
203  '05-02-2010',
204  '12-02-2010',
205  ],
206  'first day in February and every Friday' => [
207  '0 0 1 feb fri',
209  '01-02-2010',
210  '05-02-2010',
211  ],
212  '29th February leap year' => [
213  '0 0 29 feb *',
215  '29-02-2012',
216  '29-02-2016',
217  ],
218  'list of days in month' => [
219  '0 0 2,4 * *',
221  '02-01-2010',
222  '04-01-2010',
223  ],
224  'list of month' => [
225  '0 0 1 2,3 *',
227  '01-02-2010',
228  '01-03-2010',
229  ],
230  'list of days of weeks' => [
231  '0 0 * * 2,4',
233  '05-01-2010',
234  '07-01-2010',
235  ]
236  ];
237  }
238 
246  public function ‪calculateNextValueDeterminesCorrectNextTimestamp($cronCommand, $startTimestamp, $expectedTimestamp)
247  {
248  $instance = new CronCommand($cronCommand, $startTimestamp);
249  $instance->calculateNextValue();
250  $this->assertSame($expectedTimestamp, $instance->getTimestamp());
251  }
252 
260  public function ‪calculateNextValueDeterminesCorrectNextCalculatedTimestamp($cronCommand, $startTimestamp, $expectedTimestamp)
261  {
262  $instance = new CronCommand($cronCommand, $startTimestamp);
263  $instance->calculateNextValue();
264  $this->assertSame(strtotime($expectedTimestamp), $instance->getTimestamp());
265  }
266 
275  public function ‪calculateNextValueDeterminesCorrectNextTimestampOnConsecutiveCall($cronCommand, $startTimestamp, $firstTimestamp, $secondTimestamp)
276  {
277  $instance = new CronCommand($cronCommand, $firstTimestamp);
278  $instance->calculateNextValue();
279  $this->assertSame($secondTimestamp, $instance->getTimestamp());
280  }
281 
290  public function ‪calculateNextValueDeterminesCorrectNextCalculatedTimestampOnConsecutiveCall($cronCommand, $startTimestamp, $firstTimestamp, $secondTimestamp)
291  {
292  $instance = new CronCommand($cronCommand, strtotime($firstTimestamp));
293  $instance->calculateNextValue();
294  $this->assertSame(strtotime($secondTimestamp), $instance->getTimestamp());
295  }
296 
301  {
302  $backupTimezone = date_default_timezone_get();
303  date_default_timezone_set('Europe/Berlin');
304  $instance = new CronCommand('* 3 28 mar *', self::TIMESTAMP);
305  $instance->calculateNextValue();
306  date_default_timezone_set($backupTimezone);
307  $this->assertSame(1269741600, $instance->getTimestamp());
308  }
309 
314  {
315  $this->expectException(\RuntimeException::class);
316  $this->expectExceptionCode(1291501280);
317  $instance = new CronCommand('* * 31 apr *', self::TIMESTAMP);
318  $instance->calculateNextValue();
319  }
320 
324  public function ‪getTimestampReturnsInteger()
325  {
326  $instance = new CronCommand('* * * * *');
327  $this->assertInternalType(\PHPUnit\Framework\Constraint\IsType::TYPE_INT, $instance->getTimestamp());
328  }
329 
333  public function ‪getCronCommandSectionsReturnsArray()
334  {
335  $instance = new CronCommand('* * * * *');
336  $this->assertInternalType(\PHPUnit\Framework\Constraint\IsType::TYPE_ARRAY, $instance->getCronCommandSections());
337  }
338 }
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\TIMESTAMP
‪const TIMESTAMP
Definition: CronCommandTest.php:28
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\constructorSetsTimestampToGiveTimestampRoundedDownToSixtySeconds
‪constructorSetsTimestampToGiveTimestampRoundedDownToSixtySeconds()
Definition: CronCommandTest.php:94
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\constructorSetsNormalizedCronCommandSections
‪constructorSetsNormalizedCronCommandSections()
Definition: CronCommandTest.php:55
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\calculateNextValueDeterminesCorrectNextCalculatedTimestamp
‪calculateNextValueDeterminesCorrectNextCalculatedTimestamp($cronCommand, $startTimestamp, $expectedTimestamp)
Definition: CronCommandTest.php:259
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\getCronCommandSectionsReturnsArray
‪getCronCommandSectionsReturnsArray()
Definition: CronCommandTest.php:332
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\constructorSetsTimestampToGivenTimestampPlusSixtySeconds
‪constructorSetsTimestampToGivenTimestampPlusSixtySeconds()
Definition: CronCommandTest.php:85
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\calculateNextValueDeterminesCorrectNextTimestampOnChangeToSummertime
‪calculateNextValueDeterminesCorrectNextTimestampOnChangeToSummertime()
Definition: CronCommandTest.php:299
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\calculateNextValueDeterminesCorrectNextTimestamp
‪calculateNextValueDeterminesCorrectNextTimestamp($cronCommand, $startTimestamp, $expectedTimestamp)
Definition: CronCommandTest.php:245
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\constructorSetsTimestampToNowPlusOneMinuteRoundedDownToSixtySeconds
‪constructorSetsTimestampToNowPlusOneMinuteRoundedDownToSixtySeconds()
Definition: CronCommandTest.php:74
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\tearDown
‪tearDown()
Definition: CronCommandTest.php:46
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\calculateNextValueDeterminesCorrectNextTimestampOnConsecutiveCall
‪calculateNextValueDeterminesCorrectNextTimestampOnConsecutiveCall($cronCommand, $startTimestamp, $firstTimestamp, $secondTimestamp)
Definition: CronCommandTest.php:274
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\getTimestampReturnsInteger
‪getTimestampReturnsInteger()
Definition: CronCommandTest.php:323
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\calculateNextValueDeterminesCorrectNextCalculatedTimestampOnConsecutiveCall
‪calculateNextValueDeterminesCorrectNextCalculatedTimestampOnConsecutiveCall($cronCommand, $startTimestamp, $firstTimestamp, $secondTimestamp)
Definition: CronCommandTest.php:289
‪TYPO3\CMS\Scheduler\CronCommand\CronCommand
Definition: CronCommand.php:21
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\expectedTimestampDataProvider
‪static array expectedTimestampDataProvider()
Definition: CronCommandTest.php:103
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\constructorThrowsExceptionForInvalidCronCommand
‪constructorThrowsExceptionForInvalidCronCommand()
Definition: CronCommandTest.php:64
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest
Definition: CronCommandTest.php:24
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\expectedCalculatedTimestampDataProvider
‪static array expectedCalculatedTimestampDataProvider()
Definition: CronCommandTest.php:184
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\calculateNextValueThrowsExceptionWithImpossibleCronCommand
‪calculateNextValueThrowsExceptionWithImpossibleCronCommand()
Definition: CronCommandTest.php:312
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\setUp
‪setUp()
Definition: CronCommandTest.php:40
‪TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\CronCommandTest\$timezoneBackup
‪string $timezoneBackup
Definition: CronCommandTest.php:32