Skip to content
Permalink
Browse files

Fix TableRegistry behavior when called several times with the same op…

…tions
  • Loading branch information...
eryw2 committed Aug 30, 2014
1 parent 1ddd61b commit 795f643e3708b066b84467a85213cb71cc2238c3
Showing with 26 additions and 4 deletions.
  1. +14 −4 src/ORM/TableRegistry.php
  2. +12 −0 tests/TestCase/ORM/TableRegistryTest.php
@@ -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.
@@ -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'])) {
@@ -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

0 comments on commit 795f643

Please sign in to comment.
You can’t perform that action at this time.