Skip to content

Commit

Permalink
DbConfigTask now writes to datasources.php and removed remaining refe…
Browse files Browse the repository at this point in the history
…rences to DATABASE_CONFIG.
  • Loading branch information
renan committed Nov 6, 2012
1 parent 4c0ab59 commit 67b8f17
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 52 deletions.
47 changes: 18 additions & 29 deletions lib/Cake/Console/Command/Task/DbConfigTask.php
Expand Up @@ -17,6 +17,7 @@
*/
namespace Cake\Console\Command\Task;
use Cake\Console\Shell;
use Cake\Core\Configure;
use Cake\Model\ConnectionManager;

/**
Expand Down Expand Up @@ -52,14 +53,6 @@ class DbConfigTask extends Shell {
'port' => null
);

/**
* String name of the database config class name.
* Used for testing.
*
* @var string
*/
public $databaseClassName = 'DATABASE_CONFIG';

/**
* initialization callback
*
Expand Down Expand Up @@ -258,14 +251,10 @@ public function bake($configs) {
}

$filename = $this->path . 'datasources.php';
$oldConfigs = array();

if (file_exists($filename)) {
config('database');
$db = new $this->databaseClassName;
$temp = get_class_vars(get_class($db));
$oldConfigs = Configure::read('Datasource');

foreach ($temp as $configName => $info) {
foreach ($oldConfigs as $configName => $info) {
$info = array_merge($this->_defaultConfig, $info);

if (!isset($info['schema'])) {
Expand All @@ -280,7 +269,7 @@ public function bake($configs) {

$info['persistent'] = var_export((bool)$info['persistent'], true);

$oldConfigs[] = array(
$oldConfigs[$configName] = array(
'name' => $configName,
'datasource' => $info['datasource'],
'persistent' => $info['persistent'],
Expand All @@ -306,7 +295,8 @@ public function bake($configs) {

$configs = array_merge($oldConfigs, $configs);
$out = "<?php\n";
$out .= "class DATABASE_CONFIG {\n\n";
$out .= "namespace " . Configure::read('App.namespace') . "\Config;\n";
$out .= "use Cake\Core\Configure;\n\n";

foreach ($configs as $config) {
$config = array_merge($this->_defaultConfig, $config);
Expand All @@ -315,35 +305,34 @@ public function bake($configs) {
if (strpos($datasource, 'Database/') === false) {
$datasource = "Database/{$datasource}";
}
$out .= "\tpublic \${$name} = array(\n";
$out .= "\t\t'datasource' => '{$datasource}',\n";
$out .= "\t\t'persistent' => {$persistent},\n";
$out .= "\t\t'host' => '{$host}',\n";
$out .= "Configure::write('Datasource.{$name}', [\n";
$out .= "\t'datasource' => '{$datasource}',\n";
$out .= "\t'persistent' => {$persistent},\n";
$out .= "\t'host' => '{$host}',\n";

if ($port) {
$out .= "\t\t'port' => {$port},\n";
$out .= "\t'port' => {$port},\n";
}

$out .= "\t\t'login' => '{$login}',\n";
$out .= "\t\t'password' => '{$password}',\n";
$out .= "\t\t'database' => '{$database}',\n";
$out .= "\t'login' => '{$login}',\n";
$out .= "\t'password' => '{$password}',\n";
$out .= "\t'database' => '{$database}',\n";

if ($schema) {
$out .= "\t\t'schema' => '{$schema}',\n";
$out .= "\t'schema' => '{$schema}',\n";
}

if ($prefix) {
$out .= "\t\t'prefix' => '{$prefix}',\n";
$out .= "\t'prefix' => '{$prefix}',\n";
}

if ($encoding) {
$out .= "\t\t'encoding' => '{$encoding}'\n";
$out .= "\t'encoding' => '{$encoding}'\n";
}

$out .= "\t);\n";
$out .= "]);\n";
}

$out .= "}\n";
$filename = $this->path . 'datasources.php';
return $this->createFile($filename, $out);
}
Expand Down
37 changes: 20 additions & 17 deletions lib/Cake/Test/TestCase/Console/Command/Task/DbConfigTaskTest.php
Expand Up @@ -18,6 +18,7 @@
*/
namespace Cake\Test\TestCase\Console\Command\Task;
use Cake\Console\Command\Task\DbConfigTask;
use Cake\Core\Configure;
use Cake\TestSuite\TestCase;

/**
Expand Down Expand Up @@ -92,8 +93,21 @@ public function testExecuteIntoInteractive() {
$in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
$this->Task = $this->getMock(
'Cake\Console\Command\Task\DbConfigTask',
array('in', '_stop', 'createFile', 'bake'), array($out, $out, $in)
array('in', '_stop', 'createFile'), array($out, $out, $in)
);
$this->Task->path = APP . 'Config' . DS;

$expected = "<?php\n";
$expected .= "namespace App\Config;\n";
$expected .= "use Cake\Core\Configure;\n\n";
$expected .= "Configure::write('Datasource.default', [\n";
$expected .= "\t'datasource' => 'Database/mysql',\n";
$expected .= "\t'persistent' => false,\n";
$expected .= "\t'host' => 'localhost',\n";
$expected .= "\t'login' => 'root',\n";
$expected .= "\t'password' => 'password',\n";
$expected .= "\t'database' => 'cake_test',\n";
$expected .= "]);\n";

$this->Task->expects($this->once())->method('_stop');
$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('default')); //name
Expand All @@ -108,23 +122,12 @@ public function testExecuteIntoInteractive() {
$this->Task->expects($this->at(12))->method('in')->will($this->returnValue('n')); //encoding
$this->Task->expects($this->at(13))->method('in')->will($this->returnValue('y')); //looks good
$this->Task->expects($this->at(14))->method('in')->will($this->returnValue('n')); //another
$this->Task->expects($this->at(15))->method('bake')
->with(array(
array(
'name' => 'default',
'datasource' => 'mysql',
'persistent' => 'false',
'host' => 'localhost',
'login' => 'root',
'password' => 'password',
'database' => 'cake_test',
'prefix' => null,
'encoding' => null,
'port' => '',
'schema' => null
)
));
$this->Task->expects($this->at(15))->method('createFile')
->with(
$this->equalTo($this->Task->path . 'datasources.php'),
$this->equalTo($expected));

Configure::write('Datasource', array());
$result = $this->Task->execute();
}
}
Expand Up @@ -3587,14 +3587,12 @@ public function testBindMultipleTimesWithFind() {
* @return void
*/
public function testAutoFieldsWithMultipleDatabases() {
$config = new DATABASE_CONFIG();

$this->skipIf(
!isset($config->test) || !isset($config->test2),
!Configure::check('Datasource.test') || !Configure::check('Datasource.test2'),
'Primary and secondary test databases not configured, ' .
'skipping cross-database join tests. ' .
' To run these tests, you must define $test and $test2 ' .
'in your database configuration.'
'in your datasources configuration.'
);

$db = ConnectionManager::getDataSource('test2');
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/Model/ModelCrossSchemaHabtmTest.php
@@ -1,6 +1,6 @@
<?php
/**
* Tests cross database HABTM. Requires $test and $test2 to both be set in DATABASE_CONFIG
* Tests cross database HABTM. Requires $test and $test2 to both be set in datasources.php
* NOTE: When testing on MySQL, you must set 'persistent' => false on *both* database connections,
* or one connection will step on the other.
*
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/Model/ModelIntegrationTest.php
Expand Up @@ -274,7 +274,7 @@ public function testFindWithJoinsOption() {
}

/**
* Tests cross database joins. Requires $test and $test2 to both be set in DATABASE_CONFIG
* Tests cross database joins. Requires $test and $test2 to both be set in datasources.php
* NOTE: When testing on MySQL, you must set 'persistent' => false on *both* database connections,
* or one connection will step on the other.
*/
Expand Down

0 comments on commit 67b8f17

Please sign in to comment.