TYPO3 API  SVNRelease
tx_scheduler_croncmdTest.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  * Testcase for class "tx_scheduler_CronCmd"
00027  *
00028  * @package TYPO3
00029  * @subpackage tx_scheduler
00030  *
00031  * @author Christian Kuhn <lolli@schwarzbu.ch>
00032  */
00033 class tx_scheduler_croncmdTest extends tx_phpunit_testcase {
00034     /**
00035      * @const   integer timestamp of 1.1.2010 0:00 (Friday)
00036      */
00037     const TIMESTAMP = 1262300400;
00038 
00039     /**
00040      * @test
00041      */
00042     public function constructorSetsNormalizedCronCommandSections() {
00043         $instance = new tx_scheduler_CronCmd('2-3 * * * *');
00044         $this->assertSame($instance->getCronCommandSections(), array('2,3', '*', '*', '*', '*'));
00045     }
00046 
00047     /**
00048      * @test
00049      * @expectedException InvalidArgumentException
00050      */
00051     public function constructorThrowsExceptionForInvalidCronCommand() {
00052         new tx_scheduler_CronCmd('61 * * * *');
00053     }
00054 
00055     /**
00056      * @test
00057      */
00058     public function constructorSetsTimestampToNowPlusOneMinuteRoundedDownToSixtySeconds() {
00059         $instance = new tx_scheduler_CronCmd('* * * * *');
00060         $this->assertSame($instance->getTimestamp(), $GLOBALS['ACCESS_TIME'] + 60);
00061     }
00062 
00063     /**
00064      * @test
00065      */
00066     public function constructorSetsTimestampToGivenTimestampPlusSixtySeconds() {
00067         $instance = new tx_scheduler_CronCmd('* * * * *', self::TIMESTAMP);
00068         $this->assertSame($instance->getTimestamp(), self::TIMESTAMP + 60);
00069     }
00070 
00071     /**
00072      * @test
00073      */
00074     public function constructorSetsTimestampToGiveTimestampRoundedDownToSixtySeconds() {
00075         $instance = new tx_scheduler_CronCmd('* * * * *', self::TIMESTAMP + 1);
00076         $this->assertSame($instance->getTimestamp(), self::TIMESTAMP + 60);
00077     }
00078 
00079     /**
00080      * @return array
00081      *  0 => cron command
00082      *  1 => start timestamp
00083      *  2 => expected timestamp after first calculateNextValue()
00084      *  3 => expected timestamp after second calculateNextValue()
00085      */
00086     public static function expectedTimestampDataProvider() {
00087         return array(
00088             'every minute' => array(
00089                 '* * * * *',
00090                 self::TIMESTAMP,
00091                 self::TIMESTAMP + 60,
00092                 self::TIMESTAMP + 120,
00093             ),
00094             'once an hour at 1' => array(
00095                 '1 * * * *',
00096                 self::TIMESTAMP,
00097                 self::TIMESTAMP + 60,
00098                 self::TIMESTAMP + 60 + 60*60,
00099             ),
00100             'once an hour at 0' => array(
00101                 '0 * * * *',
00102                 self::TIMESTAMP,
00103                 self::TIMESTAMP + 60*60,
00104                 self::TIMESTAMP + 60*60 + 60*60,
00105             ),
00106             'once a day at 1:00' => array(
00107                 '0 1 * * *',
00108                 self::TIMESTAMP,
00109                 self::TIMESTAMP + 60*60,
00110                 self::TIMESTAMP + 60*60 + 60*60*24,
00111             ),
00112             'once a day at 0:00' => array(
00113                 '0 0 * * *',
00114                 self::TIMESTAMP,
00115                 self::TIMESTAMP + 60*60*24,
00116                 self::TIMESTAMP + 60*60*24*2,
00117             ),
00118             'every first day of month' => array(
00119                 '0 0 1 * *',
00120                 self::TIMESTAMP,
00121                 strtotime('01-02-2010'),
00122                 strtotime('01-03-2010'),
00123             ),
00124             'once a month' => array(
00125                 '0 0 4 * *',
00126                 self::TIMESTAMP,
00127                 self::TIMESTAMP + 60*60*24*3,
00128                 self::TIMESTAMP + 60*60*24*3 + 60*60*24*31,
00129             ),
00130             'once every Saturday' => array(
00131                 '0 0 * * sat',
00132                 self::TIMESTAMP,
00133                 self::TIMESTAMP + 60*60*24,
00134                 self::TIMESTAMP + 60*60*24 + 60*60*24*7,
00135             ),
00136             'once every day in February' => array(
00137                 '0 0 * feb *',
00138                 self::TIMESTAMP,
00139                 self::TIMESTAMP + 60*60*24*31,
00140                 self::TIMESTAMP + 60*60*24*31 + 60*60*24
00141             ),
00142             'once every February' => array(
00143                 '0 0 1 feb *',
00144                 self::TIMESTAMP,
00145                 self::TIMESTAMP + 60*60*24*31,
00146                 strtotime('01-02-2011'),
00147             ),
00148             'once every Friday February' => array(
00149                 '0 0 * feb fri',
00150                 self::TIMESTAMP,
00151                 strtotime('05-02-2010'),
00152                 strtotime('12-02-2010'),
00153             ),
00154             'first day in February and every Friday' => array(
00155                 '0 0 1 feb fri',
00156                 self::TIMESTAMP,
00157                 strtotime('01-02-2010'),
00158                 strtotime('05-02-2010'),
00159             ),
00160             'day of week and day of month restricted, next match in day of month field' => array(
00161                 '0 0 2 * sun',
00162                 self::TIMESTAMP,
00163                 self::TIMESTAMP + 60*60*24,
00164                 self::TIMESTAMP + 60*60*24 + 60*60*24,
00165             ),
00166             'day of week and day of month restricted, next match in day of week field' => array(
00167                 '0 0 3 * sat',
00168                 self::TIMESTAMP,
00169                 self::TIMESTAMP + 60*60*24,
00170                 self::TIMESTAMP + 60*60*24 + 60*60*24,
00171             ),
00172             '29th February leap year' => array(
00173                 '0 0 29 feb *',
00174                 self::TIMESTAMP,
00175                 strtotime('29-02-2012'),
00176                 strtotime('29-02-2016'),
00177             ),
00178             'list of minutes' => array(
00179                 '2,4 * * * *',
00180                 self::TIMESTAMP,
00181                 self::TIMESTAMP + 120,
00182                 self::TIMESTAMP + 240,
00183             ),
00184             'list of hours' => array(
00185                 '0 2,4 * * *',
00186                 self::TIMESTAMP,
00187                 self::TIMESTAMP + 60*60*2,
00188                 self::TIMESTAMP + 60*60*4,
00189             ),
00190             'list of days in month' => array(
00191                 '0 0 2,4 * *',
00192                 self::TIMESTAMP,
00193                 strtotime('02-01-2010'),
00194                 strtotime('04-01-2010'),
00195             ),
00196             'list of month' => array(
00197                 '0 0 1 2,3 *',
00198                 self::TIMESTAMP,
00199                 strtotime('01-02-2010'),
00200                 strtotime('01-03-2010'),
00201             ),
00202             'list of days of weeks' => array(
00203                 '0 0 * * 2,4',
00204                 self::TIMESTAMP,
00205                 strtotime('05-01-2010'),
00206                 strtotime('07-01-2010'),
00207             ),
00208         );
00209     }
00210 
00211     /**
00212      * @test
00213      * @dataProvider expectedTimestampDataProvider
00214      */
00215     public function calculateNextValueDeterminesCorrectNextTimestamp($cronCommand, $startTimestamp, $expectedTimestamp) {
00216         $instance = new tx_scheduler_CronCmd($cronCommand, $startTimestamp);
00217         $instance->calculateNextValue();
00218         $this->assertSame($instance->getTimestamp(), $expectedTimestamp);
00219     }
00220 
00221     /**
00222      * @test
00223      * @dataProvider expectedTimestampDataProvider
00224      */
00225     public function calculateNextValueDeterminesCorrectNextTimestampOnConsecutiveCall($cronCommand, $startTimestamp, $firstTimestamp, $secondTimestamp) {
00226         $instance = new tx_scheduler_CronCmd($cronCommand, $firstTimestamp);
00227         $instance->calculateNextValue();
00228         $this->assertSame($instance->getTimestamp(), $secondTimestamp);
00229     }
00230 
00231     /**
00232      * @test
00233      */
00234     public function calculateNextValueDeterminesCorrectNextTimestampOnChangeToSummertime() {
00235         $backupTimezone = date_default_timezone_get();
00236         date_default_timezone_set('Europe/Berlin');
00237         $instance = new tx_scheduler_CronCmd('* 3 28 mar *', self::TIMESTAMP);
00238         $instance->calculateNextValue();
00239         date_default_timezone_set($backupTimezone);
00240         $this->assertSame($instance->getTimestamp(), 1269741600);
00241     }
00242 
00243     /**
00244      * @test
00245      * @expectedException RuntimeException
00246      */
00247     public function calculateNextValueThrowsExceptionWithImpossibleCronCommand() {
00248         $instance = new tx_scheduler_CronCmd('* * 31 apr *', self::TIMESTAMP);
00249         $instance->calculateNextValue();
00250     }
00251 
00252     /**
00253      * @test
00254      */
00255     public function getTimestampReturnsInteger() {
00256         $instance = new tx_scheduler_CronCmd('* * * * *');
00257         $this->assertType('integer', $instance->getTimestamp());
00258     }
00259 
00260     /**
00261      * @test
00262      */
00263     public function getCronCommandSectionsReturnsArray() {
00264         $instance = new tx_scheduler_CronCmd('* * * * *');
00265         $this->assertType('array', $instance->getCronCommandSections());
00266     }
00267 }
00268 ?>