Adding a ConenctionManager::drop() method useful for testing and other dynamic tasks

This commit is contained in:
Jose Lorenzo Rodriguez 2011-04-16 22:57:05 -04:30
parent c80a51a163
commit f14a295a3e
2 changed files with 39 additions and 2 deletions

View file

@ -201,8 +201,7 @@ class ConnectionManager {
}
if (empty($name) || empty($config) || array_key_exists($name, self::$_connectionsEnum)) {
$null = null;
return $null;
return null;
}
self::$config->{$name} = $config;
self::$_connectionsEnum[$name] = self::_connectionData($config);
@ -210,6 +209,24 @@ class ConnectionManager {
return $return;
}
/**
* Removes a connection configuration at runtime given its name
*
* @param string $name the connection name as it was created
* @return boolean success if connection was removed, false if it does not exist
*/
public static function drop($name) {
if (empty(self::$_init)) {
self::init();
}
if (!isset(self::$config->{$name})) {
return false;
}
unset(self::$_connectionsEnum[$name], self::$_dataSources[$name], self::$config->{$name});
return true;
}
/**
* Gets a list of class and file names associated with the user-defined DataSource connections
*

View file

@ -310,4 +310,24 @@ class ConnectionManagerTest extends CakeTestCase {
$connections = ConnectionManager::enumConnectionObjects();
$this->assertEqual($expected, $connections['connection8']);
}
/**
* Tests that a connection configuration can be deleted in runtime
*
* @return void
*/
public function testDrop() {
App::build(array(
'Model/Datasource' => array(
LIBS . 'tests' . DS . 'test_app' . DS . 'Model' . DS . 'Datasource' . DS
)
));
ConnectionManager::create('droppable', array('datasource' => 'Test2Source'));
$connections = ConnectionManager::enumConnectionObjects();
$this->assertEqual(array('datasource' => 'Test2Source'), $connections['droppable']);
$this->assertTrue(ConnectionManager::drop('droppable'));
$connections = ConnectionManager::enumConnectionObjects();
$this->assertFalse(isset($connections['droppable']));
}
}