Some changes to ConnectionManager, it is now an static class. the connection objects enumeration will return the stored connections config instead of a cryptic array where only the ekys where useful

This commit is contained in:
José Lorenzo Rodríguez 2011-01-05 22:15:17 -04:30
parent 4c0e06c451
commit 54ecd2e77b
2 changed files with 19 additions and 58 deletions

View file

@ -128,18 +128,16 @@ class ConnectionManager {
/** /**
* Gets a DataSource name from an object reference. * Gets a DataSource name from an object reference.
* *
* **Warning** this method may cause fatal errors in PHP4.
*
* @param object $source DataSource object * @param object $source DataSource object
* @return string Datasource name, or null if source is not present * @return string Datasource name, or null if source is not present
* in the ConnectionManager. * in the ConnectionManager.
*/ */
public static function getSourceName(&$source) { public static function getSourceName($source) {
if (empty(self::$_init)) { if (empty(self::$_init)) {
self::init(); self::init();
} }
foreach (self::$_dataSources as $name => $ds) { foreach (self::$_dataSources as $name => $ds) {
if ($ds == $source) { if ($ds === $source) {
return $name; return $name;
} }
} }
@ -171,7 +169,7 @@ class ConnectionManager {
$plugin = $package = null; $plugin = $package = null;
if (!empty($conn['plugin'])) { if (!empty($conn['plugin'])) {
$plugin .= '.'; $plugin = $conn['plugin'] . '.';
} }
if (!empty($conn['package'])) { if (!empty($conn['package'])) {
$package = '/' . $conn['package']; $package = '/' . $conn['package'];
@ -195,7 +193,7 @@ class ConnectionManager {
if (empty(self::$_init)) { if (empty(self::$_init)) {
self::init(); self::init();
} }
return self::$_connectionsEnum; return (array) self::$config;
} }
/** /**

View file

@ -25,36 +25,6 @@ App::uses('ConnectionManager', 'Model');
*/ */
class ConnectionManagerTest extends CakeTestCase { class ConnectionManagerTest extends CakeTestCase {
/**
* setUp method
*
* @access public
* @return void
*/
function setUp() {
$this->ConnectionManager = ConnectionManager::getInstance();
}
/**
* tearDown method
*
* @access public
* @return void
*/
function tearDown() {
unset($this->ConnectionManager);
}
/**
* testInstantiation method
*
* @access public
* @return void
*/
function testInstantiation() {
$this->assertTrue(is_a($this->ConnectionManager, 'ConnectionManager'));
}
/** /**
* testEnumConnectionObjects method * testEnumConnectionObjects method
* *
@ -123,7 +93,7 @@ class ConnectionManagerTest extends CakeTestCase {
)); ));
$name = 'test_plugin_source_and_driver'; $name = 'test_plugin_source_and_driver';
$config = array('datasource' => 'TestPlugin.TestSource', 'driver' => 'TestPlugin.TestDriver'); $config = array('datasource' => 'TestPlugin.Database/TestDriver');
$connection = ConnectionManager::create($name, $config); $connection = ConnectionManager::create($name, $config);
@ -147,7 +117,7 @@ class ConnectionManagerTest extends CakeTestCase {
)); ));
$name = 'test_local_source_and_plugin_driver'; $name = 'test_local_source_and_plugin_driver';
$config = array('datasource' => 'dbo', 'driver' => 'TestPlugin.DboDummy'); $config = array('datasource' => 'TestPlugin.Database/DboDummy');
$connection = ConnectionManager::create($name, $config); $connection = ConnectionManager::create($name, $config);
@ -167,11 +137,11 @@ class ConnectionManagerTest extends CakeTestCase {
function testGetPluginDataSourceAndLocalDriver() { function testGetPluginDataSourceAndLocalDriver() {
App::build(array( App::build(array(
'plugins' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS), 'plugins' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
'datasources' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'models' . DS . 'datasources' . DS) 'Model/Datasource' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'models' . DS . 'datasources' . DS)
)); ));
$name = 'test_plugin_source_and_local_driver'; $name = 'test_plugin_source_and_local_driver';
$config = array('datasource' => 'TestPlugin.TestSource', 'driver' => 'local_driver'); $config = array('datasource' => 'Database/TestLocalDriver');
$connection = ConnectionManager::create($name, $config); $connection = ConnectionManager::create($name, $config);
@ -283,17 +253,14 @@ class ConnectionManagerTest extends CakeTestCase {
function testConnectionData() { function testConnectionData() {
App::build(array( App::build(array(
'plugins' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS), 'plugins' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
'datasources' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'models' . DS . 'datasources' . DS) 'Model/Datasource' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'models' . DS . 'datasources' . DS)
)); ));
$expected = array( $expected = array(
'filename' => 'test2_source', 'datasource' => 'Test2Source'
'classname' => 'Test2Source',
'parent' => '',
'plugin' => ''
); );
ConnectionManager::create('connection1', array('datasource' => 'Test2')); ConnectionManager::create('connection1', array('datasource' => 'Test2Source'));
$connections = ConnectionManager::enumConnectionObjects(); $connections = ConnectionManager::enumConnectionObjects();
$this->assertEqual($expected, $connections['connection1']); $this->assertEqual($expected, $connections['connection1']);
@ -301,33 +268,29 @@ class ConnectionManagerTest extends CakeTestCase {
$connections = ConnectionManager::enumConnectionObjects(); $connections = ConnectionManager::enumConnectionObjects();
$this->assertEqual($expected, $connections['connection2']); $this->assertEqual($expected, $connections['connection2']);
ConnectionManager::create('connection3', array('datasource' => 'TestPlugin.Test')); ConnectionManager::create('connection3', array('datasource' => 'TestPlugin.TestSource'));
$connections = ConnectionManager::enumConnectionObjects(); $connections = ConnectionManager::enumConnectionObjects();
$expected['filename'] = 'test_source'; $expected['datasource'] = 'TestPlugin.TestSource';
$expected['classname'] = 'TestSource';
$expected['plugin'] = 'TestPlugin';
$this->assertEqual($expected, $connections['connection3']); $this->assertEqual($expected, $connections['connection3']);
ConnectionManager::create('connection4', array('datasource' => 'TestPlugin.TestSource')); ConnectionManager::create('connection4', array('datasource' => 'TestPlugin.TestSource'));
$connections = ConnectionManager::enumConnectionObjects(); $connections = ConnectionManager::enumConnectionObjects();
$this->assertEqual($expected, $connections['connection4']); $this->assertEqual($expected, $connections['connection4']);
ConnectionManager::create('connection5', array('datasource' => 'Test2Other')); ConnectionManager::create('connection5', array('datasource' => 'Test2OtherSource'));
$connections = ConnectionManager::enumConnectionObjects(); $connections = ConnectionManager::enumConnectionObjects();
$expected['filename'] = 'test2_other_source'; $expected['datasource'] = 'Test2OtherSource';
$expected['classname'] = 'Test2OtherSource';
$expected['plugin'] = '';
$this->assertEqual($expected, $connections['connection5']); $this->assertEqual($expected, $connections['connection5']);
ConnectionManager::create('connection6', array('datasource' => 'Test2OtherSource')); ConnectionManager::create('connection6', array('datasource' => 'Test2OtherSource'));
$connections = ConnectionManager::enumConnectionObjects(); $connections = ConnectionManager::enumConnectionObjects();
$this->assertEqual($expected, $connections['connection6']); $this->assertEqual($expected, $connections['connection6']);
ConnectionManager::create('connection7', array('datasource' => 'TestPlugin.TestOther')); ConnectionManager::create('connection7', array('datasource' => 'TestPlugin.TestOtherSource'));
$connections = ConnectionManager::enumConnectionObjects(); $connections = ConnectionManager::enumConnectionObjects();
$expected['filename'] = 'test_other_source'; $expected['datasource'] = 'TestPlugin.TestOtherSource';
$expected['classname'] = 'TestOtherSource';
$expected['plugin'] = 'TestPlugin';
$this->assertEqual($expected, $connections['connection7']); $this->assertEqual($expected, $connections['connection7']);
ConnectionManager::create('connection8', array('datasource' => 'TestPlugin.TestOtherSource')); ConnectionManager::create('connection8', array('datasource' => 'TestPlugin.TestOtherSource'));