Skip to content

Commit

Permalink
Fix TableRegistry behavior when called several times with the same op…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
eryw committed Aug 30, 2014
1 parent 1ddd61b commit 795f643
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/ORM/TableRegistry.php
Expand Up @@ -75,6 +75,12 @@ class TableRegistry {
*/
protected static $_fallbacked = [];

/**
* Contains a list of options that were passed to get() method.
* @var array
*/
protected static $_options = [];

/**
* Stores a list of options to be used when instantiating an object
* with a matching alias.
Expand Down Expand Up @@ -147,15 +153,19 @@ public static function config($alias = null, $options = null) {
public static function get($name, array $options = []) {
list($plugin, $alias) = pluginSplit($name);
$exists = isset(static::$_instances[$alias]);

if ($exists && !empty($options)) {
throw new RuntimeException(sprintf(
'You cannot configure "%s", it already exists in the registry.',
$alias
));
if (static::$_options[$alias] != $options) {
throw new RuntimeException(sprintf(
'You cannot configure "%s", it already exists in the registry.',
$alias
));
}
}
if ($exists) {
return static::$_instances[$alias];
}
static::$_options[$alias] = $options;
$options = ['alias' => $alias] + $options;

if (empty($options['className'])) {
Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase/ORM/TableRegistryTest.php
Expand Up @@ -150,6 +150,18 @@ public function testGetExistingWithConfigData() {
TableRegistry::get('Users', ['table' => 'my_users']);
}

/**
* Test get() can be called several times with the same option without
* throwing an exception.
*
* @return void
*/
public function testGetWithSameOption() {
$result = TableRegistry::get('Users', ['className' => 'Cake\Test\TestCase\ORM\MyUsersTable']);
$result2 = TableRegistry::get('Users', ['className' => 'Cake\Test\TestCase\ORM\MyUsersTable']);
$this->assertEquals($result, $result2);
}

/**
* Tests that tables can be instantiated based on conventions
* and using plugin notation
Expand Down

0 comments on commit 795f643

Please sign in to comment.