Skip to content

Commit

Permalink
Adding test case for controller task.
Browse files Browse the repository at this point in the history
Refactoring listAll() to use Model task which has 
the necessary code.
  • Loading branch information
markstory committed May 15, 2009
1 parent e6881cc commit 03e231c
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 28 deletions.
39 changes: 11 additions & 28 deletions cake/console/libs/tasks/controller.php
Expand Up @@ -44,7 +44,7 @@ class ControllerTask extends Shell {
* @var array
* @access public
*/
var $tasks = array('Project');
var $tasks = array('Model', 'Project', 'Template');
/**
* path to CONTROLLERS directory
*
Expand Down Expand Up @@ -105,12 +105,8 @@ function execute() {
* @return void
**/
function all() {
$ds = 'default';
if (isset($this->params['connection'])) {
$ds = $this->params['connection'];
}
$this->interactive = false;
$this->listAll($ds, false);
$this->listAll($this->connection, false);
foreach ($this->__tables as $table) {
$model = $this->_modelName($table);
$controller = $this->_controllerName($model);
Expand Down Expand Up @@ -509,40 +505,27 @@ function bakeTest($className) {
$content = "<?php \n/* SVN FILE: $header$ */\n/* ". $className ."Controller Test cases generated on: " . date('Y-m-d H:m:s') . " : ". time() . "*/\n{$out}?>";
return $this->createFile($path . $filename, $content);
}

/**
* Outputs and gets the list of possible models or controllers from database
* Outputs and gets the list of possible controllers from database
*
* @param string $useDbConfig Database configuration name
* @param boolean $interactive Whether you are using listAll interactively and want options output.
* @return array Set of controllers
* @access public
*/
function listAll($useDbConfig = 'default', $interactive = true) {
$db =& ConnectionManager::getDataSource($useDbConfig);
$usePrefix = empty($db->config['prefix']) ? '' : $db->config['prefix'];
if ($usePrefix) {
$tables = array();
foreach ($db->listSources() as $table) {
if (!strncmp($table, $usePrefix, strlen($usePrefix))) {
$tables[] = substr($table, strlen($usePrefix));
}
}
} else {
$tables = $db->listSources();
}

if (empty($tables)) {
$this->err(__('Your database does not have any tables.', true));
$this->_stop();
function listAll($useDbConfig = null) {
if (is_null($useDbConfig)) {
$useDbConfig = $this->connection;
}
$this->__tables = $this->Model->getAllTables($useDbConfig);

$this->__tables = $tables;
if ($interactive == true) {
if ($this->interactive == true) {
$this->out('Possible Controllers based on your current database:');
$this->_controllerNames = array();
$count = count($tables);
$count = count($this->__tables);
for ($i = 0; $i < $count; $i++) {
$this->_controllerNames[] = $this->_controllerName($this->_modelName($tables[$i]));
$this->_controllerNames[] = $this->_controllerName($this->_modelName($this->__tables[$i]));
$this->out($i + 1 . ". " . $this->_controllerNames[$i]);
}
return $this->_controllerNames;
Expand Down
123 changes: 123 additions & 0 deletions cake/tests/cases/console/libs/tasks/controller.test.php
@@ -0,0 +1,123 @@
<?php
/**
* ControllerTask Test Case
*
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org
* @package cake
* @subpackage cake.tests.cases.console.libs.tasks
* @since CakePHP(tm) v 1.3
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::import('Core', 'Shell');

if (!defined('DISABLE_AUTO_DISPATCH')) {
define('DISABLE_AUTO_DISPATCH', true);
}

if (!class_exists('ShellDispatcher')) {
ob_start();
$argv = false;
require CAKE . 'console' . DS . 'cake.php';
ob_end_clean();
}

require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'controller.php';
require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'model.php';
require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'template.php';


Mock::generatePartial(
'ShellDispatcher', 'TestControllerTaskMockShellDispatcher',
array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
);

Mock::generatePartial(
'ControllerTask', 'MockControllerTask',
array('in', 'out', 'err', 'createFile', '_stop', '_checkUnitTest')
);

Mock::generatePartial(
'ModelTask', 'ControllerMockModelTask',
array('in', 'out', 'err', 'createFile', '_stop', '_checkUnitTest')
);

/**
* ControllerTaskTest class
*
* @package cake
* @subpackage cake.tests.cases.console.libs.tasks
*/
class ControllerTaskTest extends CakeTestCase {
/**
* fixtures
*
* @var array
**/
var $fixtures = array('core.article', 'core.comment', 'core.articles_tag', 'core.tag');

/**
* setUp method
*
* @return void
* @access public
*/
function startTest() {
$this->Dispatcher =& new TestControllerTaskMockShellDispatcher();
$this->Task =& new MockControllerTask($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->Model =& new ControllerMockModelTask($this->Task->Dispatch);
}

/**
* tearDown method
*
* @return void
* @access public
*/
function endTest() {
unset($this->Task, $this->Dispatcher);
ClassRegistry::flush();
}

/**
* test ListAll
*
* @return void
**/
function testListAll() {
$this->Task->connection = 'test_suite';
$this->Task->interactive = true;
$this->Task->expectAt(1, 'out', array('1. Articles'));
$this->Task->expectAt(2, 'out', array('2. ArticlesTags'));
$this->Task->expectAt(3, 'out', array('3. Comments'));
$this->Task->expectAt(4, 'out', array('4. Tags'));

$expected = array('Articles', 'ArticlesTags', 'Comments', 'Tags');
$result = $this->Task->listAll('test_suite');
$this->assertEqual($result, $expected);

$this->Task->expectAt(6, 'out', array('1. Articles'));
$this->Task->expectAt(7, 'out', array('2. ArticlesTags'));
$this->Task->expectAt(8, 'out', array('4. Comments'));
$this->Task->expectAt(9, 'out', array('5. Tags'));

$this->Task->interactive = false;
$result = $this->Task->listAll();

$expected = array('articles', 'articles_tags', 'comments', 'tags');
$this->assertEqual($result, $expected);
}
}
?>

0 comments on commit 03e231c

Please sign in to comment.