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