Skip to content

Commit

Permalink
Add missing tests.
Browse files Browse the repository at this point in the history
Make fields and primary-key options work consistently for composite
keys.
  • Loading branch information
markstory committed Mar 18, 2014
1 parent e17166c commit e1e9772
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 29 deletions.
32 changes: 3 additions & 29 deletions src/Console/Command/Task/ModelTask.php
Expand Up @@ -96,7 +96,7 @@ public function execute() {
if (empty($this->args)) {
$this->out(__d('cake_console', 'Choose a model to bake from the following:'));
foreach ($this->listAll() as $table) {
$this->out('- ' . $table);
$this->out('- ' . $this->_modelName($table));
}
return true;
}
Expand Down Expand Up @@ -330,7 +330,8 @@ public function getDisplayField($model) {
*/
public function getPrimaryKey($model) {
if (!empty($this->params['primary-key'])) {
return (array)$this->params['primary-key'];
$fields = explode(',', $this->params['primary-key']);
return array_values(array_filter(array_map('trim', $fields)));
}
return (array)$model->primaryKey();
}
Expand Down Expand Up @@ -464,33 +465,6 @@ public function getBehaviors($model) {
return $behaviors;
}

/**
* Generate a key value list of options and a prompt.
*
* @param array $options Array of options to use for the selections. indexes must start at 0
* @param string $prompt Prompt to use for options list.
* @param integer $default The default option for the given prompt.
* @return integer Result of user choice.
*/
public function inOptions($options, $prompt = null, $default = null) {
$valid = false;
$max = count($options);
while (!$valid) {
$len = strlen(count($options) + 1);
foreach ($options as $i => $option) {
$this->out(sprintf("%${len}d. %s", $i + 1, $option));
}
if (empty($prompt)) {
$prompt = __d('cake_console', 'Make a selection from the choices above');
}
$choice = $this->in($prompt, null, $default);
if (intval($choice) > 0 && intval($choice) <= $max) {
$valid = true;
}
}
return $choice - 1;
}

/**
* Bake an entity class.
*
Expand Down
50 changes: 50 additions & 0 deletions tests/TestCase/Console/Command/Task/ModelTaskTest.php
Expand Up @@ -334,6 +334,22 @@ public function testFieldsWhiteList() {
$this->assertEquals($expected, $result);
}

/**
* Test getting primary key
*
* @return void
*/
public function testGetPrimaryKey() {
$model = TableRegistry::get('BakeArticles');
$result = $this->Task->getPrimaryKey($model);
$expected = ['id'];
$this->assertEquals($expected, $result);

$this->Task->params['primary-key'] = 'id, , account_id';
$result = $this->Task->getPrimaryKey($model);
$expected = ['id', 'account_id'];
$this->assertEquals($expected, $result);
}
/**
* test getting validation rules with the no-validation rule.
*
Expand Down Expand Up @@ -379,6 +395,21 @@ public function testGetBehaviors() {
$this->assertEquals(['Timestamp'], $result);
}

/**
* Test getDisplayField() method.
*
* @return void
*/
public function testGetDisplayField() {
$model = TableRegistry::get('BakeArticles');
$result = $this->Task->getDisplayField($model);
$this->assertEquals('title', $result);

$this->Task->params['display-field'] = 'custom';
$result = $this->Task->getDisplayField($model);
$this->assertEquals('custom', $result);
}

/**
* Ensure that the fixture object is correctly called.
*
Expand Down Expand Up @@ -611,6 +642,25 @@ public function testBakeEntityWithPlugin() {
$this->Task->bakeEntity($model);
}

/**
* test that execute with no args
*
* @return void
*/
public function testExecuteNoArgs() {
$this->_useMockedOut();
$this->Task->connection = 'test';
$this->Task->path = '/my/path/';

$this->Task->expects($this->at(0))
->method('out')
->with($this->stringContains('Choose a model to bake from the following:'));
$this->Task->expects($this->at(1))
->method('out')
->with('- BakeArticles');
$this->Task->execute();
}

/**
* test that execute passes runs bake depending with named model.
*
Expand Down

0 comments on commit e1e9772

Please sign in to comment.