TYPO3 CMS  TYPO3_6-2
ArrayUtilityTest.php
Go to the documentation of this file.
1 <?php
3 
21 
26  $this->assertFalse(\TYPO3\CMS\Extbase\Utility\ArrayUtility::containsMultipleTypes(array()));
27  }
28 
33  $this->assertFalse(\TYPO3\CMS\Extbase\Utility\ArrayUtility::containsMultipleTypes(array(1, 2, 3)));
34  }
35 
40  $this->assertFalse(\TYPO3\CMS\Extbase\Utility\ArrayUtility::containsMultipleTypes(array(new \stdClass(), new \stdClass(), new \stdClass())));
41  }
42 
47  $this->assertTrue(\TYPO3\CMS\Extbase\Utility\ArrayUtility::containsMultipleTypes(array(1, 'string', 1.25, new \stdClass())));
48  }
49 
54  $array = array('Foo' => 'the value');
55  $this->assertSame('the value', \TYPO3\CMS\Extbase\Utility\ArrayUtility::getValueByPath($array, array('Foo')));
56  }
57 
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 
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 
82  $array = array('Foo' => array('Bar' => array('Baz' => array(2 => 'the value'))));
84  }
85 
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 
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 
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());
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 
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 
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 
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 
165  $array = array('Foo' => array('Bar' => array('Baz' => array(2 => 'the value'))));
167  }
168 
174  $subject = 'foobar';
176  }
177 
183  $subject = new \stdClass();
185  }
186 
191  $subject = ($subjectBackup = array('foo' => 'bar'));
193  $this->assertSame($subject, $subjectBackup);
194  }
195 
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 
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 
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 
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 
245  $array = array('Foo' => array('Bar' => array('Baz' => array(2 => 'the value'))));
247  }
248 
253  $array = array('EmptyElement' => NULL, 'Foo' => array('Bar' => array('Baz' => array('NotNull' => '', 'AnotherEmptyElement' => NULL))));
254  $expectedResult = array('Foo' => array('Bar' => array('Baz' => array('NotNull' => ''))));
256  $this->assertSame($expectedResult, $actualResult);
257  }
258 
263  $array = array('EmptyElement' => array(), 'Foo' => array('Bar' => array('Baz' => array('AnotherEmptyElement' => NULL))), 'NotNull' => 123);
264  $expectedResult = array('NotNull' => 123);
266  $this->assertSame($expectedResult, $actualResult);
267  }
268 
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 
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 
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 
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 
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 
452  public function sortArrayWithIntegerKeysSortsNumericArrays(array $arrayToSort, array $expectedArray) {
454  $this->assertSame($sortedArray, $expectedArray);
455  }
456 }
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)