[BUGFIX] Fix scheduler unit-tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Tests / Unit / CrondCommand / CronCommandTest.php
1 <?php
2 namespace TYPO3\CMS\Scheduler\Tests\Unit\CrondCommand;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2011 Christian Kuhn <lolli@schwarzbu.ch>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * Testcase for class "tx_scheduler_CronCmd"
28 *
29 * @package TYPO3
30 * @subpackage tx_scheduler
31 * @author Christian Kuhn <lolli@schwarzbu.ch>
32 */
33 class CronCommandTest extends \tx_phpunit_testcase {
34
35 /**
36 * @const integer timestamp of 1.1.2010 0:00 (Friday)
37 */
38 const TIMESTAMP = 1262300400;
39 /**
40 * @test
41 */
42 public function constructorSetsNormalizedCronCommandSections() {
43 $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('2-3 * * * *');
44 $this->assertSame($instance->getCronCommandSections(), array('2,3', '*', '*', '*', '*'));
45 }
46
47 /**
48 * @test
49 * @expectedException InvalidArgumentException
50 */
51 public function constructorThrowsExceptionForInvalidCronCommand() {
52 new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('61 * * * *');
53 }
54
55 /**
56 * @test
57 */
58 public function constructorSetsTimestampToNowPlusOneMinuteRoundedDownToSixtySeconds() {
59 $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('* * * * *');
60 $this->assertSame($instance->getTimestamp(), $GLOBALS['ACCESS_TIME'] + 60);
61 }
62
63 /**
64 * @test
65 */
66 public function constructorSetsTimestampToGivenTimestampPlusSixtySeconds() {
67 $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('* * * * *', self::TIMESTAMP);
68 $this->assertSame($instance->getTimestamp(), self::TIMESTAMP + 60);
69 }
70
71 /**
72 * @test
73 */
74 public function constructorSetsTimestampToGiveTimestampRoundedDownToSixtySeconds() {
75 $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('* * * * *', self::TIMESTAMP + 1);
76 $this->assertSame($instance->getTimestamp(), self::TIMESTAMP + 60);
77 }
78
79 /**
80 * @return array
81 */
82 static public function expectedTimestampDataProvider() {
83 return array(
84 'every minute' => array(
85 '* * * * *',
86 self::TIMESTAMP,
87 self::TIMESTAMP + 60,
88 self::TIMESTAMP + 120
89 ),
90 'once an hour at 1' => array(
91 '1 * * * *',
92 self::TIMESTAMP,
93 self::TIMESTAMP + 60,
94 (self::TIMESTAMP + 60) + 60 * 60
95 ),
96 'once an hour at 0' => array(
97 '0 * * * *',
98 self::TIMESTAMP,
99 self::TIMESTAMP + 60 * 60,
100 (self::TIMESTAMP + 60 * 60) + 60 * 60
101 ),
102 'once a day at 1:00' => array(
103 '0 1 * * *',
104 self::TIMESTAMP,
105 self::TIMESTAMP + 60 * 60,
106 (self::TIMESTAMP + 60 * 60) + (60 * 60) * 24
107 ),
108 'once a day at 0:00' => array(
109 '0 0 * * *',
110 self::TIMESTAMP,
111 self::TIMESTAMP + (60 * 60) * 24,
112 self::TIMESTAMP + ((60 * 60) * 24) * 2
113 ),
114 'every first day of month' => array(
115 '0 0 1 * *',
116 self::TIMESTAMP,
117 strtotime('01-02-2010'),
118 strtotime('01-03-2010')
119 ),
120 'once a month' => array(
121 '0 0 4 * *',
122 self::TIMESTAMP,
123 self::TIMESTAMP + ((60 * 60) * 24) * 3,
124 (self::TIMESTAMP + ((60 * 60) * 24) * 3) + ((60 * 60) * 24) * 31
125 ),
126 'once every Saturday' => array(
127 '0 0 * * sat',
128 self::TIMESTAMP,
129 self::TIMESTAMP + (60 * 60) * 24,
130 (self::TIMESTAMP + (60 * 60) * 24) + ((60 * 60) * 24) * 7
131 ),
132 'once every day in February' => array(
133 '0 0 * feb *',
134 self::TIMESTAMP,
135 self::TIMESTAMP + ((60 * 60) * 24) * 31,
136 (self::TIMESTAMP + ((60 * 60) * 24) * 31) + (60 * 60) * 24
137 ),
138 'once every February' => array(
139 '0 0 1 feb *',
140 self::TIMESTAMP,
141 self::TIMESTAMP + ((60 * 60) * 24) * 31,
142 strtotime('01-02-2011')
143 ),
144 'once every Friday February' => array(
145 '0 0 * feb fri',
146 self::TIMESTAMP,
147 strtotime('05-02-2010'),
148 strtotime('12-02-2010')
149 ),
150 'first day in February and every Friday' => array(
151 '0 0 1 feb fri',
152 self::TIMESTAMP,
153 strtotime('01-02-2010'),
154 strtotime('05-02-2010')
155 ),
156 'day of week and day of month restricted, next match in day of month field' => array(
157 '0 0 2 * sun',
158 self::TIMESTAMP,
159 self::TIMESTAMP + (60 * 60) * 24,
160 (self::TIMESTAMP + (60 * 60) * 24) + (60 * 60) * 24
161 ),
162 'day of week and day of month restricted, next match in day of week field' => array(
163 '0 0 3 * sat',
164 self::TIMESTAMP,
165 self::TIMESTAMP + (60 * 60) * 24,
166 (self::TIMESTAMP + (60 * 60) * 24) + (60 * 60) * 24
167 ),
168 '29th February leap year' => array(
169 '0 0 29 feb *',
170 self::TIMESTAMP,
171 strtotime('29-02-2012'),
172 strtotime('29-02-2016')
173 ),
174 'list of minutes' => array(
175 '2,4 * * * *',
176 self::TIMESTAMP,
177 self::TIMESTAMP + 120,
178 self::TIMESTAMP + 240
179 ),
180 'list of hours' => array(
181 '0 2,4 * * *',
182 self::TIMESTAMP,
183 self::TIMESTAMP + (60 * 60) * 2,
184 self::TIMESTAMP + (60 * 60) * 4
185 ),
186 'list of days in month' => array(
187 '0 0 2,4 * *',
188 self::TIMESTAMP,
189 strtotime('02-01-2010'),
190 strtotime('04-01-2010')
191 ),
192 'list of month' => array(
193 '0 0 1 2,3 *',
194 self::TIMESTAMP,
195 strtotime('01-02-2010'),
196 strtotime('01-03-2010')
197 ),
198 'list of days of weeks' => array(
199 '0 0 * * 2,4',
200 self::TIMESTAMP,
201 strtotime('05-01-2010'),
202 strtotime('07-01-2010')
203 )
204 );
205 }
206
207 /**
208 * @test
209 * @dataProvider expectedTimestampDataProvider
210 * @param string $cronCommand Cron command
211 * @param integer $startTimestamp Timestamp for start of calculation
212 * @param integer $expectedTimestamp Expected result (next time of execution)
213 */
214 public function calculateNextValueDeterminesCorrectNextTimestamp($cronCommand, $startTimestamp, $expectedTimestamp) {
215 $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand($cronCommand, $startTimestamp);
216 $instance->calculateNextValue();
217 $this->assertSame($instance->getTimestamp(), $expectedTimestamp);
218 }
219
220 /**
221 * @test
222 * @dataProvider expectedTimestampDataProvider
223 * @param string $cronCommand Cron command
224 * @param integer $startTimestamp Timestamp for start of calculation
225 * @param integer $firstTimestamp Timestamp of the next execution
226 * @param integer $secondTimestamp Timestamp of the further execution
227 */
228 public function calculateNextValueDeterminesCorrectNextTimestampOnConsecutiveCall($cronCommand, $startTimestamp, $firstTimestamp, $secondTimestamp) {
229 $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand($cronCommand, $firstTimestamp);
230 $instance->calculateNextValue();
231 $this->assertSame($instance->getTimestamp(), $secondTimestamp);
232 }
233
234 /**
235 * @test
236 */
237 public function calculateNextValueDeterminesCorrectNextTimestampOnChangeToSummertime() {
238 $backupTimezone = date_default_timezone_get();
239 date_default_timezone_set('Europe/Berlin');
240 $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('* 3 28 mar *', self::TIMESTAMP);
241 $instance->calculateNextValue();
242 date_default_timezone_set($backupTimezone);
243 $this->assertSame($instance->getTimestamp(), 1269741600);
244 }
245
246 /**
247 * @test
248 * @expectedException RuntimeException
249 */
250 public function calculateNextValueThrowsExceptionWithImpossibleCronCommand() {
251 $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('* * 31 apr *', self::TIMESTAMP);
252 $instance->calculateNextValue();
253 }
254
255 /**
256 * @test
257 */
258 public function getTimestampReturnsInteger() {
259 $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('* * * * *');
260 $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT, $instance->getTimestamp());
261 }
262
263 /**
264 * @test
265 */
266 public function getCronCommandSectionsReturnsArray() {
267 $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('* * * * *');
268 $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY, $instance->getCronCommandSections());
269 }
270
271 }
272
273
274 ?>