‪TYPO3CMS  10.4
BytesViewHelperTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
19 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
20 
24 class ‪BytesViewHelperTest extends ViewHelperBaseTestcase
25 {
29  protected ‪$viewHelper;
30 
31  protected function ‪setUp(): void
32  {
33  parent::setUp();
34  $this->viewHelper = new ‪BytesViewHelper();
35  $this->injectDependenciesIntoViewHelper($this->viewHelper);
36  }
37 
41  public function ‪valueDataProvider()
42  {
43  return [
44 
45  // invalid values
46  [
47  'value' => 'invalid',
48  'decimals' => null,
49  'decimalSeparator' => null,
50  'thousandsSeparator' => null,
51  'expected' => '0 B'
52  ],
53  [
54  'value' => '',
55  'decimals' => 2,
56  'decimalSeparator' => null,
57  'thousandsSeparator' => null,
58  'expected' => '0.00 B'
59  ],
60  [
61  'value' => [],
62  'decimals' => 2,
63  'decimalSeparator' => ',',
64  'thousandsSeparator' => null,
65  'expected' => '0,00 B'
66  ],
67  // valid values
68  [
69  'value' => 123,
70  'decimals' => null,
71  'decimalSeparator' => null,
72  'thousandsSeparator' => null,
73  'expected' => '123 B'
74  ],
75  [
76  'value' => '43008',
77  'decimals' => 1,
78  'decimalSeparator' => null,
79  'thousandsSeparator' => null,
80  'expected' => '42.0 KB'
81  ],
82  [
83  'value' => 1024,
84  'decimals' => 1,
85  'decimalSeparator' => null,
86  'thousandsSeparator' => null,
87  'expected' => '1.0 KB'
88  ],
89  [
90  'value' => 1023,
91  'decimals' => 2,
92  'decimalSeparator' => null,
93  'thousandsSeparator' => null,
94  'expected' => '1,023.00 B'
95  ],
96  [
97  'value' => 1073741823,
98  'decimals' => 1,
99  'decimalSeparator' => null,
100  'thousandsSeparator' => '.',
101  'expected' => '1.024.0 MB'
102  ],
103  [
104  'value' => 1024 ** 5,
105  'decimals' => 1,
106  'decimalSeparator' => null,
107  'thousandsSeparator' => null,
108  'expected' => '1.0 PB'
109  ],
110  [
111  'value' => 1024 ** 8,
112  'decimals' => 1,
113  'decimalSeparator' => null,
114  'thousandsSeparator' => null,
115  'expected' => '1.0 YB'
116  ]
117  ];
118  }
119 
129  public function ‪renderConvertsAValue($value, $decimals, $decimalSeparator, $thousandsSeparator, $expected)
130  {
131  $this->setArgumentsUnderTest(
132  $this->viewHelper,
133  [
134  'value' => $value,
135  'decimals' => $decimals,
136  'decimalSeparator' => $decimalSeparator,
137  'thousandsSeparator' => $thousandsSeparator,
138  'units' => 'B,KB,MB,GB,TB,PB,EB,ZB,YB',
139  ]
140  );
141  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
142  self::assertEquals($expected, $actualResult);
143  }
144 
149  {
150  $this->viewHelper->setRenderChildrenClosure(
151  function () {
152  return 12345;
153  }
154  );
155  $this->setArgumentsUnderTest(
156  $this->viewHelper,
157  [
158  'units' => 'B,KB,MB,GB,TB,PB,EB,ZB,YB',
159  ]
160  );
161  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
162  self::assertEquals('12 KB', $actualResult);
163  }
164 }
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\BytesViewHelperTest\setUp
‪setUp()
Definition: BytesViewHelperTest.php:30
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\BytesViewHelperTest\valueDataProvider
‪array valueDataProvider()
Definition: BytesViewHelperTest.php:40
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format
Definition: ScriptViewHelperTest.php:18
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\BytesViewHelperTest\renderConvertsAValue
‪renderConvertsAValue($value, $decimals, $decimalSeparator, $thousandsSeparator, $expected)
Definition: BytesViewHelperTest.php:128
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\BytesViewHelperTest
Definition: BytesViewHelperTest.php:25
‪TYPO3\CMS\Fluid\ViewHelpers\Format\BytesViewHelper
Definition: BytesViewHelper.php:63
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\BytesViewHelperTest\$viewHelper
‪BytesViewHelper $viewHelper
Definition: BytesViewHelperTest.php:28
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\BytesViewHelperTest\renderUsesChildNodesIfValueArgumentIsOmitted
‪renderUsesChildNodesIfValueArgumentIsOmitted()
Definition: BytesViewHelperTest.php:147