[TASK] Move arguments to initializeArguments() in CurrencyVH in ext:fluid
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / Format / CurrencyViewHelperTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase;
17 use TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper;
18
19 /**
20 * Test case
21 */
22 class CurrencyViewHelperTest extends ViewHelperBaseTestcase
23 {
24 /**
25 * @var CurrencyViewHelper
26 */
27 protected $viewHelper;
28
29 protected function setUp()
30 {
31 parent::setUp();
32 $this->viewHelper = new CurrencyViewHelper();
33 $this->injectDependenciesIntoViewHelper($this->viewHelper);
34 }
35
36 /**
37 * @test
38 */
39 public function viewHelperRoundsFloatCorrectly()
40 {
41 $this->viewHelper->setRenderChildrenClosure(
42 function () {
43 return 123.456;
44 }
45 );
46 $this->setArgumentsUnderTest(
47 $this->viewHelper,
48 [
49 ]
50 );
51 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
52 $this->assertEquals('123,46', $actualResult);
53 }
54
55 /**
56 * @test
57 */
58 public function viewHelperRendersCurrencySign()
59 {
60 $this->viewHelper->setRenderChildrenClosure(
61 function () {
62 return 123;
63 }
64 );
65 $this->setArgumentsUnderTest(
66 $this->viewHelper,
67 [
68 'currencySign' => 'foo'
69 ]
70 );
71 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
72 $this->assertEquals('123,00 foo', $actualResult);
73 }
74
75 /**
76 * @test
77 */
78 public function viewHelperRendersPrependedCurrencySign()
79 {
80 $this->viewHelper->setRenderChildrenClosure(
81 function () {
82 return 123;
83 }
84 );
85 $this->setArgumentsUnderTest(
86 $this->viewHelper,
87 [
88 'currencySign' => 'foo',
89 'decimalSeparator' => ',',
90 'thousandsSeparator' => '.',
91 'prependCurrency' => true
92 ]
93 );
94 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
95 $this->assertEquals('foo 123,00', $actualResult);
96 }
97
98 /**
99 * @test
100 */
101 public function viewHelperRespectsCurrencySeparator()
102 {
103 $this->viewHelper->setRenderChildrenClosure(
104 function () {
105 return 123;
106 }
107 );
108 $this->setArgumentsUnderTest(
109 $this->viewHelper,
110 [
111 'currencySign' => 'foo',
112 'decimalSeparator' => ',',
113 'thousandsSeparator' => '.',
114 'prependCurrency' => true,
115 'separateCurrency' => false
116 ]
117 );
118 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
119 $this->assertEquals('foo123,00', $actualResult);
120 }
121
122 /**
123 * @test
124 */
125 public function viewHelperRespectsDecimalSeparator()
126 {
127 $this->viewHelper->setRenderChildrenClosure(
128 function () {
129 return 12345;
130 }
131 );
132 $this->setArgumentsUnderTest(
133 $this->viewHelper,
134 [
135 'currencySign' => '',
136 'decimalSeparator' => '|'
137 ]
138 );
139 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
140 $this->assertEquals('12.345|00', $actualResult);
141 }
142
143 /**
144 * @test
145 */
146 public function viewHelperRespectsThousandsSeparator()
147 {
148 $this->viewHelper->setRenderChildrenClosure(
149 function () {
150 return 12345;
151 }
152 );
153 $this->setArgumentsUnderTest(
154 $this->viewHelper,
155 [
156 'currencySign' => '',
157 'decimalSeparator' => ',',
158 'thousandsSeparator' => '|'
159 ]
160 );
161 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
162 $this->assertEquals('12|345,00', $actualResult);
163 }
164
165 /**
166 * @test
167 */
168 public function viewHelperRendersNullValues()
169 {
170 $this->viewHelper->setRenderChildrenClosure(
171 function () {
172 return null;
173 }
174 );
175 $this->setArgumentsUnderTest(
176 $this->viewHelper,
177 [
178 ]
179 );
180 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
181 $this->assertEquals('0,00', $actualResult);
182 }
183
184 /**
185 * @test
186 */
187 public function viewHelperRendersEmptyString()
188 {
189 $this->viewHelper->setRenderChildrenClosure(
190 function () {
191 return '';
192 }
193 );
194 $this->setArgumentsUnderTest(
195 $this->viewHelper,
196 [
197 ]
198 );
199 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
200 $this->assertEquals('0,00', $actualResult);
201 }
202
203 /**
204 * @test
205 */
206 public function viewHelperRendersZeroValues()
207 {
208 $this->viewHelper->setRenderChildrenClosure(
209 function () {
210 return 0;
211 }
212 );
213 $this->setArgumentsUnderTest(
214 $this->viewHelper,
215 [
216 ]
217 );
218 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
219 $this->assertEquals('0,00', $actualResult);
220 }
221
222 /**
223 * @test
224 */
225 public function viewHelperRendersNegativeAmounts()
226 {
227 $this->viewHelper->setRenderChildrenClosure(
228 function () {
229 return -123.456;
230 }
231 );
232 $this->setArgumentsUnderTest(
233 $this->viewHelper,
234 [
235 ]
236 );
237 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
238 $this->assertEquals('-123,46', $actualResult);
239 }
240
241 /**
242 * @test
243 */
244 public function viewHelperRendersStringsToZeroValueFloat()
245 {
246 $this->viewHelper->setRenderChildrenClosure(
247 function () {
248 return 'TYPO3';
249 }
250 );
251 $this->setArgumentsUnderTest(
252 $this->viewHelper,
253 [
254 ]
255 );
256 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
257 $this->assertEquals('0,00', $actualResult);
258 }
259
260 /**
261 * @test
262 */
263 public function viewHelperRendersCommaValuesToValueBeforeComma()
264 {
265 $this->viewHelper->setRenderChildrenClosure(
266 function () {
267 return '12,34.00';
268 }
269 );
270 $this->setArgumentsUnderTest(
271 $this->viewHelper,
272 [
273 ]
274 );
275 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
276 $this->assertEquals('12,00', $actualResult);
277 }
278
279 /**
280 * @test
281 */
282 public function viewHelperRendersValuesWithoutDecimals()
283 {
284 $this->viewHelper->setRenderChildrenClosure(
285 function () {
286 return '54321';
287 }
288 );
289 $this->setArgumentsUnderTest(
290 $this->viewHelper,
291 [
292 'currencySign' => '',
293 'decimalSeparator' => ',',
294 'thousandsSeparator' => '.',
295 'prependCurrency' => false,
296 'separateCurrency' => true,
297 'decimals' => 0
298 ]
299 );
300 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
301 $this->assertEquals('54.321', $actualResult);
302 }
303
304 /**
305 * @test
306 */
307 public function viewHelperRendersThreeDecimals()
308 {
309 $this->viewHelper->setRenderChildrenClosure(
310 function () {
311 return '54321';
312 }
313 );
314 $this->setArgumentsUnderTest(
315 $this->viewHelper,
316 [
317 'currencySign' => '',
318 'decimalSeparator' => ',',
319 'thousandsSeparator' => '.',
320 'prependCurrency' => false,
321 'separateCurrency' => true,
322 'decimals' => 3
323 ]
324 );
325 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
326 $this->assertEquals('54.321,000', $actualResult);
327 }
328 }