TYPO3 API  SVNRelease
tx_scheduler_croncmd_normalizeTest.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2010-2011 Christian Kuhn <lolli@schwarzbu.ch>
00006  *  All rights reserved
00007  *
00008  *  This script is part of the TYPO3 project. The TYPO3 project is
00009  *  free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  The GNU General Public License can be found at
00015  *  http://www.gnu.org/copyleft/gpl.html.
00016  *
00017  *  This script is distributed in the hope that it will be useful,
00018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  *  GNU General Public License for more details.
00021  *
00022  *  This copyright notice MUST APPEAR in all copies of the script!
00023  ***************************************************************/
00024 
00025 /**
00026  * Test case for class "tx_scheduler_CronCmd_Normalize"
00027  *
00028  * @author Christian Kuhn <lolli@schwarzbu.ch>
00029  *
00030  * @package TYPO3
00031  * @subpackage tx_scheduler
00032  */
00033 class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
00034 
00035     /**
00036      * @return array
00037      */
00038     public static function normalizeValidDataProvider() {
00039         return array(
00040             '@weekly' => array('@weekly', '0 0 * * 7'),
00041             ' @weekly ' => array(' @weekly ', '0 0 * * 7'),
00042             '* * * * *' => array('* * * * *', '* * * * *'),
00043             '30 4 1,15 * 5' => array('30 4 1,15 * 5', '30 4 1,15 * 5'),
00044             '5 0 * * *' => array('5 0 * * *', '5 0 * * *'),
00045             '15 14 1 * *' => array('15 14 1 * *', '15 14 1 * *'),
00046             '0 22 * * 1-5' => array('0 22 * * 1-5', '0 22 * * 1,2,3,4,5'),
00047             '23 0-23/2 * * *' => array('23 0-23/2 * * *', '23 0,2,4,6,8,10,12,14,16,18,20,22 * * *'),
00048             '5 4 * * sun' => array('5 4 * * sun', '5 4 * * 7'),
00049             '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'),
00050             '0-20/10 * * * *' => array('0-20/10 * * * *', '0,10,20 * * * *'),
00051             '* * 2 * *' => array('* * 2 * *', '* * 2 * *'),
00052             '* * 2,7 * *' => array('* * 2,7 * *', '* * 2,7 * *'),
00053             '* * 2-4,10 * *' => array('* * 2-4,10 * *', '* * 2,3,4,10 * *'),
00054             '* * */14 * *' => array('* * */14 * *', '* * 1,15,29 * *'),
00055             '* * 2,4-6/2,*/14 * *' => array('* * 2,4-6/2,*/14 * *', '* * 1,2,4,6,15,29 * *'),
00056             '* * * * 1' => array('* * * * 1', '* * * * 1'),
00057             '0 0 * * 0' => array('0 0 * * 0', '0 0 * * 7'),
00058             '0 0 * * 7' => array('0 0 * * 7', '0 0 * * 7'),
00059             '* * 1,2 * 1' => array('* * 1,2 * 1', '* * 1,2 * 1'),
00060         );
00061     }
00062 
00063     /**
00064      * @test
00065      * @dataProvider normalizeValidDataProvider
00066      */
00067     public function normalizeConvertsCronCommand($expression, $expected) {
00068         $result = tx_scheduler_CronCmd_Normalize::normalize($expression);
00069         $this->assertEquals($expected, $result);
00070     }
00071 
00072     /**
00073      * @return array
00074      */
00075     public static function validSpecialKeywordsDataProvider() {
00076         return array(
00077             '@yearly' => array('@yearly', '0 0 1 1 *'),
00078             '@annually' => array('@annually', '0 0 1 1 *'),
00079             '@monthly' => array('@monthly', '0 0 1 * *'),
00080             '@weekly' => array('@weekly', '0 0 * * 0'),
00081             '@daily' => array('@daily', '0 0 * * *'),
00082             '@midnight' => array('@midnight', '0 0 * * *'),
00083             '@hourly' => array('@hourly', '0 * * * *'),
00084         );
00085     }
00086 
00087     /**
00088      * @test
00089      * @dataProvider validSpecialKeywordsDataProvider
00090      */
00091     public function convertKeywordsToCronCommandConvertsValidKeywords($keyword, $exptedCronCommand) {
00092         $result = tx_scheduler_CronCmd_Normalize::convertKeywordsToCronCommand($keyword);
00093         $this->assertEquals($exptedCronCommand, $result);
00094     }
00095 
00096     /**
00097      * @test
00098      */
00099     public function convertKeywordsToCronCommandReturnsUnchangedCommandIfKeywordWasNotFound() {
00100         $invalidKeyword = 'foo';
00101         $result = tx_scheduler_CronCmd_Normalize::convertKeywordsToCronCommand($invalidKeyword);
00102         $this->assertEquals($invalidKeyword, $result);
00103     }
00104 
00105     /**
00106      * @return array
00107      */
00108     public function normalizeFieldsValidDataProvider() {
00109         return array(
00110             '1-2 * * * *' => array('1-2 * * * *', '1,2 * * * *'),
00111             '* 1-2 * * *' => array('* 1-2 * * *', '* 1,2 * * *'),
00112             '* * 1-2 * *' => array('* * 1-2 * *', '* * 1,2 * *'),
00113             '* * * 1-2 *' => array('* * * 1-2 *', '* * * 1,2 *'),
00114             '* * * * 1-2' => array('* * * * 1-2', '* * * * 1,2'),
00115         );
00116     }
00117 
00118     /**
00119      * @test
00120      * @dataProvider normalizeFieldsValidDataProvider
00121      */
00122     public function normalizeFieldsConvertsField($expression, $expected) {
00123         $result = tx_scheduler_CronCmd_Normalize::normalizeFields($expression);
00124         $this->assertEquals($expected, $result);
00125     }
00126 
00127     /**
00128      * @return array
00129      */
00130     public static function normalizeMonthAndWeekdayFieldValidDataProvider() {
00131         return array(
00132             '*' => array('*', TRUE, '*'),
00133             'string 1' => array('1', TRUE, '1'),
00134             'jan' => array('jan', TRUE, '1'),
00135             'feb/2' => array('feb/2', TRUE, '2'),
00136             'jan-feb/2' => array('jan-feb/2', TRUE, '1'),
00137             '1-2' => array('1-2', TRUE, '1,2'),
00138             '1-3/2,feb,may,6' => array('1-3/2,feb,may,6', TRUE, '1,2,3,5,6'),
00139             '*/4' => array('*/4', TRUE, '1,5,9'),
00140             '*' => array('*', FALSE, '*'),
00141             'string 1' => array('1', FALSE, '1'),
00142             'fri' => array('fri', FALSE, '5'),
00143             'sun' => array('sun', FALSE, '7'),
00144             'string 0 for sunday' => array('0', FALSE, '7'),
00145             '0,1' => array('0,1', FALSE, '1,7'),
00146             '*/3' => array('*/3', FALSE, '1,4,7'),
00147             'tue/2' => array('tue/2', FALSE, '2'),
00148             '1-2' => array('1-2', FALSE, '1,2'),
00149             'tue-fri/2' => array('tue-fri/2', FALSE, '2,4'),
00150             '1-3/2,tue,fri,6' => array('1-3/2,tue,fri,6', FALSE, '1,2,3,5,6'),
00151         );
00152     }
00153 
00154     /**
00155      * @test
00156      * @dataProvider normalizeMonthAndWeekdayFieldValidDataProvider
00157      */
00158     public function normalizeMonthAndWeekdayFieldReturnsNormalizedListForValidExpression($expression, $isMonthField, $expected) {
00159         $result = tx_scheduler_CronCmd_Normalize::normalizeMonthAndWeekdayField($expression, $isMonthField);
00160         $this->assertSame($expected, $result);
00161     }
00162 
00163     /**
00164      * @return array
00165      */
00166     public static function normalizeMonthAndWeekdayFieldInvalidDataProvider() {
00167         return array(
00168             'mon' => array('mon', TRUE),
00169             '1-2/mon' => array('1-2/mon', TRUE),
00170             '0,1' => array('0,1', TRUE),
00171             'feb' => array('feb', FALSE),
00172             '1-2/feb' => array('1-2/feb', FALSE),
00173             '0-fri/2,7' => array('0-fri/2,7', FALSE, '2,4,7'),
00174         );
00175     }
00176 
00177     /**
00178      * @test
00179      * @dataProvider normalizeMonthAndWeekdayFieldInvalidDataProvider
00180      * @expectedException InvalidArgumentException
00181      */
00182     public function normalizeMonthAndWeekdayFieldThrowsExceptionForInvalidExpression($expression, $isMonthField) {
00183         $result = tx_scheduler_CronCmd_Normalize::normalizeMonthAndWeekdayField($expression, $isMonthField);
00184     }
00185 
00186     /**
00187      * @return array
00188      */
00189     public static function normalizeIntegerFieldValidDataProvider() {
00190         return array(
00191             '*' => array('*', '*'),
00192             'string 2' => array('2', '2'),
00193             'integer 3' => array(3, '3'),
00194             'list of values' => array('1,2,3', '1,2,3'),
00195             'unsorted list of values' => array('3,1,5', '1,3,5'),
00196             'duplicate values' => array('0-2/2,2', '0,2'),
00197             'additional field between steps' => array('1-3/2,2', '1,2,3'),
00198             '2-4' => array('2-4', '2,3,4'),
00199             'simple step 4/4' => array('4/4', '4'),
00200             'step 2-7/5' => array('2-7/5', '2,7'),
00201             'steps 4-12/4' => array('4-12/4', '4,8,12'),
00202             '0-59/20' => array('0-59/20', '0,20,40'),
00203             '*/20' => array('*/20', '0,20,40'),
00204         );
00205     }
00206 
00207     /**
00208      * @test
00209      * @dataProvider normalizeIntegerFieldValidDataProvider
00210      */
00211     public function normalizeIntegerFieldReturnsNormalizedListForValidExpression($expression, $expected) {
00212         $result = tx_scheduler_CronCmd_Normalize::normalizeIntegerField($expression);
00213         $this->assertSame($expected, $result);
00214     }
00215 
00216     /**
00217      * @return array
00218      */
00219     public static function normalizeIntegerFieldInvalidDataProvider() {
00220         return array(
00221             'string foo' => array('foo', 0, 59),
00222             'empty string' => array('', 0, 59),
00223             '4-3' => array('4-3', 0, 59),
00224             '/2' => array('/2', 0, 59),
00225             '/' => array('/', 0, 59),
00226             'string foo' => array('foo', 0, 59),
00227             'left bound too low' => array('2-4', 3, 4),
00228             'right bound too high' => array('2-4', 2, 3),
00229             'left and right bound' => array('2-5', 2, 4),
00230             'element in list is lower than allowed' => array('2,1,4', 2, 4),
00231             'element in list is higher than allowed' => array('2,5,4', 1, 4),
00232         );
00233     }
00234 
00235     /**
00236      * @test
00237      * @dataProvider normalizeIntegerFieldInvalidDataProvider
00238      * @expectedException InvalidArgumentException
00239      */
00240     public function normalizeIntegerFieldThrowsExceptionForInvalidExpressions($expression, $lowerBound, $upperBound) {
00241         tx_scheduler_CronCmd_Normalize::normalizeIntegerField($expression, $lowerBound, $upperBound);
00242     }
00243 
00244     /**
00245      * @test
00246      */
00247     public function splitFieldsReturnsIntegerArrayWithFieldsSplitByWhitespace() {
00248         $result = tx_scheduler_CronCmd_Normalize::splitFields('12,13 * 1-12/2,14 jan fri');
00249         $expectedResult = array(
00250             0 => '12,13',
00251             1 => '*',
00252             2 => '1-12/2,14',
00253             3 => 'jan',
00254             4 => 'fri',
00255         );
00256         $this->assertSame($expectedResult, $result);
00257     }
00258 
00259     /**
00260      * @return array
00261      */
00262     public static function invalidCronCommandFieldsDataProvider() {
00263         return array(
00264             'empty string' => array(''),
00265             'foo' => array('foo'),
00266             'integer 4' => array(4),
00267             'four fields' => array('* * * *'),
00268             'six fields' => array('* * * * * *'),
00269         );
00270     }
00271 
00272     /**
00273      * @test
00274      * @expectedException InvalidArgumentException
00275      * @dataProvider invalidCronCommandFieldsDataProvider
00276      */
00277     public function splitFieldsThrowsExceptionIfCronCommandDoesNotContainFiveFields($cronCommand) {
00278         tx_scheduler_CronCmd_Normalize::splitFields($cronCommand);
00279     }
00280 
00281     /**
00282      * @return array
00283      */
00284     public static function validRangeDataProvider() {
00285         return array(
00286             'single value' => array('3', '3'),
00287             'integer 3' => array(3, '3'),
00288             '0-0' => array('0-0', '0'),
00289             '4-4' => array('4-4', '4'),
00290             '0-3' => array('0-3', '0,1,2,3'),
00291             '4-5' => array('4-5', '4,5'),
00292         );
00293     }
00294 
00295     /**
00296      * @test
00297      * @dataProvider validRangeDataProvider
00298      */
00299     public function convertRangeToListOfValuesReturnsCorrectListForValidRanges($range, $expected) {
00300         $result = tx_scheduler_CronCmd_Normalize::convertRangeToListOfValues($range);
00301         $this->assertSame($expected, $result);
00302     }
00303 
00304     /**
00305      * @return array
00306      */
00307     public static function invalidRangeDataProvider() {
00308         return array(
00309             'empty string' => array(''),
00310             'string' => array('foo'),
00311             'single dash' => array('-'),
00312             'left part is string' => array('foo-5'),
00313             'right part is string' => array('5-foo'),
00314             'range of strings' => array('foo-bar'),
00315             'string five minus' => array('5-'),
00316             'string minus five' => array('-5'),
00317             'more than one dash' => array('2-3-4'),
00318             'left part bigger than right part' => array('6-3'),
00319         );
00320     }
00321 
00322     /**
00323      * @test
00324      * @dataProvider invalidRangeDataProvider
00325      * @expectedException InvalidArgumentException
00326      */
00327     public function convertRangeToListOfValuesThrowsExceptionForInvalidRanges($range) {
00328         tx_scheduler_CronCmd_Normalize::convertRangeToListOfValues($range);
00329     }
00330 
00331     /**
00332      * @return array
00333      */
00334     public static function validStepsDataProvider() {
00335         return array(
00336             '2/2' => array('2/2', '2'),
00337             '2,3,4/2' => array('2,3,4/2', '2,4'),
00338             '1,2,3,4,5,6,7/3' => array('1,2,3,4,5,6,7/3', '1,4,7'),
00339             '0,1,2,3,4,5,6/3' => array('0,1,2,3,4,5,6/3', '0,3,6'),
00340         );
00341     }
00342 
00343     /**
00344      * @test
00345      * @dataProvider validStepsDataProvider
00346      */
00347     public function reduceListOfValuesByStepValueReturnsCorrectListOfValues($stepExpression, $expected) {
00348         $result = tx_scheduler_CronCmd_Normalize::reduceListOfValuesByStepValue($stepExpression);
00349         $this->assertSame($expected, $result);
00350     }
00351 
00352     /**
00353      * @return array
00354      */
00355     public static function invalidStepsDataProvider() {
00356         return array(
00357             'empty string' => array(''),
00358             'slash only' => array('/'),
00359             'left part empty' => array('/2'),
00360             'right part empty' => array('2/'),
00361             'multiples slashes' => array('1/2/3'),
00362             '2-2' => array('2-2'),
00363             '2.3/2' => array('2.3/2'),
00364             '2,3,4/2.3' => array('2,3,4/2.3'),
00365             '2,3,4/2,3' => array('2,3,4/2,3'),
00366         );
00367     }
00368 
00369     /**
00370      * @test
00371      * @dataProvider invalidStepsDataProvider
00372      * @expectedException InvalidArgumentException
00373      */
00374     public function reduceListOfValuesByStepValueThrowsExceptionForInvalidStepExpressions($stepExpression) {
00375         $result = tx_scheduler_CronCmd_Normalize::reduceListOfValuesByStepValue($stepExpression);
00376     }
00377 
00378     /**
00379      * @test
00380      */
00381     public function normalizeMonthAndWeekdayNormalizesAMonth() {
00382         $result = tx_scheduler_CronCmd_Normalize::normalizeMonthAndWeekday('feb', TRUE);
00383         $this->assertSame('2', $result);
00384     }
00385 
00386     /**
00387      * @test
00388      */
00389     public function normalizeMonthAndWeekdayNormalizesAWeekday() {
00390         $result = tx_scheduler_CronCmd_Normalize::normalizeMonthAndWeekday('fri', FALSE);
00391         $this->assertSame('5', $result);
00392     }
00393 
00394     /**
00395      * @test
00396      */
00397     public function normalizeMonthAndWeekdayLeavesValueUnchanged() {
00398         $result = tx_scheduler_CronCmd_Normalize::normalizeMonthAndWeekday('2');
00399         $this->assertSame('2', $result);
00400     }
00401 
00402     /**
00403      * @return array
00404      */
00405     public static function validMonthNamesDataProvider() {
00406         return array(
00407             'jan' => array('jan', 1),
00408             'feb' => array('feb', 2),
00409             'MaR' => array('MaR', 3),
00410             'aPr' => array('aPr', 4),
00411             'MAY' => array('MAY', 5),
00412             'jun' => array('jun', 6),
00413             'jul' => array('jul', 7),
00414             'aug' => array('aug', 8),
00415             'sep' => array('sep', 9),
00416             'September' => array('September', 9),
00417             'oct' => array('oct', 10),
00418             'nov' => array('nov', 11),
00419             'dec' => array('dec', 12),
00420             'string 7' => array('7', 7),
00421             'integer 7' => array(7, 7),
00422             'string 07' => array('07', 7),
00423             'integer 07' => array(07, 7),
00424         );
00425     }
00426 
00427     /**
00428      * @test
00429      * @dataProvider validMonthNamesDataProvider
00430      */
00431     public function normalizeMonthConvertsName($monthName, $expectedInteger) {
00432         $result = tx_scheduler_CronCmd_Normalize::normalizeMonth($monthName);
00433         $this->assertEquals($expectedInteger, $result);
00434     }
00435 
00436     /**
00437      * @test
00438      * @dataProvider validMonthNamesDataProvider
00439      */
00440     public function normalizeMonthReturnsInteger($monthName, $expectedInteger) {
00441         $result = tx_scheduler_CronCmd_Normalize::normalizeMonth($monthName);
00442         $this->assertType('integer', $result);
00443     }
00444 
00445     /**
00446      * @return array
00447      */
00448     public static function invalidMonthNamesDataProvider() {
00449         return array(
00450             'sep-' => array('sep-'),
00451             '-September-' => array('-September-'),
00452             ',sep' => array(',sep'),
00453             ',September,' => array(',September,'),
00454             'sep/' => array('sep/'),
00455             '/sep' => array('/sep'),
00456             '/September/' => array('/September/'),
00457             'foo' => array('foo'),
00458             'Tuesday' => array('Tuesday'),
00459             'Tue' => array('Tue'),
00460             'string 0' => array('0'),
00461             'integer 0' => array(0),
00462             'string seven' => array('seven'),
00463             'string 13' => array('13'),
00464             'integer 13' => array(13),
00465             'integer 100' => array(100),
00466             'integer 2010' => array(2010),
00467             'string minus 7' => array('-7'),
00468             'negative integer 7' => array(-7),
00469         );
00470     }
00471 
00472     /**
00473      * @test
00474      * @expectedException InvalidArgumentException
00475      * @dataProvider invalidMonthNamesDataProvider
00476      */
00477     public function normalizeMonthThrowsExceptionForInvalidMonthRepresentation($invalidMonthName) {
00478         tx_scheduler_CronCmd_Normalize::normalizeMonth($invalidMonthName);
00479     }
00480 
00481     /**
00482      * @return array
00483      */
00484     public static function validWeekdayDataProvider() {
00485         return array(
00486             'string 1' => array('1', 1),
00487             'string 2' => array('2', 2),
00488             'string 02' => array('02', 2),
00489             'integer 02' => array(02, 2),
00490             'string 3' => array('3', 3),
00491             'string 4' => array('4', 4),
00492             'string 5' => array('5', 5),
00493             'integer 5' => array(5, 5),
00494             'string 6' => array('6', 6),
00495             'string 7' => array('7', 7),
00496             'string 0' => array('0', 7),
00497             'integer 0' => array(0, 7),
00498             'mon' => array('mon', 1),
00499             'monday' => array('monday', 1),
00500             'tue' => array('tue', 2),
00501             'tuesday' => array('tuesday', 2),
00502             'WED' => array('WED', 3),
00503             'WEDnesday' => array('WEDnesday', 3),
00504             'tHu' => array('tHu', 4),
00505             'Thursday' => array('Thursday', 4),
00506             'fri' => array('fri', 5),
00507             'friday' => array('friday', 5),
00508             'sat' => array('sat', 6),
00509             'saturday' => array('saturday', 6),
00510             'sun' => array('sun', 7),
00511             'sunday' => array('sunday', 7),
00512         );
00513     }
00514 
00515     /**
00516      * @test
00517      * @dataProvider validWeekdayDataProvider
00518      */
00519     public function normalizeWeekdayConvertsName($weekday, $expectedInteger) {
00520         $result = tx_scheduler_CronCmd_Normalize::normalizeWeekday($weekday);
00521         $this->assertEquals($expectedInteger, $result);
00522     }
00523 
00524     /**
00525      * @test
00526      * @dataProvider validWeekdayDataProvider
00527      */
00528     public function normalizeWeekdayReturnsInteger($weekday, $expectedInteger) {
00529         $result = tx_scheduler_CronCmd_Normalize::normalizeWeekday($weekday);
00530         $this->assertType('integer', $result);
00531     }
00532 
00533     /**
00534      * @return array
00535      */
00536     public static function invalidWeekdayDataProvider() {
00537         return array(
00538             '-fri' => array('-fri'),
00539             'fri-' => array('fri-'),
00540             '-friday-' => array('-friday-'),
00541             '/fri' => array('/fri'),
00542             'fri/' => array('fri/'),
00543             '/friday/' => array('/friday/'),
00544             ',fri' => array(',fri'),
00545             ',friday,' => array(',friday,'),
00546             'string minus 1' => array('-1'),
00547             'integer -1' => array(-1),
00548             'string seven' => array('seven'),
00549             'string 8' => array('8'),
00550             'string 8' => array('8'),
00551             'string 29' => array('29'),
00552             'string 2010' => array('2010'),
00553             'Jan' => array('Jan'),
00554             'January' => array('January'),
00555             'MARCH' => array('MARCH'),
00556         );
00557     }
00558 
00559     /**
00560      * @test
00561      * @dataProvider invalidWeekdayDataProvider
00562      * @expectedException InvalidArgumentException
00563      */
00564     public function normalizeWeekdayThrowsExceptionForInvalidWeekdayRepresentation($weekday) {
00565         tx_scheduler_CronCmd_Normalize::normalizeWeekday($weekday);
00566     }
00567 }
00568 ?>