[FEATURE] Add support to CurrencyViewHelper for mdash 31/57331/6
authorTimo Poppinga <timo.poppinga@zdrei.com>
Fri, 22 Jun 2018 23:45:00 +0000 (01:45 +0200)
committerSusanne Moog <susanne.moog@typo3.org>
Sat, 23 Jun 2018 14:41:12 +0000 (16:41 +0200)
If the option `useDash` is set and a round value is given,
the decimal place is rendered as a dash.
Do tests refactor, using value data provider for all tests.

Change-Id: Idd1a0216c7ff58394cf676bf14fb2042c823e4d6
Resolves: #85356
Releases: master
Reviewed-on: https://review.typo3.org/57331
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Marcus Schwemer <ms@schwemer.de>
Tested-by: Marcus Schwemer <ms@schwemer.de>
Reviewed-by: Frans Saris <franssaris@gmail.com>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
typo3/sysext/core/Documentation/Changelog/master/Feature-57331-SupportMdashInCurrencyViewHelper.rst [new file with mode: 0644]
typo3/sysext/fluid/Classes/ViewHelpers/Format/CurrencyViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Format/CurrencyViewHelperTest.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-57331-SupportMdashInCurrencyViewHelper.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-57331-SupportMdashInCurrencyViewHelper.rst
new file mode 100644 (file)
index 0000000..67f97f7
--- /dev/null
@@ -0,0 +1,27 @@
+.. include:: ../../Includes.txt
+
+=====================================================
+Feature: #57331 - Support dash in CurrencyViewHelper
+=====================================================
+
+See :issue:`57331`
+
+Description
+===========
+
+The `useDash` option is added to the CurrencyViewHelper.
+
+
+Impact
+======
+
+If the option `useDash` is set and a round value is given, the decimal place is rendered as a dash.
+
+Example:
+
+.. code-block:: html
+
+       <!-- Renders "54321.-" -->
+       <f:format.currency useDash="1">54321.00</f:format.currency>
+
+.. index:: Fluid, ext:fluid
index 1ec6d76..20d61d3 100644 (file)
@@ -71,6 +71,7 @@ class CurrencyViewHelper extends AbstractViewHelper
         $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);
+        $this->registerArgument('useDash', 'bool', 'Use the dash instead of decimal 00', false, false);
     }
 
     /**
@@ -90,6 +91,7 @@ class CurrencyViewHelper extends AbstractViewHelper
         $prependCurrency = $arguments['prependCurrency'];
         $separateCurrency = $arguments['separateCurrency'];
         $decimals = $arguments['decimals'];
+        $useDash = $arguments['useDash'];
 
         $floatToFormat = $renderChildrenClosure();
         if (empty($floatToFormat)) {
@@ -98,6 +100,11 @@ class CurrencyViewHelper extends AbstractViewHelper
             $floatToFormat = (float)$floatToFormat;
         }
         $output = number_format($floatToFormat, $decimals, $decimalSeparator, $thousandsSeparator);
+
+        if ($useDash && $floatToFormat === floor($floatToFormat)) {
+            $output = explode($decimalSeparator, $output)[0] . $decimalSeparator . '—';
+        }
+
         if ($currencySign !== '') {
             $currencySeparator = $separateCurrency ? ' ' : '';
             if ($prependCurrency === true) {
index ce77247..9c4a11a 100644 (file)
@@ -35,295 +35,179 @@ class CurrencyViewHelperTest extends ViewHelperBaseTestcase
     }
 
     /**
-     * @test
-     */
-    public function viewHelperRoundsFloatCorrectly()
-    {
-        $this->viewHelper->setRenderChildrenClosure(
-            function () {
-                return 123.456;
-            }
-        );
-        $this->setArgumentsUnderTest(
-            $this->viewHelper,
-            [
-            ]
-        );
-        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
-        $this->assertEquals('123,46', $actualResult);
-    }
-
-    /**
-     * @test
-     */
-    public function viewHelperRendersCurrencySign()
-    {
-        $this->viewHelper->setRenderChildrenClosure(
-            function () {
-                return 123;
-            }
-        );
-        $this->setArgumentsUnderTest(
-            $this->viewHelper,
-            [
-                'currencySign' => 'foo'
-            ]
-        );
-        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
-        $this->assertEquals('123,00 foo', $actualResult);
-    }
-
-    /**
-     * @test
-     */
-    public function viewHelperRendersPrependedCurrencySign()
-    {
-        $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);
-    }
-
-    /**
-     * @test
-     */
-    public function viewHelperRespectsCurrencySeparator()
-    {
-        $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);
-    }
-
-    /**
-     * @test
+     * @return array
      */
-    public function viewHelperRespectsDecimalSeparator()
+    public function valueDataProvider()
     {
-        $this->viewHelper->setRenderChildrenClosure(
-            function () {
-                return 12345;
-            }
-        );
-        $this->setArgumentsUnderTest(
-            $this->viewHelper,
-            [
-                'currencySign' => '',
-                'decimalSeparator' => '|'
-            ]
-        );
-        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
-        $this->assertEquals('12.345|00', $actualResult);
+        return [
+            'rounds float correctly' => [
+                'value' => 123.456,
+                'arguments' =>
+                    [
+                    ],
+                'expected' => '123,46',
+            ],
+            'currency sign' => [
+                'value' => 123,
+                'arguments' =>
+                    [
+                        'currencySign' => 'foo'
+                    ],
+                'expected' => '123,00 foo',
+            ],
+            'prepended currency sign' => [
+                'value' => 123,
+                'arguments' =>
+                    [
+                        'currencySign' => 'foo',
+                        'decimalSeparator' => ',',
+                        'thousandsSeparator' => '.',
+                        'prependCurrency' => true
+                    ],
+                'expected' => 'foo 123,00',
+            ],
+            'respects currency separator' =>[
+                'value' => 123,
+                'arguments' =>
+                    [
+                        'currencySign' => 'foo',
+                        'decimalSeparator' => ',',
+                        'thousandsSeparator' => '.',
+                        'prependCurrency' => true,
+                        'separateCurrency' => false
+                    ],
+                'expected' => 'foo123,00',
+            ],
+            'respects decimal separator' => [
+                'value' => 12345,
+                'arguments' =>
+                    [
+                        'currencySign' => '',
+                        'decimalSeparator' => '|'
+                    ],
+                'expected' => '12.345|00',
+            ],
+            'respects thousands separator' => [
+                'value' => 12345,
+                'arguments' =>
+                    [
+                        'currencySign' => '',
+                        'decimalSeparator' => ',',
+                        'thousandsSeparator' => '|'
+                    ],
+                'expected' => '12|345,00',
+            ],
+            'null values' => [
+                'value' => null,
+                'arguments' =>
+                    [
+                    ],
+                'expected' => '0,00',
+            ],
+            'empty string' => [
+                'value' => '',
+                'arguments' =>
+                    [
+                    ],
+                'expected' => '0,00',
+            ],
+            'zero values' => [
+                'value' => 0,
+                'arguments' =>
+                    [
+                    ],
+                'expected' => '0,00',
+            ],
+            'negative amounts' => [
+                'value' => '-123.456',
+                'arguments' =>
+                    [
+                    ],
+                'expected' => '-123,46',
+            ],
+            'strings to zero value float' => [
+                'value' => 'TYPO3',
+                'arguments' =>
+                    [
+                    ],
+                'expected' => '0,00',
+            ],
+            'comma values to value before comma' => [
+                'value' => '12,34.00',
+                'arguments' =>
+                    [
+                    ],
+                'expected' => '12,00',
+            ],
+            'without decimals' => [
+                'value' => '54321',
+                'arguments' =>
+                    [
+                        'currencySign' => '',
+                        'decimalSeparator' => ',',
+                        'thousandsSeparator' => '.',
+                        'prependCurrency' => false,
+                        'separateCurrency' => true,
+                        'decimals' => 0
+                    ],
+                'expected' => '54.321',
+            ],
+            'three decimals' => [
+                'value' => '54321',
+                'arguments' =>
+                    [
+                        'currencySign' => '',
+                        'decimalSeparator' => ',',
+                        'thousandsSeparator' => '.',
+                        'prependCurrency' => false,
+                        'separateCurrency' => true,
+                        'decimals' => 3
+                    ],
+                'expected' => '54.321,000',
+            ],
+            'with dash' => [
+                'value' => '54321.00',
+                'arguments' =>
+                    [
+                        'decimalSeparator' => ',',
+                        'thousandsSeparator' => '.',
+                        'prependCurrency' => false,
+                        'separateCurrency' => true,
+                        'useDash' => true,
+                    ],
+                'expected' => '54.321,—',
+            ],
+            'without dash' => [
+                'value' => '54321.45',
+                'arguments' =>
+                    [
+                        'decimalSeparator' => ',',
+                        'thousandsSeparator' => '.',
+                        'prependCurrency' => false,
+                        'separateCurrency' => true,
+                        'useDash' => true,
+                    ],
+                'expected' => '54.321,45',
+            ],
+        ];
     }
 
     /**
+     * @param $value
+     * @param array $arguments
+     * @param string $expected
      * @test
+     * @dataProvider valueDataProvider
      */
-    public function viewHelperRespectsThousandsSeparator()
+    public function viewHelperRender($value, array $arguments, string $expected)
     {
         $this->viewHelper->setRenderChildrenClosure(
-            function () {
-                return 12345;
+            function () use ($value) {
+                return $value;
             }
         );
-        $this->setArgumentsUnderTest(
-            $this->viewHelper,
-            [
-                'currencySign' => '',
-                'decimalSeparator' => ',',
-                'thousandsSeparator' => '|'
-            ]
-        );
-        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
-        $this->assertEquals('12|345,00', $actualResult);
-    }
-
-    /**
-     * @test
-     */
-    public function viewHelperRendersNullValues()
-    {
-        $this->viewHelper->setRenderChildrenClosure(
-            function () {
-                return null;
-            }
-        );
-        $this->setArgumentsUnderTest(
-            $this->viewHelper,
-            [
-            ]
-        );
-        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
-        $this->assertEquals('0,00', $actualResult);
-    }
-
-    /**
-     * @test
-     */
-    public function viewHelperRendersEmptyString()
-    {
-        $this->viewHelper->setRenderChildrenClosure(
-            function () {
-                return '';
-            }
-        );
-        $this->setArgumentsUnderTest(
-            $this->viewHelper,
-            [
-            ]
-        );
-        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
-        $this->assertEquals('0,00', $actualResult);
-    }
-
-    /**
-     * @test
-     */
-    public function viewHelperRendersZeroValues()
-    {
-        $this->viewHelper->setRenderChildrenClosure(
-            function () {
-                return 0;
-            }
-        );
-        $this->setArgumentsUnderTest(
-            $this->viewHelper,
-            [
-            ]
-        );
-        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
-        $this->assertEquals('0,00', $actualResult);
-    }
-
-    /**
-     * @test
-     */
-    public function viewHelperRendersNegativeAmounts()
-    {
-        $this->viewHelper->setRenderChildrenClosure(
-            function () {
-                return -123.456;
-            }
-        );
-        $this->setArgumentsUnderTest(
-            $this->viewHelper,
-            [
-            ]
-        );
-        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
-        $this->assertEquals('-123,46', $actualResult);
-    }
-
-    /**
-     * @test
-     */
-    public function viewHelperRendersStringsToZeroValueFloat()
-    {
-        $this->viewHelper->setRenderChildrenClosure(
-            function () {
-                return 'TYPO3';
-            }
-        );
-        $this->setArgumentsUnderTest(
-            $this->viewHelper,
-            [
-            ]
-        );
-        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
-        $this->assertEquals('0,00', $actualResult);
-    }
-
-    /**
-     * @test
-     */
-    public function viewHelperRendersCommaValuesToValueBeforeComma()
-    {
-        $this->viewHelper->setRenderChildrenClosure(
-            function () {
-                return '12,34.00';
-            }
-        );
-        $this->setArgumentsUnderTest(
-            $this->viewHelper,
-            [
-            ]
-        );
-        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
-        $this->assertEquals('12,00', $actualResult);
-    }
-
-    /**
-     * @test
-     */
-    public function viewHelperRendersValuesWithoutDecimals()
-    {
-        $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);
-    }
-
-    /**
-     * @test
-     */
-    public function viewHelperRendersThreeDecimals()
-    {
-        $this->viewHelper->setRenderChildrenClosure(
-            function () {
-                return '54321';
-            }
-        );
-        $this->setArgumentsUnderTest(
-            $this->viewHelper,
-            [
-                'currencySign' => '',
-                'decimalSeparator' => ',',
-                'thousandsSeparator' => '.',
-                'prependCurrency' => false,
-                'separateCurrency' => true,
-                'decimals' => 3
-            ]
-        );
+        $this->setArgumentsUnderTest($this->viewHelper, $arguments);
         $actualResult = $this->viewHelper->initializeArgumentsAndRender();
-        $this->assertEquals('54.321,000', $actualResult);
+        $this->assertEquals($expected, $actualResult);
     }
 }