[TASK] Move arguments to initializeArguments() in DateVH in ext:fluid 11/48911/5
authorRoberto Torresani <erreti@gmail.com>
Mon, 11 Jul 2016 19:09:50 +0000 (21:09 +0200)
committerSusanne Moog <typo3@susannemoog.de>
Tue, 19 Jul 2016 06:08:12 +0000 (08:08 +0200)
Move the argument registrations away from the render() method
to initializeArguments(), to prevent any errors with PHP7 and
subclassed ViewHelpers if/when render() method signatures change
Change the UnitTest DateViewHelperTest with setArguments()

Resolves: #77039
Releases: master
Change-Id: Ic8ca0c4b36d0589d884da8e364a204f8e02d0a4b
Reviewed-on: https://review.typo3.org/48911
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Joerg Boesche <typo3@joergboesche.de>
Reviewed-by: Susanne Moog <typo3@susannemoog.de>
Tested-by: Susanne Moog <typo3@susannemoog.de>
typo3/sysext/fluid/Classes/ViewHelpers/Format/DateViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Format/DateViewHelperTest.php

index 5c6ac54..707088c 100644 (file)
@@ -92,18 +92,32 @@ class DateViewHelper extends AbstractViewHelper
     protected $escapeChildren = false;
 
     /**
-     * Render the supplied DateTime object as a formatted date.
+     * Initialize arguments
      *
-     * @param mixed $date either an object implementing DateTimeInterface or a string that is accepted by DateTime constructor
-     * @param string $format Format String which is taken to format the Date/Time
-     * @param mixed $base A base time (an object implementing DateTimeInterface or a string) used if $date is a relative date specification. Defaults to current time.
+     * @return void
+     * @api
+     */
+    public function initializeArguments()
+    {
+        parent::initializeArguments();
+        $this->registerArgument('date', 'mixed', 'Either an object implementing DateTimeInterface or a string that is accepted by DateTime constructor');
+        $this->registerArgument('format', 'string', 'Format String which is taken to format the Date/Time', false, '');
+        $this->registerArgument('base', 'mixed', 'A base time (an object implementing DateTimeInterface or a string) used if $date is a relative date specification. Defaults to current time.');
+    }
+
+    /**
+     * Render the supplied DateTime object as a formatted date.
      *
      * @return string Formatted date
      * @throws Exception
      * @api
      */
-    public function render($date = null, $format = '', $base = null)
+    public function render()
     {
+        $date = $this->arguments['date'];
+        $format = $this->arguments['format'];
+        $base = $this->arguments['base'];
+
         return static::renderStatic(
             array(
                 'date' => $date,
index 7f27bbd..2870270 100644 (file)
@@ -13,14 +13,15 @@ namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format;
  *
  * The TYPO3 project - inspiring people to share!
  */
-use TYPO3\CMS\Core\Tests\UnitTestCase;
 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
+use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
+use TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase;
 use TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper;
 
 /**
  * Test case
  */
-class DateViewHelperTest extends UnitTestCase
+class DateViewHelperTest extends ViewHelperBaseTestcase
 {
     /**
      * @var array Backup of current locale, it is manipulated in tests
@@ -37,6 +38,11 @@ class DateViewHelperTest extends UnitTestCase
      */
     protected $timezone;
 
+    /**
+     * @var DateViewHelper
+     */
+    protected $viewHelper;
+
     protected function setUp()
     {
         parent::setUp();
@@ -49,10 +55,10 @@ class DateViewHelperTest extends UnitTestCase
         );
         $this->timezone = @date_default_timezone_get();
         $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = 'Y-m-d';
-        $this->subject = $this->getAccessibleMock(DateViewHelper::class, array('renderChildren'));
+        $this->viewHelper = new DateViewHelper();
+        $this->injectDependenciesIntoViewHelper($this->viewHelper);
         /** @var RenderingContext $renderingContext */
         $renderingContext = $this->createMock(\TYPO3\CMS\Fluid\Tests\Unit\Core\Rendering\RenderingContextFixture::class);
-        $this->subject->_set('renderingContext', $renderingContext);
     }
 
     protected function tearDown()
@@ -69,7 +75,13 @@ class DateViewHelperTest extends UnitTestCase
      */
     public function viewHelperFormatsDateCorrectly()
     {
-        $actualResult = $this->subject->render(new \DateTime('1980-12-13'));
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'date' => new \DateTime('1980-12-13')
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('1980-12-13', $actualResult);
     }
 
@@ -78,7 +90,13 @@ class DateViewHelperTest extends UnitTestCase
      */
     public function viewHelperFormatsDateStringCorrectly()
     {
-        $actualResult = $this->subject->render('1980-12-13');
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'date' => '1980-12-13'
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('1980-12-13', $actualResult);
     }
 
@@ -87,7 +105,14 @@ class DateViewHelperTest extends UnitTestCase
      */
     public function viewHelperRespectsCustomFormat()
     {
-        $actualResult = $this->subject->render(new \DateTime('1980-02-01'), 'd.m.Y');
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'date' => new \DateTime('1980-02-01'),
+                'format' => 'd.m.Y'
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('01.02.1980', $actualResult);
     }
 
@@ -96,7 +121,14 @@ class DateViewHelperTest extends UnitTestCase
      */
     public function viewHelperSupportsDateTimeImmutable()
     {
-        $actualResult = $this->subject->render(new \DateTimeImmutable('1980-02-01'), 'd.m.Y');
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'date' => new \DateTimeImmutable('1980-02-01'),
+                'format' => 'd.m.Y'
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('01.02.1980', $actualResult);
     }
 
@@ -105,8 +137,17 @@ class DateViewHelperTest extends UnitTestCase
      */
     public function viewHelperReturnsEmptyStringIfChildrenIsNULL()
     {
-        $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(null));
-        $actualResult = $this->subject->render();
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return null;
+            }
+        );
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('', $actualResult);
     }
 
@@ -115,7 +156,13 @@ class DateViewHelperTest extends UnitTestCase
      */
     public function viewHelperReturnsCurrentDateIfEmptyStringIsGiven()
     {
-        $actualResult = $this->subject->render('');
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'date' => ''
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $expectedResult = (new \DateTime())->format('Y-m-d');
         $this->assertEquals($expectedResult, $actualResult);
     }
@@ -125,8 +172,17 @@ class DateViewHelperTest extends UnitTestCase
      */
     public function viewHelperReturnsCurrentDateIfChildrenIsEmptyString()
     {
-        $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(''));
-        $actualResult = $this->subject->render();
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return '';
+            }
+        );
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $expectedResult = (new \DateTime())->format('Y-m-d');
         $this->assertEquals($expectedResult, $actualResult);
     }
@@ -137,7 +193,13 @@ class DateViewHelperTest extends UnitTestCase
     public function viewHelperUsesDefaultIfNoSystemFormatIsAvailable()
     {
         $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = '';
-        $actualResult = $this->subject->render('@1391876733');
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'date' => '@1391876733'
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('2014-02-08', $actualResult);
     }
 
@@ -147,7 +209,13 @@ class DateViewHelperTest extends UnitTestCase
     public function viewHelperUsesSystemFormat()
     {
         $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = 'l, j. M y';
-        $actualResult = $this->subject->render('@1391876733');
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'date' => '@1391876733'
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('Saturday, 8. Feb 14', $actualResult);
     }
 
@@ -156,10 +224,16 @@ class DateViewHelperTest extends UnitTestCase
      */
     public function viewHelperThrowsExceptionWithOriginalMessageIfDateStringCantBeParsed()
     {
-        $this->expectException(\TYPO3\CMS\Fluid\Core\ViewHelper\Exception::class);
+        $this->expectException(Exception::class);
         $this->expectExceptionCode(1241722579);
 
-        $this->subject->render('foo');
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'date' => 'foo'
+            ]
+        );
+        $this->viewHelper->initializeArgumentsAndRender();
     }
 
     /**
@@ -167,8 +241,17 @@ class DateViewHelperTest extends UnitTestCase
      */
     public function viewHelperUsesChildNodesIfDateAttributeIsNotSpecified()
     {
-        $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(new \DateTime('1980-12-13')));
-        $actualResult = $this->subject->render();
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return new \DateTime('1980-12-13');
+            }
+        );
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('1980-12-13', $actualResult);
     }
 
@@ -177,8 +260,17 @@ class DateViewHelperTest extends UnitTestCase
      */
     public function viewHelperUsesChildNodesWithTimestamp()
     {
-        $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue('1359891658' . LF));
-        $actualResult = $this->subject->render();
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return '1359891658' . LF;
+            }
+        );
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('2013-02-03', $actualResult);
     }
 
@@ -187,8 +279,13 @@ class DateViewHelperTest extends UnitTestCase
      */
     public function dateArgumentHasPriorityOverChildNodes()
     {
-        $this->subject->expects($this->never())->method('renderChildren');
-        $actualResult = $this->subject->render('1980-12-12');
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'date' => '1980-12-12'
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('1980-12-12', $actualResult);
     }
 
@@ -197,8 +294,14 @@ class DateViewHelperTest extends UnitTestCase
      */
     public function relativeDateCalculationWorksWithoutBase()
     {
-        $this->subject->expects($this->never())->method('renderChildren');
-        $actualResult = $this->subject->render('now', 'Y');
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'date' => 'now',
+                'format' => 'Y',
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals(date('Y'), $actualResult);
     }
 
@@ -207,8 +310,15 @@ class DateViewHelperTest extends UnitTestCase
      */
     public function baseArgumentIsConsideredForRelativeDate()
     {
-        $this->subject->expects($this->never())->method('renderChildren');
-        $actualResult = $this->subject->render('-1 year', 'Y', '2017-01-01');
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'date' => '-1 year',
+                'format' => 'Y',
+                'base' => '2017-01-01'
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('2016', $actualResult);
     }
 
@@ -217,8 +327,15 @@ class DateViewHelperTest extends UnitTestCase
      */
     public function baseArgumentAsDateTimeIsConsideredForRelativeDate()
     {
-        $this->subject->expects($this->never())->method('renderChildren');
-        $actualResult = $this->subject->render('-1 year', 'Y', new \DateTime('2017-01-01'));
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'date' => '-1 year',
+                'format' => 'Y',
+                'base' => new \DateTime('2017-01-01')
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('2016', $actualResult);
     }
 
@@ -227,8 +344,15 @@ class DateViewHelperTest extends UnitTestCase
      */
     public function baseArgumentDoesNotAffectAbsoluteTime()
     {
-        $this->subject->expects($this->never())->method('renderChildren');
-        $actualResult = $this->subject->render('@1435784732', 'Y', 1485907200); // somewhere in 2017
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'date' => '@1435784732',
+                'format' => 'Y',
+                'base' => 1485907200
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('2015', $actualResult);
     }
 
@@ -261,7 +385,15 @@ class DateViewHelperTest extends UnitTestCase
         $format = 'Y-m-d H:i';
 
         date_default_timezone_set($timezone);
-        $this->assertEquals($expected, $this->subject->render($date, $format));
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'date' => $date,
+                'format' => $format
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
+        $this->assertEquals($expected, $actualResult);
     }
 
     /**
@@ -305,7 +437,15 @@ class DateViewHelperTest extends UnitTestCase
         $format = 'Y-m-d H:i';
 
         date_default_timezone_set($timeZone);
-        $this->assertEquals($expected, $this->subject->render($date, $format));
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'date' => $date,
+                'format' => $format
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
+        $this->assertEquals($expected, $actualResult);
     }
 
     /**
@@ -342,7 +482,15 @@ class DateViewHelperTest extends UnitTestCase
             $this->markTestSkipped('Locale ' . $locale . ' is not available.');
         }
         $this->setCustomLocale($locale);
-        $this->assertEquals($expected, $this->subject->render($timestamp, $format));
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'date' => $timestamp,
+                'format' => $format
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
+        $this->assertEquals($expected, $actualResult);
     }
 
     /**