From ed74653d7206976a95049f01d8ac8193650fc7a2 Mon Sep 17 00:00:00 2001 From: jperras Date: Thu, 24 Sep 2009 16:23:24 -0400 Subject: [PATCH] Adding tests for ConnectionManager, and fixing a missing return value for ConnectionManager::loadDataSource(). --- cake/libs/model/connection_manager.php | 2 + .../libs/model/connection_manager.test.php | 180 ++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 cake/tests/cases/libs/model/connection_manager.test.php diff --git a/cake/libs/model/connection_manager.php b/cake/libs/model/connection_manager.php index e9dbd8990..db7d9cf59 100644 --- a/cake/libs/model/connection_manager.php +++ b/cake/libs/model/connection_manager.php @@ -189,6 +189,8 @@ class ConnectionManager extends Object { trigger_error(sprintf($error, $conn['filename']), E_USER_ERROR); return null; } + + return true; } /** diff --git a/cake/tests/cases/libs/model/connection_manager.test.php b/cake/tests/cases/libs/model/connection_manager.test.php new file mode 100644 index 000000000..4bc226854 --- /dev/null +++ b/cake/tests/cases/libs/model/connection_manager.test.php @@ -0,0 +1,180 @@ +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 + * + * @access public + * @return void + */ + function testEnumConnectionObjects() { + $sources = ConnectionManager::enumConnectionObjects(); + $this->assertTrue(count($sources) >= 1); + + $connections = array('default', 'test', 'test_suite'); + $this->assertTrue(count(array_intersect(array_keys($sources), $connections)) >= 1); + } + +/** + * testGetDataSource method + * + * @access public + * @return void + */ + function testGetDataSource() { + $connections = ConnectionManager::enumConnectionObjects(); + $this->assertTrue(count(array_keys($connections) >= 1)); + + $source = ConnectionManager::getDataSource(key($connections)); + $this->assertTrue(is_object($source)); + + } + +/** + * testSourceList method + * + * @access public + * @return void + */ + function testSourceList() { + $sources = ConnectionManager::sourceList(); + $this->assertTrue(count($sources) >= 1); + + $connections = array('default', 'test', 'test_suite'); + $this->assertTrue(count(array_intersect($sources, $connections)) >= 1); + } + +/** + * testGetSourceName method + * + * @access public + * @return void + */ + function testGetSourceName() { + $connections = ConnectionManager::enumConnectionObjects(); + $name = key($connections); + $source = ConnectionManager::getDataSource($name); + $result = ConnectionManager::getSourceName($source); + + $this->assertEqual($result, $name); + } + +/** + * testLoadDataSource method + * + * @access public + * @return void + */ + function testLoadDataSource() { + $connections = array( + array('classname' => 'DboMysql', 'filename' => 'dbo' . DS . 'dbo_mysql'), + array('classname' => 'DboMysqli', 'filename' => 'dbo' . DS . 'dbo_mysqli'), + array('classname' => 'DboMssql', 'filename' => 'dbo' . DS . 'dbo_mssql'), + array('classname' => 'DboOracle', 'filename' => 'dbo' . DS . 'dbo_oracle'), + ); + + foreach ($connections as $connection) { + $exists = class_exists($connection['classname']); + $loaded = ConnectionManager::loadDataSource($connection); + $this->assertEqual($loaded, !$exists, "%s Failed loading the {$connection['classname']} datasource"); + } + + $connection = array('classname' => 'NonExistentDataSource', 'filename' => 'non_existent'); + $this->expectError(new PatternExpectation('/Unable to load DataSource file/i')); + + $loaded = ConnectionManager::loadDataSource($connection); + $this->assertEqual($loaded, null); + } + +/** + * testCreateDataSource method + * + * @access public + * @return void + */ + function testCreateDataSourceWithIntegrationTests() { + $name = 'test_created_connection'; + + $connections = ConnectionManager::enumConnectionObjects(); + $this->assertTrue(count(array_keys($connections) >= 1)); + + $source = ConnectionManager::getDataSource(key($connections)); + $this->assertTrue(is_object($source)); + + $config = $source->config; + $connection = ConnectionManager::create($name, $config); + + $this->assertTrue(is_object($connection)); + $this->assertEqual($name, $connection->configKeyName); + $this->assertEqual($name, ConnectionManager::getSourceName($connection)); + + $source = ConnectionManager::create(null, array()); + $this->assertEqual($source, null); + + $source = ConnectionManager::create('another_test', array()); + $this->assertEqual($source, null); + + $config = array('classname' => 'DboMysql', 'filename' => 'dbo' . DS . 'dbo_mysql'); + $source = ConnectionManager::create(null, $config); + $this->assertEqual($source, null); + } +} +?> \ No newline at end of file