Skip to content

Commit

Permalink
Fixing bug In ConnectionManager::config() when passing an object inst…
Browse files Browse the repository at this point in the history
…ance

When passing an already constructed instance to ConnectionManager::config(),
it was cloning the object instead of storing the same object instance in
the registry. This made impossible inject mocked connection objects into the manager.

Additionally, made the ConnectionRegistry also accept a callable, responsible for
returning an already built connection instance. This brings the config() method
in line with how the Log class works.
  • Loading branch information
lorenzo committed Jan 11, 2016
1 parent a023c2d commit 21be0b5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/Datasource/ConnectionRegistry.php
Expand Up @@ -65,13 +65,24 @@ protected function _throwMissingClassError($class, $plugin)
*
* Part of the template method for Cake\Core\ObjectRegistry::load()
*
* @param string|object $class The classname or object to make.
* If a callable is passed as first argument, The returned value of this
* function will be the result of the callable.
*
* @param string|object|callable $class The classname or object to make.
* @param string $alias The alias of the object.
* @param array $settings An array of settings to use for the driver.
* @return object A connection with the correct settings.
*/
protected function _create($class, $alias, $settings)
{
if (is_callable($class)) {
return $class($alias);
}

if (is_object($class)) {
return $class;
}

unset($settings['className']);
return new $class($settings);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Log/LogEngineRegistry.php
Expand Up @@ -71,7 +71,7 @@ protected function _throwMissingClassError($class, $plugin)
protected function _create($class, $alias, $settings)
{
if (is_callable($class)) {
$class = $class();
$class = $class($alias);
}

if (is_object($class)) {
Expand Down
30 changes: 30 additions & 0 deletions tests/TestCase/Datasource/ConnectionManagerTest.php
Expand Up @@ -250,4 +250,34 @@ public function testParseDsn()
];
$this->assertEquals($expected, $result);
}

/**
* Tests that directly setting an instance in a config, will not return a different
* instance later on
*
* @return void
*/
public function testConfigWithObject()
{
$connection = new FakeConnection;
ConnectionManager::config('test_variant', $connection);
$this->assertSame($connection, ConnectionManager::get('test_variant'));
}

/**
* Tests configuring an instance with a callable
*
* @return void
*/
public function testConfigWithCallable()
{
$connection = new FakeConnection;
$callable = function ($alias) use ($connection) {
$this->assertEquals('test_variant', $alias);
return $connection;
};

ConnectionManager::config('test_variant', $callable);
$this->assertSame($connection, ConnectionManager::get('test_variant'));
}
}
3 changes: 2 additions & 1 deletion tests/TestCase/Log/LogTest.php
Expand Up @@ -618,7 +618,8 @@ public function testWriteUnhandled()
public function testCreateLoggerWithCallable()
{
$instance = new FileLog;
Log::config('default', function () use ($instance) {
Log::config('default', function ($alias) use ($instance) {
$this->assertEquals('default', $alias);
return $instance;
});
$this->assertSame($instance, Log::engine('default'));
Expand Down

0 comments on commit 21be0b5

Please sign in to comment.