From 5843d5e40a50f331901855b917f994f7c5513f41 Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 20 May 2009 23:15:31 -0400 Subject: [PATCH] Adding test case for DbConfig task making DbConfigTask testable. --- cake/console/libs/tasks/db_config.php | 22 ++- .../console/libs/tasks/db_config.test.php | 155 ++++++++++++++++++ 2 files changed, 168 insertions(+), 9 deletions(-) create mode 100644 cake/tests/cases/console/libs/tasks/db_config.test.php diff --git a/cake/console/libs/tasks/db_config.php b/cake/console/libs/tasks/db_config.php index 856b1226ccc..283bba1626f 100644 --- a/cake/console/libs/tasks/db_config.php +++ b/cake/console/libs/tasks/db_config.php @@ -24,9 +24,6 @@ * @lastmodified $Date$ * @license http://www.opensource.org/licenses/mit-license.php The MIT License */ -if (!class_exists('File')) { - uses('file'); -} /** * Task class for creating and updating the database configuration file. * @@ -52,6 +49,13 @@ class DbConfigTask extends Shell { 'login'=> 'root', 'password'=> 'password', 'database'=> 'project_name', 'schema'=> null, 'prefix'=> null, 'encoding' => null, 'port' => null ); +/** + * String name of the database config class name. + * Used for testing. + * + * @var string + **/ + var $databaseClassName = 'DATABASE_CONFIG'; /** * initialization callback * @@ -92,8 +96,7 @@ function __interactive() { if (preg_match('/[^a-z0-9_]/i', $name)) { $name = ''; $this->out('The name may only contain unaccented latin characters, numbers or underscores'); - } - else if (preg_match('/^[^a-z_]/i', $name)) { + } else if (preg_match('/^[^a-z_]/i', $name)) { $name = ''; $this->out('The name must start with an unaccented latin character or an underscore'); } @@ -240,7 +243,7 @@ function __verify($config) { $this->hr(); $looksGood = $this->in('Look okay?', array('y', 'n'), 'y'); - if (low($looksGood) == 'y' || low($looksGood) == 'yes') { + if (strtolower($looksGood) == 'y') { return $config; } return false; @@ -262,7 +265,7 @@ function bake($configs) { $oldConfigs = array(); if (file_exists($filename)) { - $db = new DATABASE_CONFIG; + $db = new $this->databaseClassName; $temp = get_class_vars(get_class($db)); foreach ($temp as $configName => $info) { @@ -346,7 +349,7 @@ function bake($configs) { $out .= "}\n"; $out .= "?>"; - $filename = $this->path.'database.php'; + $filename = $this->path . 'database.php'; return $this->createFile($filename, $out); } @@ -357,7 +360,7 @@ function bake($configs) { **/ function getConfig() { $useDbConfig = 'default'; - $configs = get_class_vars('DATABASE_CONFIG'); + $configs = get_class_vars($this->databaseClassName); if (!is_array($configs)) { return $this->execute(); @@ -369,5 +372,6 @@ function getConfig() { } return $useDbConfig; } + } ?> \ No newline at end of file diff --git a/cake/tests/cases/console/libs/tasks/db_config.test.php b/cake/tests/cases/console/libs/tasks/db_config.test.php new file mode 100644 index 00000000000..27a4f820ecc --- /dev/null +++ b/cake/tests/cases/console/libs/tasks/db_config.test.php @@ -0,0 +1,155 @@ + 'mysql', + 'persistent' => false, + 'host' => 'localhost', + 'login' => 'user', + 'password' => 'password', + 'database' => 'database_name', + 'prefix' => '', + ); + + var $otherOne = array( + 'driver' => 'mysql', + 'persistent' => false, + 'host' => 'localhost', + 'login' => 'user', + 'password' => 'password', + 'database' => 'other_one', + 'prefix' => '', + ); +} + +/** + * DbConfigTest class + * + * @package cake + * @subpackage cake.tests.cases.console.libs.tasks + */ +class DbConfigTaskTest extends CakeTestCase { + +/** + * setUp method + * + * @return void + * @access public + */ + function startTest() { + $this->Dispatcher =& new TestDbConfigTaskMockShellDispatcher(); + $this->Task =& new MockDbConfigTask($this->Dispatcher); + $this->Task->Dispatch =& new $this->Dispatcher; + $this->Task->Dispatch->shellPaths = Configure::read('shellPaths'); + //$this->Task->Template =& new TemplateTask($this->Task->Dispatch); + + $this->Task->params['working'] = rtrim(APP, '/'); + $this->Task->databaseClassName = 'TEST_DATABASE_CONFIG'; + } + +/** + * tearDown method + * + * @return void + * @access public + */ + function endTest() { + unset($this->Task, $this->Dispatcher); + ClassRegistry::flush(); + } + +/** + * Test the getConfig method. + * + * @return void + **/ + function testGetConfig() { + $this->Task->setReturnValueAt(0, 'in', 'otherOne'); + $result = $this->Task->getConfig(); + $this->assertEqual($result, 'otherOne'); + } + +/** + * test that initialize sets the path up. + * + * @return void + **/ + function testInitialize() { + $this->assertTrue(empty($this->Task->path)); + $this->Task->initialize(); + $this->assertFalse(empty($this->Task->path)); + $this->assertEqual($this->Task->path, APP . 'config' . DS); + + } +/** + * test execute and by extension __interactive + * + * @return void + **/ + function testExecuteIntoInteractive() { + $this->Task->initialize(); + + $this->Task->expectOnce('_stop'); + $this->Task->setReturnValue('in', 'y'); + $this->Task->setReturnValueAt(0, 'in', 'default'); + $this->Task->setReturnValueAt(1, 'in', 'n'); + $this->Task->setReturnValueAt(2, 'in', 'localhost'); + $this->Task->setReturnValueAt(3, 'in', 'n'); + $this->Task->setReturnValueAt(4, 'in', 'root'); + $this->Task->setReturnValueAt(5, 'in', 'password'); + $this->Task->setReturnValueAt(6, 'in', 'cake_test'); + $this->Task->setReturnValueAt(7, 'in', 'n'); + $this->Task->setReturnValueAt(8, 'in', 'y'); + $this->Task->setReturnValueAt(9, 'in', 'y'); + $this->Task->setReturnValueAt(10, 'in', 'y'); + $this->Task->setReturnValueAt(11, 'in', 'n'); + + $result = $this->Task->execute(); + } +} +?> \ No newline at end of file