[BUGFIX] Do not renumber unique keys in ArrayUtility
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Utility / ArrayUtilityTest.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 use TYPO3\CMS\Core\Utility\ArrayUtility;
18
19 /**
20 * Test case
21 */
22 class ArrayUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
23 {
24 ///////////////////////
25 // Tests concerning filterByValueRecursive
26 ///////////////////////
27 /**
28 * Data provider for filterByValueRecursiveCorrectlyFiltersArray
29 *
30 * Every array splits into:
31 * - String value to search for
32 * - Input array
33 * - Expected result array
34 */
35 public function filterByValueRecursive()
36 {
37 return [
38 'empty search array' => [
39 'banana',
40 [],
41 []
42 ],
43 'empty string as needle' => [
44 '',
45 [
46 '',
47 'apple'
48 ],
49 [
50 ''
51 ]
52 ],
53 'flat array searching for string' => [
54 'banana',
55 [
56 'apple',
57 'banana'
58 ],
59 [
60 1 => 'banana'
61 ]
62 ],
63 'flat array searching for string with two matches' => [
64 'banana',
65 [
66 'foo' => 'apple',
67 'firstbanana' => 'banana',
68 'secondbanana' => 'banana'
69 ],
70 [
71 'firstbanana' => 'banana',
72 'secondbanana' => 'banana'
73 ]
74 ],
75 'multi dimensional array searching for string with multiple matches' => [
76 'banana',
77 [
78 'foo' => 'apple',
79 'firstbanana' => 'banana',
80 'grape' => [
81 'foo2' => 'apple2',
82 'secondbanana' => 'banana',
83 'foo3' => []
84 ],
85 'bar' => 'orange'
86 ],
87 [
88 'firstbanana' => 'banana',
89 'grape' => [
90 'secondbanana' => 'banana'
91 ]
92 ]
93 ],
94 'multi dimensional array searching for integer with multiple matches' => [
95 42,
96 [
97 'foo' => 23,
98 'bar' => 42,
99 [
100 'foo' => 23,
101 'bar' => 42
102 ]
103 ],
104 [
105 'bar' => 42,
106 [
107 'bar' => 42
108 ]
109 ]
110 ],
111 'flat array searching for boolean TRUE' => [
112 true,
113 [
114 23 => false,
115 42 => true
116 ],
117 [
118 42 => true
119 ]
120 ],
121 'multi dimensional array searching for boolean FALSE' => [
122 false,
123 [
124 23 => false,
125 42 => true,
126 'foo' => [
127 23 => false,
128 42 => true
129 ]
130 ],
131 [
132 23 => false,
133 'foo' => [
134 23 => false
135 ]
136 ]
137 ],
138 'flat array searching for array' => [
139 [
140 'foo' => 'bar'
141 ],
142 [
143 'foo' => 'bar',
144 'foobar' => [
145 'foo' => 'bar'
146 ]
147 ],
148 [
149 'foobar' => [
150 'foo' => 'bar'
151 ]
152 ]
153 ]
154 ];
155 }
156
157 /**
158 * @test
159 * @dataProvider filterByValueRecursive
160 * @param array $needle
161 * @param array $haystack
162 * @param array $expectedResult
163 */
164 public function filterByValueRecursiveCorrectlyFiltersArray($needle, $haystack, $expectedResult)
165 {
166 $this->assertEquals(
167 $expectedResult,
168 ArrayUtility::filterByValueRecursive($needle, $haystack)
169 );
170 }
171
172 /**
173 * @test
174 */
175 public function filterByValueRecursiveMatchesReferencesToSameObject()
176 {
177 $instance = new \stdClass();
178 $this->assertEquals(
179 [$instance],
180 ArrayUtility::filterByValueRecursive($instance, [$instance])
181 );
182 }
183
184 /**
185 * @test
186 */
187 public function filterByValueRecursiveDoesNotMatchDifferentInstancesOfSameClass()
188 {
189 $this->assertEquals(
190 [],
191 ArrayUtility::filterByValueRecursive(new \stdClass(), [new \stdClass()])
192 );
193 }
194
195 ///////////////////////
196 // Tests concerning isValidPath
197 ///////////////////////
198 /**
199 * Mock the class under test, isValidPath() (method under test), calls
200 * static getValuePath() internally, which is mocked here to return a specific
201 * result. This works because of 'static' keyword' instead of 'self'
202 * for getValueByPath() call, using late static binding in PHP 5.3
203 *
204 * @test
205 */
206 public function isValidPathReturnsTrueIfPathExists()
207 {
208 $this->assertTrue(ArrayUtility::isValidPath(['foo' => 'bar'], 'foo'));
209 }
210
211 /**
212 * @test
213 */
214 public function isValidPathReturnsFalseIfPathDoesNotExist()
215 {
216 $this->assertFalse(ArrayUtility::isValidPath(['foo' => 'bar'], 'bar'));
217 }
218
219 ///////////////////////
220 // Tests concerning getValueByPath
221 ///////////////////////
222 /**
223 * @test
224 */
225 public function getValueByPathThrowsExceptionIfPathIsNotString()
226 {
227 $this->expectException(\InvalidArgumentException::class);
228 $this->expectExceptionCode(1476557628);
229
230 ArrayUtility::getValueByPath([], 123);
231 }
232
233 /**
234 * @test
235 */
236 public function getValueByPathThrowsExceptionIfPathIsEmpty()
237 {
238 $this->expectException(\RuntimeException::class);
239 $this->expectExceptionCode(1341397767);
240
241 ArrayUtility::getValueByPath([], '');
242 }
243
244 /**
245 * @test
246 */
247 public function getValueByPathReturnsFirstIndexIfPathIsZero()
248 {
249 $this->assertSame('foo', ArrayUtility::getValueByPath(['foo'], '0'));
250 }
251
252 /**
253 * @test
254 */
255 public function getValueByPathReturnsFirstIndexIfPathSegmentIsZero()
256 {
257 $this->assertSame('bar', ArrayUtility::getValueByPath(['foo' => ['bar']], 'foo/0'));
258 }
259
260 /**
261 * Data provider for getValueByPathThrowsExceptionIfPathNotExists
262 * Every array splits into:
263 * - Array to get value from
264 * - String path
265 * - Expected result
266 * @return array
267 */
268 public function getValueByPathInvalidPathDataProvider()
269 {
270 return [
271 'not existing index' => [
272 [
273 'foo' => ['foo']
274 ],
275 'foo/1',
276 false
277 ],
278 'not existing path 1' => [
279 [
280 'foo' => []
281 ],
282 'foo/bar/baz',
283 false
284 ],
285 'not existing path 2' => [
286 [
287 'foo' => [
288 'baz' => 42
289 ],
290 'bar' => []
291 ],
292 'foo/bar/baz',
293 false
294 ],
295 // Negative test: This could be improved and the test moved to
296 // the valid data provider if the method supports this
297 'doubletick encapsulated quoted doubletick does not work' => [
298 [
299 '"foo"bar"' => [
300 'baz' => 42
301 ],
302 'bar' => []
303 ],
304 '"foo\\"bar"/baz',
305 42
306 ],
307 // Negative test: Method could be improved here
308 'path with doubletick does not work' => [
309 [
310 'fo"o' => [
311 'bar' => 42
312 ]
313 ],
314 'fo"o/foobar',
315 42
316 ]
317 ];
318 }
319
320 /**
321 * @test
322 * @dataProvider getValueByPathInvalidPathDataProvider
323 * @param array $array
324 * @param string $path
325 */
326 public function getValueByPathThrowsExceptionIfPathNotExists(array $array, $path)
327 {
328 $this->expectException(\RuntimeException::class);
329 $this->expectExceptionCode(1341397869);
330
331 ArrayUtility::getValueByPath($array, $path);
332 }
333
334 /**
335 * Data provider for getValueByPathReturnsCorrectValue
336 * Every array splits into:
337 * - Array to get value from
338 * - String path
339 * - Expected result
340 */
341 public function getValueByPathValidDataProvider()
342 {
343 $testObject = new \stdClass();
344 $testObject->foo = 'foo';
345 $testObject->bar = 'bar';
346 return [
347 'integer in multi level array' => [
348 [
349 'foo' => [
350 'bar' => [
351 'baz' => 42
352 ],
353 'bar2' => []
354 ]
355 ],
356 'foo/bar/baz',
357 42
358 ],
359 'zero integer in multi level array' => [
360 [
361 'foo' => [
362 'bar' => [
363 'baz' => 0
364 ]
365 ]
366 ],
367 'foo/bar/baz',
368 0
369 ],
370 'NULL value in multi level array' => [
371 [
372 'foo' => [
373 'baz' => null
374 ]
375 ],
376 'foo/baz',
377 null
378 ],
379 'get string value' => [
380 [
381 'foo' => [
382 'baz' => 'this is a test string'
383 ]
384 ],
385 'foo/baz',
386 'this is a test string'
387 ],
388 'get boolean value: FALSE' => [
389 [
390 'foo' => [
391 'baz' => false
392 ]
393 ],
394 'foo/baz',
395 false
396 ],
397 'get boolean value: TRUE' => [
398 [
399 'foo' => [
400 'baz' => true
401 ]
402 ],
403 'foo/baz',
404 true
405 ],
406 'get object value' => [
407 [
408 'foo' => [
409 'baz' => $testObject
410 ]
411 ],
412 'foo/baz',
413 $testObject
414 ],
415 'enclosed path' => [
416 [
417 'foo/bar' => [
418 'foobar' => 42
419 ]
420 ],
421 '"foo/bar"/foobar',
422 42
423 ]
424 ];
425 }
426
427 /**
428 * @test
429 * @dataProvider getValueByPathValidDataProvider
430 * @param array $array
431 * @param string $path
432 * @param mixed $expectedResult
433 */
434 public function getValueByPathGetsCorrectValue(array $array, $path, $expectedResult)
435 {
436 $this->assertEquals($expectedResult, ArrayUtility::getValueByPath($array, $path));
437 }
438
439 /**
440 * @test
441 */
442 public function getValueByPathAcceptsDifferentDelimiter()
443 {
444 $input = [
445 'foo' => [
446 'bar' => [
447 'baz' => 42
448 ],
449 'bar2' => []
450 ]
451 ];
452 $searchPath = 'foo%bar%baz';
453 $expected = 42;
454 $delimiter = '%';
455 $this->assertEquals(
456 $expected,
457 ArrayUtility::getValueByPath($input, $searchPath, $delimiter)
458 );
459 }
460
461 ///////////////////////
462 // Tests concerning setValueByPath
463 ///////////////////////
464 /**
465 * @test
466 */
467 public function setValueByPathThrowsExceptionIfPathIsEmpty()
468 {
469 $this->expectException(\RuntimeException::class);
470 $this->expectExceptionCode(1341406194);
471
472 ArrayUtility::setValueByPath([], '', null);
473 }
474
475 /**
476 * @test
477 */
478 public function setValueByPathThrowsExceptionIfPathIsNotAString()
479 {
480 $this->expectException(\InvalidArgumentException::class);
481 $this->expectExceptionCode(1478781081);
482
483 ArrayUtility::setValueByPath([], 123, null);
484 }
485
486 /**
487 * @test
488 */
489 public function setValueByPathThrowsExceptionIfPathSegmentIsEmpty()
490 {
491 $this->expectException(\RuntimeException::class);
492 $this->expectExceptionCode(1341406846);
493
494 ArrayUtility::setValueByPath(['foo' => 'bar'], '/foo', 'value');
495 }
496
497 /**
498 * @test
499 */
500 public function setValueByPathCanUseZeroAsPathSegment()
501 {
502 $this->assertSame(['foo' => ['value']], ArrayUtility::setValueByPath(['foo' => []], 'foo/0', 'value'));
503 }
504
505 /**
506 * @test
507 */
508 public function setValueByPathCanUseZeroAsPath()
509 {
510 $this->assertSame(['value', 'bar'], ArrayUtility::setValueByPath(['foo', 'bar'], '0', 'value'));
511 }
512
513 /**
514 * Data provider for setValueByPathSetsCorrectValueDataProvider
515 *
516 * Every array splits into:
517 * - Array to set value in
518 * - String path
519 * - Value to set
520 * - Expected result
521 */
522 public function setValueByPathSetsCorrectValueDataProvider()
523 {
524 $testObject = new \stdClass();
525 $testObject->foo = 'foo';
526 $testObject->bar = 'bar';
527 return [
528 'set integer value: 42' => [
529 [
530 'foo' => [
531 'bar' => [
532 'baz' => 0
533 ]
534 ]
535 ],
536 'foo/bar/baz',
537 42,
538 [
539 'foo' => [
540 'bar' => [
541 'baz' => 42
542 ]
543 ]
544 ]
545 ],
546 'set integer value: 0' => [
547 [
548 'foo' => [
549 'bar' => [
550 'baz' => 42
551 ]
552 ]
553 ],
554 'foo/bar/baz',
555 0,
556 [
557 'foo' => [
558 'bar' => [
559 'baz' => 0
560 ]
561 ]
562 ]
563 ],
564 'set null value' => [
565 [
566 'foo' => [
567 'bar' => [
568 'baz' => 42
569 ]
570 ]
571 ],
572 'foo/bar/baz',
573 null,
574 [
575 'foo' => [
576 'bar' => [
577 'baz' => null
578 ]
579 ]
580 ]
581 ],
582 'set array value' => [
583 [
584 'foo' => [
585 'bar' => [
586 'baz' => 42
587 ]
588 ]
589 ],
590 'foo/bar/baz',
591 [
592 'foo' => 123
593 ],
594 [
595 'foo' => [
596 'bar' => [
597 'baz' => [
598 'foo' => 123
599 ]
600 ]
601 ]
602 ]
603 ],
604 'set boolean value: FALSE' => [
605 [
606 'foo' => [
607 'bar' => [
608 'baz' => true
609 ]
610 ]
611 ],
612 'foo/bar/baz',
613 false,
614 [
615 'foo' => [
616 'bar' => [
617 'baz' => false
618 ]
619 ]
620 ]
621 ],
622 'set boolean value: TRUE' => [
623 [
624 'foo' => [
625 'bar' => [
626 'baz' => null
627 ]
628 ]
629 ],
630 'foo/bar/baz',
631 true,
632 [
633 'foo' => [
634 'bar' => [
635 'baz' => true
636 ]
637 ]
638 ]
639 ],
640 'set object value' => [
641 [
642 'foo' => [
643 'bar' => [
644 'baz' => null
645 ]
646 ]
647 ],
648 'foo/bar/baz',
649 $testObject,
650 [
651 'foo' => [
652 'bar' => [
653 'baz' => $testObject
654 ]
655 ]
656 ]
657 ],
658 'multi keys in array' => [
659 [
660 'foo' => [
661 'bar' => [
662 'baz' => 'value'
663 ],
664 'bar2' => [
665 'baz' => 'value'
666 ]
667 ]
668 ],
669 'foo/bar2/baz',
670 'newValue',
671 [
672 'foo' => [
673 'bar' => [
674 'baz' => 'value'
675 ],
676 'bar2' => [
677 'baz' => 'newValue'
678 ]
679 ]
680 ]
681 ]
682 ];
683 }
684
685 /**
686 * @test
687 * @dataProvider setValueByPathSetsCorrectValueDataProvider
688 * @param array $array
689 * @param string $path
690 * @param string $value
691 * @param array $expectedResult
692 */
693 public function setValueByPathSetsCorrectValue(array $array, $path, $value, $expectedResult)
694 {
695 $this->assertEquals(
696 $expectedResult,
697 ArrayUtility::setValueByPath($array, $path, $value)
698 );
699 }
700
701 /**********************
702 /* Tests concerning removeByPath
703 ***********************/
704
705 /**
706 * @test
707 */
708 public function removeByPathThrowsExceptionIfPathIsEmpty()
709 {
710 $this->expectException(\RuntimeException::class);
711 $this->expectExceptionCode(1371757718);
712
713 ArrayUtility::removeByPath([], '');
714 }
715
716 /**
717 * @test
718 */
719 public function removeByPathThrowsExceptionIfPathIsNotAString()
720 {
721 $this->expectException(\RuntimeException::class);
722 $this->expectExceptionCode(1371757719);
723
724 ArrayUtility::removeByPath([], ['foo']);
725 }
726
727 /**
728 * @test
729 */
730 public function removeByPathThrowsExceptionWithEmptyPathSegment()
731 {
732 $inputArray = [
733 'foo' => [
734 'bar' => 42,
735 ]
736 ];
737
738 $this->expectException(\RuntimeException::class);
739 $this->expectExceptionCode(1371757720);
740
741 ArrayUtility::removeByPath($inputArray, 'foo//bar');
742 }
743
744 /**
745 * @test
746 */
747 public function removeByPathRemovesFirstIndexWithZeroAsPathSegment()
748 {
749 $inputArray = [
750 'foo' => ['bar']
751 ];
752
753 $this->assertSame(['foo' => []], ArrayUtility::removeByPath($inputArray, 'foo/0'));
754 }
755
756 /**
757 * @test
758 */
759 public function removeByPathRemovesFirstIndexWithZeroAsPath()
760 {
761 $inputArray = ['bar'];
762
763 $this->assertSame([], ArrayUtility::removeByPath($inputArray, '0'));
764 }
765
766 /**
767 * @test
768 */
769 public function removeByPathThrowsExceptionIfPathDoesNotExistInArray()
770 {
771 $inputArray = [
772 'foo' => [
773 'bar' => 42,
774 ]
775 ];
776
777 $this->expectException(\RuntimeException::class);
778 $this->expectExceptionCode(1371758436);
779
780 ArrayUtility::removeByPath($inputArray, 'foo/baz');
781 }
782
783 /**
784 * @test
785 */
786 public function removeByPathAcceptsGivenDelimiter()
787 {
788 $inputArray = [
789 'foo' => [
790 'toRemove' => 42,
791 'keep' => 23
792 ],
793 ];
794 $path = 'foo.toRemove';
795 $expected = [
796 'foo' => [
797 'keep' => 23,
798 ],
799 ];
800 $this->assertEquals(
801 $expected,
802 ArrayUtility::removeByPath($inputArray, $path, '.')
803 );
804 }
805
806 /**
807 * Data provider for removeByPathRemovesCorrectPath
808 */
809 public function removeByPathRemovesCorrectPathDataProvider()
810 {
811 return [
812 'single value' => [
813 [
814 'foo' => [
815 'toRemove' => 42,
816 'keep' => 23
817 ],
818 ],
819 'foo/toRemove',
820 [
821 'foo' => [
822 'keep' => 23,
823 ],
824 ],
825 ],
826 'whole array' => [
827 [
828 'foo' => [
829 'bar' => 42
830 ],
831 ],
832 'foo',
833 [],
834 ],
835 'sub array' => [
836 [
837 'foo' => [
838 'keep' => 23,
839 'toRemove' => [
840 'foo' => 'bar',
841 ],
842 ],
843 ],
844 'foo/toRemove',
845 [
846 'foo' => [
847 'keep' => 23,
848 ],
849 ],
850 ],
851 ];
852 }
853
854 /**
855 * @test
856 * @dataProvider removeByPathRemovesCorrectPathDataProvider
857 * @param array $array
858 * @param string $path
859 * @param array $expectedResult
860 */
861 public function removeByPathRemovesCorrectPath(array $array, $path, $expectedResult)
862 {
863 $this->assertEquals(
864 $expectedResult,
865 ArrayUtility::removeByPath($array, $path)
866 );
867 }
868
869 ///////////////////////
870 // Tests concerning sortByKeyRecursive
871 ///////////////////////
872 /**
873 * @test
874 */
875 public function sortByKeyRecursiveCheckIfSortingIsCorrect()
876 {
877 $unsortedArray = [
878 'z' => null,
879 'a' => null,
880 'd' => [
881 'c' => null,
882 'b' => null,
883 'd' => null,
884 'a' => null
885 ]
886 ];
887 $expectedResult = [
888 'a' => null,
889 'd' => [
890 'a' => null,
891 'b' => null,
892 'c' => null,
893 'd' => null
894 ],
895 'z' => null
896 ];
897 $this->assertSame($expectedResult, ArrayUtility::sortByKeyRecursive($unsortedArray));
898 }
899
900 ///////////////////////
901 // Tests concerning sortArraysByKey
902 ///////////////////////
903 /**
904 * Data provider for sortArraysByKeyCheckIfSortingIsCorrect
905 */
906 public function sortArraysByKeyCheckIfSortingIsCorrectDataProvider()
907 {
908 return [
909 'assoc array index' => [
910 [
911 '22' => [
912 'uid' => '22',
913 'title' => 'c',
914 'dummy' => 2
915 ],
916 '24' => [
917 'uid' => '24',
918 'title' => 'a',
919 'dummy' => 3
920 ],
921 '23' => [
922 'uid' => '23',
923 'title' => 'b',
924 'dummy' => 4
925 ],
926 ],
927 'title',
928 true,
929 [
930 '24' => [
931 'uid' => '24',
932 'title' => 'a',
933 'dummy' => 3
934 ],
935 '23' => [
936 'uid' => '23',
937 'title' => 'b',
938 'dummy' => 4
939 ],
940 '22' => [
941 'uid' => '22',
942 'title' => 'c',
943 'dummy' => 2
944 ],
945 ],
946 ],
947 'numeric array index' => [
948 [
949 22 => [
950 'uid' => '22',
951 'title' => 'c',
952 'dummy' => 2
953 ],
954 24 => [
955 'uid' => '24',
956 'title' => 'a',
957 'dummy' => 3
958 ],
959 23 => [
960 'uid' => '23',
961 'title' => 'b',
962 'dummy' => 4
963 ],
964 ],
965 'title',
966 true,
967 [
968 24 => [
969 'uid' => '24',
970 'title' => 'a',
971 'dummy' => 3
972 ],
973 23 => [
974 'uid' => '23',
975 'title' => 'b',
976 'dummy' => 4
977 ],
978 22 => [
979 'uid' => '22',
980 'title' => 'c',
981 'dummy' => 2
982 ],
983 ],
984 ],
985 'numeric array index DESC' => [
986 [
987 23 => [
988 'uid' => '23',
989 'title' => 'b',
990 'dummy' => 4
991 ],
992 22 => [
993 'uid' => '22',
994 'title' => 'c',
995 'dummy' => 2
996 ],
997 24 => [
998 'uid' => '24',
999 'title' => 'a',
1000 'dummy' => 3
1001 ],
1002 ],
1003 'title',
1004 false,
1005 [
1006 22 => [
1007 'uid' => '22',
1008 'title' => 'c',
1009 'dummy' => 2
1010 ],
1011 23 => [
1012 'uid' => '23',
1013 'title' => 'b',
1014 'dummy' => 4
1015 ],
1016 24 => [
1017 'uid' => '24',
1018 'title' => 'a',
1019 'dummy' => 3
1020 ],
1021 ],
1022 ],
1023 ];
1024 }
1025
1026 /**
1027 * @test
1028 * @dataProvider sortArraysByKeyCheckIfSortingIsCorrectDataProvider
1029 * @param array $array
1030 * @param string $key
1031 * @param bool $ascending
1032 * @param array $expectedResult
1033 */
1034 public function sortArraysByKeyCheckIfSortingIsCorrect(array $array, $key, $ascending, $expectedResult)
1035 {
1036 $sortedArray = ArrayUtility::sortArraysByKey($array, $key, $ascending);
1037 $this->assertSame($expectedResult, $sortedArray);
1038 }
1039
1040 /**
1041 * @test
1042 */
1043 public function sortArraysByKeyThrowsExceptionForNonExistingKey()
1044 {
1045 $this->expectException(\RuntimeException::class);
1046 $this->expectExceptionCode(1373727309);
1047
1048 ArrayUtility::sortArraysByKey([['a'], ['a']], 'dummy');
1049 }
1050
1051 ///////////////////////
1052 // Tests concerning arrayExport
1053 ///////////////////////
1054 /**
1055 * @test
1056 */
1057 public function arrayExportReturnsFormattedMultidimensionalArray()
1058 {
1059 $array = [
1060 'foo' => [
1061 'bar' => 42,
1062 'bar2' => [
1063 'baz' => 'val\'ue',
1064 'baz2' => true,
1065 'baz3' => false,
1066 'baz4' => []
1067 ]
1068 ],
1069 'baz' => 23,
1070 'foobar' => null,
1071 'qux' => 0.1,
1072 'qux2' => 0.000000001,
1073 ];
1074 $expected =
1075 '[' . LF .
1076 ' \'foo\' => [' . LF .
1077 ' \'bar\' => 42,' . LF .
1078 ' \'bar2\' => [' . LF .
1079 ' \'baz\' => \'val\\\'ue\',' . LF .
1080 ' \'baz2\' => true,' . LF .
1081 ' \'baz3\' => false,' . LF .
1082 ' \'baz4\' => [],' . LF .
1083 ' ],' . LF .
1084 ' ],' . LF .
1085 ' \'baz\' => 23,' . LF .
1086 ' \'foobar\' => null,' . LF .
1087 ' \'qux\' => 0.1,' . LF .
1088 ' \'qux2\' => 1.0E-9,' . LF .
1089 ']';
1090 $this->assertSame($expected, ArrayUtility::arrayExport($array));
1091 }
1092
1093 /**
1094 * @test
1095 */
1096 public function arrayExportThrowsExceptionIfObjectShouldBeExported()
1097 {
1098 $array = [
1099 'foo' => [
1100 'bar' => new \stdClass()
1101 ]
1102 ];
1103
1104 $this->expectException(\RuntimeException::class);
1105 $this->expectExceptionCode(1342294987);
1106
1107 ArrayUtility::arrayExport($array);
1108 }
1109
1110 /**
1111 * @test
1112 */
1113 public function arrayExportReturnsNumericArrayKeys()
1114 {
1115 $array = [
1116 'foo' => 'string key',
1117 23 => 'integer key',
1118 '42' => 'string key representing integer'
1119 ];
1120 $expected =
1121 '[' . LF .
1122 ' \'foo\' => \'string key\',' . LF .
1123 ' 23 => \'integer key\',' . LF .
1124 ' 42 => \'string key representing integer\',' . LF .
1125 ']';
1126 $this->assertSame($expected, ArrayUtility::arrayExport($array));
1127 }
1128
1129 /**
1130 * @test
1131 */
1132 public function arrayExportReturnsNoKeyIndexForConsecutiveCountedArrays()
1133 {
1134 $array = [
1135 0 => 'zero',
1136 1 => 'one',
1137 2 => 'two'
1138 ];
1139 $expected =
1140 '[' . LF .
1141 ' \'zero\',' . LF .
1142 ' \'one\',' . LF .
1143 ' \'two\',' . LF .
1144 ']';
1145 $this->assertSame($expected, ArrayUtility::arrayExport($array));
1146 }
1147
1148 /**
1149 * @test
1150 */
1151 public function arrayExportReturnsKeyIndexForNonConsecutiveCountedArrays()
1152 {
1153 $array = [
1154 0 => 'zero',
1155 1 => 'one',
1156 3 => 'three',
1157 4 => 'four'
1158 ];
1159 $expected =
1160 '[' . LF .
1161 ' 0 => \'zero\',' . LF .
1162 ' 1 => \'one\',' . LF .
1163 ' 3 => \'three\',' . LF .
1164 ' 4 => \'four\',' . LF .
1165 ']';
1166 $this->assertSame($expected, ArrayUtility::arrayExport($array));
1167 }
1168
1169 ///////////////////////
1170 // Tests concerning flatten
1171 ///////////////////////
1172
1173 /**
1174 * @return array
1175 */
1176 public function flattenCalculatesExpectedResultDataProvider()
1177 {
1178 return [
1179 'plain array' => [
1180 [
1181 'first' => 1,
1182 'second' => 2
1183 ],
1184 [
1185 'first' => 1,
1186 'second' => 2
1187 ]
1188 ],
1189 'plain array with faulty dots' => [
1190 [
1191 'first.' => 1,
1192 'second.' => 2
1193 ],
1194 [
1195 'first' => 1,
1196 'second' => 2
1197 ]
1198 ],
1199 'nested array of 2 levels' => [
1200 [
1201 'first.' => [
1202 'firstSub' => 1
1203 ],
1204 'second.' => [
1205 'secondSub' => 2
1206 ]
1207 ],
1208 [
1209 'first.firstSub' => 1,
1210 'second.secondSub' => 2
1211 ]
1212 ],
1213 'nested array of 2 levels with faulty dots' => [
1214 [
1215 'first.' => [
1216 'firstSub.' => 1
1217 ],
1218 'second.' => [
1219 'secondSub.' => 2
1220 ]
1221 ],
1222 [
1223 'first.firstSub' => 1,
1224 'second.secondSub' => 2
1225 ]
1226 ],
1227 'nested array of 3 levels' => [
1228 [
1229 'first.' => [
1230 'firstSub.' => [
1231 'firstSubSub' => 1
1232 ]
1233 ],
1234 'second.' => [
1235 'secondSub.' => [
1236 'secondSubSub' => 2
1237 ]
1238 ]
1239 ],
1240 [
1241 'first.firstSub.firstSubSub' => 1,
1242 'second.secondSub.secondSubSub' => 2
1243 ]
1244 ],
1245 'nested array of 3 levels with faulty dots' => [
1246 [
1247 'first.' => [
1248 'firstSub.' => [
1249 'firstSubSub.' => 1
1250 ]
1251 ],
1252 'second.' => [
1253 'secondSub.' => [
1254 'secondSubSub.' => 2
1255 ]
1256 ]
1257 ],
1258 [
1259 'first.firstSub.firstSubSub' => 1,
1260 'second.secondSub.secondSubSub' => 2
1261 ]
1262 ]
1263 ];
1264 }
1265
1266 /**
1267 * @test
1268 * @param array $array
1269 * @param array $expected
1270 * @dataProvider flattenCalculatesExpectedResultDataProvider
1271 */
1272 public function flattenCalculatesExpectedResult(array $array, array $expected)
1273 {
1274 $this->assertEquals($expected, ArrayUtility::flatten($array));
1275 }
1276
1277 ///////////////////////
1278 // Tests concerning intersectRecursive
1279 ///////////////////////
1280
1281 /**
1282 * @return array
1283 */
1284 public function intersectRecursiveCalculatesExpectedResultDataProvider()
1285 {
1286 $sameObject = new \stdClass();
1287 return [
1288 // array($source, $mask, $expected)
1289 'empty array is returned if source is empty array' => [
1290 [],
1291 [
1292 'foo' => 'bar',
1293 ],
1294 [],
1295 ],
1296 'empty array is returned if mask is empty' => [
1297 [
1298 'foo' => 'bar',
1299 ],
1300 [],
1301 [],
1302 ],
1303 'key is kept on first level if exists in mask' => [
1304 [
1305 'foo' => 42,
1306 ],
1307 [
1308 'foo' => 42,
1309 ],
1310 [
1311 'foo' => 42,
1312 ],
1313 ],
1314 'value of key in source is kept if mask has different value' => [
1315 [
1316 'foo' => 42,
1317 ],
1318 [
1319 'foo' => new \stdClass(),
1320 ],
1321 [
1322 'foo' => 42,
1323 ],
1324 ],
1325 'key is kept on first level if according mask value is NULL' => [
1326 [
1327 'foo' => 42,
1328 ],
1329 [
1330 'foo' => null,
1331 ],
1332 [
1333 'foo' => 42,
1334 ],
1335 ],
1336 'null in source value is kept' => [
1337 [
1338 'foo' => null,
1339 ],
1340 [
1341 'foo' => 'bar',
1342 ],
1343 [
1344 'foo' => null,
1345 ]
1346 ],
1347 'mask does not add new keys' => [
1348 [
1349 'foo' => 42,
1350 ],
1351 [
1352 'foo' => 23,
1353 'bar' => [
1354 4711
1355 ],
1356 ],
1357 [
1358 'foo' => 42,
1359 ],
1360 ],
1361 'mask does not overwrite simple values with arrays' => [
1362 [
1363 'foo' => 42,
1364 ],
1365 [
1366 'foo' => [
1367 'bar' => 23,
1368 ],
1369 ],
1370 [
1371 'foo' => 42,
1372 ],
1373 ],
1374 'key is kept on first level if according mask value is array' => [
1375 [
1376 'foo' => 42,
1377 ],
1378 [
1379 'foo' => [
1380 'bar' => 23
1381 ],
1382 ],
1383 [
1384 'foo' => 42,
1385 ],
1386 ],
1387 'full array is kept if value is array and mask value is simple type' => [
1388 [
1389 'foo' => [
1390 'bar' => 23
1391 ],
1392 ],
1393 [
1394 'foo' => 42,
1395 ],
1396 [
1397 'foo' => [
1398 'bar' => 23
1399 ],
1400 ],
1401 ],
1402 'key handling is type agnostic' => [
1403 [
1404 42 => 'foo',
1405 ],
1406 [
1407 '42' => 'bar',
1408 ],
1409 [
1410 42 => 'foo',
1411 ],
1412 ],
1413 'value is same if value is object' => [
1414 [
1415 'foo' => $sameObject,
1416 ],
1417 [
1418 'foo' => 'something',
1419 ],
1420 [
1421 'foo' => $sameObject,
1422 ],
1423 ],
1424 'mask does not add simple value to result if key does not exist in source' => [
1425 [
1426 'foo' => '42',
1427 ],
1428 [
1429 'foo' => '42',
1430 'bar' => 23
1431 ],
1432 [
1433 'foo' => '42',
1434 ],
1435 ],
1436 'array of source is kept if value of mask key exists but is no array' => [
1437 [
1438 'foo' => '42',
1439 'bar' => [
1440 'baz' => 23
1441 ],
1442 ],
1443 [
1444 'foo' => 'value is not significant',
1445 'bar' => null,
1446 ],
1447 [
1448 'foo' => '42',
1449 'bar' => [
1450 'baz' => 23
1451 ],
1452 ],
1453 ],
1454 'sub arrays are kept if mask has according sub array key and is similar array' => [
1455 [
1456 'first1' => 42,
1457 'first2' => [
1458 'second1' => 23,
1459 'second2' => 4711,
1460 ],
1461 ],
1462 [
1463 'first1' => 42,
1464 'first2' => [
1465 'second1' => 'exists but different',
1466 ],
1467 ],
1468 [
1469 'first1' => 42,
1470 'first2' => [
1471 'second1' => 23,
1472 ],
1473 ],
1474 ],
1475 ];
1476 }
1477
1478 /**
1479 * @test
1480 * @param array $source
1481 * @param array $mask
1482 * @param array $expected
1483 * @dataProvider intersectRecursiveCalculatesExpectedResultDataProvider
1484 */
1485 public function intersectRecursiveCalculatesExpectedResult(array $source, array $mask, array $expected)
1486 {
1487 $this->assertSame($expected, ArrayUtility::intersectRecursive($source, $mask));
1488 }
1489
1490 ///////////////////////
1491 // Tests concerning renumberKeysToAvoidLeapsIfKeysAreAllNumeric
1492 ///////////////////////
1493 /**
1494 * @return array
1495 */
1496 public function renumberKeysToAvoidLeapsIfKeysAreAllNumericDataProvider()
1497 {
1498 return [
1499 'empty array is returned if source is empty array' => [
1500 [],
1501 []
1502 ],
1503 'returns self if array is already numerically keyed' => [
1504 [1, 2, 3],
1505 [1, 2, 3]
1506 ],
1507 'returns correctly if keys are numeric, but contains a leap' => [
1508 [0 => 'One', 1 => 'Two', 3 => 'Three'],
1509 [0 => 'One', 1 => 'Two', 2 => 'Three'],
1510 ],
1511 'returns correctly even though keys are strings but still numeric' => [
1512 ['0' => 'One', '1' => 'Two', '3' => 'Three'],
1513 [0 => 'One', 1 => 'Two', 2 => 'Three'],
1514 ],
1515 'returns correctly if just a single keys is not numeric' => [
1516 [0 => 'Zero', '1' => 'One', 'Two' => 'Two'],
1517 [0 => 'Zero', '1' => 'One', 'Two' => 'Two'],
1518 ],
1519 'returns unchanged if keys end with a dot' => [
1520 ['2.' => 'Two', '1.' => 'One', '0.' => 'Zero'],
1521 ['2.' => 'Two', '1.' => 'One', '0.' => 'Zero'],
1522 ],
1523 'return self with nested numerically keyed array' => [
1524 [
1525 'One',
1526 'Two',
1527 'Three',
1528 [
1529 'sub.One',
1530 'sub.Two',
1531 ]
1532 ],
1533 [
1534 'One',
1535 'Two',
1536 'Three',
1537 [
1538 'sub.One',
1539 'sub.Two',
1540 ]
1541 ]
1542 ],
1543 'returns correctly with nested numerically keyed array with leaps' => [
1544 [
1545 'One',
1546 'Two',
1547 'Three',
1548 [
1549 0 => 'sub.One',
1550 2 => 'sub.Two',
1551 ]
1552 ],
1553 [
1554 'One',
1555 'Two',
1556 'Three',
1557 [
1558 'sub.One',
1559 'sub.Two',
1560 ]
1561 ]
1562 ],
1563 'returns correctly with nested string-keyed array' => [
1564 [
1565 'One',
1566 'Two',
1567 'Three',
1568 [
1569 'one' => 'sub.One',
1570 'two' => 'sub.Two',
1571 ]
1572 ],
1573 [
1574 'One',
1575 'Two',
1576 'Three',
1577 [
1578 'one' => 'sub.One',
1579 'two' => 'sub.Two',
1580 ]
1581 ]
1582 ],
1583 'returns correctly with deeply nested arrays' => [
1584 [
1585 'One',
1586 'Two',
1587 [
1588 'one' => 1,
1589 'two' => 2,
1590 'three' => [
1591 2 => 'SubSubOne',
1592 5 => 'SubSubTwo',
1593 9 => [0, 1, 2],
1594 []
1595 ]
1596 ]
1597 ],
1598 [
1599 'One',
1600 'Two',
1601 [
1602 'one' => 1,
1603 'two' => 2,
1604 'three' => [
1605 'SubSubOne',
1606 'SubSubTwo',
1607 [0, 1, 2],
1608 []
1609 ]
1610 ]
1611 ]
1612 ]
1613 ];
1614 }
1615
1616 /**
1617 * @test
1618 * @param array $inputArray
1619 * @param array $expected
1620 * @dataProvider renumberKeysToAvoidLeapsIfKeysAreAllNumericDataProvider
1621 */
1622 public function renumberKeysToAvoidLeapsIfKeysAreAllNumericReturnsExpectedOrder(array $inputArray, array $expected)
1623 {
1624 $this->assertEquals($expected, ArrayUtility::renumberKeysToAvoidLeapsIfKeysAreAllNumeric($inputArray));
1625 }
1626
1627 /**
1628 * @return array
1629 */
1630 public function mergeRecursiveWithOverruleCalculatesExpectedResultDataProvider()
1631 {
1632 return [
1633 'Override array can reset string to array' => [
1634 [
1635 'first' => [
1636 'second' => 'foo',
1637 ],
1638 ],
1639 [
1640 'first' => [
1641 'second' => ['third' => 'bar'],
1642 ],
1643 ],
1644 true,
1645 true,
1646 true,
1647 [
1648 'first' => [
1649 'second' => ['third' => 'bar'],
1650 ],
1651 ],
1652 ],
1653 'Override array does not reset array to string (weird!)' => [
1654 [
1655 'first' => [],
1656 ],
1657 [
1658 'first' => 'foo',
1659 ],
1660 true,
1661 true,
1662 true,
1663 [
1664 'first' => [], // This is rather unexpected, naive expectation: first => 'foo'
1665 ],
1666 ],
1667 'Override array does override string with null' => [
1668 [
1669 'first' => 'foo',
1670 ],
1671 [
1672 'first' => null,
1673 ],
1674 true,
1675 true,
1676 true,
1677 [
1678 'first' => null,
1679 ],
1680 ],
1681 'Override array does override null with string' => [
1682 [
1683 'first' => null,
1684 ],
1685 [
1686 'first' => 'foo',
1687 ],
1688 true,
1689 true,
1690 true,
1691 [
1692 'first' => 'foo',
1693 ],
1694 ],
1695 'Override array does override null with empty string' => [
1696 [
1697 'first' => null,
1698 ],
1699 [
1700 'first' => '',
1701 ],
1702 true,
1703 true,
1704 true,
1705 [
1706 'first' => '',
1707 ],
1708 ],
1709 'Override array does not override string with NULL if requested' => [
1710 [
1711 'first' => 'foo',
1712 ],
1713 [
1714 'first' => null,
1715 ],
1716 true,
1717 false, // no include empty values
1718 true,
1719 [
1720 'first' => 'foo',
1721 ],
1722 ],
1723 'Override array does override null with null' => [
1724 [
1725 'first' => null,
1726 ],
1727 [
1728 'first' => null,
1729 ],
1730 true,
1731 true,
1732 true,
1733 [
1734 'first' => '',
1735 ],
1736 ],
1737 'Override array can __UNSET values' => [
1738 [
1739 'first' => [
1740 'second' => 'second',
1741 'third' => 'third',
1742 ],
1743 'fifth' => [],
1744 ],
1745 [
1746 'first' => [
1747 'second' => 'overrule',
1748 'third' => '__UNSET',
1749 'fourth' => 'overrile',
1750 ],
1751 'fifth' => '__UNSET',
1752 ],
1753 true,
1754 true,
1755 true,
1756 [
1757 'first' => [
1758 'second' => 'overrule',
1759 'fourth' => 'overrile',
1760 ],
1761 ],
1762 ],
1763 'Override can add keys' => [
1764 [
1765 'first' => 'foo',
1766 ],
1767 [
1768 'second' => 'bar',
1769 ],
1770 true,
1771 true,
1772 true,
1773 [
1774 'first' => 'foo',
1775 'second' => 'bar',
1776 ],
1777 ],
1778 'Override does not add key if __UNSET' => [
1779 [
1780 'first' => 'foo',
1781 ],
1782 [
1783 'second' => '__UNSET',
1784 ],
1785 true,
1786 true,
1787 true,
1788 [
1789 'first' => 'foo',
1790 ],
1791 ],
1792 'Override does not add key if not requested' => [
1793 [
1794 'first' => 'foo',
1795 ],
1796 [
1797 'second' => 'bar',
1798 ],
1799 false, // no add keys
1800 true,
1801 true,
1802 [
1803 'first' => 'foo',
1804 ],
1805 ],
1806 'Override does not add key if not requested with add include empty values' => [
1807 [
1808 'first' => 'foo',
1809 ],
1810 [
1811 'second' => 'bar',
1812 ],
1813 false, // no add keys
1814 false, // no include empty values
1815 true,
1816 [
1817 'first' => 'foo',
1818 ],
1819 ],
1820 'Override does not override string with empty string if requested' => [
1821 [
1822 'first' => 'foo',
1823 ],
1824 [
1825 'first' => '',
1826 ],
1827 true,
1828 false, // no include empty values
1829 true,
1830 [
1831 'first' => 'foo',
1832 ],
1833 ],
1834 'Override array does merge instead of __UNSET if requested (weird!)' => [
1835 [
1836 'first' => [
1837 'second' => 'second',
1838 'third' => 'third',
1839 ],
1840 'fifth' => [],
1841 ],
1842 [
1843 'first' => [
1844 'second' => 'overrule',
1845 'third' => '__UNSET',
1846 'fourth' => 'overrile',
1847 ],
1848 'fifth' => '__UNSET',
1849 ],
1850 true,
1851 true,
1852 false,
1853 [
1854 'first' => [
1855 'second' => 'overrule',
1856 'third' => '__UNSET', // overruled
1857 'fourth' => 'overrile',
1858 ],
1859 'fifth' => [], // not overruled with string here, naive expectation: 'fifth' => '__UNSET'
1860 ],
1861 ],
1862 ];
1863 }
1864
1865 /**
1866 * @test
1867 * @dataProvider mergeRecursiveWithOverruleCalculatesExpectedResultDataProvider
1868 * @param array $input1 Input 1
1869 * @param array $input2 Input 2
1870 * @param bool $addKeys TRUE if should add keys, else FALSE
1871 * @param bool $includeEmptyValues TRUE if should include empty values, else FALSE
1872 * @param bool $enableUnsetFeature TRUE if should enable unset feature, else FALSE
1873 * @param array $expected expected array
1874 */
1875 public function mergeRecursiveWithOverruleCalculatesExpectedResult($input1, $input2, $addKeys, $includeEmptyValues, $enableUnsetFeature, $expected)
1876 {
1877 ArrayUtility::mergeRecursiveWithOverrule($input1, $input2, $addKeys, $includeEmptyValues, $enableUnsetFeature);
1878 $this->assertEquals($expected, $input1);
1879 }
1880
1881 //////////////////////////////////
1882 // Tests concerning removeArrayEntryByValue
1883 //////////////////////////////////
1884 /**
1885 * @test
1886 */
1887 public function checkRemoveArrayEntryByValueRemovesEntriesFromOneDimensionalArray()
1888 {
1889 $inputArray = [
1890 '0' => 'test1',
1891 '1' => 'test2',
1892 '2' => 'test3',
1893 '3' => 'test2'
1894 ];
1895 $compareValue = 'test2';
1896 $expectedResult = [
1897 '0' => 'test1',
1898 '2' => 'test3'
1899 ];
1900 $actualResult = ArrayUtility::removeArrayEntryByValue($inputArray, $compareValue);
1901 $this->assertEquals($expectedResult, $actualResult);
1902 }
1903
1904 /**
1905 * @test
1906 */
1907 public function checkRemoveArrayEntryByValueRemovesEntriesFromMultiDimensionalArray()
1908 {
1909 $inputArray = [
1910 '0' => 'foo',
1911 '1' => [
1912 '10' => 'bar'
1913 ],
1914 '2' => 'bar'
1915 ];
1916 $compareValue = 'bar';
1917 $expectedResult = [
1918 '0' => 'foo',
1919 '1' => []
1920 ];
1921 $actualResult = ArrayUtility::removeArrayEntryByValue($inputArray, $compareValue);
1922 $this->assertEquals($expectedResult, $actualResult);
1923 }
1924
1925 /**
1926 * @test
1927 */
1928 public function checkRemoveArrayEntryByValueRemovesEntryWithEmptyString()
1929 {
1930 $inputArray = [
1931 '0' => 'foo',
1932 '1' => '',
1933 '2' => 'bar'
1934 ];
1935 $compareValue = '';
1936 $expectedResult = [
1937 '0' => 'foo',
1938 '2' => 'bar'
1939 ];
1940 $actualResult = ArrayUtility::removeArrayEntryByValue($inputArray, $compareValue);
1941 $this->assertEquals($expectedResult, $actualResult);
1942 }
1943
1944 //////////////////////////////////
1945 // Tests concerning keepItemsInArray
1946 //////////////////////////////////
1947 /**
1948 * @test
1949 * @dataProvider keepItemsInArrayWorksWithOneArgumentDataProvider
1950 * @param mixed $search The items which are allowed/kept in the array
1951 * @param array $array target array
1952 * @param array $expected expected array
1953 */
1954 public function keepItemsInArrayWorksWithOneArgument($search, $array, $expected)
1955 {
1956 $this->assertEquals($expected, ArrayUtility::keepItemsInArray($array, $search));
1957 }
1958
1959 /**
1960 * Data provider for keepItemsInArrayWorksWithOneArgument
1961 *
1962 * @return array
1963 */
1964 public function keepItemsInArrayWorksWithOneArgumentDataProvider()
1965 {
1966 $array = [
1967 'one' => 'one',
1968 'two' => 'two',
1969 'three' => 'three'
1970 ];
1971 return [
1972 'Empty argument will match "all" elements' => [null, $array, $array],
1973 'No match' => ['four', $array, []],
1974 'One match' => ['two', $array, ['two' => 'two']],
1975 'Multiple matches' => ['two,one', $array, ['one' => 'one', 'two' => 'two']],
1976 'Argument can be an array' => [['three'], $array, ['three' => 'three']]
1977 ];
1978 }
1979
1980 /**
1981 * Shows the example from the doc comment where
1982 * a function is used to reduce the sub arrays to one item which
1983 * is then used for the matching.
1984 *
1985 * @test
1986 */
1987 public function keepItemsInArrayCanUseClosure()
1988 {
1989 $array = [
1990 'aa' => ['first', 'second'],
1991 'bb' => ['third', 'fourth'],
1992 'cc' => ['fifth', 'sixth']
1993 ];
1994 $expected = ['bb' => ['third', 'fourth']];
1995 $keepItems = 'third';
1996 $match = ArrayUtility::keepItemsInArray(
1997 $array,
1998 $keepItems,
1999 function ($value) {
2000 return $value[0];
2001 }
2002 );
2003 $this->assertEquals($expected, $match);
2004 }
2005
2006 //////////////////////////////////
2007 // Tests concerning remapArrayKeys
2008 //////////////////////////////////
2009 /**
2010 * @test
2011 */
2012 public function remapArrayKeysExchangesKeysWithGivenMapping()
2013 {
2014 $array = [
2015 'one' => 'one',
2016 'two' => 'two',
2017 'three' => 'three'
2018 ];
2019 $keyMapping = [
2020 'one' => '1',
2021 'two' => '2'
2022 ];
2023 $expected = [
2024 '1' => 'one',
2025 '2' => 'two',
2026 'three' => 'three'
2027 ];
2028 ArrayUtility::remapArrayKeys($array, $keyMapping);
2029 $this->assertEquals($expected, $array);
2030 }
2031
2032 //////////////////////////////////////
2033 // Tests concerning arrayDiffAssocRecursive
2034 //////////////////////////////////////
2035 /**
2036 * @test
2037 */
2038 public function arrayDiffAssocRecursiveHandlesOneDimensionalArrays()
2039 {
2040 $array1 = [
2041 'key1' => 'value1',
2042 'key2' => 'value2',
2043 'key3' => 'value3'
2044 ];
2045 $array2 = [
2046 'key1' => 'value1',
2047 'key3' => 'value3'
2048 ];
2049 $expectedResult = [
2050 'key2' => 'value2'
2051 ];
2052 $actualResult = ArrayUtility::arrayDiffAssocRecursive($array1, $array2);
2053 $this->assertEquals($expectedResult, $actualResult);
2054 }
2055
2056 /**
2057 * @test
2058 */
2059 public function arrayDiffAssocRecursiveHandlesMultiDimensionalArrays()
2060 {
2061 $array1 = [
2062 'key1' => 'value1',
2063 'key2' => [
2064 'key21' => 'value21',
2065 'key22' => 'value22',
2066 'key23' => [
2067 'key231' => 'value231',
2068 'key232' => 'value232'
2069 ]
2070 ]
2071 ];
2072 $array2 = [
2073 'key1' => 'value1',
2074 'key2' => [
2075 'key21' => 'value21',
2076 'key23' => [
2077 'key231' => 'value231'
2078 ]
2079 ]
2080 ];
2081 $expectedResult = [
2082 'key2' => [
2083 'key22' => 'value22',
2084 'key23' => [
2085 'key232' => 'value232'
2086 ]
2087 ]
2088 ];
2089 $actualResult = ArrayUtility::arrayDiffAssocRecursive($array1, $array2);
2090 $this->assertEquals($expectedResult, $actualResult);
2091 }
2092
2093 /**
2094 * @test
2095 */
2096 public function arrayDiffAssocRecursiveHandlesMixedArrays()
2097 {
2098 $array1 = [
2099 'key1' => [
2100 'key11' => 'value11',
2101 'key12' => 'value12'
2102 ],
2103 'key2' => 'value2',
2104 'key3' => 'value3'
2105 ];
2106 $array2 = [
2107 'key1' => 'value1',
2108 'key2' => [
2109 'key21' => 'value21'
2110 ]
2111 ];
2112 $expectedResult = [
2113 'key3' => 'value3'
2114 ];
2115 $actualResult = ArrayUtility::arrayDiffAssocRecursive($array1, $array2);
2116 $this->assertEquals($expectedResult, $actualResult);
2117 }
2118
2119 //////////////////////////////////////
2120 // Tests concerning naturalKeySortRecursive
2121 //////////////////////////////////////
2122
2123 /**
2124 * @test
2125 */
2126 public function naturalKeySortRecursiveSortsOneDimensionalArrayByNaturalOrder()
2127 {
2128 $testArray = [
2129 'bb' => 'bb',
2130 'ab' => 'ab',
2131 '123' => '123',
2132 'aaa' => 'aaa',
2133 'abc' => 'abc',
2134 '23' => '23',
2135 'ba' => 'ba',
2136 'bad' => 'bad',
2137 '2' => '2',
2138 'zap' => 'zap',
2139 '210' => '210'
2140 ];
2141 $expectedResult = [
2142 '2',
2143 '23',
2144 '123',
2145 '210',
2146 'aaa',
2147 'ab',
2148 'abc',
2149 'ba',
2150 'bad',
2151 'bb',
2152 'zap'
2153 ];
2154 ArrayUtility::naturalKeySortRecursive($testArray);
2155 $this->assertEquals($expectedResult, array_values($testArray));
2156 }
2157
2158 /**
2159 * @test
2160 */
2161 public function naturalKeySortRecursiveSortsMultiDimensionalArrayByNaturalOrder()
2162 {
2163 $testArray = [
2164 '2' => '2',
2165 'bb' => 'bb',
2166 'ab' => 'ab',
2167 '23' => '23',
2168 'aaa' => [
2169 'bb' => 'bb',
2170 'ab' => 'ab',
2171 '123' => '123',
2172 'aaa' => 'aaa',
2173 '2' => '2',
2174 'abc' => 'abc',
2175 'ba' => 'ba',
2176 '23' => '23',
2177 'bad' => [
2178 'bb' => 'bb',
2179 'ab' => 'ab',
2180 '123' => '123',
2181 'aaa' => 'aaa',
2182 'abc' => 'abc',
2183 '23' => '23',
2184 'ba' => 'ba',
2185 'bad' => 'bad',
2186 '2' => '2',
2187 'zap' => 'zap',
2188 '210' => '210'
2189 ],
2190 '210' => '210',
2191 'zap' => 'zap'
2192 ],
2193 'abc' => 'abc',
2194 'ba' => 'ba',
2195 '210' => '210',
2196 'bad' => 'bad',
2197 '123' => '123',
2198 'zap' => 'zap'
2199 ];
2200 $expectedResult = [
2201 '2',
2202 '23',
2203 '123',
2204 '210',
2205 'aaa',
2206 'ab',
2207 'abc',
2208 'ba',
2209 'bad',
2210 'bb',
2211 'zap'
2212 ];
2213 ArrayUtility::naturalKeySortRecursive($testArray);
2214 $this->assertEquals($expectedResult, array_values(array_keys($testArray['aaa']['bad'])));
2215 $this->assertEquals($expectedResult, array_values(array_keys($testArray['aaa'])));
2216 $this->assertEquals($expectedResult, array_values(array_keys($testArray)));
2217 }
2218
2219 /**
2220 * Data provider for filterAndSortByNumericKeysBehavesCorrectlyForAcceptAnyKeysIsTrue
2221 *
2222 * @return array
2223 */
2224 public function filterAndSortByNumericKeysWithAcceptAnyKey()
2225 {
2226 return [
2227 'ordered list of plain numeric keys' => [
2228 'input' => [
2229 '10' => 'foo',
2230 '20' => 'bar',
2231 ],
2232 'expected' => [
2233 10,
2234 20,
2235 ],
2236 ],
2237 'unordered list of plain numeric keys' => [
2238 'input' => [
2239 '20' => 'bar',
2240 '10' => 'foo',
2241 ],
2242 'expected' => [
2243 10,
2244 20,
2245 ],
2246 ],
2247 'list of string keys' => [
2248 'input' => [
2249 '10.' => [
2250 'wrap' => 'foo',
2251 ],
2252 '20.' => [
2253 'wrap' => 'bar',
2254 ],
2255 ],
2256 'expected' => [
2257 10,
2258 20,
2259 ],
2260 ],
2261 'list of mixed keys' => [
2262 'input' => [
2263 '10' => 'foo',
2264 '20.' => [
2265 'wrap' => 'bar',
2266 ],
2267 ],
2268 'expected' => [
2269 10,
2270 20,
2271 ],
2272 ],
2273 'list of mixed keys with one not interpreted as integer' => [
2274 'input' => [
2275 '10' => 'foo',
2276 'bla20.' => [
2277 'wrap' => 'bar',
2278 ],
2279 ],
2280 'expected' => [
2281 0,
2282 10,
2283 ],
2284 ],
2285 'list of mixed keys with more than one not interpreted as integer' => [
2286 'input' => [
2287 '10' => 'foo',
2288 'bla20.' => [
2289 'wrap' => 'bar',
2290 ],
2291 'bla21.' => [
2292 'wrap' => 'foobar',
2293 ],
2294 ],
2295 'expected' => [
2296 0,
2297 10,
2298 ],
2299 ],
2300 ];
2301 }
2302
2303 /**
2304 * @test
2305 * @dataProvider filterAndSortByNumericKeysWithAcceptAnyKey
2306 *
2307 * @param array $input
2308 * @param array $expected
2309 */
2310 public function filterAndSortByNumericKeysBehavesCorrectlyForAcceptAnyKeysIsTrue($input, $expected)
2311 {
2312 $result = ArrayUtility::filterAndSortByNumericKeys($input, true);
2313 $this->assertEquals($result, $expected);
2314 }
2315
2316 /**
2317 * Data provider for filterAndSortByNumericKeysBehavesCorrectlyForAcceptAnyKeysIsFalse
2318 *
2319 * @return array
2320 */
2321 public function filterAndSortByNumericKeysWithoutAcceptAnyKey()
2322 {
2323 return [
2324 'ordered list of plain numeric keys' => [
2325 'input' => [
2326 '10' => 'foo',
2327 '20' => 'bar',
2328 ],
2329 'expected' => [
2330 10,
2331 20,
2332 ],
2333 ],
2334 'unordered list of plain numeric keys' => [
2335 'input' => [
2336 '20' => 'bar',
2337 '10' => 'foo',
2338 ],
2339 'expected' => [
2340 10,
2341 20,
2342 ],
2343 ],
2344 'list of string keys' => [
2345 'input' => [
2346 '10.' => [
2347 'wrap' => 'foo',
2348 ],
2349 '20.' => [
2350 'wrap' => 'bar',
2351 ],
2352 ],
2353 'expected' => [],
2354 ],
2355 'list of mixed keys' => [
2356 'input' => [
2357 '10' => 'foo',
2358 '20.' => [
2359 'wrap' => 'bar',
2360 ],
2361 ],
2362 'expected' => [
2363 10,
2364 ],
2365 ],
2366 ];
2367 }
2368
2369 /**
2370 * @test
2371 * @dataProvider filterAndSortByNumericKeysWithoutAcceptAnyKey
2372 *
2373 * @param array $input
2374 * @param array $expected
2375 */
2376 public function filterAndSortByNumericKeysBehavesCorrectlyForAcceptAnyKeysIsFalse($input, $expected)
2377 {
2378 $result = ArrayUtility::filterAndSortByNumericKeys($input);
2379 $this->assertEquals($result, $expected);
2380 }
2381
2382 /**
2383 * dataProvider for sortArrayWithIntegerKeys
2384 *
2385 * @return array
2386 */
2387 public function sortArrayWithIntegerKeysDataProvider()
2388 {
2389 return [
2390 [
2391 [
2392 '20' => 'test1',
2393 '11' => 'test2',
2394 '16' => 'test3',
2395 ],
2396 [
2397 '11' => 'test2',
2398 '16' => 'test3',
2399 '20' => 'test1',
2400 ]
2401 ],
2402 [
2403 [
2404 '20' => 'test1',
2405 '16.5' => 'test2',
2406 '16' => 'test3',
2407 ],
2408 [
2409 '20' => 'test1',
2410 '16.5' => 'test2',
2411 '16' => 'test3',
2412 ]
2413 ],
2414 [
2415 [
2416 '20' => 'test20',
2417 'somestring' => 'teststring',
2418 '16' => 'test16',
2419 ],
2420 [
2421 '20' => 'test20',
2422 'somestring' => 'teststring',
2423 '16' => 'test16',
2424 ]
2425 ],
2426 ];
2427 }
2428
2429 /**
2430 * @test
2431 *
2432 * @param array $arrayToSort
2433 * @param array $expectedArray
2434 *
2435 * @dataProvider sortArrayWithIntegerKeysDataProvider
2436 */
2437 public function sortArrayWithIntegerKeysSortsNumericArrays(array $arrayToSort, array $expectedArray)
2438 {
2439 $sortedArray = ArrayUtility::sortArrayWithIntegerKeys($arrayToSort);
2440 $this->assertSame($sortedArray, $expectedArray);
2441 }
2442
2443 /**
2444 * @test
2445 */
2446 public function assertAllArrayKeysAreValidThrowsExceptionOnNotAllowedArrayKeys()
2447 {
2448 $this->expectException(\InvalidArgumentException::class);
2449 $this->expectExceptionCode(1325697085);
2450
2451 $arrayToTest = [
2452 'roger' => '',
2453 'francine' => '',
2454 'stan' => '',
2455 ];
2456
2457 $allowedArrayKeys = [
2458 'roger',
2459 'francine',
2460 ];
2461
2462 ArrayUtility::assertAllArrayKeysAreValid($arrayToTest, $allowedArrayKeys);
2463 }
2464
2465 /**
2466 * @test
2467 */
2468 public function assertAllArrayKeysAreValidReturnsNullOnAllowedArrayKeys()
2469 {
2470 $arrayToTest = [
2471 'roger' => '',
2472 'francine' => '',
2473 'stan' => '',
2474 ];
2475
2476 $allowedArrayKeys = [
2477 'roger',
2478 'francine',
2479 'stan',
2480 ];
2481
2482 ArrayUtility::assertAllArrayKeysAreValid($arrayToTest, $allowedArrayKeys);
2483 }
2484
2485 /**
2486 * @test
2487 */
2488 public function sortArrayWithIntegerKeysRecursiveExpectSorting()
2489 {
2490 $input = [
2491 20 => 'b',
2492 10 => 'a',
2493 40 => 'd',
2494 30 => 'c',
2495 50 => [
2496 20 => 'a',
2497 10 => 'b',
2498 ],
2499 ];
2500
2501 $expected = [
2502 10 => 'a',
2503 20 => 'b',
2504 30 => 'c',
2505 40 => 'd',
2506 50 => [
2507 10 => 'b',
2508 20 => 'a',
2509 ],
2510 ];
2511
2512 $this->assertSame($expected, ArrayUtility::sortArrayWithIntegerKeysRecursive($input));
2513 }
2514
2515 /**
2516 * @test
2517 */
2518 public function sortArrayWithIntegerKeysRecursiveExpectNoSorting()
2519 {
2520 $input = [
2521 'b' => 'b',
2522 10 => 'a',
2523 40 => 'd',
2524 30 => 'c',
2525 ];
2526
2527 $expected = [
2528 'b' => 'b',
2529 10 => 'a',
2530 40 => 'd',
2531 30 => 'c',
2532 ];
2533
2534 $this->assertSame($expected, ArrayUtility::sortArrayWithIntegerKeysRecursive($input));
2535 }
2536
2537 /**
2538 * @test
2539 */
2540 public function reIndexNumericArrayKeysRecursiveExpectReindexing()
2541 {
2542 $input = [
2543 20 => 'b',
2544 10 => 'a',
2545 40 => 'd',
2546 30 => 'c',
2547 50 => [
2548 20 => 'a',
2549 10 => 'b',
2550 ],
2551 ];
2552
2553 $expected = [
2554 0 => 'b',
2555 1 => 'a',
2556 2 => 'd',
2557 3 => 'c',
2558 4 => [
2559 0 => 'a',
2560 1 => 'b',
2561 ],
2562 ];
2563
2564 $this->assertSame($expected, ArrayUtility::reIndexNumericArrayKeysRecursive($input));
2565 }
2566
2567 /**
2568 * @test
2569 */
2570 public function reIndexNumericArrayKeysRecursiveExpectNoReindexing()
2571 {
2572 $input = [
2573 'a' => 'b',
2574 10 => 'a',
2575 40 => 'd',
2576 30 => 'c',
2577 50 => [
2578 20 => 'a',
2579 10 => 'b',
2580 ],
2581 ];
2582
2583 $expected = [
2584 'a' => 'b',
2585 10 => 'a',
2586 40 => 'd',
2587 30 => 'c',
2588 50 => [
2589 0 => 'a',
2590 1 => 'b',
2591 ],
2592 ];
2593
2594 $this->assertSame($expected, ArrayUtility::reIndexNumericArrayKeysRecursive($input));
2595 }
2596
2597 /**
2598 * @test
2599 */
2600 public function removeNullValuesRecursiveExpectRemoval()
2601 {
2602 $input = [
2603 'a' => 'a',
2604 'b' => [
2605 'c' => null,
2606 'd' => 'd',
2607 ],
2608 ];
2609
2610 $expected = [
2611 'a' => 'a',
2612 'b' => [
2613 'd' => 'd',
2614 ],
2615 ];
2616
2617 $this->assertSame($expected, ArrayUtility::removeNullValuesRecursive($input));
2618 }
2619
2620 /**
2621 * @test
2622 */
2623 public function stripTagsFromValuesRecursiveExpectRemoval()
2624 {
2625 $input = [
2626 'a' => 'a',
2627 'b' => [
2628 'c' => '<b>i am evil</b>',
2629 'd' => 'd',
2630 ],
2631 ];
2632
2633 $expected = [
2634 'a' => 'a',
2635 'b' => [
2636 'c' => 'i am evil',
2637 'd' => 'd',
2638 ],
2639 ];
2640
2641 $this->assertSame($expected, ArrayUtility::stripTagsFromValuesRecursive($input));
2642 }
2643
2644 /**
2645 * @test
2646 */
2647 public function convertBooleanStringsToBooleanRecursiveExpectConverting()
2648 {
2649 $input = [
2650 'a' => 'a',
2651 'b' => [
2652 'c' => 'true',
2653 'd' => 'd',
2654 ],
2655 ];
2656
2657 $expected = [
2658 'a' => 'a',
2659 'b' => [
2660 'c' => true,
2661 'd' => 'd',
2662 ],
2663 ];
2664
2665 $this->assertSame($expected, ArrayUtility::convertBooleanStringsToBooleanRecursive($input));
2666 }
2667 }