TYPO3 CMS  TYPO3_7-6
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 
21 {
23  // Tests concerning forceIntegerInRange
25 
31  {
32  return [
33  'negativeValue' => [0, -10],
34  'normalValue' => [30, 30],
35  'veryHighValue' => [2000000000, PHP_INT_MAX],
36  'zeroValue' => [0, 0],
37  'anotherNormalValue' => [12309, 12309]
38  ];
39  }
40 
45  public function forceIntegerInRangeForcesIntegerIntoDefaultBoundaries($expected, $value)
46  {
47  $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($value, 0));
48  }
49 
54  {
55  $this->assertEquals(42, \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange('', 0, 2000000000, 42));
56  }
57 
59  // Tests concerning convertToPositiveInteger
61 
65  {
66  $this->assertEquals(0, \TYPO3\CMS\Core\Utility\MathUtility::convertToPositiveInteger(-123));
67  }
68 
73  {
74  $this->assertEquals(123, \TYPO3\CMS\Core\Utility\MathUtility::convertToPositiveInteger(123));
75  }
76 
78  // Tests concerning canBeInterpretedAsInteger
80 
86  {
87  return [
88  'int' => [32425],
89  'negative int' => [-32425],
90  'largest int' => [PHP_INT_MAX],
91  'int as string' => ['32425'],
92  'negative int as string' => ['-32425'],
93  'zero' => [0],
94  'zero as string' => ['0']
95  ];
96  }
97 
103  {
104  $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($int));
105  }
106 
113  {
114  $objectWithNumericalStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
115  $objectWithNumericalStringRepresentation->setString('1234');
116  $objectWithNonNumericalStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
117  $objectWithNonNumericalStringRepresentation->setString('foo');
118  $objectWithEmptyStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
119  $objectWithEmptyStringRepresentation->setString('');
120  return [
121  'int as string with leading zero' => ['01234'],
122  'positive int as string with plus modifier' => ['+1234'],
123  'negative int as string with leading zero' => ['-01234'],
124  'largest int plus one' => [PHP_INT_MAX + 1],
125  'string' => ['testInt'],
126  'empty string' => [''],
127  'int in string' => ['5 times of testInt'],
128  'int as string with space after' => ['5 '],
129  'int as string with space before' => [' 5'],
130  'int as string with many spaces before' => [' 5'],
131  'float' => [3.14159],
132  'float as string' => ['3.14159'],
133  'float as string only a dot' => ['10.'],
134  'float as string trailing zero would evaluate to int 10' => ['10.0'],
135  'float as string trailing zeros would evaluate to int 10' => ['10.00'],
136  'null' => [null],
137  'empty array' => [[]],
138  'int in array' => [[32425]],
139  'int as string in array' => [['32425']],
140  'object without string representation' => [new \stdClass()],
141  'object with numerical string representation' => [$objectWithNumericalStringRepresentation],
142  'object without numerical string representation' => [$objectWithNonNumericalStringRepresentation],
143  'object with empty string representation' => [$objectWithEmptyStringRepresentation]
144  ];
145  }
146 
152  {
153  $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($int));
154  }
155 
157  // Tests concerning canBeInterpretedAsFloat
159 
165  {
166  // testcases for Integer apply for float as well
168  $floatTestcases = [
169  'zero as float' => [(float) 0],
170  'negative float' => [(float) -7.5],
171  'negative float as string with exp #1' => ['-7.5e3'],
172  'negative float as string with exp #2' => ['-7.5e03'],
173  'negative float as string with exp #3' => ['-7.5e-3'],
174  'float' => [3.14159],
175  'float as string' => ['3.14159'],
176  'float as string only a dot' => ['10.'],
177  'float as string trailing zero' => ['10.0'],
178  'float as string trailing zeros' => ['10.00'],
179  ];
180  return array_merge($intTestcases, $floatTestcases);
181  }
182 
188  {
189  $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsFloat($val));
190  }
191 
198  {
199  $objectWithNumericalStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
200  $objectWithNumericalStringRepresentation->setString('1234');
201  $objectWithNonNumericalStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
202  $objectWithNonNumericalStringRepresentation->setString('foo');
203  $objectWithEmptyStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
204  $objectWithEmptyStringRepresentation->setString('');
205  return [
206  // 'int as string with leading zero' => array('01234'),
207  // 'positive int as string with plus modifier' => array('+1234'),
208  // 'negative int as string with leading zero' => array('-01234'),
209  // 'largest int plus one' => array(PHP_INT_MAX + 1),
210  'string' => ['testInt'],
211  'empty string' => [''],
212  'int in string' => ['5 times of testInt'],
213  'int as string with space after' => ['5 '],
214  'int as string with space before' => [' 5'],
215  'int as string with many spaces before' => [' 5'],
216  'null' => [null],
217  'empty array' => [[]],
218  'int in array' => [[32425]],
219  'int as string in array' => [['32425']],
220  'negative float as string with invalid chars in exponent' => ['-7.5eX3'],
221  'object without string representation' => [new \stdClass()],
222  'object with numerical string representation' => [$objectWithNumericalStringRepresentation],
223  'object without numerical string representation' => [$objectWithNonNumericalStringRepresentation],
224  'object with empty string representation' => [$objectWithEmptyStringRepresentation]
225  ];
226  }
227 
233  {
234  $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsFloat($int));
235  }
236 
238  // Tests concerning calculateWithPriorityToAdditionAndSubtraction
240 
246  {
247  return [
248  'add' => [9, '6 + 3'],
249  'substract with positive result' => [3, '6 - 3'],
250  'substract with negative result' => [-3, '3 - 6'],
251  'multiply' => [6, '2 * 3'],
252  'divide' => [2.5, '5 / 2'],
253  'modulus' => [1, '5 % 2'],
254  'power' => [8, '2 ^ 3'],
255  'three operands with non integer result' => [6.5, '5 + 3 / 2'],
256  'three operands with power' => [14, '5 + 3 ^ 2'],
257  'three operads with modulus' => [4, '5 % 2 + 3'],
258  'four operands' => [3, '2 + 6 / 2 - 2'],
259  'division by zero when dividing' => ['ERROR: dividing by zero', '2 / 0'],
260  'division by zero with modulus' => ['ERROR: dividing by zero', '2 % 0']
261  ];
262  }
263 
269  {
270  $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\MathUtility::calculateWithPriorityToAdditionAndSubtraction($expression));
271  }
272 
274  // Tests concerning calcParenthesis
276 
282  {
283  return [
284  'starts with parenthesis' => [18, '(6 + 3) * 2'],
285  'ends with parenthesis' => [6, '2 * (6 - 3)'],
286  'multiple parentheses' => [-6, '(3 - 6) * (4 - 2)'],
287  'nested parentheses' => [22, '2 * (3 + 2 + (3 * 2))'],
288  'parenthesis with division' => [15, '5 / 2 * (3 * 2)']
289  ];
290  }
291 
296  public function calculateWithParenthesesCorrectlyCalculatesExpression($expected, $expression)
297  {
298  $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\MathUtility::calculateWithParentheses($expression));
299  }
300 
302  // Tests concerning isIntegerInRange
304 
308  {
309  $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(1, 1, 2));
310  }
311 
316  {
317  $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(2, 1, 2));
318  }
319 
324  {
325  $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(10, 1, 100));
326  }
327 
332  {
333  $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(10, 1, 2));
334  }
335 
340  {
341  return [
342  'negative integer' => [-1],
343  'float' => [1.5],
344  'string' => ['string'],
345  'array' => [[]],
346  'object' => [new \stdClass()],
347  'boolean FALSE' => [false],
348  'NULL' => [null]
349  ];
350  }
351 
356  public function isIntegerInRangeRejectsOtherDataTypes($inputValue)
357  {
358  $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange($inputValue, 0, 10));
359  }
360 }
calculateWithPriorityToAdditionAndSubtractionCorrectlyCalculatesExpression($expected, $expression)
calculateWithParenthesesCorrectlyCalculatesExpression($expected, $expression)
forceIntegerInRangeForcesIntegerIntoDefaultBoundaries($expected, $value)