[TASK] Re-work/simplify copyright header in PHP files - Part 8
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Utility / ArrayUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\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 * Test case
19 */
20 class ArrayUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
21
22 /**
23 * @test
24 */
25 public function containsMultipleTypesReturnsFalseOnEmptyArray() {
26 $this->assertFalse(\TYPO3\CMS\Extbase\Utility\ArrayUtility::containsMultipleTypes(array()));
27 }
28
29 /**
30 * @test
31 */
32 public function containsMultipleTypesReturnsFalseOnArrayWithIntegers() {
33 $this->assertFalse(\TYPO3\CMS\Extbase\Utility\ArrayUtility::containsMultipleTypes(array(1, 2, 3)));
34 }
35
36 /**
37 * @test
38 */
39 public function containsMultipleTypesReturnsFalseOnArrayWithObjects() {
40 $this->assertFalse(\TYPO3\CMS\Extbase\Utility\ArrayUtility::containsMultipleTypes(array(new \stdClass(), new \stdClass(), new \stdClass())));
41 }
42
43 /**
44 * @test
45 */
46 public function containsMultipleTypesReturnsTrueOnMixedArray() {
47 $this->assertTrue(\TYPO3\CMS\Extbase\Utility\ArrayUtility::containsMultipleTypes(array(1, 'string', 1.25, new \stdClass())));
48 }
49
50 /**
51 * @test
52 */
53 public function getValueByPathReturnsTheValueOfANestedArrayByFollowingTheGivenSimplePath() {
54 $array = array('Foo' => 'the value');
55 $this->assertSame('the value', \TYPO3\CMS\Extbase\Utility\ArrayUtility::getValueByPath($array, array('Foo')));
56 }
57
58 /**
59 * @test
60 */
61 public function getValueByPathReturnsTheValueOfANestedArrayByFollowingTheGivenPath() {
62 $array = array('Foo' => array('Bar' => array('Baz' => array(2 => 'the value'))));
63 $this->assertSame('the value', \TYPO3\CMS\Extbase\Utility\ArrayUtility::getValueByPath($array, array('Foo', 'Bar', 'Baz', 2)));
64 }
65
66 /**
67 * @test
68 */
69 public function getValueByPathReturnsTheValueOfANestedArrayByFollowingTheGivenPathIfPathIsString() {
70 $path = 'Foo.Bar.Baz.2';
71 $array = array('Foo' => array('Bar' => array('Baz' => array(2 => 'the value'))));
72 $expectedResult = 'the value';
73 $actualResult = \TYPO3\CMS\Extbase\Utility\ArrayUtility::getValueByPath($array, $path);
74 $this->assertSame($expectedResult, $actualResult);
75 }
76
77 /**
78 * @test
79 * @expectedException \InvalidArgumentException
80 */
81 public function getValueByPathThrowsExceptionIfPathIsNoArrayOrString() {
82 $array = array('Foo' => array('Bar' => array('Baz' => array(2 => 'the value'))));
83 \TYPO3\CMS\Extbase\Utility\ArrayUtility::getValueByPath($array, NULL);
84 }
85
86 /**
87 * @test
88 */
89 public function getValueByPathReturnsNullIfTheSegementsOfThePathDontExist() {
90 $array = array('Foo' => array('Bar' => array('Baz' => array(2 => 'the value'))));
91 $this->assertNull(\TYPO3\CMS\Extbase\Utility\ArrayUtility::getValueByPath($array, array('Foo', 'Bar', 'Bax', 2)));
92 }
93
94 /**
95 * @test
96 */
97 public function getValueByPathReturnsNullIfThePathHasMoreSegmentsThanTheGivenArray() {
98 $array = array('Foo' => array('Bar' => array('Baz' => 'the value')));
99 $this->assertNull(\TYPO3\CMS\Extbase\Utility\ArrayUtility::getValueByPath($array, array('Foo', 'Bar', 'Baz', 'Bux')));
100 }
101
102 /**
103 * @test
104 */
105 public function convertObjectToArrayConvertsNestedObjectsToArray() {
106 $object = new \stdClass();
107 $object->a = 'v';
108 $object->b = new \stdClass();
109 $object->b->c = 'w';
110 $object->d = array('i' => 'foo', 'j' => 12, 'k' => TRUE, 'l' => new \stdClass());
111 $array = \TYPO3\CMS\Extbase\Utility\ArrayUtility::convertObjectToArray($object);
112 $expected = array(
113 'a' => 'v',
114 'b' => array(
115 'c' => 'w'
116 ),
117 'd' => array(
118 'i' => 'foo',
119 'j' => 12,
120 'k' => TRUE,
121 'l' => array()
122 )
123 );
124 $this->assertSame($expected, $array);
125 }
126
127 /**
128 * @test
129 */
130 public function setValueByPathSetsValueRecursivelyIfPathIsArray() {
131 $array = array();
132 $path = array('foo', 'bar', 'baz');
133 $expectedValue = array('foo' => array('bar' => array('baz' => 'The Value')));
134 $actualValue = \TYPO3\CMS\Extbase\Utility\ArrayUtility::setValueByPath($array, $path, 'The Value');
135 $this->assertSame($expectedValue, $actualValue);
136 }
137
138 /**
139 * @test
140 */
141 public function setValueByPathSetsValueRecursivelyIfPathIsString() {
142 $array = array();
143 $path = 'foo.bar.baz';
144 $expectedValue = array('foo' => array('bar' => array('baz' => 'The Value')));
145 $actualValue = \TYPO3\CMS\Extbase\Utility\ArrayUtility::setValueByPath($array, $path, 'The Value');
146 $this->assertSame($expectedValue, $actualValue);
147 }
148
149 /**
150 * @test
151 */
152 public function setValueByPathRecursivelyMergesAnArray() {
153 $array = array('foo' => array('bar' => 'should be overriden'), 'bar' => 'Baz');
154 $path = array('foo', 'bar', 'baz');
155 $expectedValue = array('foo' => array('bar' => array('baz' => 'The Value')), 'bar' => 'Baz');
156 $actualValue = \TYPO3\CMS\Extbase\Utility\ArrayUtility::setValueByPath($array, $path, 'The Value');
157 $this->assertSame($expectedValue, $actualValue);
158 }
159
160 /**
161 * @test
162 * @expectedException \InvalidArgumentException
163 */
164 public function setValueByPathThrowsExceptionIfPathIsNoArrayOrString() {
165 $array = array('Foo' => array('Bar' => array('Baz' => array(2 => 'the value'))));
166 \TYPO3\CMS\Extbase\Utility\ArrayUtility::setValueByPath($array, NULL, 'Some Value');
167 }
168
169 /**
170 * @test
171 * @expectedException \InvalidArgumentException
172 */
173 public function setValueByPathThrowsExceptionIfSubjectIsNoArray() {
174 $subject = 'foobar';
175 \TYPO3\CMS\Extbase\Utility\ArrayUtility::setValueByPath($subject, 'foo', 'bar');
176 }
177
178 /**
179 * @test
180 * @expectedException \InvalidArgumentException
181 */
182 public function setValueByPathThrowsExceptionIfSubjectIsNoArrayAccess() {
183 $subject = new \stdClass();
184 \TYPO3\CMS\Extbase\Utility\ArrayUtility::setValueByPath($subject, 'foo', 'bar');
185 }
186
187 /**
188 * @test
189 */
190 public function setValueByLeavesInputArrayUnchanged() {
191 $subject = ($subjectBackup = array('foo' => 'bar'));
192 \TYPO3\CMS\Extbase\Utility\ArrayUtility::setValueByPath($subject, 'foo', 'baz');
193 $this->assertSame($subject, $subjectBackup);
194 }
195
196 /**
197 * @test
198 */
199 public function unsetValueByPathDoesNotModifyAnArrayIfThePathWasNotFound() {
200 $array = array('foo' => array('bar' => array('baz' => 'Some Value')), 'bar' => 'Baz');
201 $path = array('foo', 'bar', 'nonExistingKey');
202 $expectedValue = $array;
203 $actualValue = \TYPO3\CMS\Extbase\Utility\ArrayUtility::unsetValueByPath($array, $path);
204 $this->assertSame($expectedValue, $actualValue);
205 }
206
207 /**
208 * @test
209 */
210 public function unsetValueByPathRemovesSpecifiedKey() {
211 $array = array('foo' => array('bar' => array('baz' => 'Some Value')), 'bar' => 'Baz');
212 $path = array('foo', 'bar', 'baz');
213 $expectedValue = array('foo' => array('bar' => array()), 'bar' => 'Baz');
214 $actualValue = \TYPO3\CMS\Extbase\Utility\ArrayUtility::unsetValueByPath($array, $path);
215 $this->assertSame($expectedValue, $actualValue);
216 }
217
218 /**
219 * @test
220 */
221 public function unsetValueByPathRemovesSpecifiedKeyIfPathIsString() {
222 $array = array('foo' => array('bar' => array('baz' => 'Some Value')), 'bar' => 'Baz');
223 $path = 'foo.bar.baz';
224 $expectedValue = array('foo' => array('bar' => array()), 'bar' => 'Baz');
225 $actualValue = \TYPO3\CMS\Extbase\Utility\ArrayUtility::unsetValueByPath($array, $path);
226 $this->assertSame($expectedValue, $actualValue);
227 }
228
229 /**
230 * @test
231 */
232 public function unsetValueByPathRemovesSpecifiedBranch() {
233 $array = array('foo' => array('bar' => array('baz' => 'Some Value')), 'bar' => 'Baz');
234 $path = array('foo');
235 $expectedValue = array('bar' => 'Baz');
236 $actualValue = \TYPO3\CMS\Extbase\Utility\ArrayUtility::unsetValueByPath($array, $path);
237 $this->assertSame($expectedValue, $actualValue);
238 }
239
240 /**
241 * @test
242 * @expectedException \InvalidArgumentException
243 */
244 public function unsetValueByPathThrowsExceptionIfPathIsNoArrayOrString() {
245 $array = array('Foo' => array('Bar' => array('Baz' => array(2 => 'the value'))));
246 \TYPO3\CMS\Extbase\Utility\ArrayUtility::unsetValueByPath($array, NULL);
247 }
248
249 /**
250 * @test
251 */
252 public function removeEmptyElementsRecursivelyRemovesNullValues() {
253 $array = array('EmptyElement' => NULL, 'Foo' => array('Bar' => array('Baz' => array('NotNull' => '', 'AnotherEmptyElement' => NULL))));
254 $expectedResult = array('Foo' => array('Bar' => array('Baz' => array('NotNull' => ''))));
255 $actualResult = \TYPO3\CMS\Extbase\Utility\ArrayUtility::removeEmptyElementsRecursively($array);
256 $this->assertSame($expectedResult, $actualResult);
257 }
258
259 /**
260 * @test
261 */
262 public function removeEmptyElementsRecursivelyRemovesEmptySubArrays() {
263 $array = array('EmptyElement' => array(), 'Foo' => array('Bar' => array('Baz' => array('AnotherEmptyElement' => NULL))), 'NotNull' => 123);
264 $expectedResult = array('NotNull' => 123);
265 $actualResult = \TYPO3\CMS\Extbase\Utility\ArrayUtility::removeEmptyElementsRecursively($array);
266 $this->assertSame($expectedResult, $actualResult);
267 }
268
269 public function arrayMergeRecursiveOverruleData() {
270 return array(
271 'simple usage' => array(
272 'inputArray1' => array(
273 'k1' => 'v1',
274 'k2' => 'v2'
275 ),
276 'inputArray2' => array(
277 'k2' => 'v2a',
278 'k3' => 'v3'
279 ),
280 'dontAddNewKeys' => FALSE,
281 // default
282 'emptyValuesOverride' => TRUE,
283 // default
284 'expected' => array(
285 'k1' => 'v1',
286 'k2' => 'v2a',
287 'k3' => 'v3'
288 )
289 ),
290 'simple usage with recursion' => array(
291 'inputArray1' => array(
292 'k1' => 'v1',
293 'k2' => array(
294 'k2.1' => 'v2.1',
295 'k2.2' => 'v2.2'
296 )
297 ),
298 'inputArray2' => array(
299 'k2' => array(
300 'k2.2' => 'v2.2a',
301 'k2.3' => 'v2.3'
302 ),
303 'k3' => 'v3'
304 ),
305 'dontAddNewKeys' => FALSE,
306 // default
307 'emptyValuesOverride' => TRUE,
308 // default
309 'expected' => array(
310 'k1' => 'v1',
311 'k2' => array(
312 'k2.1' => 'v2.1',
313 'k2.2' => 'v2.2a',
314 'k2.3' => 'v2.3'
315 ),
316 'k3' => 'v3'
317 )
318 ),
319 'simple type should override array (k2)' => array(
320 'inputArray1' => array(
321 'k1' => 'v1',
322 'k2' => array(
323 'k2.1' => 'v2.1'
324 )
325 ),
326 'inputArray2' => array(
327 'k2' => 'v2a',
328 'k3' => 'v3'
329 ),
330 'dontAddNewKeys' => FALSE,
331 // default
332 'emptyValuesOverride' => TRUE,
333 // default
334 'expected' => array(
335 'k1' => 'v1',
336 'k2' => 'v2a',
337 'k3' => 'v3'
338 )
339 ),
340 'null should override array (k2)' => array(
341 'inputArray1' => array(
342 'k1' => 'v1',
343 'k2' => array(
344 'k2.1' => 'v2.1'
345 )
346 ),
347 'inputArray2' => array(
348 'k2' => NULL,
349 'k3' => 'v3'
350 ),
351 'dontAddNewKeys' => FALSE,
352 // default
353 'emptyValuesOverride' => TRUE,
354 // default
355 'expected' => array(
356 'k1' => 'v1',
357 'k2' => NULL,
358 'k3' => 'v3'
359 )
360 )
361 );
362 }
363
364 /**
365 * @test
366 *
367 * @param array $inputArray1
368 * @param array $inputArray2
369 * @param boolean $dontAddNewKeys
370 * @param boolean $emptyValuesOverride
371 * @param array $expected
372 *
373 * @dataProvider arrayMergeRecursiveOverruleData
374 */
375 public function arrayMergeRecursiveOverruleMergesSimpleArrays(array $inputArray1, array $inputArray2, $dontAddNewKeys, $emptyValuesOverride, array $expected) {
376 $this->assertSame($expected, \TYPO3\CMS\Extbase\Utility\ArrayUtility::arrayMergeRecursiveOverrule($inputArray1, $inputArray2, $dontAddNewKeys, $emptyValuesOverride));
377 }
378
379 /**
380 * @test
381 */
382 public function integerExplodeReturnsArrayOfIntegers() {
383 $inputString = '1,2,3,4,5,6';
384 $expected = array(1, 2, 3, 4, 5, 6);
385 $this->assertSame($expected, \TYPO3\CMS\Extbase\Utility\ArrayUtility::integerExplode(',', $inputString));
386 }
387
388 /**
389 * @test
390 */
391 public function integerExplodeReturnsZeroForStringValues() {
392 $inputString = '1,abc,3,,5';
393 $expected = array(1, 0, 3, 0, 5);
394 $this->assertSame($expected, \TYPO3\CMS\Extbase\Utility\ArrayUtility::integerExplode(',', $inputString));
395 }
396
397 /**
398 * dataProvider for sortArrayWithIntegerKeys
399 *
400 * @return array
401 */
402 public function sortArrayWithIntegerKeysDataProvider() {
403 return array(
404 array(
405 array(
406 '20' => 'test1',
407 '11' => 'test2',
408 '16' => 'test3',
409 ),
410 array(
411 '11' => 'test2',
412 '16' => 'test3',
413 '20' => 'test1',
414 )
415 ),
416 array(
417 array(
418 '20' => 'test1',
419 '16.5' => 'test2',
420 '16' => 'test3',
421 ),
422 array(
423 '20' => 'test1',
424 '16.5' => 'test2',
425 '16' => 'test3',
426 )
427 ),
428 array(
429 array(
430 '20' => 'test20',
431 'somestring' => 'teststring',
432 '16' => 'test16',
433 ),
434 array(
435 '20' => 'test20',
436 'somestring' => 'teststring',
437 '16' => 'test16',
438 )
439 ),
440 );
441 }
442
443 /**
444 * @test
445 *
446 * @param array $arrayToSort
447 * @param array $expectedArray
448 *
449 * @dataProvider sortArrayWithIntegerKeysDataProvider
450 */
451
452 public function sortArrayWithIntegerKeysSortsNumericArrays(array $arrayToSort, array $expectedArray) {
453 $sortedArray = \TYPO3\CMS\Extbase\Utility\ArrayUtility::sortArrayWithIntegerKeys($arrayToSort);
454 $this->assertSame($sortedArray, $expectedArray);
455 }
456 }