Skip to content

Commit

Permalink
Starting to move ConnectionManager and ConnectionRegistry to the Data…
Browse files Browse the repository at this point in the history
…source namespace
  • Loading branch information
lorenzo committed Feb 17, 2014
1 parent 9e3ffba commit e33938f
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/Database/Connection.php
Expand Up @@ -90,8 +90,8 @@ class Connection {
public function __construct($config) {
$this->_config = $config;

if (!empty($config['datasource'])) {
$this->driver($config['datasource'], $config);
if (!empty($config['driver'])) {
$this->driver($config['driver'], $config);
}

if (!empty($config['log'])) {
Expand Down
@@ -1,7 +1,5 @@
<?php
/**
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand All @@ -14,14 +12,11 @@
* @since CakePHP(tm) v 0.10.x.1402
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Database;
namespace Cake\Datasource;

use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\StaticConfigTrait;
use Cake\Database\Connection;
use Cake\Database\ConnectionRegistry;
use Cake\Error;
use Cake\Datasource\ConnectionRegistry;
use Cake\Datasource\Error\MissingDatasourceConfigException;

/**
* Manages and loads instances of Connection
Expand All @@ -30,7 +25,7 @@
* a registry for the connections defined in an application.
*
* Provides an interface for loading and enumerating connections defined in
* app/Config/datasources.php
* App/Config/app.php
*/
class ConnectionManager {

Expand Down Expand Up @@ -98,7 +93,7 @@ public static function config($key, $config = null) {
*/
public static function alias($from, $to) {
if (empty(static::$_config[$to]) && empty(static::$_config[$from])) {
throw new Error\MissingDatasourceConfigException(
throw new MissingDatasourceConfigException(
sprintf('Cannot create alias of "%s" as it does not exist.', $from)
);
}
Expand Down Expand Up @@ -151,7 +146,7 @@ public static function get($name, $useAliases = true) {
*
* @param string $name The name of the DataSource, as defined in app/Config/datasources.php
* @return DataSource Instance
* @throws Cake\Error\MissingDatasourceException
* @throws Cake\Error\MissingDatasourceConfigException
* @deprecated Will be removed in 3.0 stable.
*/
public static function getDataSource($name) {
Expand Down
Expand Up @@ -14,12 +14,10 @@
* @since CakePHP(tm) v3.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Database;
namespace Cake\Datasource;

use Cake\Core\App;
use Cake\Database\Connection;
use Cake\Database\Exception\MissingDriverException;
use Cake\Error;
use Cake\Datasource\Error\MissingDatasourceException;
use Cake\Utility\ObjectRegistry;

/**
Expand All @@ -41,7 +39,7 @@ protected function _resolveClassName($class) {
if (is_object($class)) {
return $class;
}
return App::classname($class, 'Database/Driver');
return App::classname($class, 'Datasource');
}

/**
Expand All @@ -51,36 +49,32 @@ protected function _resolveClassName($class) {
*
* @param string $class The classname that is missing.
* @param string $plugin The plugin the driver is missing in.
* @throws Cake\Database\Exception\MissingDriverException
* @throws Cake\Datasource\Error\MissingDatasourceException
*/
protected function _throwMissingClassError($class, $plugin) {
throw new MissingDriverException([
throw new MissingDatasourceException([
'class' => $class,
'plugin' => $plugin,
]);
}

/**
* Create the connection object with the correct driver.
* Create the connection object with the correct settings.
*
* Part of the template method for Cake\Utility\ObjectRegistry::load()
*
* @param string|Driver $class The classname or object to make.
* @param string|object $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 Connection A connection with the correct driver.
* @return object A connection with the correct settings.
*/
protected function _create($class, $alias, $settings) {
if (is_object($class)) {
$instance = $class;
}

unset($settings['className']);
if (!isset($instance)) {
$instance = new $class($settings);
}
$settings['datasource'] = $instance;
return new Connection($settings);
return new $class($settings);
}

/**
Expand Down
Expand Up @@ -13,7 +13,9 @@
* @since CakePHP(tm) v 3.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Error;
namespace Cake\Datasource\Error;

use Cake\Error\Exception;

/**
* Exception class to be thrown when a datasource configuration is not found
Expand Down
Expand Up @@ -15,7 +15,9 @@
* @since CakePHP(tm) v 3.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Error;
namespace Cake\Datasource\Error;

use Cake\Error\Exception;

/**
* Used when a datasource cannot be found.
Expand Down
3 changes: 1 addition & 2 deletions src/TestSuite/Fixture/FixtureManager.php
Expand Up @@ -19,8 +19,7 @@
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Database\Connection;
use Cake\Database\ConnectionManager;
use Cake\DataSource\ConnectionManager;
use Cake\Error;
use Cake\TestSuite\Fixture\TestFixture;
use Cake\TestSuite\TestCase;
Expand Down
14 changes: 7 additions & 7 deletions tests/TestCase/Database/ConnectionTest.php
Expand Up @@ -70,7 +70,7 @@ public function testConnect() {
* @return void
*/
public function testMissingDriver() {
$connection = new Connection(['datasource' => '\Foo\InvalidDriver']);
$connection = new Connection(['driver' => '\Foo\InvalidDriver']);
}

/**
Expand All @@ -82,7 +82,7 @@ public function testMissingDriver() {
*/
public function testDisabledDriver() {
$mock = $this->getMock('\Cake\Database\Connection\Driver', ['enabled'], [], 'DriverMock');
$connection = new Connection(['datasource' => $mock]);
$connection = new Connection(['driver' => $mock]);
}

/**
Expand Down Expand Up @@ -572,7 +572,7 @@ public function testQuoteIdentifier() {
$driver->expects($this->once())
->method('enabled')
->will($this->returnValue(true));
$connection = new Connection(['datasource' => $driver]);
$connection = new Connection(['driver' => $driver]);

$result = $connection->quoteIdentifier('name');
$expected = '"name"';
Expand Down Expand Up @@ -732,7 +732,7 @@ public function testLogCommitTransaction() {
$connection = $this->getMock(
'\Cake\Database\Connection',
['connect'],
[['datasource' => $driver]]
[['driver' => $driver]]
);

$logger = $this->getMock('\Cake\Database\Log\QueryLogger');
Expand All @@ -759,7 +759,7 @@ public function testTransactionalSuccess() {
$connection = $this->getMock(
'\Cake\Database\Connection',
['connect', 'commit', 'begin'],
[['datasource' => $driver]]
[['driver' => $driver]]
);
$connection->expects($this->at(0))->method('begin');
$connection->expects($this->at(1))->method('commit');
Expand All @@ -781,7 +781,7 @@ public function testTransactionalFail() {
$connection = $this->getMock(
'\Cake\Database\Connection',
['connect', 'commit', 'begin', 'rollback'],
[['datasource' => $driver]]
[['driver' => $driver]]
);
$connection->expects($this->at(0))->method('begin');
$connection->expects($this->at(1))->method('rollback');
Expand All @@ -806,7 +806,7 @@ public function testTransactionalWithException() {
$connection = $this->getMock(
'\Cake\Database\Connection',
['connect', 'commit', 'begin', 'rollback'],
[['datasource' => $driver]]
[['driver' => $driver]]
);
$connection->expects($this->at(0))->method('begin');
$connection->expects($this->at(1))->method('rollback');
Expand Down
Expand Up @@ -11,14 +11,17 @@
* @since CakePHP(tm) v 1.2.0.5550
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Database;
namespace Cake\Test\TestCase\Datasource;

use Cake\Core\App;
use Cake\Core\Plugin;
use Cake\Database\ConnectionManager;
use Cake\Datasource\ConnectionManager;
use Cake\Database\Driver\Sqlite;
use Cake\TestSuite\TestCase;

class FakeConnection {
}

/**
* ConnectionManager Test
*/
Expand All @@ -44,10 +47,11 @@ public function tearDown() {
public static function configProvider() {
return [
'Array of data using classname key.' => [[
'className' => 'Sqlite',
'className' => __NAMESPACE__ . '\FakeConnection',
'instance' => 'Sqlite',
'database' => ':memory:',
]],
'Direct instance' => [new Sqlite(['database' => ':memory:'])],
'Direct instance' => [new FakeConnection],
];
}

Expand All @@ -62,7 +66,7 @@ public function testConfigVariants($settings) {
ConnectionManager::config('test_variant', $settings);

$ds = ConnectionManager::get('test_variant');
$this->assertInstanceOf('Cake\Database\Connection', $ds);
$this->assertInstanceOf(__NAMESPACE__ . '\FakeConnection', $ds);
$this->assertContains('test_variant', ConnectionManager::configured());
}

Expand Down
5 changes: 3 additions & 2 deletions tests/init.php
Expand Up @@ -13,7 +13,7 @@

use Cake\Cache\Cache;
use Cake\Core\Configure;
use Cake\Database\ConnectionManager;
use Cake\Datasource\ConnectionManager;
use Cake\I18n\I18n;
use Cake\Log\Log;

Expand Down Expand Up @@ -98,7 +98,8 @@
]);

ConnectionManager::config('test', [
'className' => getenv('db_class'),
'className' => 'Cake\Database\Connection',
'driver' => getenv('db_class'),
'dsn' => getenv('db_dsn'),
'database' => getenv('db_database'),
'login' => getenv('db_login'),
Expand Down

0 comments on commit e33938f

Please sign in to comment.