‪TYPO3CMS  ‪main
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 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
26 final class ‪MathUtilityTest extends UnitTestCase
27 {
29  {
30  return [
31  'negativeValue' => [0, -10],
32  'normalValue' => [30, 30],
33  'veryHighValue' => [2000000000, PHP_INT_MAX],
34  'zeroValue' => [0, 0],
35  'anotherNormalValue' => [12309, 12309],
36  ];
37  }
38 
39  #[DataProvider('forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider')]
40  #[Test]
41  public function ‪forceIntegerInRangeForcesIntegerIntoDefaultBoundaries($expected, $value): void
42  {
43  self::assertEquals($expected, ‪MathUtility::forceIntegerInRange($value, 0));
44  }
45 
46  #[Test]
48  {
49  self::assertEquals(42, ‪MathUtility::forceIntegerInRange('', 0, 2000000000, 42));
50  }
51 
52  #[Test]
54  {
55  self::assertEquals(0, ‪MathUtility::convertToPositiveInteger(-123));
56  }
57 
58  #[Test]
60  {
61  self::assertEquals(123, ‪MathUtility::convertToPositiveInteger(123));
62  }
63 
65  {
66  return [
67  'int' => [32425],
68  'negative int' => [-32425],
69  'largest int' => [PHP_INT_MAX],
70  'int as string' => ['32425'],
71  'negative int as string' => ['-32425'],
72  'zero' => [0],
73  'zero as string' => ['0'],
74  'true' => [true],
75  ];
76  }
77 
78  #[DataProvider('functionCanBeInterpretedAsIntegerValidDataProvider')]
79  #[Test]
80  public function ‪canBeInterpretedAsIntegerReturnsTrue($int): void
81  {
82  self::assertTrue(‪MathUtility::canBeInterpretedAsInteger($int));
83  }
84 
86  {
87  $objectWithNumericalStringRepresentation = new ‪MathUtilityTestClassWithStringRepresentationFixture();
88  $objectWithNumericalStringRepresentation->setString('1234');
89  $objectWithNonNumericalStringRepresentation = new ‪MathUtilityTestClassWithStringRepresentationFixture();
90  $objectWithNonNumericalStringRepresentation->setString('foo');
91  $objectWithEmptyStringRepresentation = new ‪MathUtilityTestClassWithStringRepresentationFixture();
92  $objectWithEmptyStringRepresentation->setString('');
93  return [
94  'int as string with leading zero' => ['01234'],
95  'positive int as string with plus modifier' => ['+1234'],
96  'negative int as string with leading zero' => ['-01234'],
97  'largest int plus one' => [PHP_INT_MAX + 1],
98  'string' => ['testInt'],
99  'empty string' => [''],
100  'int in string' => ['5 times of testInt'],
101  'int as string with space after' => ['5 '],
102  'int as string with space before' => [' 5'],
103  'int as string with many spaces before' => [' 5'],
104  'float' => [3.14159],
105  'float as string' => ['3.14159'],
106  'float as string only a dot' => ['10.'],
107  'float as string trailing zero would evaluate to int 10' => ['10.0'],
108  'float as string trailing zeros would evaluate to int 10' => ['10.00'],
109  'null' => [null],
110  'false' => [false],
111  'empty array' => [[]],
112  'int in array' => [[32425]],
113  'int as string in array' => [['32425']],
114  'object without string representation' => [new \stdClass()],
115  'object with numerical string representation' => [$objectWithNumericalStringRepresentation],
116  'object without numerical string representation' => [$objectWithNonNumericalStringRepresentation],
117  'object with empty string representation' => [$objectWithEmptyStringRepresentation],
118  ];
119  }
120 
121  #[DataProvider('functionCanBeInterpretedAsIntegerInvalidDataProvider')]
122  #[Test]
123  public function ‪canBeInterpretedAsIntegerReturnsFalse($int): void
124  {
125  self::assertFalse(‪MathUtility::canBeInterpretedAsInteger($int));
126  }
127 
129  {
130  // testcases for Integer apply for float as well
132  $floatTestcases = [
133  'zero as float' => [(float)0],
134  'negative float' => [(float)-7.5],
135  'negative float as string with exp #1' => ['-7.5e3'],
136  'negative float as string with exp #2' => ['-7.5e03'],
137  'negative float as string with exp #3' => ['-7.5e-3'],
138  'float' => [3.14159],
139  'float as string' => ['3.14159'],
140  'float as string only a dot' => ['10.'],
141  'float as string trailing zero' => ['10.0'],
142  'float as string trailing zeros' => ['10.00'],
143  ];
144  return array_merge($intTestcases, $floatTestcases);
145  }
146 
147  #[DataProvider('functionCanBeInterpretedAsFloatValidDataProvider')]
148  #[Test]
149  public function ‪canBeInterpretedAsFloatReturnsTrue($val): void
150  {
151  self::assertTrue(‪MathUtility::canBeInterpretedAsFloat($val));
152  }
153 
155  {
156  $objectWithNumericalStringRepresentation = new ‪MathUtilityTestClassWithStringRepresentationFixture();
157  $objectWithNumericalStringRepresentation->setString('1234');
158  $objectWithNonNumericalStringRepresentation = new ‪MathUtilityTestClassWithStringRepresentationFixture();
159  $objectWithNonNumericalStringRepresentation->setString('foo');
160  $objectWithEmptyStringRepresentation = new ‪MathUtilityTestClassWithStringRepresentationFixture();
161  $objectWithEmptyStringRepresentation->setString('');
162  return [
163  // 'int as string with leading zero' => array('01234'),
164  // 'positive int as string with plus modifier' => array('+1234'),
165  // 'negative int as string with leading zero' => array('-01234'),
166  // 'largest int plus one' => array(PHP_INT_MAX + 1),
167  'string' => ['testInt'],
168  'empty string' => [''],
169  'int in string' => ['5 times of testInt'],
170  'int as string with space after' => ['5 '],
171  'int as string with space before' => [' 5'],
172  'int as string with many spaces before' => [' 5'],
173  'null' => [null],
174  'empty array' => [[]],
175  'int in array' => [[32425]],
176  'int as string in array' => [['32425']],
177  'negative float as string with invalid chars in exponent' => ['-7.5eX3'],
178  'object without string representation' => [new \stdClass()],
179  'object with numerical string representation' => [$objectWithNumericalStringRepresentation],
180  'object without numerical string representation' => [$objectWithNonNumericalStringRepresentation],
181  'object with empty string representation' => [$objectWithEmptyStringRepresentation],
182  ];
183  }
184 
185  #[DataProvider('functionCanBeInterpretedAsFloatInvalidDataProvider')]
186  #[Test]
187  public function ‪canBeInterpretedAsFloatReturnsFalse($int): void
188  {
189  self::assertFalse(‪MathUtility::canBeInterpretedAsFloat($int));
190  }
191 
193  {
194  return [
195  'add' => [9, '6 + 3'],
196  'substract with positive result' => [3, '6 - 3'],
197  'substract with negative result' => [-3, '3 - 6'],
198  'multiply' => [6, '2 * 3'],
199  'divide' => [2.5, '5 / 2'],
200  'modulus' => [1, '5 % 2'],
201  'power' => [8, '2 ^ 3'],
202  'three operands with non integer result' => [6.5, '5 + 3 / 2'],
203  'three operands with power' => [14, '5 + 3 ^ 2'],
204  'three operands with modulus' => [4, '5 % 2 + 3'],
205  'four operands' => [3, '2 + 6 / 2 - 2'],
206  'division by zero when dividing' => ['ERROR: dividing by zero', '2 / 0'],
207  'division by zero with modulus' => ['ERROR: dividing by zero', '2 % 0'],
208  ];
209  }
210 
211  #[DataProvider('calculateWithPriorityToAdditionAndSubtractionDataProvider')]
212  #[Test]
214  {
215  self::assertEquals($expected, ‪MathUtility::calculateWithPriorityToAdditionAndSubtraction($expression));
216  }
217 
218  public static function ‪calculateWithParenthesesDataProvider(): array
219  {
220  return [
221  'starts with parenthesis' => [18, '(6 + 3) * 2'],
222  'ends with parenthesis' => [6, '2 * (6 - 3)'],
223  'multiple parentheses' => [-6, '(3 - 6) * (4 - 2)'],
224  'nested parentheses' => [22, '2 * (3 + 2 + (3 * 2))'],
225  'parenthesis with division' => [15, '5 / 2 * (3 * 2)'],
226  ];
227  }
228 
229  #[DataProvider('calculateWithParenthesesDataProvider')]
230  #[Test]
231  public function ‪calculateWithParenthesesCorrectlyCalculatesExpression($expected, $expression): void
232  {
233  self::assertEquals($expected, ‪MathUtility::calculateWithParentheses($expression));
234  }
235 
236  #[Test]
238  {
239  self::assertTrue(‪MathUtility::isIntegerInRange(1, 1, 2));
240  }
241 
242  #[Test]
244  {
245  self::assertTrue(‪MathUtility::isIntegerInRange(2, 1, 2));
246  }
247 
248  #[Test]
250  {
251  self::assertTrue(‪MathUtility::isIntegerInRange(10, 1, 100));
252  }
253 
254  #[Test]
256  {
257  self::assertFalse(‪MathUtility::isIntegerInRange(10, 1, 2));
258  }
259 
261  {
262  return [
263  'negative integer' => [-1],
264  'float' => [1.5],
265  'string' => ['string'],
266  'array' => [[]],
267  'object' => [new \stdClass()],
268  'boolean FALSE' => [false],
269  'NULL' => [null],
270  ];
271  }
272 
273  #[DataProvider('isIntegerInRangeRejectsOtherDataTypesDataProvider')]
274  #[Test]
275  public function ‪isIntegerInRangeRejectsOtherDataTypes($inputValue): void
276  {
277  self::assertFalse(‪MathUtility::isIntegerInRange($inputValue, 0, 10));
278  }
279 }
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeRejectsValueOutsideOfRange
‪isIntegerInRangeRejectsValueOutsideOfRange()
Definition: MathUtilityTest.php:255
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\canBeInterpretedAsFloatReturnsTrue
‪canBeInterpretedAsFloatReturnsTrue($val)
Definition: MathUtilityTest.php:149
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\canBeInterpretedAsIntegerReturnsTrue
‪canBeInterpretedAsIntegerReturnsTrue($int)
Definition: MathUtilityTest.php:80
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\functionCanBeInterpretedAsFloatInvalidDataProvider
‪static functionCanBeInterpretedAsFloatInvalidDataProvider()
Definition: MathUtilityTest.php:154
‪TYPO3\CMS\Core\Utility\MathUtility\calculateWithParentheses
‪static string calculateWithParentheses(string $string)
Definition: MathUtility.php:167
‪TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture
Definition: MathUtilityTestClassWithStringRepresentationFixture.php:24
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\functionCanBeInterpretedAsFloatValidDataProvider
‪static functionCanBeInterpretedAsFloatValidDataProvider()
Definition: MathUtilityTest.php:128
‪TYPO3\CMS\Core\Tests\Unit\Utility
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\calculateWithPriorityToAdditionAndSubtractionDataProvider
‪static calculateWithPriorityToAdditionAndSubtractionDataProvider()
Definition: MathUtilityTest.php:192
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Core\Utility\MathUtility\convertToPositiveInteger
‪static convertToPositiveInteger(mixed $theInt)
Definition: MathUtility.php:55
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\calculateWithParenthesesCorrectlyCalculatesExpression
‪calculateWithParenthesesCorrectlyCalculatesExpression($expected, $expression)
Definition: MathUtilityTest.php:231
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\forceIntegerInRangeForcesIntegerIntoDefaultBoundaries
‪forceIntegerInRangeForcesIntegerIntoDefaultBoundaries($expected, $value)
Definition: MathUtilityTest.php:41
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest
Definition: MathUtilityTest.php:27
‪TYPO3\CMS\Core\Utility\MathUtility\calculateWithPriorityToAdditionAndSubtraction
‪static float string calculateWithPriorityToAdditionAndSubtraction(string $string)
Definition: MathUtility.php:107
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\calculateWithPriorityToAdditionAndSubtractionCorrectlyCalculatesExpression
‪calculateWithPriorityToAdditionAndSubtractionCorrectlyCalculatesExpression($expected, $expression)
Definition: MathUtilityTest.php:213
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\canBeInterpretedAsIntegerReturnsFalse
‪canBeInterpretedAsIntegerReturnsFalse($int)
Definition: MathUtilityTest.php:123
‪TYPO3\CMS\Core\Utility\MathUtility\isIntegerInRange
‪static isIntegerInRange(mixed $value, int $minimum, int $maximum)
Definition: MathUtility.php:196
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeAcceptsValueInRange
‪isIntegerInRangeAcceptsValueInRange()
Definition: MathUtilityTest.php:249
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\calculateWithParenthesesDataProvider
‪static calculateWithParenthesesDataProvider()
Definition: MathUtilityTest.php:218
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeRejectsOtherDataTypes
‪isIntegerInRangeRejectsOtherDataTypes($inputValue)
Definition: MathUtilityTest.php:275
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\convertToPositiveIntegerReturnsTheInputValueForPositiveValues
‪convertToPositiveIntegerReturnsTheInputValueForPositiveValues()
Definition: MathUtilityTest.php:59
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\canBeInterpretedAsFloatReturnsFalse
‪canBeInterpretedAsFloatReturnsFalse($int)
Definition: MathUtilityTest.php:187
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeRejectsOtherDataTypesDataProvider
‪static isIntegerInRangeRejectsOtherDataTypesDataProvider()
Definition: MathUtilityTest.php:260
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeIncludesUpperBoundary
‪isIntegerInRangeIncludesUpperBoundary()
Definition: MathUtilityTest.php:243
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider
‪static forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider()
Definition: MathUtilityTest.php:28
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsFloat
‪static bool canBeInterpretedAsFloat(mixed $var)
Definition: MathUtility.php:86
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\functionCanBeInterpretedAsIntegerValidDataProvider
‪static functionCanBeInterpretedAsIntegerValidDataProvider()
Definition: MathUtilityTest.php:64
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange(mixed $theInt, int $min, int $max=2000000000, int $defaultValue=0)
Definition: MathUtility.php:34
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\isIntegerInRangeIncludesLowerBoundary
‪isIntegerInRangeIncludesLowerBoundary()
Definition: MathUtilityTest.php:237
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\convertToPositiveIntegerReturnsZeroForNegativeValues
‪convertToPositiveIntegerReturnsZeroForNegativeValues()
Definition: MathUtilityTest.php:53
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\forceIntegerInRangeSetsDefaultValueIfZeroValueIsGiven
‪forceIntegerInRangeSetsDefaultValueIfZeroValueIsGiven()
Definition: MathUtilityTest.php:47
‪TYPO3\CMS\Core\Tests\Unit\Utility\MathUtilityTest\functionCanBeInterpretedAsIntegerInvalidDataProvider
‪static functionCanBeInterpretedAsIntegerInvalidDataProvider()
Definition: MathUtilityTest.php:85