[TASK] Shorten the extend class in all unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Utility / MathUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Utility;
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
17 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
18
19 /**
20 * Testcase for class \TYPO3\CMS\Core\Utility\MathUtility
21 */
22 class MathUtilityTest extends UnitTestCase
23 {
24 //////////////////////////////////
25 // Tests concerning forceIntegerInRange
26 //////////////////////////////////
27 /**
28 * Data provider for forceIntegerInRangeForcesIntegerIntoBoundaries
29 *
30 * @return array expected values, arithmetic expression
31 */
32 public function forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider()
33 {
34 return [
35 'negativeValue' => [0, -10],
36 'normalValue' => [30, 30],
37 'veryHighValue' => [2000000000, PHP_INT_MAX],
38 'zeroValue' => [0, 0],
39 'anotherNormalValue' => [12309, 12309]
40 ];
41 }
42
43 /**
44 * @test
45 * @dataProvider forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider
46 */
47 public function forceIntegerInRangeForcesIntegerIntoDefaultBoundaries($expected, $value)
48 {
49 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($value, 0));
50 }
51
52 /**
53 * @test
54 */
55 public function forceIntegerInRangeSetsDefaultValueIfZeroValueIsGiven()
56 {
57 $this->assertEquals(42, \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange('', 0, 2000000000, 42));
58 }
59
60 //////////////////////////////////
61 // Tests concerning convertToPositiveInteger
62 //////////////////////////////////
63 /**
64 * @test
65 */
66 public function convertToPositiveIntegerReturnsZeroForNegativeValues()
67 {
68 $this->assertEquals(0, \TYPO3\CMS\Core\Utility\MathUtility::convertToPositiveInteger(-123));
69 }
70
71 /**
72 * @test
73 */
74 public function convertToPositiveIntegerReturnsTheInputValueForPositiveValues()
75 {
76 $this->assertEquals(123, \TYPO3\CMS\Core\Utility\MathUtility::convertToPositiveInteger(123));
77 }
78
79 ///////////////////////////////
80 // Tests concerning canBeInterpretedAsInteger
81 ///////////////////////////////
82 /**
83 * Data provider for canBeInterpretedAsIntegerReturnsTrue
84 *
85 * @return array Data sets
86 */
87 public function functionCanBeInterpretedAsIntegerValidDataProvider()
88 {
89 return [
90 'int' => [32425],
91 'negative int' => [-32425],
92 'largest int' => [PHP_INT_MAX],
93 'int as string' => ['32425'],
94 'negative int as string' => ['-32425'],
95 'zero' => [0],
96 'zero as string' => ['0']
97 ];
98 }
99
100 /**
101 * @test
102 * @dataProvider functionCanBeInterpretedAsIntegerValidDataProvider
103 */
104 public function canBeInterpretedAsIntegerReturnsTrue($int)
105 {
106 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($int));
107 }
108
109 /**
110 * Data provider for canBeInterpretedAsIntegerReturnsFalse
111 *
112 * @return array Data sets
113 */
114 public function functionCanBeInterpretedAsIntegerInvalidDataProvider()
115 {
116 $objectWithNumericalStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
117 $objectWithNumericalStringRepresentation->setString('1234');
118 $objectWithNonNumericalStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
119 $objectWithNonNumericalStringRepresentation->setString('foo');
120 $objectWithEmptyStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
121 $objectWithEmptyStringRepresentation->setString('');
122 return [
123 'int as string with leading zero' => ['01234'],
124 'positive int as string with plus modifier' => ['+1234'],
125 'negative int as string with leading zero' => ['-01234'],
126 'largest int plus one' => [PHP_INT_MAX + 1],
127 'string' => ['testInt'],
128 'empty string' => [''],
129 'int in string' => ['5 times of testInt'],
130 'int as string with space after' => ['5 '],
131 'int as string with space before' => [' 5'],
132 'int as string with many spaces before' => [' 5'],
133 'float' => [3.14159],
134 'float as string' => ['3.14159'],
135 'float as string only a dot' => ['10.'],
136 'float as string trailing zero would evaluate to int 10' => ['10.0'],
137 'float as string trailing zeros would evaluate to int 10' => ['10.00'],
138 'null' => [null],
139 'empty array' => [[]],
140 'int in array' => [[32425]],
141 'int as string in array' => [['32425']],
142 'object without string representation' => [new \stdClass()],
143 'object with numerical string representation' => [$objectWithNumericalStringRepresentation],
144 'object without numerical string representation' => [$objectWithNonNumericalStringRepresentation],
145 'object with empty string representation' => [$objectWithEmptyStringRepresentation]
146 ];
147 }
148
149 /**
150 * @test
151 * @dataProvider functionCanBeInterpretedAsIntegerInvalidDataProvider
152 */
153 public function canBeInterpretedAsIntegerReturnsFalse($int)
154 {
155 $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($int));
156 }
157
158 ///////////////////////////////
159 // Tests concerning canBeInterpretedAsFloat
160 ///////////////////////////////
161 /**
162 * Data provider for canBeInterpretedAsFloatReturnsTrue
163 *
164 * @return array Data sets
165 */
166 public function functionCanBeInterpretedAsFloatValidDataProvider()
167 {
168 // testcases for Integer apply for float as well
169 $intTestcases = $this->functionCanBeInterpretedAsIntegerValidDataProvider();
170 $floatTestcases = [
171 'zero as float' => [(float)0],
172 'negative float' => [(float)-7.5],
173 'negative float as string with exp #1' => ['-7.5e3'],
174 'negative float as string with exp #2' => ['-7.5e03'],
175 'negative float as string with exp #3' => ['-7.5e-3'],
176 'float' => [3.14159],
177 'float as string' => ['3.14159'],
178 'float as string only a dot' => ['10.'],
179 'float as string trailing zero' => ['10.0'],
180 'float as string trailing zeros' => ['10.00'],
181 ];
182 return array_merge($intTestcases, $floatTestcases);
183 }
184
185 /**
186 * @test
187 * @dataProvider functionCanBeInterpretedAsFloatValidDataProvider
188 */
189 public function canBeInterpretedAsFloatReturnsTrue($val)
190 {
191 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsFloat($val));
192 }
193
194 /**
195 * Data provider for canBeInterpretedAsFloatReturnsFalse
196 *
197 * @return array Data sets
198 */
199 public function functionCanBeInterpretedAsFloatInvalidDataProvider()
200 {
201 $objectWithNumericalStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
202 $objectWithNumericalStringRepresentation->setString('1234');
203 $objectWithNonNumericalStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
204 $objectWithNonNumericalStringRepresentation->setString('foo');
205 $objectWithEmptyStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
206 $objectWithEmptyStringRepresentation->setString('');
207 return [
208 // 'int as string with leading zero' => array('01234'),
209 // 'positive int as string with plus modifier' => array('+1234'),
210 // 'negative int as string with leading zero' => array('-01234'),
211 // 'largest int plus one' => array(PHP_INT_MAX + 1),
212 'string' => ['testInt'],
213 'empty string' => [''],
214 'int in string' => ['5 times of testInt'],
215 'int as string with space after' => ['5 '],
216 'int as string with space before' => [' 5'],
217 'int as string with many spaces before' => [' 5'],
218 'null' => [null],
219 'empty array' => [[]],
220 'int in array' => [[32425]],
221 'int as string in array' => [['32425']],
222 'negative float as string with invalid chars in exponent' => ['-7.5eX3'],
223 'object without string representation' => [new \stdClass()],
224 'object with numerical string representation' => [$objectWithNumericalStringRepresentation],
225 'object without numerical string representation' => [$objectWithNonNumericalStringRepresentation],
226 'object with empty string representation' => [$objectWithEmptyStringRepresentation]
227 ];
228 }
229
230 /**
231 * @test
232 * @dataProvider functionCanBeInterpretedAsFloatInvalidDataProvider
233 */
234 public function canBeInterpretedAsFloatReturnsFalse($int)
235 {
236 $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsFloat($int));
237 }
238
239 //////////////////////////////////
240 // Tests concerning calculateWithPriorityToAdditionAndSubtraction
241 //////////////////////////////////
242 /**
243 * Data provider for calculateWithPriorityToAdditionAndSubtraction
244 *
245 * @return array expected values, arithmetic expression
246 */
247 public function calculateWithPriorityToAdditionAndSubtractionDataProvider()
248 {
249 return [
250 'add' => [9, '6 + 3'],
251 'substract with positive result' => [3, '6 - 3'],
252 'substract with negative result' => [-3, '3 - 6'],
253 'multiply' => [6, '2 * 3'],
254 'divide' => [2.5, '5 / 2'],
255 'modulus' => [1, '5 % 2'],
256 'power' => [8, '2 ^ 3'],
257 'three operands with non integer result' => [6.5, '5 + 3 / 2'],
258 'three operands with power' => [14, '5 + 3 ^ 2'],
259 'three operads with modulus' => [4, '5 % 2 + 3'],
260 'four operands' => [3, '2 + 6 / 2 - 2'],
261 'division by zero when dividing' => ['ERROR: dividing by zero', '2 / 0'],
262 'division by zero with modulus' => ['ERROR: dividing by zero', '2 % 0']
263 ];
264 }
265
266 /**
267 * @test
268 * @dataProvider calculateWithPriorityToAdditionAndSubtractionDataProvider
269 */
270 public function calculateWithPriorityToAdditionAndSubtractionCorrectlyCalculatesExpression($expected, $expression)
271 {
272 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\MathUtility::calculateWithPriorityToAdditionAndSubtraction($expression));
273 }
274
275 //////////////////////////////////
276 // Tests concerning calcParenthesis
277 //////////////////////////////////
278 /**
279 * Data provider for calcParenthesis
280 *
281 * @return array expected values, arithmetic expression
282 */
283 public function calculateWithParenthesesDataProvider()
284 {
285 return [
286 'starts with parenthesis' => [18, '(6 + 3) * 2'],
287 'ends with parenthesis' => [6, '2 * (6 - 3)'],
288 'multiple parentheses' => [-6, '(3 - 6) * (4 - 2)'],
289 'nested parentheses' => [22, '2 * (3 + 2 + (3 * 2))'],
290 'parenthesis with division' => [15, '5 / 2 * (3 * 2)']
291 ];
292 }
293
294 /**
295 * @test
296 * @dataProvider calculateWithParenthesesDataProvider
297 */
298 public function calculateWithParenthesesCorrectlyCalculatesExpression($expected, $expression)
299 {
300 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\MathUtility::calculateWithParentheses($expression));
301 }
302
303 //////////////////////////////////
304 // Tests concerning isIntegerInRange
305 //////////////////////////////////
306 /**
307 * @test
308 */
309 public function isIntegerInRangeIncludesLowerBoundary()
310 {
311 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(1, 1, 2));
312 }
313
314 /**
315 * @test
316 */
317 public function isIntegerInRangeIncludesUpperBoundary()
318 {
319 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(2, 1, 2));
320 }
321
322 /**
323 * @test
324 */
325 public function isIntegerInRangeAcceptsValueInRange()
326 {
327 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(10, 1, 100));
328 }
329
330 /**
331 * @test
332 */
333 public function isIntegerInRangeRejectsValueOutsideOfRange()
334 {
335 $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(10, 1, 2));
336 }
337
338 /**
339 * Data provider or isIntegerInRangeRejectsOtherDataTypes
340 */
341 public function isIntegerInRangeRejectsOtherDataTypesDataProvider()
342 {
343 return [
344 'negative integer' => [-1],
345 'float' => [1.5],
346 'string' => ['string'],
347 'array' => [[]],
348 'object' => [new \stdClass()],
349 'boolean FALSE' => [false],
350 'NULL' => [null]
351 ];
352 }
353
354 /**
355 * @test
356 * @dataProvider isIntegerInRangeRejectsOtherDataTypesDataProvider
357 */
358 public function isIntegerInRangeRejectsOtherDataTypes($inputValue)
359 {
360 $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange($inputValue, 0, 10));
361 }
362 }