TYPO3  7.6
CronCommandTest.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Scheduler\Tests\Unit\CronCommand;
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 
22 class CronCommandTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
23 {
27  const TIMESTAMP = 1262304000;
28 
32  protected $timezoneBackup = '';
33 
40  protected function setUp()
41  {
42  $this->timezoneBackup = date_default_timezone_get();
43  date_default_timezone_set('UTC');
44  }
45 
46  protected function tearDown()
47  {
48  date_default_timezone_set($this->timezoneBackup);
49  parent::tearDown();
50  }
51 
56  {
57  $instance = new CronCommand('2-3 * * * *');
58  $this->assertSame(array('2,3', '*', '*', '*', '*'), $instance->getCronCommandSections());
59  }
60 
66  {
67  new CronCommand('61 * * * *');
68  }
69 
74  {
75  $instance = new CronCommand('* * * * *');
76  $currentTime = time();
77  $expectedTime = $currentTime - ($currentTime % 60) + 60;
78  $this->assertSame($expectedTime, $instance->getTimestamp());
79  }
80 
85  {
86  $instance = new CronCommand('* * * * *', self::TIMESTAMP);
87  $this->assertSame(self::TIMESTAMP + 60, $instance->getTimestamp());
88  }
89 
94  {
95  $instance = new CronCommand('* * * * *', self::TIMESTAMP + 1);
96  $this->assertSame(self::TIMESTAMP + 60, $instance->getTimestamp());
97  }
98 
102  public static function expectedTimestampDataProvider()
103  {
104  return array(
105  'every minute' => array(
106  '* * * * *',
107  self::TIMESTAMP,
108  self::TIMESTAMP + 60,
109  self::TIMESTAMP + 120
110  ),
111  'once an hour at 1' => array(
112  '1 * * * *',
113  self::TIMESTAMP,
114  self::TIMESTAMP + 60,
115  self::TIMESTAMP + 60 + 60 * 60
116  ),
117  'once an hour at 0' => array(
118  '0 * * * *',
119  self::TIMESTAMP,
120  self::TIMESTAMP + 60 * 60,
121  self::TIMESTAMP + 60 * 60 + 60 * 60
122  ),
123  'once a day at 1:00' => array(
124  '0 1 * * *',
125  self::TIMESTAMP,
126  self::TIMESTAMP + 60 * 60,
127  self::TIMESTAMP + 60 * 60 + 60 * 60 * 24
128  ),
129  'once a day at 0:00' => array(
130  '0 0 * * *',
131  self::TIMESTAMP,
132  self::TIMESTAMP + 60 * 60 * 24,
133  self::TIMESTAMP + 60 * 60 * 24 * 2
134  ),
135  'once a month' => array(
136  '0 0 4 * *',
137  self::TIMESTAMP,
138  self::TIMESTAMP + 60 * 60 * 24 * 3,
139  self::TIMESTAMP + 60 * 60 * 24 * 3 + 60 * 60 * 24 * 31
140  ),
141  'once every Saturday' => array(
142  '0 0 * * sat',
143  self::TIMESTAMP,
144  self::TIMESTAMP + 60 * 60 * 24,
145  self::TIMESTAMP + 60 * 60 * 24 + 60 * 60 * 24 * 7
146  ),
147  'once every day in February' => array(
148  '0 0 * feb *',
149  self::TIMESTAMP,
150  self::TIMESTAMP + 60 * 60 * 24 * 31,
151  self::TIMESTAMP + 60 * 60 * 24 * 31 + 60 * 60 * 24
152  ),
153  'day of week and day of month restricted, next match in day of month field' => array(
154  '0 0 2 * sun',
155  self::TIMESTAMP,
156  self::TIMESTAMP + 60 * 60 * 24,
157  self::TIMESTAMP + 60 * 60 * 24 + 60 * 60 * 24
158  ),
159  'day of week and day of month restricted, next match in day of week field' => array(
160  '0 0 3 * sat',
161  self::TIMESTAMP,
162  self::TIMESTAMP + 60 * 60 * 24,
163  self::TIMESTAMP + 60 * 60 * 24 + 60 * 60 * 24
164  ),
165  'list of minutes' => array(
166  '2,4 * * * *',
167  self::TIMESTAMP,
168  self::TIMESTAMP + 120,
169  self::TIMESTAMP + 240
170  ),
171  'list of hours' => array(
172  '0 2,4 * * *',
173  self::TIMESTAMP,
174  self::TIMESTAMP + 60 * 60 * 2,
175  self::TIMESTAMP + 60 * 60 * 4
176  ),
177  );
178  }
179 
184  {
185  return array(
186  'every first day of month' => array(
187  '0 0 1 * *',
188  self::TIMESTAMP,
189  '01-02-2010',
190  '01-03-2010',
191  ),
192  'once every February' => array(
193  '0 0 1 feb *',
194  self::TIMESTAMP,
195  '01-02-2010',
196  '01-02-2011',
197  ),
198  'once every Friday February' => array(
199  '0 0 * feb fri',
200  self::TIMESTAMP,
201  '05-02-2010',
202  '12-02-2010',
203  ),
204  'first day in February and every Friday' => array(
205  '0 0 1 feb fri',
206  self::TIMESTAMP,
207  '01-02-2010',
208  '05-02-2010',
209  ),
210  '29th February leap year' => array(
211  '0 0 29 feb *',
212  self::TIMESTAMP,
213  '29-02-2012',
214  '29-02-2016',
215  ),
216  'list of days in month' => array(
217  '0 0 2,4 * *',
218  self::TIMESTAMP,
219  '02-01-2010',
220  '04-01-2010',
221  ),
222  'list of month' => array(
223  '0 0 1 2,3 *',
224  self::TIMESTAMP,
225  '01-02-2010',
226  '01-03-2010',
227  ),
228  'list of days of weeks' => array(
229  '0 0 * * 2,4',
230  self::TIMESTAMP,
231  '05-01-2010',
232  '07-01-2010',
233  )
234  );
235  }
236 
244  public function calculateNextValueDeterminesCorrectNextTimestamp($cronCommand, $startTimestamp, $expectedTimestamp)
245  {
246  $instance = new CronCommand($cronCommand, $startTimestamp);
247  $instance->calculateNextValue();
248  $this->assertSame($expectedTimestamp, $instance->getTimestamp());
249  }
250 
258  public function calculateNextValueDeterminesCorrectNextCalculatedTimestamp($cronCommand, $startTimestamp, $expectedTimestamp)
259  {
260  $instance = new CronCommand($cronCommand, $startTimestamp);
261  $instance->calculateNextValue();
262  $this->assertSame(strtotime($expectedTimestamp), $instance->getTimestamp());
263  }
264 
273  public function calculateNextValueDeterminesCorrectNextTimestampOnConsecutiveCall($cronCommand, $startTimestamp, $firstTimestamp, $secondTimestamp)
274  {
275  $instance = new CronCommand($cronCommand, $firstTimestamp);
276  $instance->calculateNextValue();
277  $this->assertSame($secondTimestamp, $instance->getTimestamp());
278  }
279 
288  public function calculateNextValueDeterminesCorrectNextCalculatedTimestampOnConsecutiveCall($cronCommand, $startTimestamp, $firstTimestamp, $secondTimestamp)
289  {
290  $instance = new CronCommand($cronCommand, strtotime($firstTimestamp));
291  $instance->calculateNextValue();
292  $this->assertSame(strtotime($secondTimestamp), $instance->getTimestamp());
293  }
294 
299  {
300  $backupTimezone = date_default_timezone_get();
301  date_default_timezone_set('Europe/Berlin');
302  $instance = new CronCommand('* 3 28 mar *', self::TIMESTAMP);
303  $instance->calculateNextValue();
304  date_default_timezone_set($backupTimezone);
305  $this->assertSame(1269741600, $instance->getTimestamp());
306  }
307 
313  {
314  $instance = new CronCommand('* * 31 apr *', self::TIMESTAMP);
315  $instance->calculateNextValue();
316  }
317 
321  public function getTimestampReturnsInteger()
322  {
323  $instance = new CronCommand('* * * * *');
324  $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT, $instance->getTimestamp());
325  }
326 
331  {
332  $instance = new CronCommand('* * * * *');
333  $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY, $instance->getCronCommandSections());
334  }
335 }