Skip to content

Commit

Permalink
Merge pull request #5280 from deizel/duplicate-object
Browse files Browse the repository at this point in the history
Fix ObjectRegistry duplicate detection when comparing nested arrays
  • Loading branch information
markstory committed Nov 29, 2014
2 parents 7303e22 + 0cd5658 commit 8eded25
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Core/ObjectRegistry.php
Expand Up @@ -114,10 +114,11 @@ protected function _checkDuplicate($name, $config) {
if (!$hasConfig) {
throw new RuntimeException($msg);
}
unset($config['enabled']);
if ($hasConfig && array_diff_assoc($config, $existing->config()) != []) {
$existingConfig = $existing->config();
unset($config['enabled'], $existingConfig['enabled']);
if ($hasConfig && json_encode($config) !== json_encode($existingConfig)) {
$msg .= ' with the following config: ';
$msg .= var_export($this->{$name}->config(), true);
$msg .= var_export($existingConfig, true);
$msg .= ' which differs from ' . var_export($config, true);
throw new RuntimeException($msg);
}
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase/Controller/ComponentTest.php
Expand Up @@ -105,6 +105,25 @@ public function testMultipleComponentInitialize() {
$this->assertSame($Banana->testField, $Orange->Banana->testField, 'References are broken');
}

/**
* Test a duplicate component being loaded more than once with same and differing configurations.
*
* @expectedException RuntimeException
* @expectedExceptionMessage The "Banana" alias has already been loaded with the following config:
* @return void
*/
public function testDuplicateComponentInitialize() {
$Collection = new ComponentRegistry();
$Collection->load('Banana', ['property' => ['closure' => function () {
}]]);
$Collection->load('Banana', ['property' => ['closure' => function () {
}]]);

$this->assertInstanceOf('TestApp\Controller\Component\BananaComponent', $Collection->Banana, 'class is wrong');

$Collection->load('Banana', ['property' => ['differs']]);
}

/**
* Test mutually referencing components.
*
Expand Down

0 comments on commit 8eded25

Please sign in to comment.