cakephp2-php8/lib/Cake/Test/Case/Model/ConnectionManagerTest.php
Mark Story 91a1cf81f3 Merge branch '1.3' into merger
Conflicts:
	app/Config/acl.ini.php
	app/config/database.php.default
	app/webroot/css.php
	app/webroot/css/cake.generic.css
	cake/basics.php
	cake/bootstrap.php
	cake/config/paths.php
	cake/console/cake.php
	cake/console/error.php
	cake/console/libs/acl.php
	cake/console/libs/bake.php
	cake/console/libs/i18n.php
	cake/console/libs/shell.php
	cake/console/libs/tasks/extract.php
	cake/console/libs/tasks/plugin.php
	cake/console/libs/tasks/project.php
	cake/console/libs/testsuite.php
	cake/console/templates/default/classes/test.ctp
	cake/console/templates/default/views/home.ctp
	cake/console/templates/default/views/view.ctp
	cake/console/templates/skel/config/database.php.default
	cake/console/templates/skel/views/elements/email/text/default.ctp
	cake/console/templates/skel/webroot/css.php
	cake/dispatcher.php
	cake/libs/cache.php
	cake/libs/cake_session.php
	cake/libs/configure.php
	cake/libs/controller/component.php
	cake/libs/controller/components/auth.php
	cake/libs/controller/components/email.php
	cake/libs/controller/components/request_handler.php
	cake/libs/controller/components/security.php
	cake/libs/controller/controller.php
	cake/libs/controller/scaffold.php
	cake/libs/error.php
	cake/libs/magic_db.php
	cake/libs/model/behaviors/acl.php
	cake/libs/model/connection_manager.php
	cake/libs/model/datasources/dbo/dbo_mysqli.php
	cake/libs/model/model_behavior.php
	cake/libs/overloadable.php
	cake/libs/overloadable_php4.php
	cake/libs/overloadable_php5.php
	cake/libs/router.php
	cake/libs/view/errors/missing_action.ctp
	cake/libs/view/errors/missing_behavior_class.ctp
	cake/libs/view/errors/missing_behavior_file.ctp
	cake/libs/view/errors/missing_component_class.ctp
	cake/libs/view/errors/missing_component_file.ctp
	cake/libs/view/errors/missing_connection.ctp
	cake/libs/view/errors/missing_controller.ctp
	cake/libs/view/errors/missing_helper_class.ctp
	cake/libs/view/errors/missing_helper_file.ctp
	cake/libs/view/errors/missing_layout.ctp
	cake/libs/view/errors/missing_model.ctp
	cake/libs/view/errors/missing_scaffolddb.ctp
	cake/libs/view/errors/missing_table.ctp
	cake/libs/view/errors/missing_view.ctp
	cake/libs/view/errors/private_action.ctp
	cake/libs/view/errors/scaffold_error.ctp
	cake/libs/view/helpers/ajax.php
	cake/libs/view/helpers/javascript.php
	cake/libs/view/helpers/js.php
	cake/libs/view/helpers/session.php
	cake/libs/view/helpers/xml.php
	cake/libs/view/media.php
	cake/libs/view/pages/home.ctp
	cake/libs/view/scaffolds/edit.ctp
	cake/libs/view/scaffolds/index.ctp
	cake/libs/view/scaffolds/view.ctp
	cake/libs/view/view.php
	cake/libs/xml.php
	cake/tests/cases/console/cake.test.php
	cake/tests/cases/console/libs/acl.test.php
	cake/tests/cases/console/libs/api.test.php
	cake/tests/cases/console/libs/bake.test.php
	cake/tests/cases/console/libs/shell.test.php
	cake/tests/cases/console/libs/tasks/controller.test.php
	cake/tests/cases/console/libs/tasks/db_config.test.php
	cake/tests/cases/console/libs/tasks/fixture.test.php
	cake/tests/cases/console/libs/tasks/model.test.php
	cake/tests/cases/console/libs/tasks/plugin.test.php
	cake/tests/cases/console/libs/tasks/project.test.php
	cake/tests/cases/console/libs/tasks/test.test.php
	cake/tests/cases/console/libs/tasks/view.test.php
	cake/tests/cases/dispatcher.test.php
	cake/tests/cases/libs/cache/apc.test.php
	cake/tests/cases/libs/cake_session.test.php
	cake/tests/cases/libs/cake_test_case.test.php
	cake/tests/cases/libs/code_coverage_manager.test.php
	cake/tests/cases/libs/configure.test.php
	cake/tests/cases/libs/controller/component.test.php
	cake/tests/cases/libs/controller/components/auth.test.php
	cake/tests/cases/libs/controller/components/cookie.test.php
	cake/tests/cases/libs/controller/components/request_handler.test.php
	cake/tests/cases/libs/controller/components/session.test.php
	cake/tests/cases/libs/controller/controller.test.php
	cake/tests/cases/libs/controller/pages_controller.test.php
	cake/tests/cases/libs/error.test.php
	cake/tests/cases/libs/http_socket.test.php
	cake/tests/cases/libs/magic_db.test.php
	cake/tests/cases/libs/model/datasources/dbo/dbo_mssql.test.php
	cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php
	cake/tests/cases/libs/model/datasources/dbo/dbo_mysqli.test.php
	cake/tests/cases/libs/model/datasources/dbo_source.test.php
	cake/tests/cases/libs/model/models.php
	cake/tests/cases/libs/overloadable.test.php
	cake/tests/cases/libs/test_manager.test.php
	cake/tests/cases/libs/view/helpers/ajax.test.php
	cake/tests/cases/libs/view/helpers/javascript.test.php
	cake/tests/cases/libs/view/helpers/session.test.php
	cake/tests/cases/libs/view/helpers/xml.test.php
	cake/tests/cases/libs/view/media.test.php
	cake/tests/cases/libs/view/theme.test.php
	cake/tests/cases/libs/xml.test.php
	cake/tests/fixtures/aco_fixture.php
	cake/tests/fixtures/translate_fixture.php
	cake/tests/groups/acl.group.php
	cake/tests/groups/bake.group.php
	cake/tests/groups/behaviors.group.php
	cake/tests/groups/cache.group.php
	cake/tests/groups/components.group.php
	cake/tests/groups/configure.group.php
	cake/tests/groups/console.group.php
	cake/tests/groups/controller.group.php
	cake/tests/groups/database.group.php
	cake/tests/groups/helpers.group.php
	cake/tests/groups/i18n.group.php
	cake/tests/groups/javascript.group.php
	cake/tests/groups/lib.group.php
	cake/tests/groups/model.group.php
	cake/tests/groups/no_cross_contamination.group.php
	cake/tests/groups/routing_system.group.php
	cake/tests/groups/socket.group.php
	cake/tests/groups/test_suite.group.php
	cake/tests/groups/view.group.php
	cake/tests/groups/xml.group.php
	cake/tests/lib/cake_test_case.php
	cake/tests/lib/cake_test_model.php
	cake/tests/lib/cake_test_suite_dispatcher.php
	cake/tests/lib/cake_web_test_case.php
	cake/tests/lib/code_coverage_manager.php
	cake/tests/lib/reporter/cake_base_reporter.php
	cake/tests/lib/reporter/cake_cli_reporter.php
	cake/tests/lib/reporter/cake_text_reporter.php
	cake/tests/lib/templates/menu.php
	cake/tests/lib/templates/simpletest.php
	cake/tests/lib/test_manager.php
	cake/tests/test_app/controllers/tests_apps_controller.php
	cake/tests/test_app/libs/cache/test_app_cache.php
	cake/tests/test_app/libs/library.php
	cake/tests/test_app/libs/log/test_app_log.php
	cake/tests/test_app/plugins/test_plugin/config/load.php
	cake/tests/test_app/plugins/test_plugin/config/more.load.php
	cake/tests/test_app/plugins/test_plugin/controllers/components/other_component.php
	cake/tests/test_app/plugins/test_plugin/controllers/components/plugins_component.php
	cake/tests/test_app/plugins/test_plugin/controllers/components/test_plugin_component.php
	cake/tests/test_app/plugins/test_plugin/controllers/components/test_plugin_other_component.php
	cake/tests/test_app/plugins/test_plugin/controllers/test_plugin_controller.php
	cake/tests/test_app/plugins/test_plugin/controllers/tests_controller.php
	cake/tests/test_app/plugins/test_plugin/libs/cache/test_plugin_cache.php
	cake/tests/test_app/plugins/test_plugin/libs/log/test_plugin_log.php
	cake/tests/test_app/plugins/test_plugin/libs/test_plugin_library.php
	cake/tests/test_app/plugins/test_plugin/test_plugin_app_controller.php
	cake/tests/test_app/plugins/test_plugin/test_plugin_app_model.php
	cake/tests/test_app/plugins/test_plugin/vendors/sample/sample_plugin.php
	cake/tests/test_app/plugins/test_plugin/vendors/welcome.php
	cake/tests/test_app/plugins/test_plugin/views/helpers/other_helper.php
	cake/tests/test_app/plugins/test_plugin/views/helpers/plugged_helper.php
	cake/tests/test_app/vendors/Test/MyTest.php
	cake/tests/test_app/vendors/Test/hello.php
	cake/tests/test_app/vendors/sample/configure_test_vendor_sample.php
	cake/tests/test_app/vendors/shells/sample.php
	cake/tests/test_app/vendors/somename/some.name.php
	cake/tests/test_app/vendors/welcome.php
	cake/tests/test_app/views/elements/email/text/default.ctp
	cake/tests/test_app/views/layouts/default.ctp
	cake/tests/test_app/views/posts/test_nocache_tags.ctp
	lib/Cake/Cache/Engine/MemcacheEngine.php
	lib/Cake/Config/config.php
	lib/Cake/Console/Command/Task/ModelTask.php
	lib/Cake/Console/Templates/skel/webroot/css/cake.generic.css
	lib/Cake/Console/Templates/skel/webroot/test.php
	lib/Cake/Console/cake.bat
	lib/Cake/Controller/Component/CookieComponent.php
	lib/Cake/Log/CakeLog.php
	lib/Cake/Model/CakeSchema.php
	lib/Cake/Test/Case/Log/Engine/FileLog.php
	lib/Cake/Test/Case/View/Helper/FormHelperTest.php
	lib/Cake/Test/test_app/View/Emails/html/custom.ctp
	lib/Cake/Test/test_app/View/Emails/text/custom.ctp
	lib/Cake/TestSuite/templates/header.php
	lib/Cake/Utility/Sanitize.php
	lib/Cake/Utility/Validation.php
	lib/Cake/VERSION.txt
	lib/Cake/View/Helper/FormHelper.php
2011-06-23 12:48:06 -07:00

357 lines
11 KiB
PHP

<?php
/**
* Connection Manager tests
*
*
* PHP 5
*
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @package cake.tests.cases.libs
* @since CakePHP(tm) v 1.2.0.5550
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('ConnectionManager', 'Model');
/**
* ConnectionManagerTest
*
* @package cake.tests.cases.models
*/
class ConnectionManagerTest extends CakeTestCase {
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
CakePlugin::unload();
}
/**
* testEnumConnectionObjects method
*
* @access public
* @return void
*/
public function testEnumConnectionObjects() {
$sources = ConnectionManager::enumConnectionObjects();
$this->assertTrue(count($sources) >= 1);
$connections = array('default', 'test', 'test');
$this->assertTrue(count(array_intersect(array_keys($sources), $connections)) >= 1);
}
/**
* testGetDataSource method
*
* @access public
* @return void
*/
public function testGetDataSource() {
App::build(array(
'Model/Datasource' => array(
CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS . 'Datasource' . DS
)
));
$name = 'test_get_datasource';
$config = array('datasource' => 'Test2Source');
$connection = ConnectionManager::create($name, $config);
$connections = ConnectionManager::enumConnectionObjects();
$this->assertTrue((bool)(count(array_keys($connections) >= 1)));
$source = ConnectionManager::getDataSource('test_get_datasource');
$this->assertTrue(is_object($source));
ConnectionManager::drop('test_get_datasource');
}
/**
* testGetDataSourceException() method
*
* @return void
* @expectedException MissingDatasourceConfigException
*/
public function testGetDataSourceException() {
ConnectionManager::getDataSource('non_existent_source');
}
/**
* testGetPluginDataSource method
*
* @access public
* @return void
*/
public function testGetPluginDataSource() {
App::build(array(
'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
));
CakePlugin::load('TestPlugin');
$name = 'test_source';
$config = array('datasource' => 'TestPlugin.TestSource');
$connection = ConnectionManager::create($name, $config);
$this->assertTrue(class_exists('TestSource'));
$this->assertEqual($connection->configKeyName, $name);
$this->assertEqual($connection->config, $config);
ConnectionManager::drop($name);
}
/**
* testGetPluginDataSourceAndPluginDriver method
*
* @access public
* @return void
*/
public function testGetPluginDataSourceAndPluginDriver() {
App::build(array(
'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
));
CakePlugin::load('TestPlugin');
$name = 'test_plugin_source_and_driver';
$config = array('datasource' => 'TestPlugin.Database/TestDriver');
$connection = ConnectionManager::create($name, $config);
$this->assertTrue(class_exists('TestSource'));
$this->assertTrue(class_exists('TestDriver'));
$this->assertEqual($connection->configKeyName, $name);
$this->assertEqual($connection->config, $config);
ConnectionManager::drop($name);
}
/**
* testGetLocalDataSourceAndPluginDriver method
*
* @access public
* @return void
*/
public function testGetLocalDataSourceAndPluginDriver() {
App::build(array(
'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
));
CakePlugin::load('TestPlugin');
$name = 'test_local_source_and_plugin_driver';
$config = array('datasource' => 'TestPlugin.Database/DboDummy');
$connection = ConnectionManager::create($name, $config);
$this->assertTrue(class_exists('DboSource'));
$this->assertTrue(class_exists('DboDummy'));
$this->assertEqual($connection->configKeyName, $name);
ConnectionManager::drop($name);
}
/**
* testGetPluginDataSourceAndLocalDriver method
*
* @access public
* @return void
*/
public function testGetPluginDataSourceAndLocalDriver() {
App::build(array(
'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
'Model/Datasource/Database' => array(
CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS . 'Datasource' . DS . 'Database' . DS
)
));
$name = 'test_plugin_source_and_local_driver';
$config = array('datasource' => 'Database/TestLocalDriver');
$connection = ConnectionManager::create($name, $config);
$this->assertTrue(class_exists('TestSource'));
$this->assertTrue(class_exists('TestLocalDriver'));
$this->assertEqual($connection->configKeyName, $name);
$this->assertEqual($connection->config, $config);
ConnectionManager::drop($name);
}
/**
* testSourceList method
*
* @access public
* @return void
*/
public function testSourceList() {
ConnectionManager::getDataSource('test');
$sources = ConnectionManager::sourceList();
$this->assertTrue(count($sources) >= 1);
$this->assertTrue(in_array('test', array_keys($sources)));
}
/**
* testGetSourceName method
*
* @access public
* @return void
*/
public function testGetSourceName() {
$connections = ConnectionManager::enumConnectionObjects();
$source = ConnectionManager::getDataSource('test');
$result = ConnectionManager::getSourceName($source);
$this->assertEqual('test', $result);
$source = new StdClass();
$result = ConnectionManager::getSourceName($source);
$this->assertNull($result);
}
/**
* testLoadDataSource method
*
* @access public
* @return void
*/
public function testLoadDataSource() {
$connections = array(
array('classname' => 'Mysql', 'filename' => 'Mysql', 'package' => 'Database'),
array('classname' => 'Postgres', 'filename' => 'Postgres', 'package' => 'Database'),
array('classname' => 'Sqlite', 'filename' => 'Sqlite', 'package' => 'Database'),
);
foreach ($connections as $connection) {
$exists = class_exists($connection['classname']);
$loaded = ConnectionManager::loadDataSource($connection);
$this->assertEqual($loaded, !$exists, "Failed loading the {$connection['classname']} datasource");
}
}
/**
* testLoadDataSourceException() method
*
* @return void
* @expectedException MissingDatasourceFileException
*/
public function testLoadDataSourceException() {
$connection = array('classname' => 'NonExistentDataSource', 'filename' => 'non_existent');
$loaded = ConnectionManager::loadDataSource($connection);
}
/**
* testCreateDataSource method
*
* @access public
* @return void
*/
public function testCreateDataSourceWithIntegrationTests() {
$name = 'test_created_connection';
$connections = ConnectionManager::enumConnectionObjects();
$this->assertTrue((bool)(count(array_keys($connections) >= 1)));
$source = ConnectionManager::getDataSource('test');
$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);
}
/**
* testConnectionData method
*
* @access public
* @return void
*/
public function testConnectionData() {
App::build(array(
'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
'Model/Datasource' => array(
CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS . 'Datasource' . DS
)
));
CakePlugin::loadAll();
$expected = array(
'datasource' => 'Test2Source'
);
ConnectionManager::create('connection1', array('datasource' => 'Test2Source'));
$connections = ConnectionManager::enumConnectionObjects();
$this->assertEqual($expected, $connections['connection1']);
ConnectionManager::drop('connection1');
ConnectionManager::create('connection2', array('datasource' => 'Test2Source'));
$connections = ConnectionManager::enumConnectionObjects();
$this->assertEqual($expected, $connections['connection2']);
ConnectionManager::drop('connection2');
ConnectionManager::create('connection3', array('datasource' => 'TestPlugin.TestSource'));
$connections = ConnectionManager::enumConnectionObjects();
$expected['datasource'] = 'TestPlugin.TestSource';
$this->assertEqual($expected, $connections['connection3']);
ConnectionManager::drop('connection3');
ConnectionManager::create('connection4', array('datasource' => 'TestPlugin.TestSource'));
$connections = ConnectionManager::enumConnectionObjects();
$this->assertEqual($expected, $connections['connection4']);
ConnectionManager::drop('connection4');
ConnectionManager::create('connection5', array('datasource' => 'Test2OtherSource'));
$connections = ConnectionManager::enumConnectionObjects();
$expected['datasource'] = 'Test2OtherSource';
$this->assertEqual($expected, $connections['connection5']);
ConnectionManager::drop('connection5');
ConnectionManager::create('connection6', array('datasource' => 'Test2OtherSource'));
$connections = ConnectionManager::enumConnectionObjects();
$this->assertEqual($expected, $connections['connection6']);
ConnectionManager::drop('connection6');
ConnectionManager::create('connection7', array('datasource' => 'TestPlugin.TestOtherSource'));
$connections = ConnectionManager::enumConnectionObjects();
$expected['datasource'] = 'TestPlugin.TestOtherSource';
$this->assertEqual($expected, $connections['connection7']);
ConnectionManager::drop('connection7');
ConnectionManager::create('connection8', array('datasource' => 'TestPlugin.TestOtherSource'));
$connections = ConnectionManager::enumConnectionObjects();
$this->assertEqual($expected, $connections['connection8']);
ConnectionManager::drop('connection8');
}
/**
* Tests that a connection configuration can be deleted in runtime
*
* @return void
*/
public function testDrop() {
App::build(array(
'Model/Datasource' => array(
CAKE . 'Test' . 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']));
}
}