TYPO3 CMS  TYPO3_7-6
ArrayUtilityTest.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 {
26  {
27  $this->assertFalse(\TYPO3\CMS\Extbase\Utility\ArrayUtility::containsMultipleTypes([]));
28  }
29 
34  {
35  $this->assertFalse(\TYPO3\CMS\Extbase\Utility\ArrayUtility::containsMultipleTypes([1, 2, 3]));
36  }
37 
42  {
43  $this->assertFalse(\TYPO3\CMS\Extbase\Utility\ArrayUtility::containsMultipleTypes([new \stdClass(), new \stdClass(), new \stdClass()]));
44  }
45 
50  {
51  $this->assertTrue(\TYPO3\CMS\Extbase\Utility\ArrayUtility::containsMultipleTypes([1, 'string', 1.25, new \stdClass()]));
52  }
53 
58  {
59  $array = ['Foo' => 'the value'];
60  $this->assertSame('the value', \TYPO3\CMS\Extbase\Utility\ArrayUtility::getValueByPath($array, ['Foo']));
61  }
62 
67  {
68  $array = ['Foo' => ['Bar' => ['Baz' => [2 => 'the value']]]];
69  $this->assertSame('the value', \TYPO3\CMS\Extbase\Utility\ArrayUtility::getValueByPath($array, ['Foo', 'Bar', 'Baz', 2]));
70  }
71 
76  {
77  $path = 'Foo.Bar.Baz.2';
78  $array = ['Foo' => ['Bar' => ['Baz' => [2 => 'the value']]]];
79  $expectedResult = 'the value';
80  $actualResult = \TYPO3\CMS\Extbase\Utility\ArrayUtility::getValueByPath($array, $path);
81  $this->assertSame($expectedResult, $actualResult);
82  }
83 
89  {
90  $array = ['Foo' => ['Bar' => ['Baz' => [2 => 'the value']]]];
92  }
93 
98  {
99  $array = ['Foo' => ['Bar' => ['Baz' => [2 => 'the value']]]];
100  $this->assertNull(\TYPO3\CMS\Extbase\Utility\ArrayUtility::getValueByPath($array, ['Foo', 'Bar', 'Bax', 2]));
101  }
102 
107  {
108  $array = ['Foo' => ['Bar' => ['Baz' => 'the value']]];
109  $this->assertNull(\TYPO3\CMS\Extbase\Utility\ArrayUtility::getValueByPath($array, ['Foo', 'Bar', 'Baz', 'Bux']));
110  }
111 
116  {
117  $object = new \stdClass();
118  $object->a = 'v';
119  $object->b = new \stdClass();
120  $object->b->c = 'w';
121  $object->d = ['i' => 'foo', 'j' => 12, 'k' => true, 'l' => new \stdClass()];
123  $expected = [
124  'a' => 'v',
125  'b' => [
126  'c' => 'w'
127  ],
128  'd' => [
129  'i' => 'foo',
130  'j' => 12,
131  'k' => true,
132  'l' => []
133  ]
134  ];
135  $this->assertSame($expected, $array);
136  }
137 
142  {
143  $array = [];
144  $path = ['foo', 'bar', 'baz'];
145  $expectedValue = ['foo' => ['bar' => ['baz' => 'The Value']]];
146  $actualValue = \TYPO3\CMS\Extbase\Utility\ArrayUtility::setValueByPath($array, $path, 'The Value');
147  $this->assertSame($expectedValue, $actualValue);
148  }
149 
154  {
155  $array = [];
156  $path = 'foo.bar.baz';
157  $expectedValue = ['foo' => ['bar' => ['baz' => 'The Value']]];
158  $actualValue = \TYPO3\CMS\Extbase\Utility\ArrayUtility::setValueByPath($array, $path, 'The Value');
159  $this->assertSame($expectedValue, $actualValue);
160  }
161 
166  {
167  $array = ['foo' => ['bar' => 'should be overriden'], 'bar' => 'Baz'];
168  $path = ['foo', 'bar', 'baz'];
169  $expectedValue = ['foo' => ['bar' => ['baz' => 'The Value']], 'bar' => 'Baz'];
170  $actualValue = \TYPO3\CMS\Extbase\Utility\ArrayUtility::setValueByPath($array, $path, 'The Value');
171  $this->assertSame($expectedValue, $actualValue);
172  }
173 
179  {
180  $array = ['Foo' => ['Bar' => ['Baz' => [2 => 'the value']]]];
182  }
183 
189  {
190  $subject = 'foobar';
192  }
193 
199  {
200  $subject = new \stdClass();
202  }
203 
208  {
209  $subject = ($subjectBackup = ['foo' => 'bar']);
211  $this->assertSame($subject, $subjectBackup);
212  }
213 
218  {
219  $array = ['foo' => ['bar' => ['baz' => 'Some Value']], 'bar' => 'Baz'];
220  $path = ['foo', 'bar', 'nonExistingKey'];
221  $expectedValue = $array;
222  $actualValue = \TYPO3\CMS\Extbase\Utility\ArrayUtility::unsetValueByPath($array, $path);
223  $this->assertSame($expectedValue, $actualValue);
224  }
225 
230  {
231  $array = ['foo' => ['bar' => ['baz' => 'Some Value']], 'bar' => 'Baz'];
232  $path = ['foo', 'bar', 'baz'];
233  $expectedValue = ['foo' => ['bar' => []], 'bar' => 'Baz'];
234  $actualValue = \TYPO3\CMS\Extbase\Utility\ArrayUtility::unsetValueByPath($array, $path);
235  $this->assertSame($expectedValue, $actualValue);
236  }
237 
242  {
243  $array = ['foo' => ['bar' => ['baz' => 'Some Value']], 'bar' => 'Baz'];
244  $path = 'foo.bar.baz';
245  $expectedValue = ['foo' => ['bar' => []], 'bar' => 'Baz'];
246  $actualValue = \TYPO3\CMS\Extbase\Utility\ArrayUtility::unsetValueByPath($array, $path);
247  $this->assertSame($expectedValue, $actualValue);
248  }
249 
254  {
255  $array = ['foo' => ['bar' => ['baz' => 'Some Value']], 'bar' => 'Baz'];
256  $path = ['foo'];
257  $expectedValue = ['bar' => 'Baz'];
258  $actualValue = \TYPO3\CMS\Extbase\Utility\ArrayUtility::unsetValueByPath($array, $path);
259  $this->assertSame($expectedValue, $actualValue);
260  }
261 
267  {
268  $array = ['Foo' => ['Bar' => ['Baz' => [2 => 'the value']]]];
270  }
271 
276  {
277  $array = ['EmptyElement' => null, 'Foo' => ['Bar' => ['Baz' => ['NotNull' => '', 'AnotherEmptyElement' => null]]]];
278  $expectedResult = ['Foo' => ['Bar' => ['Baz' => ['NotNull' => '']]]];
280  $this->assertSame($expectedResult, $actualResult);
281  }
282 
287  {
288  $array = ['EmptyElement' => [], 'Foo' => ['Bar' => ['Baz' => ['AnotherEmptyElement' => null]]], 'NotNull' => 123];
289  $expectedResult = ['NotNull' => 123];
291  $this->assertSame($expectedResult, $actualResult);
292  }
293 
295  {
296  return [
297  'simple usage' => [
298  'inputArray1' => [
299  'k1' => 'v1',
300  'k2' => 'v2'
301  ],
302  'inputArray2' => [
303  'k2' => 'v2a',
304  'k3' => 'v3'
305  ],
306  'dontAddNewKeys' => false,
307  // default
308  'emptyValuesOverride' => true,
309  // default
310  'expected' => [
311  'k1' => 'v1',
312  'k2' => 'v2a',
313  'k3' => 'v3'
314  ]
315  ],
316  'simple usage with recursion' => [
317  'inputArray1' => [
318  'k1' => 'v1',
319  'k2' => [
320  'k2.1' => 'v2.1',
321  'k2.2' => 'v2.2'
322  ]
323  ],
324  'inputArray2' => [
325  'k2' => [
326  'k2.2' => 'v2.2a',
327  'k2.3' => 'v2.3'
328  ],
329  'k3' => 'v3'
330  ],
331  'dontAddNewKeys' => false,
332  // default
333  'emptyValuesOverride' => true,
334  // default
335  'expected' => [
336  'k1' => 'v1',
337  'k2' => [
338  'k2.1' => 'v2.1',
339  'k2.2' => 'v2.2a',
340  'k2.3' => 'v2.3'
341  ],
342  'k3' => 'v3'
343  ]
344  ],
345  'simple type should override array (k2)' => [
346  'inputArray1' => [
347  'k1' => 'v1',
348  'k2' => [
349  'k2.1' => 'v2.1'
350  ]
351  ],
352  'inputArray2' => [
353  'k2' => 'v2a',
354  'k3' => 'v3'
355  ],
356  'dontAddNewKeys' => false,
357  // default
358  'emptyValuesOverride' => true,
359  // default
360  'expected' => [
361  'k1' => 'v1',
362  'k2' => 'v2a',
363  'k3' => 'v3'
364  ]
365  ],
366  'null should override array (k2)' => [
367  'inputArray1' => [
368  'k1' => 'v1',
369  'k2' => [
370  'k2.1' => 'v2.1'
371  ]
372  ],
373  'inputArray2' => [
374  'k2' => null,
375  'k3' => 'v3'
376  ],
377  'dontAddNewKeys' => false,
378  // default
379  'emptyValuesOverride' => true,
380  // default
381  'expected' => [
382  'k1' => 'v1',
383  'k2' => null,
384  'k3' => 'v3'
385  ]
386  ]
387  ];
388  }
389 
401  public function arrayMergeRecursiveOverruleMergesSimpleArrays(array $inputArray1, array $inputArray2, $dontAddNewKeys, $emptyValuesOverride, array $expected)
402  {
403  $this->assertSame($expected, \TYPO3\CMS\Extbase\Utility\ArrayUtility::arrayMergeRecursiveOverrule($inputArray1, $inputArray2, $dontAddNewKeys, $emptyValuesOverride));
404  }
405 
410  {
411  $inputString = '1,2,3,4,5,6';
412  $expected = [1, 2, 3, 4, 5, 6];
413  $this->assertSame($expected, \TYPO3\CMS\Extbase\Utility\ArrayUtility::integerExplode(',', $inputString));
414  }
415 
420  {
421  $inputString = '1,abc,3,,5';
422  $expected = [1, 0, 3, 0, 5];
423  $this->assertSame($expected, \TYPO3\CMS\Extbase\Utility\ArrayUtility::integerExplode(',', $inputString));
424  }
425 
432  {
433  return [
434  [
435  [
436  '20' => 'test1',
437  '11' => 'test2',
438  '16' => 'test3',
439  ],
440  [
441  '11' => 'test2',
442  '16' => 'test3',
443  '20' => 'test1',
444  ]
445  ],
446  [
447  [
448  '20' => 'test1',
449  '16.5' => 'test2',
450  '16' => 'test3',
451  ],
452  [
453  '20' => 'test1',
454  '16.5' => 'test2',
455  '16' => 'test3',
456  ]
457  ],
458  [
459  [
460  '20' => 'test20',
461  'somestring' => 'teststring',
462  '16' => 'test16',
463  ],
464  [
465  '20' => 'test20',
466  'somestring' => 'teststring',
467  '16' => 'test16',
468  ]
469  ],
470  ];
471  }
472 
481  public function sortArrayWithIntegerKeysSortsNumericArrays(array $arrayToSort, array $expectedArray)
482  {
484  $this->assertSame($sortedArray, $expectedArray);
485  }
486 }
sortArrayWithIntegerKeysSortsNumericArrays(array $arrayToSort, array $expectedArray)
static removeEmptyElementsRecursively(array $array)
static setValueByPath($subject, $path, $value)
static unsetValueByPath(array $array, $path)
arrayMergeRecursiveOverruleMergesSimpleArrays(array $inputArray1, array $inputArray2, $dontAddNewKeys, $emptyValuesOverride, array $expected)
static getValueByPath(array &$array, $path)