‪TYPO3CMS  11.5
MathUtilityTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23 
27 class ‪MathUtilityTest extends UnitTestCase
28 {
30  // Tests concerning forceIntegerInRange
32 
38  {
39  return [
40  'negativeValue' => [0, -10],
41  'normalValue' => [30, 30],
42  'veryHighValue' => [2000000000, PHP_INT_MAX],
43  'zeroValue' => [0, 0],
44  'anotherNormalValue' => [12309, 12309],
45  ];
46  }
47 
52  public function ‪forceIntegerInRangeForcesIntegerIntoDefaultBoundaries($expected, $value): void
53  {
54  self::assertEquals($expected, ‪MathUtility::forceIntegerInRange($value, 0));
55  }
56 
61  {
62  self::assertEquals(42, ‪MathUtility::forceIntegerInRange('', 0, 2000000000, 42));
63  }
64 
66  // Tests concerning convertToPositiveInteger
68 
72  {
73  self::assertEquals(0, ‪MathUtility::convertToPositiveInteger(-123));
74  }
75 
80  {
81  self::assertEquals(123, ‪MathUtility::convertToPositiveInteger(123));
82  }
83 
85  // Tests concerning canBeInterpretedAsInteger
87 
93  {
94  return [
95  'int' => [32425],
96  'negative int' => [-32425],
97  'largest int' => [PHP_INT_MAX],
98  'int as string' => ['32425'],
99  'negative int as string' => ['-32425'],
100  'zero' => [0],
101  'zero as string' => ['0'],
102  ];
103  }
104 
109  public function ‪canBeInterpretedAsIntegerReturnsTrue($int): void
110  {
111  self::assertTrue(‪MathUtility::canBeInterpretedAsInteger($int));
112  }
113 
120  {
121  $objectWithNumericalStringRepresentation = new ‪MathUtilityTestClassWithStringRepresentationFixture();
122  $objectWithNumericalStringRepresentation->setString('1234');
123  $objectWithNonNumericalStringRepresentation = new ‪MathUtilityTestClassWithStringRepresentationFixture();
124  $objectWithNonNumericalStringRepresentation->setString('foo');
125  $objectWithEmptyStringRepresentation = new ‪MathUtilityTestClassWithStringRepresentationFixture();
126  $objectWithEmptyStringRepresentation->setString('');
127  return [
128  'int as string with leading zero' => ['01234'],
129  'positive int as string with plus modifier' => ['+1234'],
130  'negative int as string with leading zero' => ['-01234'],
131  'largest int plus one' => [PHP_INT_MAX + 1],
132  'string' => ['testInt'],
133  'empty string' => [''],
134  'int in string' => ['5 times of testInt'],
135  'int as string with space after' => ['5 '],
136  'int as string with space before' => [' 5'],
137  'int as string with many spaces before' => [' 5'],
138  'float' => [3.14159],
139  'float as string' => ['3.14159'],
140  'float as string only a dot' => ['10.'],
141  'float as string trailing zero would evaluate to int 10' => ['10.0'],
142  'float as string trailing zeros would evaluate to int 10' => ['10.00'],
143  'null' => [null],
144  'empty array' => [[]],
145  'int in array' => [[32425]],
146  'int as string in array' => [['32425']],
147  'object without string representation' => [new \stdClass()],
148  'object with numerical string representation' => [$objectWithNumericalStringRepresentation],
149  'object without numerical string representation' => [$objectWithNonNumericalStringRepresentation],
150  'object with empty string representation' => [$objectWithEmptyStringRepresentation],
151  ];
152  }
153 
158  public function ‪canBeInterpretedAsIntegerReturnsFalse($int): void
159  {
160  self::assertFalse(‪MathUtility::canBeInterpretedAsInteger($int));
161  }
162 
164  // Tests concerning canBeInterpretedAsFloat
166 
172  {
173  // testcases for Integer apply for float as well
175  $floatTestcases = [
176  'zero as float' => [(float)0],
177  'negative float' => [(float)-7.5],
178  'negative float as string with exp #1' => ['-7.5e3'],
179  'negative float as string with exp #2' => ['-7.5e03'],
180  'negative float as string with exp #3' => ['-7.5e-3'],
181  'float' => [3.14159],
182  'float as string' => ['3.14159'],
183  'float as string only a dot' => ['10.'],
184  'float as string trailing zero' => ['10.0'],
185  'float as string trailing zeros' => ['10.00'],
186  ];
187  return array_merge($intTestcases, $floatTestcases);
188  }
189 
194  public function ‪canBeInterpretedAsFloatReturnsTrue($val): void
195  {
196  self::assertTrue(‪MathUtility::canBeInterpretedAsFloat($val));
197  }
198 
205  {
206  $objectWithNumericalStringRepresentation = new ‪MathUtilityTestClassWithStringRepresentationFixture();
207  $objectWithNumericalStringRepresentation->setString('1234');
208  $objectWithNonNumericalStringRepresentation = new ‪MathUtilityTestClassWithStringRepresentationFixture();
209  $objectWithNonNumericalStringRepresentation->setString('foo');
210  $objectWithEmptyStringRepresentation = new ‪MathUtilityTestClassWithStringRepresentationFixture();
211  $objectWithEmptyStringRepresentation->setString('');
212  return [
213  // 'int as string with leading zero' => array('01234'),
214  // 'positive int as string with plus modifier' => array('+1234'),
215  // 'negative int as string with leading zero' => array('-01234'),
216  // 'largest int plus one' => array(PHP_INT_MAX + 1),
217  'string' => ['testInt'],
218  'empty string' => [''],
219  'int in string' => ['5 times of testInt'],
220  'int as string with space after' => ['5 '],
221  'int as string with space before' => [' 5'],
222  'int as string with many spaces before' => [' 5'],
223  'null' => [null],
224  'empty array' => [[]],
225  'int in array' => [[32425]],
226  'int as string in array' => [['32425']],
227  'negative float as string with invalid chars in exponent' => ['-7.5eX3'],
228  'object without string representation' => [new \stdClass()],
229  'object with numerical string representation' => [$objectWithNumericalStringRepresentation],
230  'object without numerical string representation' => [$objectWithNonNumericalStringRepresentation],
231  'object with empty string representation' => [$objectWithEmptyStringRepresentation],
232  ];
233  }
234 
239  public function ‪canBeInterpretedAsFloatReturnsFalse($int): void
240  {
241  self::assertFalse(‪MathUtility::canBeInterpretedAsFloat($int));
242  }
243 
245  // Tests concerning calculateWithPriorityToAdditionAndSubtraction
247 
253  {
254  return [
255  'add' => [9, '6 + 3'],
256  'substract with positive result' => [3, '6 - 3'],
257  'substract with negative result' => [-3, '3 - 6'],
258  'multiply' => [6, '2 * 3'],
259  'divide' => [2.5, '5 / 2'],
260  'modulus' => [1, '5 % 2'],
261  'power' => [8, '2 ^ 3'],
262  'three operands with non integer result' => [6.5, '5 + 3 / 2'],
263  'three operands with power' => [14, '5 + 3 ^ 2'],
264  'three operands with modulus' => [4, '5 % 2 + 3'],
265  'four operands' => [3, '2 + 6 / 2 - 2'],
266  'division by zero when dividing' => ['ERROR: dividing by zero', '2 / 0'],
267  'division by zero with modulus' => ['ERROR: dividing by zero', '2 % 0'],
268  ];
269  }
270 
276  {
277  self::assertEquals($expected, ‪MathUtility::calculateWithPriorityToAdditionAndSubtraction($expression));
278  }
279 
281  // Tests concerning calcParenthesis
283 
289  {
290  return [
291  'starts with parenthesis' => [18, '(6 + 3) * 2'],
292  'ends with parenthesis' => [6, '2 * (6 - 3)'],
293  'multiple parentheses' => [-6, '(3 - 6) * (4 - 2)'],
294  'nested parentheses' => [22, '2 * (3 + 2 + (3 * 2))'],
295  'parenthesis with division' => [15, '5 / 2 * (3 * 2)'],
296  ];
297  }
298 
303  public function ‪calculateWithParenthesesCorrectlyCalculatesExpression($expected, $expression): void
304  {
305  self::assertEquals($expected, ‪MathUtility::calculateWithParentheses($expression));
306  }
307 
309  // Tests concerning isIntegerInRange
311 
315  {
316  self::assertTrue(‪MathUtility::isIntegerInRange(1, 1, 2));
317  }
318 
323  {
324  self::assertTrue(‪MathUtility::isIntegerInRange(2, 1, 2));
325  }
326 
331  {
332  self::assertTrue(‪MathUtility::isIntegerInRange(10, 1, 100));
333  }
334 
339  {
340  self::assertFalse(‪MathUtility::isIntegerInRange(10, 1, 2));
341  }
342 
347  {
348  return [
349  'negative integer' => [-1],
350  'float' => [1.5],
351  'string' => ['string'],
352  'array' => [[]],
353  'object' => [new \stdClass()],
354  'boolean FALSE' => [false],
355  'NULL' => [null],
356  ];
357  }
358 
363  public function ‪isIntegerInRangeRejectsOtherDataTypes($inputValue): void
364  {
365  self::assertFalse(‪MathUtility::isIntegerInRange($inputValue, 0, 10));
366  }
367 }
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\functionCanBeInterpretedAsIntegerValidDataProvider
‪array functionCanBeInterpretedAsIntegerValidDataProvider()
Definition: MathUtilityTest.php:92
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeRejectsOtherDataTypesDataProvider
‪isIntegerInRangeRejectsOtherDataTypesDataProvider()
Definition: MathUtilityTest.php:346
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider
‪array forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider()
Definition: MathUtilityTest.php:37
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeRejectsValueOutsideOfRange
‪isIntegerInRangeRejectsValueOutsideOfRange()
Definition: MathUtilityTest.php:338
‪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:194
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\canBeInterpretedAsIntegerReturnsTrue
‪canBeInterpretedAsIntegerReturnsTrue($int)
Definition: MathUtilityTest.php:109
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\functionCanBeInterpretedAsFloatValidDataProvider
‪array functionCanBeInterpretedAsFloatValidDataProvider()
Definition: MathUtilityTest.php:171
‪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:252
‪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:24
‪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:303
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\forceIntegerInRangeForcesIntegerIntoDefaultBoundaries
‪forceIntegerInRangeForcesIntegerIntoDefaultBoundaries($expected, $value)
Definition: MathUtilityTest.php:52
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\functionCanBeInterpretedAsIntegerInvalidDataProvider
‪array functionCanBeInterpretedAsIntegerInvalidDataProvider()
Definition: MathUtilityTest.php:119
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest
Definition: MathUtilityTest.php:28
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\calculateWithPriorityToAdditionAndSubtractionCorrectlyCalculatesExpression
‪calculateWithPriorityToAdditionAndSubtractionCorrectlyCalculatesExpression($expected, $expression)
Definition: MathUtilityTest.php:275
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\functionCanBeInterpretedAsFloatInvalidDataProvider
‪array functionCanBeInterpretedAsFloatInvalidDataProvider()
Definition: MathUtilityTest.php:204
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\canBeInterpretedAsIntegerReturnsFalse
‪canBeInterpretedAsIntegerReturnsFalse($int)
Definition: MathUtilityTest.php:158
‪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:330
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeRejectsOtherDataTypes
‪isIntegerInRangeRejectsOtherDataTypes($inputValue)
Definition: MathUtilityTest.php:363
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\convertToPositiveIntegerReturnsTheInputValueForPositiveValues
‪convertToPositiveIntegerReturnsTheInputValueForPositiveValues()
Definition: MathUtilityTest.php:79
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\canBeInterpretedAsFloatReturnsFalse
‪canBeInterpretedAsFloatReturnsFalse($int)
Definition: MathUtilityTest.php:239
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeIncludesUpperBoundary
‪isIntegerInRangeIncludesUpperBoundary()
Definition: MathUtilityTest.php:322
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\calculateWithParenthesesDataProvider
‪array calculateWithParenthesesDataProvider()
Definition: MathUtilityTest.php:288
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeIncludesLowerBoundary
‪isIntegerInRangeIncludesLowerBoundary()
Definition: MathUtilityTest.php:314
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\convertToPositiveIntegerReturnsZeroForNegativeValues
‪convertToPositiveIntegerReturnsZeroForNegativeValues()
Definition: MathUtilityTest.php:71
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\forceIntegerInRangeSetsDefaultValueIfZeroValueIsGiven
‪forceIntegerInRangeSetsDefaultValueIfZeroValueIsGiven()
Definition: MathUtilityTest.php:60