[TASK] Clean up API of tx_scheduler_CronCmd_Normalize
authorChristian Kuhn <lolli@schwarzbu.ch>
Thu, 18 Aug 2011 22:42:15 +0000 (00:42 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Sun, 21 Aug 2011 11:42:54 +0000 (13:42 +0200)
The only public API method in tx_scheduler_CronCmd_Normalize is normalize().
The patch makes all non API methods protected and uses an accessible proxy
class in unit tests to make the low level methods testable. This cleans
up the API and still gives detailed unit tests.

Change-Id: I6845d049523b68e6f57d86ad03ef0a48c453df81
Resolves: #29100
Reviewed-on: http://review.typo3.org/4421
Reviewed-by: Björn Pedersen
Reviewed-by: Susanne Moog
Tested-by: Susanne Moog
Reviewed-by: Philipp Gampe
Tested-by: Philipp Gampe
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
typo3/sysext/scheduler/class.tx_scheduler_croncmd_normalize.php
typo3/sysext/scheduler/tests/class.tx_scheduler_croncmd_normalizeTest.php

index e05824e..5f9388a 100644 (file)
@@ -34,7 +34,7 @@
  * @package TYPO3
  * @subpackage scheduler
  */
-final class tx_scheduler_CronCmd_Normalize {
+class tx_scheduler_CronCmd_Normalize {
 
        /**
         * Main API method: Get the cron command and normalize it.
@@ -62,7 +62,7 @@ final class tx_scheduler_CronCmd_Normalize {
         * @param string $cronCommand Cron command
         * @return string Normalized cron command if keyword was found, else unchanged cron command
         */
-       public static function convertKeywordsToCronCommand($cronCommand) {
+       protected static function convertKeywordsToCronCommand($cronCommand) {
                switch ($cronCommand) {
                        case '@yearly':
                        case '@annually':
@@ -92,7 +92,7 @@ final class tx_scheduler_CronCmd_Normalize {
         * @param string $cronCommand cron command
         * @return string Normalized cron command
         */
-       public static function normalizeFields($cronCommand) {
+       protected static function normalizeFields($cronCommand) {
                $fieldArray = self::splitFields($cronCommand);
 
                $fieldArray[0] = self::normalizeIntegerField($fieldArray[0], 0, 59);
@@ -117,7 +117,7 @@ final class tx_scheduler_CronCmd_Normalize {
         *              3 => month field
         *              4 => day of week field
         */
-       public static function splitFields($cronCommand) {
+       protected static function splitFields($cronCommand) {
                $fields = explode(' ', $cronCommand);
 
                if (count($fields) !== 5) {
@@ -137,7 +137,7 @@ final class tx_scheduler_CronCmd_Normalize {
         * @param boolean $isMonthField TRUE if month field is handled, FALSE for weekday field
         * @return string Normalized expression
         */
-       public static function normalizeMonthAndWeekdayField($expression, $isMonthField = TRUE) {
+       protected static function normalizeMonthAndWeekdayField($expression, $isMonthField = TRUE) {
                if ((string)$expression === '*') {
                        $fieldValues = '*';
                } else {
@@ -182,7 +182,7 @@ final class tx_scheduler_CronCmd_Normalize {
         * @param integer $upperBound Upper limit of result list
         * @return string Normalized expression
         */
-       public static function normalizeIntegerField($expression, $lowerBound = 0, $upperBound = 59) {
+       protected static function normalizeIntegerField($expression, $lowerBound = 0, $upperBound = 59) {
                if ((string)$expression === '*') {
                        $fieldValues = '*';
                } else {
@@ -252,7 +252,7 @@ final class tx_scheduler_CronCmd_Normalize {
         * @param string $range Integer-integer
         * @return array
         */
-       public static function convertRangeToListOfValues($range) {
+       protected static function convertRangeToListOfValues($range) {
                if (strlen($range) === 0) {
                        throw new InvalidArgumentException(
                                'Unable to convert range to list of values with empty string.',
@@ -313,7 +313,7 @@ final class tx_scheduler_CronCmd_Normalize {
         * @param string $stepExpression Step value expression
         * @return string Comma separated list of valid values
         */
-       public static function reduceListOfValuesByStepValue($stepExpression) {
+       protected static function reduceListOfValuesByStepValue($stepExpression) {
                if (strlen($stepExpression) === 0) {
                        throw new InvalidArgumentException(
                                'Unable to convert step values.',
@@ -395,7 +395,7 @@ final class tx_scheduler_CronCmd_Normalize {
         * @param boolean $isMonth TRUE if a month is handled, FALSE for weekday
         * @return string normalized month or weekday
         */
-       public static function normalizeMonthAndWeekday($expression, $isMonth = TRUE) {
+       protected static function normalizeMonthAndWeekday($expression, $isMonth = TRUE) {
                $expression = $isMonth ? self::normalizeMonth($expression) : self::normalizeWeekday($expression);
 
                return (string)$expression;
@@ -409,7 +409,7 @@ final class tx_scheduler_CronCmd_Normalize {
         * @param string $month Month representation
         * @return integer month integer representation between 1 and 12
         */
-       public static function normalizeMonth($month) {
+       protected static function normalizeMonth($month) {
                $timestamp = strtotime('2010-' . $month . '-01');
 
                        // timestamp must be >= 2010-01-01 and <= 2010-12-01
@@ -431,7 +431,7 @@ final class tx_scheduler_CronCmd_Normalize {
         * @param string $weekday Weekday representation
         * @return integer weekday integer representation between 1 and 7
         */
-       public static function normalizeWeekday($weekday) {
+       protected static function normalizeWeekday($weekday) {
                $normalizedWeekday = FALSE;
 
                        // 0 (sunday) -> 7
index c6b82c5..449171a 100644 (file)
 class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
 
        /**
+        * Create a subclass of tx_scheduler_CronCmd_Normalize with
+        * protected methods made public
+        *
+        * @return string Name of the accessible proxy class
+        */
+       protected function getAccessibleProxy() {
+               $className = 'tx_scheduler_CronCmd_NormalizeAccessibleProxy';
+               if (!class_exists($className, FALSE)) {
+                       eval(
+                               'class ' . $className . ' extends tx_scheduler_CronCmd_Normalize {' .
+                               '  public static function convertKeywordsToCronCommand($cronCommand) {' .
+                               '    return parent::convertKeywordsToCronCommand($cronCommand);' .
+                               '  }' .
+                               '  public static function normalizeFields($cronCommand) {' .
+                               '    return parent::normalizeFields($cronCommand);' .
+                               '  }' .
+                               '  public static function normalizeMonthAndWeekdayField($expression, $isMonthField = TRUE) {' .
+                               '    return parent::normalizeMonthAndWeekdayField($expression, $isMonthField);' .
+                               '  }' .
+                               '  public static function normalizeIntegerField($expression, $lowerBound = 0, $upperBound = 59) {' .
+                               '    return parent::normalizeIntegerField($expression, $lowerBound , $upperBound);' .
+                               '  }' .
+                               '  public static function splitFields($cronCommand) {' .
+                               '    return parent::splitFields($cronCommand);' .
+                               '  }' .
+                               '  public static function convertRangeToListOfValues($range) {' .
+                               '    return parent::convertRangeToListOfValues($range);' .
+                               '  }' .
+                               '  public static function reduceListOfValuesByStepValue($stepExpression) {' .
+                               '    return parent::reduceListOfValuesByStepValue($stepExpression);' .
+                               '  }' .
+                               '  public static function normalizeMonthAndWeekday($expression, $isMonth = TRUE) {' .
+                               '    return parent::normalizeMonthAndWeekday($expression, $isMonth);' .
+                               '  }' .
+                               '  public static function normalizeMonth($month) {' .
+                               '    return parent::normalizeMonth($month);' .
+                               '  }' .
+                               '  public static function normalizeWeekday($weekday) {' .
+                               '    return parent::normalizeWeekday($weekday);' .
+                               '  }' .
+                               '}'
+                       );
+               }
+
+               return $className;
+       }
+
+       /**
         * @return array
         */
        public static function normalizeValidDataProvider() {
@@ -93,7 +141,8 @@ class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
         * @param string $expectedCronCommand Expected result (normalized cron command syntax)
         */
        public function convertKeywordsToCronCommandConvertsValidKeywords($keyword, $expectedCronCommand) {
-               $result = tx_scheduler_CronCmd_Normalize::convertKeywordsToCronCommand($keyword);
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $result = $accessibleProxyClassName::convertKeywordsToCronCommand($keyword);
                $this->assertEquals($expectedCronCommand, $result);
        }
 
@@ -102,7 +151,8 @@ class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
         */
        public function convertKeywordsToCronCommandReturnsUnchangedCommandIfKeywordWasNotFound() {
                $invalidKeyword = 'foo';
-               $result = tx_scheduler_CronCmd_Normalize::convertKeywordsToCronCommand($invalidKeyword);
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $result = $accessibleProxyClassName::convertKeywordsToCronCommand($invalidKeyword);
                $this->assertEquals($invalidKeyword, $result);
        }
 
@@ -126,7 +176,8 @@ class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
         * @param string $expected Expected result (normalized cron command syntax)
         */
        public function normalizeFieldsConvertsField($expression, $expected) {
-               $result = tx_scheduler_CronCmd_Normalize::normalizeFields($expression);
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $result = $accessibleProxyClassName::normalizeFields($expression);
                $this->assertEquals($expected, $result);
        }
 
@@ -165,7 +216,8 @@ class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
         * @param string $expected Expected result (normalized months or weekdays)
         */
        public function normalizeMonthAndWeekdayFieldReturnsNormalizedListForValidExpression($expression, $isMonthField, $expected) {
-               $result = tx_scheduler_CronCmd_Normalize::normalizeMonthAndWeekdayField($expression, $isMonthField);
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $result = $accessibleProxyClassName::normalizeMonthAndWeekdayField($expression, $isMonthField);
                $this->assertSame($expected, $result);
        }
 
@@ -191,7 +243,8 @@ class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
         * @param boolean $isMonthField Flag to designate month field or not
         */
        public function normalizeMonthAndWeekdayFieldThrowsExceptionForInvalidExpression($expression, $isMonthField) {
-               $result = tx_scheduler_CronCmd_Normalize::normalizeMonthAndWeekdayField($expression, $isMonthField);
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $result = $accessibleProxyClassName::normalizeMonthAndWeekdayField($expression, $isMonthField);
        }
 
        /**
@@ -222,7 +275,8 @@ class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
         * @param string $expected Expected result (normalized integer or integer list)
         */
        public function normalizeIntegerFieldReturnsNormalizedListForValidExpression($expression, $expected) {
-               $result = tx_scheduler_CronCmd_Normalize::normalizeIntegerField($expression);
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $result = $accessibleProxyClassName::normalizeIntegerField($expression);
                $this->assertSame($expected, $result);
        }
 
@@ -255,14 +309,16 @@ class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
         *
         */
        public function normalizeIntegerFieldThrowsExceptionForInvalidExpressions($expression, $lowerBound, $upperBound) {
-               tx_scheduler_CronCmd_Normalize::normalizeIntegerField($expression, $lowerBound, $upperBound);
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $accessibleProxyClassName::normalizeIntegerField($expression, $lowerBound, $upperBound);
        }
 
        /**
         * @test
         */
        public function splitFieldsReturnsIntegerArrayWithFieldsSplitByWhitespace() {
-               $result = tx_scheduler_CronCmd_Normalize::splitFields('12,13 * 1-12/2,14 jan fri');
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $result = $accessibleProxyClassName::splitFields('12,13 * 1-12/2,14 jan fri');
                $expectedResult = array(
                        0 => '12,13',
                        1 => '*',
@@ -293,7 +349,8 @@ class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
         * @param string $cronCommand Invalid cron command
         */
        public function splitFieldsThrowsExceptionIfCronCommandDoesNotContainFiveFields($cronCommand) {
-               tx_scheduler_CronCmd_Normalize::splitFields($cronCommand);
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $accessibleProxyClassName::splitFields($cronCommand);
        }
 
        /**
@@ -317,7 +374,8 @@ class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
         * @param string $expected Expected result (normalized range)
         */
        public function convertRangeToListOfValuesReturnsCorrectListForValidRanges($range, $expected) {
-               $result = tx_scheduler_CronCmd_Normalize::convertRangeToListOfValues($range);
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $result = $accessibleProxyClassName::convertRangeToListOfValues($range);
                $this->assertSame($expected, $result);
        }
 
@@ -346,7 +404,8 @@ class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
         * @param string $range Cron command range expression (invalid)
         */
        public function convertRangeToListOfValuesThrowsExceptionForInvalidRanges($range) {
-               tx_scheduler_CronCmd_Normalize::convertRangeToListOfValues($range);
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $accessibleProxyClassName::convertRangeToListOfValues($range);
        }
 
        /**
@@ -368,7 +427,8 @@ class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
         * @param string $expected Expected result (normalized range)
         */
        public function reduceListOfValuesByStepValueReturnsCorrectListOfValues($stepExpression, $expected) {
-               $result = tx_scheduler_CronCmd_Normalize::reduceListOfValuesByStepValue($stepExpression);
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $result = $accessibleProxyClassName::reduceListOfValuesByStepValue($stepExpression);
                $this->assertSame($expected, $result);
        }
 
@@ -396,14 +456,16 @@ class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
         * @param string $stepExpression Cron command step expression (invalid)
         */
        public function reduceListOfValuesByStepValueThrowsExceptionForInvalidStepExpressions($stepExpression) {
-               tx_scheduler_CronCmd_Normalize::reduceListOfValuesByStepValue($stepExpression);
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $accessibleProxyClassName::reduceListOfValuesByStepValue($stepExpression);
        }
 
        /**
         * @test
         */
        public function normalizeMonthAndWeekdayNormalizesAMonth() {
-               $result = tx_scheduler_CronCmd_Normalize::normalizeMonthAndWeekday('feb', TRUE);
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $result = $accessibleProxyClassName::normalizeMonthAndWeekday('feb', TRUE);
                $this->assertSame('2', $result);
        }
 
@@ -411,7 +473,8 @@ class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
         * @test
         */
        public function normalizeMonthAndWeekdayNormalizesAWeekday() {
-               $result = tx_scheduler_CronCmd_Normalize::normalizeMonthAndWeekday('fri', FALSE);
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $result = $accessibleProxyClassName::normalizeMonthAndWeekday('fri', FALSE);
                $this->assertSame('5', $result);
        }
 
@@ -419,7 +482,8 @@ class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
         * @test
         */
        public function normalizeMonthAndWeekdayLeavesValueUnchanged() {
-               $result = tx_scheduler_CronCmd_Normalize::normalizeMonthAndWeekday('2');
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $result = $accessibleProxyClassName::normalizeMonthAndWeekday('2');
                $this->assertSame('2', $result);
        }
 
@@ -455,7 +519,8 @@ class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
         * @param integer $expectedInteger Number of the month
         */
        public function normalizeMonthConvertsName($monthName, $expectedInteger) {
-               $result = tx_scheduler_CronCmd_Normalize::normalizeMonth($monthName);
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $result = $accessibleProxyClassName::normalizeMonth($monthName);
                $this->assertEquals($expectedInteger, $result);
        }
 
@@ -466,7 +531,8 @@ class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
         * @param integer $expectedInteger Number of the month (not used)
         */
        public function normalizeMonthReturnsInteger($monthName, $expectedInteger) {
-               $result = tx_scheduler_CronCmd_Normalize::normalizeMonth($monthName);
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $result = $accessibleProxyClassName::normalizeMonth($monthName);
                $this->assertInternalType(PHPUnit_Framework_Constraint_IsType::TYPE_INT, $result);
        }
 
@@ -504,7 +570,8 @@ class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
         * @param string $invalidMonthName Month name (invalid)
         */
        public function normalizeMonthThrowsExceptionForInvalidMonthRepresentation($invalidMonthName) {
-               tx_scheduler_CronCmd_Normalize::normalizeMonth($invalidMonthName);
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $accessibleProxyClassName::normalizeMonth($invalidMonthName);
        }
 
        /**
@@ -548,7 +615,8 @@ class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
         * @param integer $expectedInteger Number of weekday
         */
        public function normalizeWeekdayConvertsName($weekday, $expectedInteger) {
-               $result = tx_scheduler_CronCmd_Normalize::normalizeWeekday($weekday);
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $result = $accessibleProxyClassName::normalizeWeekday($weekday);
                $this->assertEquals($expectedInteger, $result);
        }
 
@@ -559,7 +627,8 @@ class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
         * @param integer $expectedInteger Number of weekday (not used)
         */
        public function normalizeWeekdayReturnsInteger($weekday, $expectedInteger) {
-               $result = tx_scheduler_CronCmd_Normalize::normalizeWeekday($weekday);
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $result = $accessibleProxyClassName::normalizeWeekday($weekday);
                $this->assertInternalType(PHPUnit_Framework_Constraint_IsType::TYPE_INT, $result);
        }
 
@@ -596,7 +665,8 @@ class tx_scheduler_CronCmd_NormalizeTest extends tx_phpunit_testcase {
         * @param string $weekday Weekday expression (invalid)
         */
        public function normalizeWeekdayThrowsExceptionForInvalidWeekdayRepresentation($weekday) {
-               tx_scheduler_CronCmd_Normalize::normalizeWeekday($weekday);
+               $accessibleProxyClassName = $this->getAccessibleProxy();
+               $accessibleProxyClassName::normalizeWeekday($weekday);
        }
 }
 ?>
\ No newline at end of file