Skip to content

Commit

Permalink
Update tests to use new ConnectionManager.
Browse files Browse the repository at this point in the history
Update tests to use the new API's in ConnectionManager.
  • Loading branch information
markstory committed Aug 24, 2013
1 parent 7413b34 commit dfe4355
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 37 deletions.
7 changes: 5 additions & 2 deletions lib/Cake/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ class Connection {
*/
public function __construct($config) {
$this->_config = $config;
$this->driver($config['datasource'], $config);

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

if (!empty($config['log'])) {
$this->logQueries($config['log']);
Expand Down Expand Up @@ -152,7 +155,7 @@ public function driver($driver = null, $config = null) {
$driver = new $driver($config);
}
if (!$driver->enabled()) {
throw new MissingExtensionException(['driver' => get_class($this->_driver)]);
throw new MissingExtensionException(['driver' => get_class($driver)]);
}
return $this->_driver = $driver;
}
Expand Down
42 changes: 28 additions & 14 deletions lib/Cake/Test/TestCase/Database/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Model\ConnectionManager;
use Cake\Database\ConnectionManager;
use Cake\TestSuite\TestCase;

/**
Expand Down Expand Up @@ -60,13 +60,14 @@ public function testMissingDriver() {

/**
* Tests trying to use a disabled driver throws an exception
*
* @expectedException Cake\Database\Exception\MissingExtensionException
* @expectedExceptionMessage Database driver DriverMock cannot be used due to a missing PHP extension or unmet dependency
* @return void
**/
*/
public function testDisabledDriver() {
$mock = $this->getMock('\Cake\Database\Connection\Driver', ['enabled'], [], 'DriverMock');
$connection = new Connection(['datasource' => get_class($mock)]);
$connection = new Connection(['datasource' => $mock]);
}

/**
Expand All @@ -76,10 +77,10 @@ public function testDisabledDriver() {
* @return void
**/
public function testWrongCredentials() {
$this->skipIf(Configure::read('Datasource.test.dsn'));
$connection = new Connection(['database' => 'foobar'] + Configure::read('Datasource.test'));
$config = ConnectionManager::config('test');
$this->skipIf(isset($config['dsn']), 'Datasource has dsn, skipping.');
$connection = new Connection(['database' => '_probably_not_there_'] + ConnectionManager::config('test'));
$connection->connect();
$this->skipIf($connection->driver() instanceof \Cake\Database\Driver\Sqlite);
}

/**
Expand Down Expand Up @@ -546,10 +547,11 @@ public function testQuote() {
* @return void
*/
public function testQuoteIdentifier() {
$connection = new Connection(Configure::read('Datasource.test'));

$driver = $this->getObjectForTrait('\Cake\Database\Dialect\SqliteDialectTrait');
$connection->driver($driver);
$driver = $this->getMock('Cake\Database\Driver\Sqlite', ['enabled']);
$driver->expects($this->once())
->method('enabled')
->will($this->returnValue(true));
$connection = new Connection(['datasource' => $driver]);

$result = $connection->quoteIdentifier('name');
$expected = '"name"';
Expand Down Expand Up @@ -676,8 +678,14 @@ public function testLogBeginRollbackTransaction() {
$connection = $this->getMock(
'\Cake\Database\Connection',
['connect'],
[Configure::read('Datasource.test')]
[['log' => true]]
);
$driver = $this->getMock('Cake\Database\Driver');
$driver->expects($this->once())
->method('enabled')
->will($this->returnValue(true));
$connection->driver($driver);

$logger = $this->getMock('\Cake\Database\Log\QueryLogger');
$connection->logger($logger);
$logger->expects($this->at(0))->method('log')
Expand All @@ -690,7 +698,7 @@ public function testLogBeginRollbackTransaction() {
$this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
$this->attributeEqualTo('query', 'ROLLBACK')
));
$connection->logQueries(true);

$connection->begin();
$connection->begin(); //This one will not be logged
$connection->rollback();
Expand All @@ -702,13 +710,19 @@ public function testLogBeginRollbackTransaction() {
* @return void
*/
public function testLogCommitTransaction() {
$logger = $this->getMock('\Cake\Database\Log\QueryLogger');
$driver = $this->getMock('Cake\Database\Driver');
$driver->expects($this->once())
->method('enabled')
->will($this->returnValue(true));
$connection = $this->getMock(
'\Cake\Database\Connection',
['connect'],
[Configure::read('Datasource.test')]
[['datasource' => $driver]]
);

$logger = $this->getMock('\Cake\Database\Log\QueryLogger');
$connection->logger($logger);

$logger->expects($this->at(1))->method('log')
->with($this->logicalAnd(
$this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/Database/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use Cake\Core\Configure;
use Cake\Database\Query;
use Cake\Model\ConnectionManager;
use Cake\Database\ConnectionManager;
use Cake\TestSuite\TestCase;

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/Cake/Test/TestCase/Database/Schema/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace Cake\Test\TestCase\Database\Schema;

use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Database\ConnectionManager;
use Cake\Database\Schema\Collection;
use Cake\Database\Schema\Table;
use Cake\TestSuite\TestCase;
Expand All @@ -34,7 +34,7 @@ class CollectionTest extends TestCase {
*/
public function setUp() {
parent::setUp();
$this->connection = new Connection(Configure::read('Datasource.test'));
$this->connection = ConnectionManager::get('test');
}

/**
Expand Down
12 changes: 6 additions & 6 deletions lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace Cake\Test\TestCase\Database\Schema;

use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Database\ConnectionManager;
use Cake\Database\Schema\Collection as SchemaCollection;
use Cake\Database\Schema\MysqlSchema;
use Cake\Database\Schema\Table;
Expand All @@ -34,8 +34,8 @@ class MysqlSchemaTest extends TestCase {
* @return void
*/
protected function _needsConnection() {
$config = Configure::read('Datasource.test');
$this->skipIf(strpos($config['datasource'], 'Mysql') === false, 'Not using Mysql for test config');
$config = ConnectionManager::config('test');
$this->skipIf(strpos($config['className'], 'Mysql') === false, 'Not using Mysql for test config');
}

/**
Expand Down Expand Up @@ -171,7 +171,7 @@ protected function _createTables($connection) {
* @return void
*/
public function testListTables() {
$connection = new Connection(Configure::read('Datasource.test'));
$connection = ConnectionManager::get('test');
$this->_createTables($connection);

$schema = new SchemaCollection($connection);
Expand All @@ -189,7 +189,7 @@ public function testListTables() {
* @return void
*/
public function testDescribeTable() {
$connection = new Connection(Configure::read('Datasource.test'));
$connection = ConnectionManager::get('test');
$this->_createTables($connection);

$schema = new SchemaCollection($connection);
Expand Down Expand Up @@ -276,7 +276,7 @@ public function testDescribeTable() {
* @return void
*/
public function testDescribeTableIndexes() {
$connection = new Connection(Configure::read('Datasource.test'));
$connection = ConnectionManager::get('test');
$this->_createTables($connection);

$schema = new SchemaCollection($connection);
Expand Down
12 changes: 6 additions & 6 deletions lib/Cake/Test/TestCase/Database/Schema/PostgresSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace Cake\Test\TestCase\Database\Schema;

use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Database\ConnectionManager;
use Cake\Database\Schema\Collection as SchemaCollection;
use Cake\Database\Schema\PostgresSchema;
use Cake\Database\Schema\Table;
Expand All @@ -34,8 +34,8 @@ class PostgresSchemaTest extends TestCase {
* @return void
*/
protected function _needsConnection() {
$config = Configure::read('Datasource.test');
$this->skipIf(strpos($config['datasource'], 'Postgres') === false, 'Not using Postgres for test config');
$config = ConnectionManager::config('test');
$this->skipIf(strpos($config['className'], 'Postgres') === false, 'Not using Postgres for test config');
}

/**
Expand Down Expand Up @@ -202,7 +202,7 @@ public function testConvertColumnType($input, $expected) {
* @return void
*/
public function testListTables() {
$connection = new Connection(Configure::read('Datasource.test'));
$connection = ConnectionManager::get('test');
$this->_createTables($connection);

$schema = new SchemaCollection($connection);
Expand All @@ -219,7 +219,7 @@ public function testListTables() {
* @return void
*/
public function testDescribeTable() {
$connection = new Connection(Configure::read('Datasource.test'));
$connection = ConnectionManager::get('test');
$this->_createTables($connection);

$schema = new SchemaCollection($connection);
Expand Down Expand Up @@ -301,7 +301,7 @@ public function testDescribeTable() {
* @return void
*/
public function testDescribeTableIndexes() {
$connection = new Connection(Configure::read('Datasource.test'));
$connection = ConnectionManager::get('test');
$this->_createTables($connection);

$schema = new SchemaCollection($connection);
Expand Down
12 changes: 6 additions & 6 deletions lib/Cake/Test/TestCase/Database/Schema/SqliteSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace Cake\Test\TestCase\Database\Schema;

use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Database\ConnectionManager;
use Cake\Database\Schema\Collection as SchemaCollection;
use Cake\Database\Schema\SqliteSchema;
use Cake\Database\Schema\Table;
Expand All @@ -34,8 +34,8 @@ class SqliteSchemaTest extends TestCase {
* @return void
*/
protected function _needsConnection() {
$config = Configure::read('Datasource.test');
$this->skipIf(strpos($config['datasource'], 'Sqlite') === false, 'Not using Sqlite for test config');
$config = ConnectionManager::config('test');
$this->skipIf(strpos($config['className'], 'Sqlite') === false, 'Not using Sqlite for test config');
}

/**
Expand Down Expand Up @@ -163,7 +163,7 @@ protected function _createTables($connection) {
* @return void
*/
public function testListTables() {
$connection = new Connection(Configure::read('Datasource.test'));
$connection = ConnectionManager::get('test');
$this->_createTables($connection);

$schema = new SchemaCollection($connection);
Expand All @@ -182,7 +182,7 @@ public function testListTables() {
* @return void
*/
public function testDescribeTable() {
$connection = new Connection(Configure::read('Datasource.test'));
$connection = ConnectionManager::get('test');
$this->_createTables($connection);

$schema = new SchemaCollection($connection);
Expand Down Expand Up @@ -256,7 +256,7 @@ public function testDescribeTable() {
* @return void
*/
public function testDescribeTableIndexes() {
$connection = new Connection(Configure::read('Datasource.test'));
$connection = ConnectionManager::get('test');
$this->_createTables($connection);

$schema = new SchemaCollection($connection);
Expand Down

0 comments on commit dfe4355

Please sign in to comment.