[BUGFIX] Correctly handle identical arrays in arrayDiffAssocRecursive 24/55924/2
authorMarkus Klein <markus.klein@typo3.org>
Tue, 27 Feb 2018 14:57:58 +0000 (15:57 +0100)
committerMarkus Klein <markus.klein@typo3.org>
Tue, 27 Feb 2018 15:45:06 +0000 (16:45 +0100)
Add a new test to make sure that identical input arrays also deliver
an empty result as difference.

Resolves: #84067
Releases: master, 8.7, 7.6
Change-Id: Ia16ca9560094c4ae42eb69cac9e09cd4bef7dc4e
Reviewed-on: https://review.typo3.org/55924
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
typo3/sysext/core/Classes/Utility/ArrayUtility.php
typo3/sysext/core/Tests/Unit/Utility/ArrayUtilityTest.php

index 932a992..bf6f46d 100644 (file)
@@ -764,7 +764,10 @@ class ArrayUtility
                 $differenceArray[$key] = $value;
             } elseif (is_array($value)) {
                 if (is_array($array2[$key])) {
-                    $differenceArray[$key] = self::arrayDiffAssocRecursive($value, $array2[$key]);
+                    $recursiveResult = self::arrayDiffAssocRecursive($value, $array2[$key]);
+                    if (!empty($recursiveResult)) {
+                        $differenceArray[$key] = $recursiveResult;
+                    }
                 }
             }
         }
index 3f0d45d..91d002a 100644 (file)
@@ -2101,7 +2101,7 @@ class ArrayUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
             ]
         ];
         $array2 = [
-            'key1' => 'value1',
+            'key1' => 'valueDoesNotMatter',
             'key2' => [
                 'key21' => 'value21',
                 'key23' => [
@@ -2137,7 +2137,7 @@ class ArrayUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         $array2 = [
             'key1' => 'value1',
             'key2' => [
-                'key21' => 'value21'
+                'key21' => 'valueDoesNotMatter'
             ]
         ];
         $expectedResult = [
@@ -2147,6 +2147,32 @@ class ArrayUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         $this->assertEquals($expectedResult, $actualResult);
     }
 
+    /**
+     * @test
+     */
+    public function arrayDiffAssocRecursiveReturnsEmptyIfEqual()
+    {
+        $array1 = [
+            'key1' => [
+                'key11' => 'value11',
+                'key12' => 'value12'
+            ],
+            'key2' => 'value2',
+            'key3' => 'value3'
+        ];
+        $array2 = [
+            'key1' => [
+                'key11' => 'valueDoesNotMatter',
+                'key12' => 'value12'
+            ],
+            'key2' => 'value2',
+            'key3' => 'value3'
+        ];
+        $expectedResult = [];
+        $actualResult = ArrayUtility::arrayDiffAssocRecursive($array1, $array2);
+        $this->assertEquals($expectedResult, $actualResult);
+    }
+
     //////////////////////////////////////
     // Tests concerning naturalKeySortRecursive
     //////////////////////////////////////