[TASK] Make Format/CurrencyViewHelper compilable 36/37636/2
authorAnja Leichsenring <aleichsenring@ab-softlab.de>
Sun, 8 Mar 2015 13:47:38 +0000 (14:47 +0100)
committerChristian Kuhn <lolli@schwarzbu.ch>
Thu, 12 Mar 2015 11:23:51 +0000 (12:23 +0100)
Change-Id: Ibc3a46edb317e6a8cfd17192ace88b7684280468
Resolves: #65596
Releases: master
Reviewed-on: http://review.typo3.org/37636
Reviewed-by: Michael Oehlhof <typo3@oehlhof.de>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/fluid/Classes/ViewHelpers/Format/CurrencyViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Format/CurrencyViewHelperTest.php

index 341dd32..15491ca 100644 (file)
@@ -10,6 +10,10 @@ namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
  *                                                                        *
  * The TYPO3 project - inspiring people to share!                         *
  *                                                                        */
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
+use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
+
 /**
  * Formats a given float to a currency representation.
  *
@@ -39,7 +43,7 @@ namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
  *
  * @api
  */
-class CurrencyViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
+class CurrencyViewHelper extends AbstractViewHelper implements CompilableInterface {
 
        /**
         * @param string $currencySign (optional) The currency sign, eg $ or €.
@@ -52,7 +56,36 @@ class CurrencyViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHe
         * @api
         */
        public function render($currencySign = '', $decimalSeparator = ',', $thousandsSeparator = '.', $prependCurrency = FALSE, $separateCurrency = TRUE, $decimals = 2) {
-               $floatToFormat = $this->renderChildren();
+               return self::renderStatic(
+                       array(
+                               'currencySign' => $currencySign,
+                               'decimalSeparator' => $decimalSeparator,
+                               'thousandsSeparator' => $thousandsSeparator,
+                               'prependCurrency' => $prependCurrency,
+                               'separateCurrency' => $separateCurrency,
+                               'decimals' => $decimals
+                       ),
+                       $this->buildRenderChildrenClosure(),
+                       $this->renderingContext
+               );
+       }
+
+       /**
+        * @param array $arguments
+        * @param callable $renderChildrenClosure
+        * @param RenderingContextInterface $renderingContext
+        *
+        * @return string
+        */
+       static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
+               $currencySign = $arguments['currencySign'];
+               $decimalSeparator = $arguments['decimalSeparator'];
+               $thousandsSeparator = $arguments['thousandsSeparator'];
+               $prependCurrency = $arguments['prependCurrency'];
+               $separateCurrency = $arguments['separateCurrency'];
+               $decimals = $arguments['decimals'];
+
+               $floatToFormat = $renderChildrenClosure();
                if (empty($floatToFormat)) {
                        $floatToFormat = 0.0;
                } else {
index 8cb5564..c17bf2f 100644 (file)
@@ -10,6 +10,8 @@ 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\ViewHelpers\Format\CurrencyViewHelper;
 
 /**
  * Test case
@@ -17,12 +19,23 @@ namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format;
 class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
 
        /**
+        * @var CurrencyViewHelper
+        */
+       protected $subject;
+
+       public function setUp() {
+               $this->subject = $this->getAccessibleMock(CurrencyViewHelper::class, array('renderChildren'));
+               /** @var RenderingContext $renderingContext */
+               $renderingContext = $this->getMock(RenderingContext::class);
+               $this->subject->_set('renderingContext', $renderingContext);
+       }
+
+       /**
         * @test
         */
        public function viewHelperRoundsFloatCorrectly() {
-               $viewHelper = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper::class, array('renderChildren'));
-               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(123.456));
-               $actualResult = $viewHelper->render();
+               $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(123.456));
+               $actualResult = $this->subject->render();
                $this->assertEquals('123,46', $actualResult);
        }
 
@@ -30,9 +43,8 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function viewHelperRendersCurrencySign() {
-               $viewHelper = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper::class, array('renderChildren'));
-               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(123));
-               $actualResult = $viewHelper->render('foo');
+               $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(123));
+               $actualResult = $this->subject->render('foo');
                $this->assertEquals('123,00 foo', $actualResult);
        }
 
@@ -40,9 +52,8 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function viewHelperRendersPrependedCurrencySign() {
-               $viewHelper = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper::class, array('renderChildren'));
-               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(123));
-               $actualResult = $viewHelper->render('foo', ',', '.', TRUE);
+               $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(123));
+               $actualResult = $this->subject->render('foo', ',', '.', TRUE);
                $this->assertEquals('foo 123,00', $actualResult);
        }
 
@@ -50,9 +61,8 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function viewHelperRespectsCurrencySeparator() {
-               $viewHelper = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper::class, array('renderChildren'));
-               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(123));
-               $actualResult = $viewHelper->render('foo', ',', '.', TRUE, FALSE);
+               $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(123));
+               $actualResult = $this->subject->render('foo', ',', '.', TRUE, FALSE);
                $this->assertEquals('foo123,00', $actualResult);
        }
 
@@ -60,9 +70,8 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function viewHelperRespectsDecimalSeparator() {
-               $viewHelper = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper::class, array('renderChildren'));
-               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(12345));
-               $actualResult = $viewHelper->render('', '|');
+               $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(12345));
+               $actualResult = $this->subject->render('', '|');
                $this->assertEquals('12.345|00', $actualResult);
        }
 
@@ -70,9 +79,8 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function viewHelperRespectsThousandsSeparator() {
-               $viewHelper = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper::class, array('renderChildren'));
-               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(12345));
-               $actualResult = $viewHelper->render('', ',', '|');
+               $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(12345));
+               $actualResult = $this->subject->render('', ',', '|');
                $this->assertEquals('12|345,00', $actualResult);
        }
 
@@ -80,9 +88,8 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function viewHelperRendersNullValues() {
-               $viewHelper = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper::class, array('renderChildren'));
-               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(NULL));
-               $actualResult = $viewHelper->render();
+               $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(NULL));
+               $actualResult = $this->subject->render();
                $this->assertEquals('0,00', $actualResult);
        }
 
@@ -90,9 +97,8 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function viewHelperRendersEmptyString() {
-               $viewHelper = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper::class, array('renderChildren'));
-               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(''));
-               $actualResult = $viewHelper->render();
+               $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(''));
+               $actualResult = $this->subject->render();
                $this->assertEquals('0,00', $actualResult);
        }
 
@@ -100,9 +106,8 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function viewHelperRendersZeroValues() {
-               $viewHelper = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper::class, array('renderChildren'));
-               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(0));
-               $actualResult = $viewHelper->render();
+               $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(0));
+               $actualResult = $this->subject->render();
                $this->assertEquals('0,00', $actualResult);
        }
 
@@ -110,9 +115,8 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function viewHelperRendersNegativeAmounts() {
-               $viewHelper = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper::class, array('renderChildren'));
-               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(-123.456));
-               $actualResult = $viewHelper->render();
+               $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(-123.456));
+               $actualResult = $this->subject->render();
                $this->assertEquals('-123,46', $actualResult);
        }
 
@@ -120,9 +124,8 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function viewHelperRendersStringsToZeroValueFloat() {
-               $viewHelper = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper::class, array('renderChildren'));
-               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('TYPO3'));
-               $actualResult = $viewHelper->render();
+               $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue('TYPO3'));
+               $actualResult = $this->subject->render();
                $this->assertEquals('0,00', $actualResult);
        }
 
@@ -130,9 +133,8 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function viewHelperRendersCommaValuesToValueBeforeComma() {
-               $viewHelper = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper::class, array('renderChildren'));
-               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('12,34.00'));
-               $actualResult = $viewHelper->render();
+               $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue('12,34.00'));
+               $actualResult = $this->subject->render();
                $this->assertEquals('12,00', $actualResult);
        }
 
@@ -140,9 +142,8 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function viewHelperRendersValuesWithoutDecimals() {
-               $viewHelper = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper::class, array('renderChildren'));
-               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('54321'));
-               $actualResult = $viewHelper->render('', ',', '.', FALSE, TRUE, 0);
+               $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue('54321'));
+               $actualResult = $this->subject->render('', ',', '.', FALSE, TRUE, 0);
                $this->assertEquals('54.321', $actualResult);
        }
 
@@ -150,9 +151,8 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function viewHelperRendersThreeDecimals() {
-               $viewHelper = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper::class, array('renderChildren'));
-               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('54321'));
-               $actualResult = $viewHelper->render('', ',', '.', FALSE, TRUE, 3);
+               $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue('54321'));
+               $actualResult = $this->subject->render('', ',', '.', FALSE, TRUE, 3);
                $this->assertEquals('54.321,000', $actualResult);
        }