‪TYPO3CMS  10.4
MathUtilityTest.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 
20 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
21 
25 class ‪MathUtilityTest extends UnitTestCase
26 {
28  // Tests concerning forceIntegerInRange
30 
36  {
37  return [
38  'negativeValue' => [0, -10],
39  'normalValue' => [30, 30],
40  'veryHighValue' => [2000000000, PHP_INT_MAX],
41  'zeroValue' => [0, 0],
42  'anotherNormalValue' => [12309, 12309]
43  ];
44  }
45 
51  {
52  self::assertEquals($expected, ‪MathUtility::forceIntegerInRange($value, 0));
53  }
54 
59  {
60  self::assertEquals(42, ‪MathUtility::forceIntegerInRange('', 0, 2000000000, 42));
61  }
62 
64  // Tests concerning convertToPositiveInteger
66 
70  {
71  self::assertEquals(0, ‪MathUtility::convertToPositiveInteger(-123));
72  }
73 
78  {
79  self::assertEquals(123, ‪MathUtility::convertToPositiveInteger(123));
80  }
81 
83  // Tests concerning canBeInterpretedAsInteger
85 
91  {
92  return [
93  'int' => [32425],
94  'negative int' => [-32425],
95  'largest int' => [PHP_INT_MAX],
96  'int as string' => ['32425'],
97  'negative int as string' => ['-32425'],
98  'zero' => [0],
99  'zero as string' => ['0']
100  ];
101  }
102 
108  {
109  self::assertTrue(‪MathUtility::canBeInterpretedAsInteger($int));
110  }
111 
118  {
119  $objectWithNumericalStringRepresentation = new ‪MathUtilityTestClassWithStringRepresentationFixture();
120  $objectWithNumericalStringRepresentation->setString('1234');
121  $objectWithNonNumericalStringRepresentation = new ‪MathUtilityTestClassWithStringRepresentationFixture();
122  $objectWithNonNumericalStringRepresentation->setString('foo');
123  $objectWithEmptyStringRepresentation = new ‪MathUtilityTestClassWithStringRepresentationFixture();
124  $objectWithEmptyStringRepresentation->setString('');
125  return [
126  'int as string with leading zero' => ['01234'],
127  'positive int as string with plus modifier' => ['+1234'],
128  'negative int as string with leading zero' => ['-01234'],
129  'largest int plus one' => [PHP_INT_MAX + 1],
130  'string' => ['testInt'],
131  'empty string' => [''],
132  'int in string' => ['5 times of testInt'],
133  'int as string with space after' => ['5 '],
134  'int as string with space before' => [' 5'],
135  'int as string with many spaces before' => [' 5'],
136  'float' => [3.14159],
137  'float as string' => ['3.14159'],
138  'float as string only a dot' => ['10.'],
139  'float as string trailing zero would evaluate to int 10' => ['10.0'],
140  'float as string trailing zeros would evaluate to int 10' => ['10.00'],
141  'null' => [null],
142  'empty array' => [[]],
143  'int in array' => [[32425]],
144  'int as string in array' => [['32425']],
145  'object without string representation' => [new \stdClass()],
146  'object with numerical string representation' => [$objectWithNumericalStringRepresentation],
147  'object without numerical string representation' => [$objectWithNonNumericalStringRepresentation],
148  'object with empty string representation' => [$objectWithEmptyStringRepresentation]
149  ];
150  }
151 
157  {
158  self::assertFalse(‪MathUtility::canBeInterpretedAsInteger($int));
159  }
160 
162  // Tests concerning canBeInterpretedAsFloat
164 
170  {
171  // testcases for Integer apply for float as well
173  $floatTestcases = [
174  'zero as float' => [(float)0],
175  'negative float' => [(float)-7.5],
176  'negative float as string with exp #1' => ['-7.5e3'],
177  'negative float as string with exp #2' => ['-7.5e03'],
178  'negative float as string with exp #3' => ['-7.5e-3'],
179  'float' => [3.14159],
180  'float as string' => ['3.14159'],
181  'float as string only a dot' => ['10.'],
182  'float as string trailing zero' => ['10.0'],
183  'float as string trailing zeros' => ['10.00'],
184  ];
185  return array_merge($intTestcases, $floatTestcases);
186  }
187 
193  {
194  self::assertTrue(‪MathUtility::canBeInterpretedAsFloat($val));
195  }
196 
203  {
204  $objectWithNumericalStringRepresentation = new ‪MathUtilityTestClassWithStringRepresentationFixture();
205  $objectWithNumericalStringRepresentation->setString('1234');
206  $objectWithNonNumericalStringRepresentation = new ‪MathUtilityTestClassWithStringRepresentationFixture();
207  $objectWithNonNumericalStringRepresentation->setString('foo');
208  $objectWithEmptyStringRepresentation = new ‪MathUtilityTestClassWithStringRepresentationFixture();
209  $objectWithEmptyStringRepresentation->setString('');
210  return [
211  // 'int as string with leading zero' => array('01234'),
212  // 'positive int as string with plus modifier' => array('+1234'),
213  // 'negative int as string with leading zero' => array('-01234'),
214  // 'largest int plus one' => array(PHP_INT_MAX + 1),
215  'string' => ['testInt'],
216  'empty string' => [''],
217  'int in string' => ['5 times of testInt'],
218  'int as string with space after' => ['5 '],
219  'int as string with space before' => [' 5'],
220  'int as string with many spaces before' => [' 5'],
221  'null' => [null],
222  'empty array' => [[]],
223  'int in array' => [[32425]],
224  'int as string in array' => [['32425']],
225  'negative float as string with invalid chars in exponent' => ['-7.5eX3'],
226  'object without string representation' => [new \stdClass()],
227  'object with numerical string representation' => [$objectWithNumericalStringRepresentation],
228  'object without numerical string representation' => [$objectWithNonNumericalStringRepresentation],
229  'object with empty string representation' => [$objectWithEmptyStringRepresentation]
230  ];
231  }
232 
238  {
239  self::assertFalse(‪MathUtility::canBeInterpretedAsFloat($int));
240  }
241 
243  // Tests concerning calculateWithPriorityToAdditionAndSubtraction
245 
251  {
252  return [
253  'add' => [9, '6 + 3'],
254  'substract with positive result' => [3, '6 - 3'],
255  'substract with negative result' => [-3, '3 - 6'],
256  'multiply' => [6, '2 * 3'],
257  'divide' => [2.5, '5 / 2'],
258  'modulus' => [1, '5 % 2'],
259  'power' => [8, '2 ^ 3'],
260  'three operands with non integer result' => [6.5, '5 + 3 / 2'],
261  'three operands with power' => [14, '5 + 3 ^ 2'],
262  'three operands with modulus' => [4, '5 % 2 + 3'],
263  'four operands' => [3, '2 + 6 / 2 - 2'],
264  'division by zero when dividing' => ['ERROR: dividing by zero', '2 / 0'],
265  'division by zero with modulus' => ['ERROR: dividing by zero', '2 % 0']
266  ];
267  }
268 
274  {
275  self::assertEquals($expected, ‪MathUtility::calculateWithPriorityToAdditionAndSubtraction($expression));
276  }
277 
279  // Tests concerning calcParenthesis
281 
287  {
288  return [
289  'starts with parenthesis' => [18, '(6 + 3) * 2'],
290  'ends with parenthesis' => [6, '2 * (6 - 3)'],
291  'multiple parentheses' => [-6, '(3 - 6) * (4 - 2)'],
292  'nested parentheses' => [22, '2 * (3 + 2 + (3 * 2))'],
293  'parenthesis with division' => [15, '5 / 2 * (3 * 2)']
294  ];
295  }
296 
301  public function ‪calculateWithParenthesesCorrectlyCalculatesExpression($expected, $expression)
302  {
303  self::assertEquals($expected, ‪MathUtility::calculateWithParentheses($expression));
304  }
305 
307  // Tests concerning isIntegerInRange
309 
313  {
314  self::assertTrue(‪MathUtility::isIntegerInRange(1, 1, 2));
315  }
316 
321  {
322  self::assertTrue(‪MathUtility::isIntegerInRange(2, 1, 2));
323  }
324 
329  {
330  self::assertTrue(‪MathUtility::isIntegerInRange(10, 1, 100));
331  }
332 
337  {
338  self::assertFalse(‪MathUtility::isIntegerInRange(10, 1, 2));
339  }
340 
345  {
346  return [
347  'negative integer' => [-1],
348  'float' => [1.5],
349  'string' => ['string'],
350  'array' => [[]],
351  'object' => [new \stdClass()],
352  'boolean FALSE' => [false],
353  'NULL' => [null]
354  ];
355  }
356 
361  public function ‪isIntegerInRangeRejectsOtherDataTypes($inputValue)
362  {
363  self::assertFalse(‪MathUtility::isIntegerInRange($inputValue, 0, 10));
364  }
365 }
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\functionCanBeInterpretedAsIntegerValidDataProvider
‪array functionCanBeInterpretedAsIntegerValidDataProvider()
Definition: MathUtilityTest.php:90
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeRejectsOtherDataTypesDataProvider
‪isIntegerInRangeRejectsOtherDataTypesDataProvider()
Definition: MathUtilityTest.php:344
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider
‪array forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider()
Definition: MathUtilityTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeRejectsValueOutsideOfRange
‪isIntegerInRangeRejectsValueOutsideOfRange()
Definition: MathUtilityTest.php:336
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\canBeInterpretedAsFloatReturnsTrue
‪canBeInterpretedAsFloatReturnsTrue($val)
Definition: MathUtilityTest.php:192
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\canBeInterpretedAsIntegerReturnsTrue
‪canBeInterpretedAsIntegerReturnsTrue($int)
Definition: MathUtilityTest.php:107
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\functionCanBeInterpretedAsFloatValidDataProvider
‪array functionCanBeInterpretedAsFloatValidDataProvider()
Definition: MathUtilityTest.php:169
‪TYPO3\CMS\Core\Utility\MathUtility\calculateWithPriorityToAdditionAndSubtraction
‪static int calculateWithPriorityToAdditionAndSubtraction($string)
Definition: MathUtility.php:112
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\calculateWithPriorityToAdditionAndSubtractionDataProvider
‪array calculateWithPriorityToAdditionAndSubtractionDataProvider()
Definition: MathUtilityTest.php:250
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:32
‪TYPO3\CMS\Core\Utility\MathUtility\isIntegerInRange
‪static bool isIntegerInRange($value, $minimum, $maximum)
Definition: MathUtility.php:202
‪TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture
Definition: MathUtilityTestClassWithStringRepresentationFixture.php:22
‪TYPO3\CMS\Core\Tests\Unit\Utility
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsFloat
‪static bool canBeInterpretedAsFloat($var)
Definition: MathUtility.php:91
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\calculateWithParenthesesCorrectlyCalculatesExpression
‪calculateWithParenthesesCorrectlyCalculatesExpression($expected, $expression)
Definition: MathUtilityTest.php:301
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\forceIntegerInRangeForcesIntegerIntoDefaultBoundaries
‪forceIntegerInRangeForcesIntegerIntoDefaultBoundaries($expected, $value)
Definition: MathUtilityTest.php:50
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\functionCanBeInterpretedAsIntegerInvalidDataProvider
‪array functionCanBeInterpretedAsIntegerInvalidDataProvider()
Definition: MathUtilityTest.php:117
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest
Definition: MathUtilityTest.php:26
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\calculateWithPriorityToAdditionAndSubtractionCorrectlyCalculatesExpression
‪calculateWithPriorityToAdditionAndSubtractionCorrectlyCalculatesExpression($expected, $expression)
Definition: MathUtilityTest.php:273
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\functionCanBeInterpretedAsFloatInvalidDataProvider
‪array functionCanBeInterpretedAsFloatInvalidDataProvider()
Definition: MathUtilityTest.php:202
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\canBeInterpretedAsIntegerReturnsFalse
‪canBeInterpretedAsIntegerReturnsFalse($int)
Definition: MathUtilityTest.php:156
‪TYPO3\CMS\Core\Utility\MathUtility\convertToPositiveInteger
‪static int convertToPositiveInteger($theInt)
Definition: MathUtility.php:56
‪TYPO3\CMS\Core\Utility\MathUtility\calculateWithParentheses
‪static int calculateWithParentheses($string)
Definition: MathUtility.php:172
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeAcceptsValueInRange
‪isIntegerInRangeAcceptsValueInRange()
Definition: MathUtilityTest.php:328
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeRejectsOtherDataTypes
‪isIntegerInRangeRejectsOtherDataTypes($inputValue)
Definition: MathUtilityTest.php:361
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\convertToPositiveIntegerReturnsTheInputValueForPositiveValues
‪convertToPositiveIntegerReturnsTheInputValueForPositiveValues()
Definition: MathUtilityTest.php:77
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\canBeInterpretedAsFloatReturnsFalse
‪canBeInterpretedAsFloatReturnsFalse($int)
Definition: MathUtilityTest.php:237
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeIncludesUpperBoundary
‪isIntegerInRangeIncludesUpperBoundary()
Definition: MathUtilityTest.php:320
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\calculateWithParenthesesDataProvider
‪array calculateWithParenthesesDataProvider()
Definition: MathUtilityTest.php:286
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeIncludesLowerBoundary
‪isIntegerInRangeIncludesLowerBoundary()
Definition: MathUtilityTest.php:312
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\convertToPositiveIntegerReturnsZeroForNegativeValues
‪convertToPositiveIntegerReturnsZeroForNegativeValues()
Definition: MathUtilityTest.php:69
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\forceIntegerInRangeSetsDefaultValueIfZeroValueIsGiven
‪forceIntegerInRangeSetsDefaultValueIfZeroValueIsGiven()
Definition: MathUtilityTest.php:58