‪TYPO3CMS  9.5
BytesViewHelperTest.php
Go to the documentation of this file.
1 <?php
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 
18 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
19 
23 class ‪BytesViewHelperTest extends ViewHelperBaseTestcase
24 {
28  protected ‪$viewHelper;
29 
30  protected function ‪setUp()
31  {
32  parent::setUp();
33  $this->viewHelper = new ‪BytesViewHelper();
34  $this->injectDependenciesIntoViewHelper($this->viewHelper);
35  }
36 
40  public function ‪valueDataProvider()
41  {
42  return [
43 
44  // invalid values
45  [
46  'value' => 'invalid',
47  'decimals' => null,
48  'decimalSeparator' => null,
49  'thousandsSeparator' => null,
50  'expected' => '0 B'
51  ],
52  [
53  'value' => '',
54  'decimals' => 2,
55  'decimalSeparator' => null,
56  'thousandsSeparator' => null,
57  'expected' => '0.00 B'
58  ],
59  [
60  'value' => [],
61  'decimals' => 2,
62  'decimalSeparator' => ',',
63  'thousandsSeparator' => null,
64  'expected' => '0,00 B'
65  ],
66  // valid values
67  [
68  'value' => 123,
69  'decimals' => null,
70  'decimalSeparator' => null,
71  'thousandsSeparator' => null,
72  'expected' => '123 B'
73  ],
74  [
75  'value' => '43008',
76  'decimals' => 1,
77  'decimalSeparator' => null,
78  'thousandsSeparator' => null,
79  'expected' => '42.0 KB'
80  ],
81  [
82  'value' => 1024,
83  'decimals' => 1,
84  'decimalSeparator' => null,
85  'thousandsSeparator' => null,
86  'expected' => '1.0 KB'
87  ],
88  [
89  'value' => 1023,
90  'decimals' => 2,
91  'decimalSeparator' => null,
92  'thousandsSeparator' => null,
93  'expected' => '1,023.00 B'
94  ],
95  [
96  'value' => 1073741823,
97  'decimals' => 1,
98  'decimalSeparator' => null,
99  'thousandsSeparator' => '.',
100  'expected' => '1.024.0 MB'
101  ],
102  [
103  'value' => pow(1024, 5),
104  'decimals' => 1,
105  'decimalSeparator' => null,
106  'thousandsSeparator' => null,
107  'expected' => '1.0 PB'
108  ],
109  [
110  'value' => pow(1024, 8),
111  'decimals' => 1,
112  'decimalSeparator' => null,
113  'thousandsSeparator' => null,
114  'expected' => '1.0 YB'
115  ]
116  ];
117  }
118 
128  public function ‪renderConvertsAValue($value, $decimals, $decimalSeparator, $thousandsSeparator, $expected)
129  {
130  $this->setArgumentsUnderTest(
131  $this->viewHelper,
132  [
133  'value' => $value,
134  'decimals' => $decimals,
135  'decimalSeparator' => $decimalSeparator,
136  'thousandsSeparator' => $thousandsSeparator,
137  'units' => 'B,KB,MB,GB,TB,PB,EB,ZB,YB',
138  ]
139  );
140  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
141  $this->assertEquals($expected, $actualResult);
142  }
143 
148  {
149  $this->viewHelper->setRenderChildrenClosure(
150  function () {
151  return 12345;
152  }
153  );
154  $this->setArgumentsUnderTest(
155  $this->viewHelper,
156  [
157  'units' => 'B,KB,MB,GB,TB,PB,EB,ZB,YB',
158  ]
159  );
160  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
161  $this->assertEquals('12 KB', $actualResult);
162  }
163 }
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\BytesViewHelperTest\setUp
‪setUp()
Definition: BytesViewHelperTest.php:29
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\BytesViewHelperTest\valueDataProvider
‪array valueDataProvider()
Definition: BytesViewHelperTest.php:39
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format
Definition: BytesViewHelperTest.php:2
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\BytesViewHelperTest\renderConvertsAValue
‪renderConvertsAValue($value, $decimals, $decimalSeparator, $thousandsSeparator, $expected)
Definition: BytesViewHelperTest.php:127
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\BytesViewHelperTest
Definition: BytesViewHelperTest.php:24
‪TYPO3\CMS\Fluid\ViewHelpers\Format\BytesViewHelper
Definition: BytesViewHelper.php:62
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\BytesViewHelperTest\$viewHelper
‪BytesViewHelper $viewHelper
Definition: BytesViewHelperTest.php:27
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\BytesViewHelperTest\renderUsesChildNodesIfValueArgumentIsOmitted
‪renderUsesChildNodesIfValueArgumentIsOmitted()
Definition: BytesViewHelperTest.php:146