Skip to content

Commit

Permalink
Start fixing tests for ControllerTask.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Mar 20, 2014
1 parent 4d02519 commit 79d753c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 151 deletions.
113 changes: 28 additions & 85 deletions src/Console/Command/Task/ControllerTask.php
Expand Up @@ -56,42 +56,28 @@ public function initialize() {
public function execute() {
parent::execute();

if (empty($this->args)) {
return $this->_interactive();
if (!isset($this->connection)) {
$this->connection = 'default';
}

if (isset($this->args[0])) {
if (!isset($this->connection)) {
$this->connection = 'default';
}
if (strtolower($this->args[0]) === 'all') {
return $this->all();
if (empty($this->args)) {
$this->out(__d('cake_console', 'Possible Controllers based on your current database:'));
foreach ($this->listAll() as $table) {
$this->out('- ' . $this->_controllerName($table));
}
return true;
}

$controller = $this->_controllerName($this->args[0]);
$actions = '';
if (strtolower($this->args[0]) === 'all') {
return $this->all();
}

if (!empty($this->params['public'])) {
$this->out(__d('cake_console', 'Baking basic crud methods for ') . $controller);
$actions .= $this->bakeActions($controller);
}
if (!empty($this->params['admin'])) {
$admin = $this->Project->getPrefix();
if ($admin) {
$this->out(__d('cake_console', 'Adding %s methods', $admin));
$actions .= "\n" . $this->bakeActions($controller, $admin);
}
}
if (empty($actions)) {
$actions = 'scaffold';
}
$controller = $this->_controllerName($this->args[0]);
$this->out(__d('cake_console', 'Baking basic crud methods for ') . $controller);
$actions = $this->bakeActions($controller);

if ($this->bake($controller, $actions)) {
if ($this->_checkUnitTest()) {
$this->bakeTest($controller);
}
}
}
$this->bake($controller, $actions);
$this->bakeTest($controller);
}

/**
Expand All @@ -100,10 +86,8 @@ public function execute() {
* @return void
*/
public function all() {
$this->interactive = false;
$this->listAll($this->connection, false);
$this->listAll();
ClassRegistry::config('Model', ['ds' => $this->connection]);
$unitTestExists = $this->_checkUnitTest();

$admin = false;
if (!empty($this->params['admin'])) {
Expand All @@ -121,9 +105,8 @@ public function all() {
$this->out(__d('cake_console', 'Adding %s methods', $admin));
$actions .= "\n" . $this->bakeActions($controller, $admin);
}
if ($this->bake($controller, $actions) && $unitTestExists) {
$this->bakeTest($controller);
}
$this->bake($controller, $actions);
$this->bakeTest($controller);
$controllersCreated++;
}
}
Expand Down Expand Up @@ -357,6 +340,9 @@ public function bake($controllerName, $actions = '', $helpers = null, $component
* @return string Baked test
*/
public function bakeTest($className) {
if (!empty($this->params['no-test'])) {
return;
}
$this->Test->plugin = $this->plugin;
$this->Test->connection = $this->connection;
$this->Test->interactive = $this->interactive;
Expand Down Expand Up @@ -412,55 +398,9 @@ protected function _doPropertyChoices($prompt, $example) {
* @param string $useDbConfig Database configuration name
* @return array Set of controllers
*/
public function listAll($useDbConfig = null) {
if ($useDbConfig === null) {
$useDbConfig = $this->connection;
}
$this->__tables = $this->Model->getAllTables($useDbConfig);

if ($this->interactive) {
$this->out(__d('cake_console', 'Possible Controllers based on your current database:'));
$this->hr();
$this->_controllerNames = [];
$count = count($this->__tables);
for ($i = 0; $i < $count; $i++) {
$this->_controllerNames[] = $this->_controllerName($this->_modelName($this->__tables[$i]));
$this->out(sprintf("%2d. %s", $i + 1, $this->_controllerNames[$i]));
}
return $this->_controllerNames;
}
return $this->__tables;
}

/**
* Forces the user to specify the controller he wants to bake, and returns the selected controller name.
*
* @param string $useDbConfig Connection name to get a controller name for.
* @return string Controller name
*/
public function getName($useDbConfig = null) {
$controllers = $this->listAll($useDbConfig);
$enteredController = '';

while (!$enteredController) {
$enteredController = $this->in(__d('cake_console', "Enter a number from the list above,\ntype in the name of another controller, or 'q' to exit"), null, 'q');
if ($enteredController === 'q') {
$this->out(__d('cake_console', 'Exit'));
return $this->_stop();
}

if (!$enteredController || intval($enteredController) > count($controllers)) {
$this->err(__d('cake_console', "The Controller name you supplied was empty,\nor the number you selected was not an option. Please try again."));
$enteredController = '';
}
}

if (intval($enteredController) > 0 && intval($enteredController) <= count($controllers)) {
$controllerName = $controllers[intval($enteredController) - 1];
} else {
$controllerName = Inflector::camelize($enteredController);
}
return $controllerName;
public function listAll() {
$this->Model->connection = $this->connection;
return $this->Model->listAll();
}

/**
Expand Down Expand Up @@ -489,6 +429,9 @@ public function getOptionParser() {
'help' => __d('cake_console', 'The comma separated list of helpers to use.')
])->addOption('prefix', [
'help' => __d('cake_console', 'The namespace/routing prefix to use.')
])->addOption('no-test', [
'boolean' => true,
'help' => __d('cake_console', 'Do not generate a test skeleton.')
])->addOption('force', [
'short' => 'f',
'help' => __d('cake_console', 'Force overwriting existing files without prompting.')
Expand Down

0 comments on commit 79d753c

Please sign in to comment.