[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Utility / MathUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Utility;
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 /**
18 * Testcase for class \TYPO3\CMS\Core\Utility\MathUtility
19 */
20 class MathUtilityTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
21 {
22 //////////////////////////////////
23 // Tests concerning forceIntegerInRange
24 //////////////////////////////////
25 /**
26 * Data provider for forceIntegerInRangeForcesIntegerIntoBoundaries
27 *
28 * @return array expected values, arithmetic expression
29 */
30 public function forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider()
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
41 /**
42 * @test
43 * @dataProvider forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider
44 */
45 public function forceIntegerInRangeForcesIntegerIntoDefaultBoundaries($expected, $value)
46 {
47 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($value, 0));
48 }
49
50 /**
51 * @test
52 */
53 public function forceIntegerInRangeSetsDefaultValueIfZeroValueIsGiven()
54 {
55 $this->assertEquals(42, \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange('', 0, 2000000000, 42));
56 }
57
58 //////////////////////////////////
59 // Tests concerning convertToPositiveInteger
60 //////////////////////////////////
61 /**
62 * @test
63 */
64 public function convertToPositiveIntegerReturnsZeroForNegativeValues()
65 {
66 $this->assertEquals(0, \TYPO3\CMS\Core\Utility\MathUtility::convertToPositiveInteger(-123));
67 }
68
69 /**
70 * @test
71 */
72 public function convertToPositiveIntegerReturnsTheInputValueForPositiveValues()
73 {
74 $this->assertEquals(123, \TYPO3\CMS\Core\Utility\MathUtility::convertToPositiveInteger(123));
75 }
76
77 ///////////////////////////////
78 // Tests concerning canBeInterpretedAsInteger
79 ///////////////////////////////
80 /**
81 * Data provider for canBeInterpretedAsIntegerReturnsTrue
82 *
83 * @return array Data sets
84 */
85 public function functionCanBeInterpretedAsIntegerValidDataProvider()
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
98 /**
99 * @test
100 * @dataProvider functionCanBeInterpretedAsIntegerValidDataProvider
101 */
102 public function canBeInterpretedAsIntegerReturnsTrue($int)
103 {
104 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($int));
105 }
106
107 /**
108 * Data provider for canBeInterpretedAsIntegerReturnsFalse
109 *
110 * @return array Data sets
111 */
112 public function functionCanBeInterpretedAsIntegerInvalidDataProvider()
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
147 /**
148 * @test
149 * @dataProvider functionCanBeInterpretedAsIntegerInvalidDataProvider
150 */
151 public function canBeInterpretedAsIntegerReturnsFalse($int)
152 {
153 $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($int));
154 }
155
156 ///////////////////////////////
157 // Tests concerning canBeInterpretedAsFloat
158 ///////////////////////////////
159 /**
160 * Data provider for canBeInterpretedAsFloatReturnsTrue
161 *
162 * @return array Data sets
163 */
164 public function functionCanBeInterpretedAsFloatValidDataProvider()
165 {
166 // testcases for Integer apply for float as well
167 $intTestcases = $this->functionCanBeInterpretedAsIntegerValidDataProvider();
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
183 /**
184 * @test
185 * @dataProvider functionCanBeInterpretedAsFloatValidDataProvider
186 */
187 public function canBeInterpretedAsFloatReturnsTrue($val)
188 {
189 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsFloat($val));
190 }
191
192 /**
193 * Data provider for canBeInterpretedAsFloatReturnsFalse
194 *
195 * @return array Data sets
196 */
197 public function functionCanBeInterpretedAsFloatInvalidDataProvider()
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
228 /**
229 * @test
230 * @dataProvider functionCanBeInterpretedAsFloatInvalidDataProvider
231 */
232 public function canBeInterpretedAsFloatReturnsFalse($int)
233 {
234 $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsFloat($int));
235 }
236
237 //////////////////////////////////
238 // Tests concerning calculateWithPriorityToAdditionAndSubtraction
239 //////////////////////////////////
240 /**
241 * Data provider for calculateWithPriorityToAdditionAndSubtraction
242 *
243 * @return array expected values, arithmetic expression
244 */
245 public function calculateWithPriorityToAdditionAndSubtractionDataProvider()
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
264 /**
265 * @test
266 * @dataProvider calculateWithPriorityToAdditionAndSubtractionDataProvider
267 */
268 public function calculateWithPriorityToAdditionAndSubtractionCorrectlyCalculatesExpression($expected, $expression)
269 {
270 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\MathUtility::calculateWithPriorityToAdditionAndSubtraction($expression));
271 }
272
273 //////////////////////////////////
274 // Tests concerning calcParenthesis
275 //////////////////////////////////
276 /**
277 * Data provider for calcParenthesis
278 *
279 * @return array expected values, arithmetic expression
280 */
281 public function calculateWithParenthesesDataProvider()
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
292 /**
293 * @test
294 * @dataProvider calculateWithParenthesesDataProvider
295 */
296 public function calculateWithParenthesesCorrectlyCalculatesExpression($expected, $expression)
297 {
298 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\MathUtility::calculateWithParentheses($expression));
299 }
300
301 //////////////////////////////////
302 // Tests concerning isIntegerInRange
303 //////////////////////////////////
304 /**
305 * @test
306 */
307 public function isIntegerInRangeIncludesLowerBoundary()
308 {
309 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(1, 1, 2));
310 }
311
312 /**
313 * @test
314 */
315 public function isIntegerInRangeIncludesUpperBoundary()
316 {
317 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(2, 1, 2));
318 }
319
320 /**
321 * @test
322 */
323 public function isIntegerInRangeAcceptsValueInRange()
324 {
325 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(10, 1, 100));
326 }
327
328 /**
329 * @test
330 */
331 public function isIntegerInRangeRejectsValueOutsideOfRange()
332 {
333 $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(10, 1, 2));
334 }
335
336 /**
337 * Data provider or isIntegerInRangeRejectsOtherDataTypes
338 */
339 public function isIntegerInRangeRejectsOtherDataTypesDataProvider()
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
352 /**
353 * @test
354 * @dataProvider isIntegerInRangeRejectsOtherDataTypesDataProvider
355 */
356 public function isIntegerInRangeRejectsOtherDataTypes($inputValue)
357 {
358 $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange($inputValue, 0, 10));
359 }
360 }