Browse Source

Fix duplicate errors when config data is not different.

Perform a more careful inspection of config data when checking
duplicates. Simply check the json_encode() value has issues when default
config is involved. array_diff* have similar issues with nested data.

Since the empty config data will be the most common case the previous
optimization is still valid and very useful.

Refs #5286
Mark Story 11 years ago
parent
commit
3d7b9f9909
2 changed files with 25 additions and 4 deletions
  1. 14 4
      src/Core/ObjectRegistry.php
  2. 11 0
      tests/TestCase/View/HelperRegistryTest.php

+ 14 - 4
src/Core/ObjectRegistry.php

@@ -114,14 +114,24 @@ abstract class ObjectRegistry {
 		if (!$hasConfig) {
 			throw new RuntimeException($msg);
 		}
-		$existingConfig = $existing->config();
-		unset($config['enabled'], $existingConfig['enabled']);
-
 		if (empty($config)) {
 			return;
 		}
+		$existingConfig = $existing->config();
+		unset($config['enabled'], $existingConfig['enabled']);
 
-		if ($hasConfig && json_encode($config) !== json_encode($existingConfig)) {
+		$fail = false;
+		foreach ($config as $key => $value) {
+			if (isset($existingConfig[$key]) && $existingConfig[$key] !== $value) {
+				$fail = true;
+				break;
+			}
+			if (!array_key_exists($key, $existingConfig)) {
+				$fail = true;
+				break;
+			}
+		}
+		if ($fail) {
 			$msg .= ' with the following config: ';
 			$msg .= var_export($existingConfig, true);
 			$msg .= ' which differs from ' . var_export($config, true);

+ 11 - 0
tests/TestCase/View/HelperRegistryTest.php

@@ -262,4 +262,15 @@ class HelperRegistryTest extends TestCase {
 		$this->Helpers->load('Html', ['same' => 'stuff']);
 	}
 
+/**
+ * Loading a helper with different config, should throw an exception
+ *
+ * @expectedException RuntimeException
+ * @return void
+ */
+	public function testLoadMultipleTimesDifferentConfigValues() {
+		$this->Helpers->load('Html', ['key' => 'value']);
+		$this->Helpers->load('Html', ['key' => 'new value']);
+	}
+
 }