[TASK] Move arguments to initializeArguments() in CurrencyVH in ext:fluid 20/48920/7
authorRoberto Torresani <erreti@gmail.com>
Tue, 12 Jul 2016 09:39:37 +0000 (11:39 +0200)
committerWouter Wolters <typo3@wouterwolters.nl>
Tue, 19 Jul 2016 17:30:48 +0000 (19:30 +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 CurrencyViewHelperTest with setArguments()

Resolves: #77056
Releases: master
Change-Id: Ia754cc36d071d3108548927dad164d684eae782c
Reviewed-on: https://review.typo3.org/48920
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
typo3/sysext/fluid/Classes/ViewHelpers/Format/CurrencyViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Format/CurrencyViewHelperTest.php

index b2725cb..36c88b8 100644 (file)
@@ -56,26 +56,30 @@ class CurrencyViewHelper extends AbstractViewHelper
     protected $escapeChildren = false;
 
     /**
-     * @param string $currencySign (optional) The currency sign, eg $ or €.
-     * @param string $decimalSeparator (optional) The separator for the decimal point.
-     * @param string $thousandsSeparator (optional) The thousands separator.
-     * @param bool $prependCurrency (optional) Select if the curreny sign should be prepended
-     * @param bool $separateCurrency (optional) Separate the currency sign from the number by a single space, defaults to true due to backwards compatibility
-     * @param int $decimals (optional) Set decimals places.
+     * Initialize arguments.
+     *
+     * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
+     */
+    public function initializeArguments()
+    {
+        parent::initializeArguments();
+        $this->registerArgument('currencySign', 'string', 'The currency sign, eg $ or €.', false, '');
+        $this->registerArgument('decimalSeparator', 'string', 'The separator for the decimal point.', false, ',');
+        $this->registerArgument('thousandsSeparator', 'string', 'The thousands separator.', false, '.');
+        $this->registerArgument('prependCurrency', 'bool', 'Select if the currency sign should be prepended', false, false);
+        $this->registerArgument('separateCurrency', 'bool', 'Separate the currency sign from the number by a single space, defaults to true due to backwards compatibility', false, true);
+        $this->registerArgument('decimals', 'int', 'Set decimals places.', false, 2);
+    }
+
+    /**
+     *
      * @return string the formatted amount.
      * @api
      */
-    public function render($currencySign = '', $decimalSeparator = ',', $thousandsSeparator = '.', $prependCurrency = false, $separateCurrency = true, $decimals = 2)
+    public function render()
     {
         return static::renderStatic(
-            array(
-                'currencySign' => $currencySign,
-                'decimalSeparator' => $decimalSeparator,
-                'thousandsSeparator' => $thousandsSeparator,
-                'prependCurrency' => $prependCurrency,
-                'separateCurrency' => $separateCurrency,
-                'decimals' => $decimals
-            ),
+            $this->arguments,
             $this->buildRenderChildrenClosure(),
             $this->renderingContext
         );
index 312f5cd..6cb732a 100644 (file)
@@ -13,25 +13,24 @@ namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format;
  *
  * The TYPO3 project - inspiring people to share!
  */
-use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
+use TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase;
 use TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper;
 
 /**
  * Test case
  */
-class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
+class CurrencyViewHelperTest extends ViewHelperBaseTestcase
 {
     /**
      * @var CurrencyViewHelper
      */
-    protected $subject;
+    protected $viewHelper;
 
     protected function setUp()
     {
-        $this->subject = $this->getAccessibleMock(CurrencyViewHelper::class, array('renderChildren'));
-        /** @var RenderingContext $renderingContext */
-        $renderingContext = $this->createMock(\TYPO3\CMS\Fluid\Tests\Unit\Core\Rendering\RenderingContextFixture::class);
-        $this->subject->_set('renderingContext', $renderingContext);
+        parent::setUp();
+        $this->viewHelper = new CurrencyViewHelper();
+        $this->injectDependenciesIntoViewHelper($this->viewHelper);
     }
 
     /**
@@ -39,8 +38,17 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function viewHelperRoundsFloatCorrectly()
     {
-        $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(123.456));
-        $actualResult = $this->subject->render();
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return 123.456;
+            }
+        );
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('123,46', $actualResult);
     }
 
@@ -49,8 +57,18 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function viewHelperRendersCurrencySign()
     {
-        $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(123));
-        $actualResult = $this->subject->render('foo');
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return 123;
+            }
+        );
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'currencySign' => 'foo'
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('123,00 foo', $actualResult);
     }
 
@@ -59,8 +77,21 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function viewHelperRendersPrependedCurrencySign()
     {
-        $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(123));
-        $actualResult = $this->subject->render('foo', ',', '.', true);
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return 123;
+            }
+        );
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'currencySign' => 'foo',
+                'decimalSeparator' => ',',
+                'thousandsSeparator' => '.',
+                'prependCurrency' => true
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('foo 123,00', $actualResult);
     }
 
@@ -69,8 +100,22 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function viewHelperRespectsCurrencySeparator()
     {
-        $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(123));
-        $actualResult = $this->subject->render('foo', ',', '.', true, false);
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return 123;
+            }
+        );
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'currencySign' => 'foo',
+                'decimalSeparator' => ',',
+                'thousandsSeparator' => '.',
+                'prependCurrency' => true,
+                'separateCurrency' => false
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('foo123,00', $actualResult);
     }
 
@@ -79,8 +124,19 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function viewHelperRespectsDecimalSeparator()
     {
-        $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(12345));
-        $actualResult = $this->subject->render('', '|');
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return 12345;
+            }
+        );
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'currencySign' => '',
+                'decimalSeparator' => '|'
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('12.345|00', $actualResult);
     }
 
@@ -89,8 +145,20 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function viewHelperRespectsThousandsSeparator()
     {
-        $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(12345));
-        $actualResult = $this->subject->render('', ',', '|');
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return 12345;
+            }
+        );
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'currencySign' => '',
+                'decimalSeparator' => ',',
+                'thousandsSeparator' => '|'
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('12|345,00', $actualResult);
     }
 
@@ -99,8 +167,17 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function viewHelperRendersNullValues()
     {
-        $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('0,00', $actualResult);
     }
 
@@ -109,8 +186,17 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function viewHelperRendersEmptyString()
     {
-        $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();
         $this->assertEquals('0,00', $actualResult);
     }
 
@@ -119,8 +205,17 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function viewHelperRendersZeroValues()
     {
-        $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(0));
-        $actualResult = $this->subject->render();
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return 0;
+            }
+        );
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('0,00', $actualResult);
     }
 
@@ -129,8 +224,17 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function viewHelperRendersNegativeAmounts()
     {
-        $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(-123.456));
-        $actualResult = $this->subject->render();
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return -123.456;
+            }
+        );
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('-123,46', $actualResult);
     }
 
@@ -139,8 +243,17 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function viewHelperRendersStringsToZeroValueFloat()
     {
-        $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue('TYPO3'));
-        $actualResult = $this->subject->render();
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return 'TYPO3';
+            }
+        );
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('0,00', $actualResult);
     }
 
@@ -149,8 +262,17 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function viewHelperRendersCommaValuesToValueBeforeComma()
     {
-        $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue('12,34.00'));
-        $actualResult = $this->subject->render();
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return '12,34.00';
+            }
+        );
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('12,00', $actualResult);
     }
 
@@ -159,8 +281,23 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function viewHelperRendersValuesWithoutDecimals()
     {
-        $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue('54321'));
-        $actualResult = $this->subject->render('', ',', '.', false, true, 0);
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return '54321';
+            }
+        );
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'currencySign' => '',
+                'decimalSeparator' => ',',
+                'thousandsSeparator' => '.',
+                'prependCurrency' => false,
+                'separateCurrency' => true,
+                'decimals' => 0
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('54.321', $actualResult);
     }
 
@@ -169,8 +306,23 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function viewHelperRendersThreeDecimals()
     {
-        $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue('54321'));
-        $actualResult = $this->subject->render('', ',', '.', false, true, 3);
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return '54321';
+            }
+        );
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'currencySign' => '',
+                'decimalSeparator' => ',',
+                'thousandsSeparator' => '.',
+                'prependCurrency' => false,
+                'separateCurrency' => true,
+                'decimals' => 3
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('54.321,000', $actualResult);
     }
 }