Browse Source

Merge pull request #15194 from cakephp/fix-15190-3x

3.x - Fix Hash::mergeDiff() not handling scalar values.
othercorey 5 years ago
parent
commit
953b49aa6f
2 changed files with 16 additions and 2 deletions
  1. 2 2
      src/Utility/Hash.php
  2. 14 0
      tests/TestCase/Utility/HashTest.php

+ 2 - 2
src/Utility/Hash.php

@@ -1123,8 +1123,8 @@ class Hash
         foreach ($compare as $key => $value) {
             if (!array_key_exists($key, $data)) {
                 $data[$key] = $value;
-            } elseif (is_array($value)) {
-                $data[$key] = static::mergeDiff($data[$key], $compare[$key]);
+            } elseif (is_array($value) && is_array($data[$key])) {
+                $data[$key] = static::mergeDiff($data[$key], $value);
             }
         }
 

+ 14 - 0
tests/TestCase/Utility/HashTest.php

@@ -3230,6 +3230,20 @@ class HashTest extends TestCase
     }
 
     /**
+     * Test mergeDiff() with scalar elements.
+     *
+     * @return void
+     */
+    public function testMergeDiffWithScalarValue()
+    {
+        $result = Hash::mergeDiff(['a' => 'value'], ['a' => ['value']]);
+        $this->assertSame(['a' => 'value'], $result);
+
+        $result = Hash::mergeDiff(['a' => ['value']], ['a' => 'value']);
+        $this->assertSame(['a' => ['value']], $result);
+    }
+
+    /**
      * Tests Hash::expand
      *
      * @return void