[BUGFIX] Fix scheduler unit-tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Tests / Unit / CrondCommand / NormalizeCommandTest.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 * Test case for class "tx_scheduler_CronCmd_Normalize"
28 *
29 * @author Christian Kuhn <lolli@schwarzbu.ch>
30 * @package TYPO3
31 * @subpackage tx_scheduler
32 */
33 class NormalizeCommandTest extends \tx_phpunit_testcase {
34
35 /**
36 * Create a subclass of tx_scheduler_CronCmd_Normalize with
37 * protected methods made public
38 *
39 * @return string Name of the accessible proxy class
40 */
41 protected function getAccessibleProxy() {
42 $className = 'NormalizeCommand' . uniqid();
43 $fullClassName = 'TYPO3\\CMS\\Scheduler\\CronCommand\\' . $className;
44 if (!class_exists($className, FALSE)) {
45 eval(
46 'namespace TYPO3\CMS\Scheduler\CronCommand;' .
47 'class ' . $className . ' extends NormalizeCommand {' .
48 ' public static function convertKeywordsToCronCommand($cronCommand) {' .
49 ' return parent::convertKeywordsToCronCommand($cronCommand);' .
50 ' }' .
51 ' public static function normalizeFields($cronCommand) {' .
52 ' return parent::normalizeFields($cronCommand);' .
53 ' }' .
54 ' public static function normalizeMonthAndWeekdayField($expression, $isMonthField = TRUE) {' .
55 ' return parent::normalizeMonthAndWeekdayField($expression, $isMonthField);' .
56 ' }' .
57 ' public static function normalizeIntegerField($expression, $lowerBound = 0, $upperBound = 59) {' .
58 ' return parent::normalizeIntegerField($expression, $lowerBound, $upperBound);' .
59 ' }' .
60 ' public static function splitFields($cronCommand) {' .
61 ' return parent::splitFields($cronCommand);' .
62 ' }' .
63 ' public static function convertRangeToListOfValues($range) {' .
64 ' return parent::convertRangeToListOfValues($range);' .
65 ' }' .
66 ' public static function reduceListOfValuesByStepValue($stepExpression) {' .
67 ' return parent::reduceListOfValuesByStepValue($stepExpression);' .
68 ' }' .
69 ' public static function normalizeMonthAndWeekday($expression, $isMonth = TRUE) {' .
70 ' return parent::normalizeMonthAndWeekday($expression, $isMonth);' .
71 ' }' .
72 ' public static function normalizeMonth($month) {' .
73 ' return parent::normalizeMonth($month);' .
74 ' }' .
75 ' public static function normalizeWeekday($weekday) {' .
76 ' return parent::normalizeWeekday($weekday);' .
77 ' }' .
78 '}'
79 );
80 }
81 return $fullClassName;
82 }
83
84 /**
85 * @return array
86 */
87 static public function normalizeValidDataProvider() {
88 return array(
89 '@weekly' => array('@weekly', '0 0 * * 7'),
90 ' @weekly ' => array(' @weekly ', '0 0 * * 7'),
91 '* * * * *' => array('* * * * *', '* * * * *'),
92 '30 4 1,15 * 5' => array('30 4 1,15 * 5', '30 4 1,15 * 5'),
93 '5 0 * * *' => array('5 0 * * *', '5 0 * * *'),
94 '15 14 1 * *' => array('15 14 1 * *', '15 14 1 * *'),
95 '0 22 * * 1-5' => array('0 22 * * 1-5', '0 22 * * 1,2,3,4,5'),
96 '23 0-23/2 * * *' => array('23 0-23/2 * * *', '23 0,2,4,6,8,10,12,14,16,18,20,22 * * *'),
97 '5 4 * * sun' => array('5 4 * * sun', '5 4 * * 7'),
98 '0-3/2,7 0,4 20-22, feb,mar-jun/2,7 1-3,sun' => array('0-3/2,7 0,4 20-22 feb,mar-jun/2,7 1-3,sun', '0,2,7 0,4 20,21,22 2,3,5,7 1,2,3,7'),
99 '0-20/10 * * * *' => array('0-20/10 * * * *', '0,10,20 * * * *'),
100 '* * 2 * *' => array('* * 2 * *', '* * 2 * *'),
101 '* * 2,7 * *' => array('* * 2,7 * *', '* * 2,7 * *'),
102 '* * 2-4,10 * *' => array('* * 2-4,10 * *', '* * 2,3,4,10 * *'),
103 '* * */14 * *' => array('* * */14 * *', '* * 1,15,29 * *'),
104 '* * 2,4-6/2,*/14 * *' => array('* * 2,4-6/2,*/14 * *', '* * 1,2,4,6,15,29 * *'),
105 '* * * * 1' => array('* * * * 1', '* * * * 1'),
106 '0 0 * * 0' => array('0 0 * * 0', '0 0 * * 7'),
107 '0 0 * * 7' => array('0 0 * * 7', '0 0 * * 7'),
108 '* * 1,2 * 1' => array('* * 1,2 * 1', '* * 1,2 * 1')
109 );
110 }
111
112 /**
113 * @test
114 * @dataProvider normalizeValidDataProvider
115 * @param string $expression Cron command to test
116 * @param string $expected Expected result (normalized cron command syntax)
117 */
118 public function normalizeConvertsCronCommand($expression, $expected) {
119 $result = \TYPO3\CMS\Scheduler\CronCommand\NormalizeCommand::normalize($expression);
120 $this->assertEquals($expected, $result);
121 }
122
123 /**
124 * @return array
125 */
126 static public function validSpecialKeywordsDataProvider() {
127 return array(
128 '@yearly' => array('@yearly', '0 0 1 1 *'),
129 '@annually' => array('@annually', '0 0 1 1 *'),
130 '@monthly' => array('@monthly', '0 0 1 * *'),
131 '@weekly' => array('@weekly', '0 0 * * 0'),
132 '@daily' => array('@daily', '0 0 * * *'),
133 '@midnight' => array('@midnight', '0 0 * * *'),
134 '@hourly' => array('@hourly', '0 * * * *')
135 );
136 }
137
138 /**
139 * @test
140 * @dataProvider validSpecialKeywordsDataProvider
141 * @param string $keyword Cron command keyword
142 * @param string $expectedCronCommand Expected result (normalized cron command syntax)
143 */
144 public function convertKeywordsToCronCommandConvertsValidKeywords($keyword, $expectedCronCommand) {
145 $accessibleProxyClassName = $this->getAccessibleProxy();
146 $result = $accessibleProxyClassName::convertKeywordsToCronCommand($keyword);
147 $this->assertEquals($expectedCronCommand, $result);
148 }
149
150 /**
151 * @test
152 */
153 public function convertKeywordsToCronCommandReturnsUnchangedCommandIfKeywordWasNotFound() {
154 $invalidKeyword = 'foo';
155 $accessibleProxyClassName = $this->getAccessibleProxy();
156 $result = $accessibleProxyClassName::convertKeywordsToCronCommand($invalidKeyword);
157 $this->assertEquals($invalidKeyword, $result);
158 }
159
160 /**
161 * @return array
162 */
163 public function normalizeFieldsValidDataProvider() {
164 return array(
165 '1-2 * * * *' => array('1-2 * * * *', '1,2 * * * *'),
166 '* 1-2 * * *' => array('* 1-2 * * *', '* 1,2 * * *'),
167 '* * 1-2 * *' => array('* * 1-2 * *', '* * 1,2 * *'),
168 '* * * 1-2 *' => array('* * * 1-2 *', '* * * 1,2 *'),
169 '* * * * 1-2' => array('* * * * 1-2', '* * * * 1,2')
170 );
171 }
172
173 /**
174 * @test
175 * @dataProvider normalizeFieldsValidDataProvider
176 * @param string $expression Cron command to normalize
177 * @param string $expected Expected result (normalized cron command syntax)
178 */
179 public function normalizeFieldsConvertsField($expression, $expected) {
180 $accessibleProxyClassName = $this->getAccessibleProxy();
181 $result = $accessibleProxyClassName::normalizeFields($expression);
182 $this->assertEquals($expected, $result);
183 }
184
185 /**
186 * @return array
187 */
188 static public function normalizeMonthAndWeekdayFieldValidDataProvider() {
189 return array(
190 '*' => array('*', TRUE, '*'),
191 'string 1' => array('1', TRUE, '1'),
192 'jan' => array('jan', TRUE, '1'),
193 'feb/2' => array('feb/2', TRUE, '2'),
194 'jan-feb/2' => array('jan-feb/2', TRUE, '1'),
195 '1-2' => array('1-2', TRUE, '1,2'),
196 '1-3/2,feb,may,6' => array('1-3/2,feb,may,6', TRUE, '1,2,3,5,6'),
197 '*/4' => array('*/4', TRUE, '1,5,9'),
198 '*' => array('*', FALSE, '*'),
199 'string 1' => array('1', FALSE, '1'),
200 'fri' => array('fri', FALSE, '5'),
201 'sun' => array('sun', FALSE, '7'),
202 'string 0 for sunday' => array('0', FALSE, '7'),
203 '0,1' => array('0,1', FALSE, '1,7'),
204 '*/3' => array('*/3', FALSE, '1,4,7'),
205 'tue/2' => array('tue/2', FALSE, '2'),
206 '1-2' => array('1-2', FALSE, '1,2'),
207 'tue-fri/2' => array('tue-fri/2', FALSE, '2,4'),
208 '1-3/2,tue,fri,6' => array('1-3/2,tue,fri,6', FALSE, '1,2,3,5,6')
209 );
210 }
211
212 /**
213 * @test
214 * @dataProvider normalizeMonthAndWeekdayFieldValidDataProvider
215 * @param string $expression Cron command partial expression for month and weekday fields
216 * @param boolean $isMonthField Flag to designate month field or not
217 * @param string $expected Expected result (normalized months or weekdays)
218 */
219 public function normalizeMonthAndWeekdayFieldReturnsNormalizedListForValidExpression($expression, $isMonthField, $expected) {
220 $accessibleProxyClassName = $this->getAccessibleProxy();
221 $result = $accessibleProxyClassName::normalizeMonthAndWeekdayField($expression, $isMonthField);
222 $this->assertSame($expected, $result);
223 }
224
225 /**
226 * @return array
227 */
228 static public function normalizeMonthAndWeekdayFieldInvalidDataProvider() {
229 return array(
230 'mon' => array('mon', TRUE),
231 '1-2/mon' => array('1-2/mon', TRUE),
232 '0,1' => array('0,1', TRUE),
233 'feb' => array('feb', FALSE),
234 '1-2/feb' => array('1-2/feb', FALSE),
235 '0-fri/2,7' => array('0-fri/2,7', FALSE, '2,4,7')
236 );
237 }
238
239 /**
240 * @test
241 * @dataProvider normalizeMonthAndWeekdayFieldInvalidDataProvider
242 * @expectedException InvalidArgumentException
243 * @param string $expression Cron command partial expression for month and weekday fields (invalid)
244 * @param boolean $isMonthField Flag to designate month field or not
245 */
246 public function normalizeMonthAndWeekdayFieldThrowsExceptionForInvalidExpression($expression, $isMonthField) {
247 $accessibleProxyClassName = $this->getAccessibleProxy();
248 $result = $accessibleProxyClassName::normalizeMonthAndWeekdayField($expression, $isMonthField);
249 }
250
251 /**
252 * @return array
253 */
254 static public function normalizeIntegerFieldValidDataProvider() {
255 return array(
256 '*' => array('*', '*'),
257 'string 2' => array('2', '2'),
258 'integer 3' => array(3, '3'),
259 'list of values' => array('1,2,3', '1,2,3'),
260 'unsorted list of values' => array('3,1,5', '1,3,5'),
261 'duplicate values' => array('0-2/2,2', '0,2'),
262 'additional field between steps' => array('1-3/2,2', '1,2,3'),
263 '2-4' => array('2-4', '2,3,4'),
264 'simple step 4/4' => array('4/4', '4'),
265 'step 2-7/5' => array('2-7/5', '2,7'),
266 'steps 4-12/4' => array('4-12/4', '4,8,12'),
267 '0-59/20' => array('0-59/20', '0,20,40'),
268 '*/20' => array('*/20', '0,20,40')
269 );
270 }
271
272 /**
273 * @test
274 * @dataProvider normalizeIntegerFieldValidDataProvider
275 * @param string $expression Cron command partial integer expression
276 * @param string $expected Expected result (normalized integer or integer list)
277 */
278 public function normalizeIntegerFieldReturnsNormalizedListForValidExpression($expression, $expected) {
279 $accessibleProxyClassName = $this->getAccessibleProxy();
280 $result = $accessibleProxyClassName::normalizeIntegerField($expression);
281 $this->assertSame($expected, $result);
282 }
283
284 /**
285 * @return array
286 */
287 static public function normalizeIntegerFieldInvalidDataProvider() {
288 return array(
289 'string foo' => array('foo', 0, 59),
290 'empty string' => array('', 0, 59),
291 '4-3' => array('4-3', 0, 59),
292 '/2' => array('/2', 0, 59),
293 '/' => array('/', 0, 59),
294 'string foo' => array('foo', 0, 59),
295 'left bound too low' => array('2-4', 3, 4),
296 'right bound too high' => array('2-4', 2, 3),
297 'left and right bound' => array('2-5', 2, 4),
298 'element in list is lower than allowed' => array('2,1,4', 2, 4),
299 'element in list is higher than allowed' => array('2,5,4', 1, 4)
300 );
301 }
302
303 /**
304 * @test
305 * @dataProvider normalizeIntegerFieldInvalidDataProvider
306 * @expectedException InvalidArgumentException
307 * @param string $expression Cron command partial integer expression (invalid)
308 * @param integer $lowerBound Lower limit
309 * @param integer $upperBound Upper limit
310 */
311 public function normalizeIntegerFieldThrowsExceptionForInvalidExpressions($expression, $lowerBound, $upperBound) {
312 $accessibleProxyClassName = $this->getAccessibleProxy();
313 $accessibleProxyClassName::normalizeIntegerField($expression, $lowerBound, $upperBound);
314 }
315
316 /**
317 * @test
318 */
319 public function splitFieldsReturnsIntegerArrayWithFieldsSplitByWhitespace() {
320 $accessibleProxyClassName = $this->getAccessibleProxy();
321 $result = $accessibleProxyClassName::splitFields('12,13 * 1-12/2,14 jan fri');
322 $expectedResult = array(
323 0 => '12,13',
324 1 => '*',
325 2 => '1-12/2,14',
326 3 => 'jan',
327 4 => 'fri'
328 );
329 $this->assertSame($expectedResult, $result);
330 }
331
332 /**
333 * @return array
334 */
335 static public function invalidCronCommandFieldsDataProvider() {
336 return array(
337 'empty string' => array(''),
338 'foo' => array('foo'),
339 'integer 4' => array(4),
340 'four fields' => array('* * * *'),
341 'six fields' => array('* * * * * *')
342 );
343 }
344
345 /**
346 * @test
347 * @expectedException InvalidArgumentException
348 * @dataProvider invalidCronCommandFieldsDataProvider
349 * @param string $cronCommand Invalid cron command
350 */
351 public function splitFieldsThrowsExceptionIfCronCommandDoesNotContainFiveFields($cronCommand) {
352 $accessibleProxyClassName = $this->getAccessibleProxy();
353 $accessibleProxyClassName::splitFields($cronCommand);
354 }
355
356 /**
357 * @return array
358 */
359 static public function validRangeDataProvider() {
360 return array(
361 'single value' => array('3', '3'),
362 'integer 3' => array(3, '3'),
363 '0-0' => array('0-0', '0'),
364 '4-4' => array('4-4', '4'),
365 '0-3' => array('0-3', '0,1,2,3'),
366 '4-5' => array('4-5', '4,5')
367 );
368 }
369
370 /**
371 * @test
372 * @dataProvider validRangeDataProvider
373 * @param string $range Cron command range expression
374 * @param string $expected Expected result (normalized range)
375 */
376 public function convertRangeToListOfValuesReturnsCorrectListForValidRanges($range, $expected) {
377 $accessibleProxyClassName = $this->getAccessibleProxy();
378 $result = $accessibleProxyClassName::convertRangeToListOfValues($range);
379 $this->assertSame($expected, $result);
380 }
381
382 /**
383 * @return array
384 */
385 static public function invalidRangeDataProvider() {
386 return array(
387 'empty string' => array(''),
388 'string' => array('foo'),
389 'single dash' => array('-'),
390 'left part is string' => array('foo-5'),
391 'right part is string' => array('5-foo'),
392 'range of strings' => array('foo-bar'),
393 'string five minus' => array('5-'),
394 'string minus five' => array('-5'),
395 'more than one dash' => array('2-3-4'),
396 'left part bigger than right part' => array('6-3')
397 );
398 }
399
400 /**
401 * @test
402 * @dataProvider invalidRangeDataProvider
403 * @expectedException InvalidArgumentException
404 * @param string $range Cron command range expression (invalid)
405 */
406 public function convertRangeToListOfValuesThrowsExceptionForInvalidRanges($range) {
407 $accessibleProxyClassName = $this->getAccessibleProxy();
408 $accessibleProxyClassName::convertRangeToListOfValues($range);
409 }
410
411 /**
412 * @return array
413 */
414 static public function validStepsDataProvider() {
415 return array(
416 '2/2' => array('2/2', '2'),
417 '2,3,4/2' => array('2,3,4/2', '2,4'),
418 '1,2,3,4,5,6,7/3' => array('1,2,3,4,5,6,7/3', '1,4,7'),
419 '0,1,2,3,4,5,6/3' => array('0,1,2,3,4,5,6/3', '0,3,6')
420 );
421 }
422
423 /**
424 * @test
425 * @dataProvider validStepsDataProvider
426 * @param string $stepExpression Cron command step expression
427 * @param string $expected Expected result (normalized range)
428 */
429 public function reduceListOfValuesByStepValueReturnsCorrectListOfValues($stepExpression, $expected) {
430 $accessibleProxyClassName = $this->getAccessibleProxy();
431 $result = $accessibleProxyClassName::reduceListOfValuesByStepValue($stepExpression);
432 $this->assertSame($expected, $result);
433 }
434
435 /**
436 * @return array
437 */
438 static public function invalidStepsDataProvider() {
439 return array(
440 'empty string' => array(''),
441 'slash only' => array('/'),
442 'left part empty' => array('/2'),
443 'right part empty' => array('2/'),
444 'multiples slashes' => array('1/2/3'),
445 '2-2' => array('2-2'),
446 '2.3/2' => array('2.3/2'),
447 '2,3,4/2.3' => array('2,3,4/2.3'),
448 '2,3,4/2,3' => array('2,3,4/2,3')
449 );
450 }
451
452 /**
453 * @test
454 * @dataProvider invalidStepsDataProvider
455 * @expectedException InvalidArgumentException
456 * @param string $stepExpression Cron command step expression (invalid)
457 */
458 public function reduceListOfValuesByStepValueThrowsExceptionForInvalidStepExpressions($stepExpression) {
459 $accessibleProxyClassName = $this->getAccessibleProxy();
460 $accessibleProxyClassName::reduceListOfValuesByStepValue($stepExpression);
461 }
462
463 /**
464 * @test
465 */
466 public function normalizeMonthAndWeekdayNormalizesAMonth() {
467 $accessibleProxyClassName = $this->getAccessibleProxy();
468 $result = $accessibleProxyClassName::normalizeMonthAndWeekday('feb', TRUE);
469 $this->assertSame('2', $result);
470 }
471
472 /**
473 * @test
474 */
475 public function normalizeMonthAndWeekdayNormalizesAWeekday() {
476 $accessibleProxyClassName = $this->getAccessibleProxy();
477 $result = $accessibleProxyClassName::normalizeMonthAndWeekday('fri', FALSE);
478 $this->assertSame('5', $result);
479 }
480
481 /**
482 * @test
483 */
484 public function normalizeMonthAndWeekdayLeavesValueUnchanged() {
485 $accessibleProxyClassName = $this->getAccessibleProxy();
486 $result = $accessibleProxyClassName::normalizeMonthAndWeekday('2');
487 $this->assertSame('2', $result);
488 }
489
490 /**
491 * @return array
492 */
493 static public function validMonthNamesDataProvider() {
494 return array(
495 'jan' => array('jan', 1),
496 'feb' => array('feb', 2),
497 'MaR' => array('MaR', 3),
498 'aPr' => array('aPr', 4),
499 'MAY' => array('MAY', 5),
500 'jun' => array('jun', 6),
501 'jul' => array('jul', 7),
502 'aug' => array('aug', 8),
503 'sep' => array('sep', 9),
504 'September' => array('September', 9),
505 'oct' => array('oct', 10),
506 'nov' => array('nov', 11),
507 'dec' => array('dec', 12),
508 'string 7' => array('7', 7),
509 'integer 7' => array(7, 7),
510 'string 07' => array('07', 7),
511 'integer 07' => array(7, 7)
512 );
513 }
514
515 /**
516 * @test
517 * @dataProvider validMonthNamesDataProvider
518 * @param string $monthName Month name
519 * @param integer $expectedInteger Number of the month
520 */
521 public function normalizeMonthConvertsName($monthName, $expectedInteger) {
522 $accessibleProxyClassName = $this->getAccessibleProxy();
523 $result = $accessibleProxyClassName::normalizeMonth($monthName);
524 $this->assertEquals($expectedInteger, $result);
525 }
526
527 /**
528 * @test
529 * @dataProvider validMonthNamesDataProvider
530 * @param string $monthName Month name
531 * @param integer $expectedInteger Number of the month (not used)
532 */
533 public function normalizeMonthReturnsInteger($monthName, $expectedInteger) {
534 $accessibleProxyClassName = $this->getAccessibleProxy();
535 $result = $accessibleProxyClassName::normalizeMonth($monthName);
536 $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT, $result);
537 }
538
539 /**
540 * @return array
541 */
542 static public function invalidMonthNamesDataProvider() {
543 return array(
544 'sep-' => array('sep-'),
545 '-September-' => array('-September-'),
546 ',sep' => array(',sep'),
547 ',September,' => array(',September,'),
548 'sep/' => array('sep/'),
549 '/sep' => array('/sep'),
550 '/September/' => array('/September/'),
551 'foo' => array('foo'),
552 'Tuesday' => array('Tuesday'),
553 'Tue' => array('Tue'),
554 'string 0' => array('0'),
555 'integer 0' => array(0),
556 'string seven' => array('seven'),
557 'string 13' => array('13'),
558 'integer 13' => array(13),
559 'integer 100' => array(100),
560 'integer 2010' => array(2010),
561 'string minus 7' => array('-7'),
562 'negative integer 7' => array(-7)
563 );
564 }
565
566 /**
567 * @test
568 * @expectedException InvalidArgumentException
569 * @dataProvider invalidMonthNamesDataProvider
570 * @param string $invalidMonthName Month name (invalid)
571 */
572 public function normalizeMonthThrowsExceptionForInvalidMonthRepresentation($invalidMonthName) {
573 $accessibleProxyClassName = $this->getAccessibleProxy();
574 $accessibleProxyClassName::normalizeMonth($invalidMonthName);
575 }
576
577 /**
578 * @return array
579 */
580 static public function validWeekdayDataProvider() {
581 return array(
582 'string 1' => array('1', 1),
583 'string 2' => array('2', 2),
584 'string 02' => array('02', 2),
585 'integer 02' => array(2, 2),
586 'string 3' => array('3', 3),
587 'string 4' => array('4', 4),
588 'string 5' => array('5', 5),
589 'integer 5' => array(5, 5),
590 'string 6' => array('6', 6),
591 'string 7' => array('7', 7),
592 'string 0' => array('0', 7),
593 'integer 0' => array(0, 7),
594 'mon' => array('mon', 1),
595 'monday' => array('monday', 1),
596 'tue' => array('tue', 2),
597 'tuesday' => array('tuesday', 2),
598 'WED' => array('WED', 3),
599 'WEDnesday' => array('WEDnesday', 3),
600 'tHu' => array('tHu', 4),
601 'Thursday' => array('Thursday', 4),
602 'fri' => array('fri', 5),
603 'friday' => array('friday', 5),
604 'sat' => array('sat', 6),
605 'saturday' => array('saturday', 6),
606 'sun' => array('sun', 7),
607 'sunday' => array('sunday', 7)
608 );
609 }
610
611 /**
612 * @test
613 * @dataProvider validWeekdayDataProvider
614 * @param string $weekday Weekday expression
615 * @param integer $expectedInteger Number of weekday
616 */
617 public function normalizeWeekdayConvertsName($weekday, $expectedInteger) {
618 $accessibleProxyClassName = $this->getAccessibleProxy();
619 $result = $accessibleProxyClassName::normalizeWeekday($weekday);
620 $this->assertEquals($expectedInteger, $result);
621 }
622
623 /**
624 * @test
625 * @dataProvider validWeekdayDataProvider
626 * @param string $weekday Weekday expression
627 * @param integer $expectedInteger Number of weekday (not used)
628 */
629 public function normalizeWeekdayReturnsInteger($weekday, $expectedInteger) {
630 $accessibleProxyClassName = $this->getAccessibleProxy();
631 $result = $accessibleProxyClassName::normalizeWeekday($weekday);
632 $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT, $result);
633 }
634
635 /**
636 * @return array
637 */
638 static public function invalidWeekdayDataProvider() {
639 return array(
640 '-fri' => array('-fri'),
641 'fri-' => array('fri-'),
642 '-friday-' => array('-friday-'),
643 '/fri' => array('/fri'),
644 'fri/' => array('fri/'),
645 '/friday/' => array('/friday/'),
646 ',fri' => array(',fri'),
647 ',friday,' => array(',friday,'),
648 'string minus 1' => array('-1'),
649 'integer -1' => array(-1),
650 'string seven' => array('seven'),
651 'string 8' => array('8'),
652 'string 8' => array('8'),
653 'string 29' => array('29'),
654 'string 2010' => array('2010'),
655 'Jan' => array('Jan'),
656 'January' => array('January'),
657 'MARCH' => array('MARCH')
658 );
659 }
660
661 /**
662 * @test
663 * @dataProvider invalidWeekdayDataProvider
664 * @expectedException InvalidArgumentException
665 * @param string $weekday Weekday expression (invalid)
666 */
667 public function normalizeWeekdayThrowsExceptionForInvalidWeekdayRepresentation($weekday) {
668 $accessibleProxyClassName = $this->getAccessibleProxy();
669 $accessibleProxyClassName::normalizeWeekday($weekday);
670 }
671
672 }
673
674
675 ?>