[TASK] Make NumberViewHelper compilable 61/39161/6
authorMarkus Sommer <markussom@posteo.de>
Fri, 1 May 2015 16:21:52 +0000 (18:21 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Fri, 1 May 2015 18:24:40 +0000 (20:24 +0200)
Resolves: #66712
Releases: master
Change-Id: I49a446ecabf0893e6231e687744cfad0fada1135
Reviewed-on: http://review.typo3.org/39161
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
Reviewed-by: Jan Helke <typo3@helke.de>
Tested-by: Jan Helke <typo3@helke.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/fluid/Classes/ViewHelpers/Format/NumberViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Format/NumberViewHelperTest.php

index b4f2137..04d5f82 100644 (file)
@@ -11,8 +11,13 @@ 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 number with custom precision, decimal point and grouped thousands.
+ *
  * @see http://www.php.net/manual/en/function.number-format.php
  *
  * = Examples =
@@ -33,7 +38,7 @@ namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
  *
  * @api
  */
-class NumberViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
+class NumberViewHelper extends AbstractViewHelper implements CompilableInterface {
 
        /**
         * Format the numeric value as a number with grouped thousands, decimal point and
@@ -42,12 +47,36 @@ class NumberViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelp
         * @param int $decimals The number of digits after the decimal point
         * @param string $decimalSeparator The decimal point character
         * @param string $thousandsSeparator The character for grouping the thousand digits
+        *
         * @return string The formatted number
         * @api
         */
        public function render($decimals = 2, $decimalSeparator = '.', $thousandsSeparator = ',') {
-               $stringToFormat = $this->renderChildren();
-               return number_format($stringToFormat, $decimals, $decimalSeparator, $thousandsSeparator);
+               return self::renderStatic(
+                       array(
+                               'decimals' => $decimals,
+                               'decimalSeparator' => $decimalSeparator,
+                               'thousandsSeparator' => $thousandsSeparator,
+                       ),
+                       $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) {
+
+               $decimals = $arguments['decimals'];
+               $decimalSeparator = $arguments['decimalSeparator'];
+               $thousandsSeparator = $arguments['thousandsSeparator'];
+
+               $stringToFormat = $renderChildrenClosure();
+               return number_format($stringToFormat, $decimals, $decimalSeparator, $thousandsSeparator);
+       }
 }
index fb34fc4..ccd3d3d 100644 (file)
@@ -10,30 +10,39 @@ 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\ViewHelpers\Format\NumberViewHelper;
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
 
 /**
  * Test case
  */
-class NumberViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
+class NumberViewHelperTest extends UnitTestCase {
+
+       /**
+        * @var \PHPUnit_Framework_MockObject_MockObject|NumberViewHelper
+        */
+       protected $fixture;
+
+       public function setUp() {
+               $this->fixture = $this->getMock(NumberViewHelper::class, array('renderChildren'));
+               $this->fixture->expects($this->once())->method('renderChildren')->will($this->returnValue(10000.0 / 3.0));
+               $renderingContext = $this->getMock(RenderingContext::class);
+               $this->fixture->setRenderingContext($renderingContext);
+       }
 
        /**
         * @test
         */
        public function formatNumberDefaultsToEnglishNotationWithTwoDecimals() {
-               $viewHelper = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Format\NumberViewHelper::class, array('renderChildren'));
-               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(10000.0 / 3.0));
-               $actualResult = $viewHelper->render();
-               $this->assertEquals('3,333.33', $actualResult);
+               $this->assertEquals('3,333.33', $this->fixture->render());
        }
 
        /**
         * @test
         */
        public function formatNumberWithDecimalsDecimalPointAndSeparator() {
-               $viewHelper = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Format\NumberViewHelper::class, array('renderChildren'));
-               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(10000.0 / 3.0));
-               $actualResult = $viewHelper->render(3, ',', '.');
-               $this->assertEquals('3.333,333', $actualResult);
+               $this->assertEquals('3.333,333', $this->fixture->render(3, ',', '.'));
        }
 
 }