TYPO3 CMS  TYPO3_6-2
CronCommandTest.php
Go to the documentation of this file.
1 <?php
3 
22 
26  const TIMESTAMP = 1262304000;
27 
31  protected $timezoneBackup = '';
32 
39  public function setUp() {
40  $this->timezoneBackup = date_default_timezone_get();
41  date_default_timezone_set('UTC');
42  }
43 
44  public function tearDown() {
45  date_default_timezone_set($this->timezoneBackup);
46  parent::tearDown();
47  }
48 
53  $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('2-3 * * * *');
54  $this->assertSame(array('2,3', '*', '*', '*', '*'), $instance->getCronCommandSections());
55  }
56 
62  new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('61 * * * *');
63  }
64 
69  $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('* * * * *');
70  $currentTime = time();
71  $expectedTime = $currentTime - ($currentTime % 60) + 60;
72  $this->assertSame($expectedTime, $instance->getTimestamp());
73  }
74 
79  $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('* * * * *', self::TIMESTAMP);
80  $this->assertSame(self::TIMESTAMP + 60, $instance->getTimestamp());
81  }
82 
87  $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('* * * * *', self::TIMESTAMP + 1);
88  $this->assertSame(self::TIMESTAMP + 60, $instance->getTimestamp());
89  }
90 
94  static public function expectedTimestampDataProvider() {
95  return array(
96  'every minute' => array(
97  '* * * * *',
98  self::TIMESTAMP,
99  self::TIMESTAMP + 60,
100  self::TIMESTAMP + 120
101  ),
102  'once an hour at 1' => array(
103  '1 * * * *',
104  self::TIMESTAMP,
105  self::TIMESTAMP + 60,
106  self::TIMESTAMP + 60 + 60 * 60
107  ),
108  'once an hour at 0' => array(
109  '0 * * * *',
110  self::TIMESTAMP,
111  self::TIMESTAMP + 60 * 60,
112  self::TIMESTAMP + 60 * 60 + 60 * 60
113  ),
114  'once a day at 1:00' => array(
115  '0 1 * * *',
116  self::TIMESTAMP,
117  self::TIMESTAMP + 60 * 60,
118  self::TIMESTAMP + 60 * 60 + 60 * 60 * 24
119  ),
120  'once a day at 0:00' => array(
121  '0 0 * * *',
122  self::TIMESTAMP,
123  self::TIMESTAMP + 60 * 60 * 24,
124  self::TIMESTAMP + 60 * 60 * 24 * 2
125  ),
126  'once a month' => array(
127  '0 0 4 * *',
128  self::TIMESTAMP,
129  self::TIMESTAMP + 60 * 60 * 24 * 3,
130  self::TIMESTAMP + 60 * 60 * 24 * 3 + 60 * 60 * 24 * 31
131  ),
132  'once every Saturday' => array(
133  '0 0 * * sat',
134  self::TIMESTAMP,
135  self::TIMESTAMP + 60 * 60 * 24,
136  self::TIMESTAMP + 60 * 60 * 24 + 60 * 60 * 24 * 7
137  ),
138  'once every day in February' => array(
139  '0 0 * feb *',
140  self::TIMESTAMP,
141  self::TIMESTAMP + 60 * 60 * 24 * 31,
142  self::TIMESTAMP + 60 * 60 * 24 * 31 + 60 * 60 * 24
143  ),
144  'day of week and day of month restricted, next match in day of month field' => array(
145  '0 0 2 * sun',
146  self::TIMESTAMP,
147  self::TIMESTAMP + 60 * 60 * 24,
148  self::TIMESTAMP + 60 * 60 * 24 + 60 * 60 * 24
149  ),
150  'day of week and day of month restricted, next match in day of week field' => array(
151  '0 0 3 * sat',
152  self::TIMESTAMP,
153  self::TIMESTAMP + 60 * 60 * 24,
154  self::TIMESTAMP + 60 * 60 * 24 + 60 * 60 * 24
155  ),
156  'list of minutes' => array(
157  '2,4 * * * *',
158  self::TIMESTAMP,
159  self::TIMESTAMP + 120,
160  self::TIMESTAMP + 240
161  ),
162  'list of hours' => array(
163  '0 2,4 * * *',
164  self::TIMESTAMP,
165  self::TIMESTAMP + 60 * 60 * 2,
166  self::TIMESTAMP + 60 * 60 * 4
167  ),
168  );
169  }
170 
174  static public function expectedCalculatedTimestampDataProvider() {
175  return array(
176  'every first day of month' => array(
177  '0 0 1 * *',
178  self::TIMESTAMP,
179  '01-02-2010',
180  '01-03-2010',
181  ),
182  'once every February' => array(
183  '0 0 1 feb *',
184  self::TIMESTAMP,
185  '01-02-2010',
186  '01-02-2011',
187  ),
188  'once every Friday February' => array(
189  '0 0 * feb fri',
190  self::TIMESTAMP,
191  '05-02-2010',
192  '12-02-2010',
193  ),
194  'first day in February and every Friday' => array(
195  '0 0 1 feb fri',
196  self::TIMESTAMP,
197  '01-02-2010',
198  '05-02-2010',
199  ),
200  '29th February leap year' => array(
201  '0 0 29 feb *',
202  self::TIMESTAMP,
203  '29-02-2012',
204  '29-02-2016',
205  ),
206  'list of days in month' => array(
207  '0 0 2,4 * *',
208  self::TIMESTAMP,
209  '02-01-2010',
210  '04-01-2010',
211  ),
212  'list of month' => array(
213  '0 0 1 2,3 *',
214  self::TIMESTAMP,
215  '01-02-2010',
216  '01-03-2010',
217  ),
218  'list of days of weeks' => array(
219  '0 0 * * 2,4',
220  self::TIMESTAMP,
221  '05-01-2010',
222  '07-01-2010',
223  )
224  );
225  }
226 
234  public function calculateNextValueDeterminesCorrectNextTimestamp($cronCommand, $startTimestamp, $expectedTimestamp) {
235  $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand($cronCommand, $startTimestamp);
236  $instance->calculateNextValue();
237  $this->assertSame($expectedTimestamp, $instance->getTimestamp());
238  }
239 
247  public function calculateNextValueDeterminesCorrectNextCalculatedTimestamp($cronCommand, $startTimestamp, $expectedTimestamp) {
248  $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand($cronCommand, $startTimestamp);
249  $instance->calculateNextValue();
250  $this->assertSame(strtotime($expectedTimestamp), $instance->getTimestamp());
251  }
252 
261  public function calculateNextValueDeterminesCorrectNextTimestampOnConsecutiveCall($cronCommand, $startTimestamp, $firstTimestamp, $secondTimestamp) {
262  $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand($cronCommand, $firstTimestamp);
263  $instance->calculateNextValue();
264  $this->assertSame($secondTimestamp, $instance->getTimestamp());
265  }
266 
275  public function calculateNextValueDeterminesCorrectNextCalculatedTimestampOnConsecutiveCall($cronCommand, $startTimestamp, $firstTimestamp, $secondTimestamp) {
276  $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand($cronCommand, strtotime($firstTimestamp));
277  $instance->calculateNextValue();
278  $this->assertSame(strtotime($secondTimestamp), $instance->getTimestamp());
279  }
280 
285  $backupTimezone = date_default_timezone_get();
286  date_default_timezone_set('Europe/Berlin');
287  $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('* 3 28 mar *', self::TIMESTAMP);
288  $instance->calculateNextValue();
289  date_default_timezone_set($backupTimezone);
290  $this->assertSame(1269741600, $instance->getTimestamp());
291  }
292 
298  $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('* * 31 apr *', self::TIMESTAMP);
299  $instance->calculateNextValue();
300  }
301 
305  public function getTimestampReturnsInteger() {
306  $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('* * * * *');
307  $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT, $instance->getTimestamp());
308  }
309 
314  $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('* * * * *');
315  $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY, $instance->getCronCommandSections());
316  }
317 
318 }
calculateNextValueDeterminesCorrectNextCalculatedTimestampOnConsecutiveCall($cronCommand, $startTimestamp, $firstTimestamp, $secondTimestamp)
calculateNextValueDeterminesCorrectNextTimestamp($cronCommand, $startTimestamp, $expectedTimestamp)
calculateNextValueDeterminesCorrectNextTimestampOnConsecutiveCall($cronCommand, $startTimestamp, $firstTimestamp, $secondTimestamp)
calculateNextValueDeterminesCorrectNextCalculatedTimestamp($cronCommand, $startTimestamp, $expectedTimestamp)