TYPO3 CMS  TYPO3_7-6
BytesViewHelperTest.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
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 3 of the *
9  * License, or (at your option) any later version. *
10  * *
11  * The TYPO3 project - inspiring people to share! *
12  * */
13 
16 
21 {
25  protected $viewHelper;
26 
27  protected function setUp()
28  {
29  parent::setUp();
30 
31  // XXX: This is bad from a testing POV but the only option right now
32  $reflectionClass = new \ReflectionClass(\TYPO3\CMS\Extbase\Utility\LocalizationUtility::class);
33  $property = $reflectionClass->getProperty('configurationManager');
34  $property->setAccessible(true);
35  $property->setValue($this->getMock(ConfigurationManagerInterface::class));
36 
37  $this->viewHelper = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Format\BytesViewHelper::class, ['renderChildren']);
38  $this->injectDependenciesIntoViewHelper($this->viewHelper);
39  $this->viewHelper->initializeArguments();
40  }
41 
45  public function valueDataProvider()
46  {
47  return [
48 
49  // invalid values
50  [
51  'value' => 'invalid',
52  'decimals' => null,
53  'decimalSeparator' => null,
54  'thousandsSeparator' => null,
55  'expected' => '0 B'
56  ],
57  [
58  'value' => '',
59  'decimals' => 2,
60  'decimalSeparator' => null,
61  'thousandsSeparator' => null,
62  'expected' => '0.00 B'
63  ],
64  [
65  'value' => [],
66  'decimals' => 2,
67  'decimalSeparator' => ',',
68  'thousandsSeparator' => null,
69  'expected' => '0,00 B'
70  ],
71  // valid values
72  [
73  'value' => 123,
74  'decimals' => null,
75  'decimalSeparator' => null,
76  'thousandsSeparator' => null,
77  'expected' => '123 B'
78  ],
79  [
80  'value' => '43008',
81  'decimals' => 1,
82  'decimalSeparator' => null,
83  'thousandsSeparator' => null,
84  'expected' => '42.0 KB'
85  ],
86  [
87  'value' => 1024,
88  'decimals' => 1,
89  'decimalSeparator' => null,
90  'thousandsSeparator' => null,
91  'expected' => '1.0 KB'
92  ],
93  [
94  'value' => 1023,
95  'decimals' => 2,
96  'decimalSeparator' => null,
97  'thousandsSeparator' => null,
98  'expected' => '1,023.00 B'
99  ],
100  [
101  'value' => 1073741823,
102  'decimals' => 1,
103  'decimalSeparator' => null,
104  'thousandsSeparator' => '.',
105  'expected' => '1.024.0 MB'
106  ],
107  [
108  'value' => pow(1024, 5),
109  'decimals' => 1,
110  'decimalSeparator' => null,
111  'thousandsSeparator' => null,
112  'expected' => '1.0 PB'
113  ],
114  [
115  'value' => pow(1024, 8),
116  'decimals' => 1,
117  'decimalSeparator' => null,
118  'thousandsSeparator' => null,
119  'expected' => '1.0 YB'
120  ]
121  ];
122  }
123 
133  public function renderCorrectlyConvertsAValue($value, $decimals, $decimalSeparator, $thousandsSeparator, $expected)
134  {
135  $actualResult = $this->viewHelper->render($value, $decimals, $decimalSeparator, $thousandsSeparator);
136  $this->assertEquals($expected, $actualResult);
137  }
138 
143  {
144  $this->viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(12345));
145  $actualResult = $this->viewHelper->render();
146  $this->assertEquals('12 KB', $actualResult);
147  }
148 }
injectDependenciesIntoViewHelper(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper $viewHelper)
renderCorrectlyConvertsAValue($value, $decimals, $decimalSeparator, $thousandsSeparator, $expected)