[TASK] Re-work/simplify copyright header in PHP files - Part 9
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Tests / Unit / CronCommand / CronCommandTest.php
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 /**
17 * Test case
18 *
19 * @author Christian Kuhn <lolli@schwarzbu.ch>
20 */
21 class CronCommandTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
22
23 /**
24 * @const integer timestamp of 1.1.2010 0:00 (Friday), timezone UTC/GMT
25 */
26 const TIMESTAMP = 1262304000;
27
28 /**
29 * @var string Selected timezone backup
30 */
31 protected $timezoneBackup = '';
32
33 /**
34 * We're fiddling with hard timestamps in the tests, but time methods in
35 * the system under test do use timezone settings. Therefore we backup the
36 * current timezone setting, set it to UTC explicitly and reconstitute it
37 * again in tearDown()
38 */
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
49 /**
50 * @test
51 */
52 public function constructorSetsNormalizedCronCommandSections() {
53 $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('2-3 * * * *');
54 $this->assertSame(array('2,3', '*', '*', '*', '*'), $instance->getCronCommandSections());
55 }
56
57 /**
58 * @test
59 * @expectedException \InvalidArgumentException
60 */
61 public function constructorThrowsExceptionForInvalidCronCommand() {
62 new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('61 * * * *');
63 }
64
65 /**
66 * @test
67 */
68 public function constructorSetsTimestampToNowPlusOneMinuteRoundedDownToSixtySeconds() {
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
75 /**
76 * @test
77 */
78 public function constructorSetsTimestampToGivenTimestampPlusSixtySeconds() {
79 $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('* * * * *', self::TIMESTAMP);
80 $this->assertSame(self::TIMESTAMP + 60, $instance->getTimestamp());
81 }
82
83 /**
84 * @test
85 */
86 public function constructorSetsTimestampToGiveTimestampRoundedDownToSixtySeconds() {
87 $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('* * * * *', self::TIMESTAMP + 1);
88 $this->assertSame(self::TIMESTAMP + 60, $instance->getTimestamp());
89 }
90
91 /**
92 * @return array
93 */
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
171 /**
172 * @return array
173 */
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
227 /**
228 * @test
229 * @dataProvider expectedTimestampDataProvider
230 * @param string $cronCommand Cron command
231 * @param integer $startTimestamp Timestamp for start of calculation
232 * @param integer $expectedTimestamp Expected result (next time of execution)
233 */
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
240 /**
241 * @test
242 * @dataProvider expectedCalculatedTimestampDataProvider
243 * @param string $cronCommand Cron command
244 * @param integer $startTimestamp Timestamp for start of calculation
245 * @param string $expectedTimestamp Expected result (next time of execution), to be feeded to strtotime
246 */
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
253 /**
254 * @test
255 * @dataProvider expectedTimestampDataProvider
256 * @param string $cronCommand Cron command
257 * @param integer $startTimestamp [unused] Timestamp for start of calculation
258 * @param integer $firstTimestamp Timestamp of the next execution
259 * @param integer $secondTimestamp Timestamp of the further execution
260 */
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
267 /**
268 * @test
269 * @dataProvider expectedCalculatedTimestampDataProvider
270 * @param string $cronCommand Cron command
271 * @param integer $startTimestamp [unused] Timestamp for start of calculation
272 * @param string $firstTimestamp Timestamp of the next execution, to be fed to strtotime
273 * @param string $secondTimestamp Timestamp of the further execution, to be fed to strtotime
274 */
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
281 /**
282 * @test
283 */
284 public function calculateNextValueDeterminesCorrectNextTimestampOnChangeToSummertime() {
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
293 /**
294 * @test
295 * @expectedException \RuntimeException
296 */
297 public function calculateNextValueThrowsExceptionWithImpossibleCronCommand() {
298 $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('* * 31 apr *', self::TIMESTAMP);
299 $instance->calculateNextValue();
300 }
301
302 /**
303 * @test
304 */
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
310 /**
311 * @test
312 */
313 public function getCronCommandSectionsReturnsArray() {
314 $instance = new \TYPO3\CMS\Scheduler\CronCommand\CronCommand('* * * * *');
315 $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY, $instance->getCronCommandSections());
316 }
317
318 }