‪TYPO3CMS  9.5
MathUtilityTest.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 
17 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
18 
22 class ‪MathUtilityTest extends UnitTestCase
23 {
25  // Tests concerning forceIntegerInRange
27 
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 
48  {
49  $this->assertEquals($expected, \‪TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($value, 0));
50  }
51 
56  {
57  $this->assertEquals(42, \‪TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange('', 0, 2000000000, 42));
58  }
59 
61  // Tests concerning convertToPositiveInteger
63 
67  {
68  $this->assertEquals(0, \‪TYPO3\CMS\Core\Utility\MathUtility::convertToPositiveInteger(-123));
69  }
70 
75  {
76  $this->assertEquals(123, \‪TYPO3\CMS\Core\Utility\MathUtility::convertToPositiveInteger(123));
77  }
78 
80  // Tests concerning canBeInterpretedAsInteger
82 
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 
105  {
106  $this->assertTrue(\‪TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($int));
107  }
108 
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 
154  {
155  $this->assertFalse(\‪TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($int));
156  }
157 
159  // Tests concerning canBeInterpretedAsFloat
161 
167  {
168  // testcases for Integer apply for float as well
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 
190  {
191  $this->assertTrue(\‪TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsFloat($val));
192  }
193 
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 
235  {
236  $this->assertFalse(\‪TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsFloat($int));
237  }
238 
240  // Tests concerning calculateWithPriorityToAdditionAndSubtraction
242 
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 
271  {
272  $this->assertEquals($expected, \‪TYPO3\CMS\Core\Utility\MathUtility::calculateWithPriorityToAdditionAndSubtraction($expression));
273  }
274 
276  // Tests concerning calcParenthesis
278 
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 
298  public function ‪calculateWithParenthesesCorrectlyCalculatesExpression($expected, $expression)
299  {
300  $this->assertEquals($expected, \‪TYPO3\CMS\Core\Utility\MathUtility::calculateWithParentheses($expression));
301  }
302 
304  // Tests concerning isIntegerInRange
306 
310  {
311  $this->assertTrue(\‪TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(1, 1, 2));
312  }
313 
318  {
319  $this->assertTrue(\‪TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(2, 1, 2));
320  }
321 
326  {
327  $this->assertTrue(\‪TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(10, 1, 100));
328  }
329 
334  {
335  $this->assertFalse(\‪TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(10, 1, 2));
336  }
337 
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 
358  public function ‪isIntegerInRangeRejectsOtherDataTypes($inputValue)
359  {
360  $this->assertFalse(\‪TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange($inputValue, 0, 10));
361  }
362 }
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\functionCanBeInterpretedAsIntegerValidDataProvider
‪array functionCanBeInterpretedAsIntegerValidDataProvider()
Definition: MathUtilityTest.php:87
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeRejectsOtherDataTypesDataProvider
‪isIntegerInRangeRejectsOtherDataTypesDataProvider()
Definition: MathUtilityTest.php:341
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider
‪array forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider()
Definition: MathUtilityTest.php:32
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeRejectsValueOutsideOfRange
‪isIntegerInRangeRejectsValueOutsideOfRange()
Definition: MathUtilityTest.php:333
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\canBeInterpretedAsFloatReturnsTrue
‪canBeInterpretedAsFloatReturnsTrue($val)
Definition: MathUtilityTest.php:189
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\canBeInterpretedAsIntegerReturnsTrue
‪canBeInterpretedAsIntegerReturnsTrue($int)
Definition: MathUtilityTest.php:104
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\functionCanBeInterpretedAsFloatValidDataProvider
‪array functionCanBeInterpretedAsFloatValidDataProvider()
Definition: MathUtilityTest.php:166
‪TYPO3
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\calculateWithPriorityToAdditionAndSubtractionDataProvider
‪array calculateWithPriorityToAdditionAndSubtractionDataProvider()
Definition: MathUtilityTest.php:247
‪TYPO3\CMS\Core\Tests\Unit\Utility
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\calculateWithParenthesesCorrectlyCalculatesExpression
‪calculateWithParenthesesCorrectlyCalculatesExpression($expected, $expression)
Definition: MathUtilityTest.php:298
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\forceIntegerInRangeForcesIntegerIntoDefaultBoundaries
‪forceIntegerInRangeForcesIntegerIntoDefaultBoundaries($expected, $value)
Definition: MathUtilityTest.php:47
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\functionCanBeInterpretedAsIntegerInvalidDataProvider
‪array functionCanBeInterpretedAsIntegerInvalidDataProvider()
Definition: MathUtilityTest.php:114
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest
Definition: MathUtilityTest.php:23
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\calculateWithPriorityToAdditionAndSubtractionCorrectlyCalculatesExpression
‪calculateWithPriorityToAdditionAndSubtractionCorrectlyCalculatesExpression($expected, $expression)
Definition: MathUtilityTest.php:270
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\functionCanBeInterpretedAsFloatInvalidDataProvider
‪array functionCanBeInterpretedAsFloatInvalidDataProvider()
Definition: MathUtilityTest.php:199
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\canBeInterpretedAsIntegerReturnsFalse
‪canBeInterpretedAsIntegerReturnsFalse($int)
Definition: MathUtilityTest.php:153
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeAcceptsValueInRange
‪isIntegerInRangeAcceptsValueInRange()
Definition: MathUtilityTest.php:325
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeRejectsOtherDataTypes
‪isIntegerInRangeRejectsOtherDataTypes($inputValue)
Definition: MathUtilityTest.php:358
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\convertToPositiveIntegerReturnsTheInputValueForPositiveValues
‪convertToPositiveIntegerReturnsTheInputValueForPositiveValues()
Definition: MathUtilityTest.php:74
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\canBeInterpretedAsFloatReturnsFalse
‪canBeInterpretedAsFloatReturnsFalse($int)
Definition: MathUtilityTest.php:234
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeIncludesUpperBoundary
‪isIntegerInRangeIncludesUpperBoundary()
Definition: MathUtilityTest.php:317
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\calculateWithParenthesesDataProvider
‪array calculateWithParenthesesDataProvider()
Definition: MathUtilityTest.php:283
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeIncludesLowerBoundary
‪isIntegerInRangeIncludesLowerBoundary()
Definition: MathUtilityTest.php:309
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\convertToPositiveIntegerReturnsZeroForNegativeValues
‪convertToPositiveIntegerReturnsZeroForNegativeValues()
Definition: MathUtilityTest.php:66
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\forceIntegerInRangeSetsDefaultValueIfZeroValueIsGiven
‪forceIntegerInRangeSetsDefaultValueIfZeroValueIsGiven()
Definition: MathUtilityTest.php:55