Skip to content

Commit

Permalink
Adding ability for SQlite to execute init commands con connect
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Mar 16, 2013
1 parent 2a89ec0 commit e6d4f12
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 3 deletions.
11 changes: 10 additions & 1 deletion lib/Cake/Model/Datasource/Database/Driver/Sqlite.php
Expand Up @@ -38,6 +38,7 @@ class Sqlite extends \Cake\Model\Datasource\Database\Driver {
'database' => ':memory:',
'encoding' => 'utf8',
'flags' => [],
'init' => [],

This comment has been minimized.

Copy link
@markstory

markstory Mar 17, 2013

Member

Ah this is very similar to the settings feature in 2.x that I referred to earlier.

'dsn' => null
];

Expand All @@ -58,7 +59,15 @@ public function connect(array $config) {
$config['dsn'] = "sqlite:{$config['database']}";
}

return $this->_connect($config);
$this->_connect($config);

if (!empty($config['init'])) {
foreach ((array)$config['init'] as $command) {
$this->connection()->exec($command);
}
}

return true;
}

/**
Expand Down
Expand Up @@ -17,14 +17,13 @@
*/
namespace Cake\Test\TestCase\Model\Datasource\Database\Driver;

use Cake\Model\Datasource\Database\Driver;
use \PDO;

/**
* Tests Mysql driver
*
*/
class MysqlTests extends \Cake\TestSuite\TestCase {
class MysqlTest extends \Cake\TestSuite\TestCase {

/**
* Test connecting to Mysql with default configuration
Expand Down
@@ -0,0 +1,93 @@
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Model
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Datasource\Database\Driver;

use \PDO;

/**
* Tests Sqlite driver
*
*/
class SqliteTest extends \Cake\TestSuite\TestCase {

/**
* Test connecting to Sqlite with default configuration
*
* @return void
*/
public function testConnectionConfigDefault() {
$driver = $this->getMock('Cake\Model\Datasource\Database\driver\Sqlite', ['_connect']);
$expected = [
'persistent' => false,
'database' => ':memory:',
'encoding' => 'utf8',
'login' => null,
'password' => null,
'flags' => [],
'init' => [],
'dsn' => 'sqlite::memory:'
];

$expected['flags'] += [
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];
$driver->expects($this->once())->method('_connect')
->with($expected);
$driver->connect([]);
}

/**
* Test connecting to Sqlite with custom configuration
*
* @return void
*/
public function testConnectionConfigCustom() {
$driver = $this->getMock('Cake\Model\Datasource\Database\driver\Sqlite', ['_connect', 'connection']);
$config = [
'persistent' => true,
'host' => 'foo',
'database' => 'bar.db',
'flags' => [1 => true, 2 => false],
'encoding' => 'a-language',
'init' => ['Execute this', 'this too']
];

$expected = $config;
$expected += ['login' => null, 'password' => null];
$expected['dsn'] = 'sqlite:bar.db';
$expected['flags'] += [
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];

$connection = $this->getMock('StdClass', ['exec']);
$connection->expects($this->at(0))->method('exec')->with('Execute this');
$connection->expects($this->at(1))->method('exec')->with('this too');
$connection->expects($this->exactly(2))->method('exec');

$driver->expects($this->once())->method('_connect')
->with($expected);
$driver->expects($this->any())->method('connection')
->will($this->returnValue($connection));
$driver->connect($config);
}

}


0 comments on commit e6d4f12

Please sign in to comment.