From e1e9772b6685ff1d0258282ebc70d57a38bcab7b Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 16 Mar 2014 21:40:28 -0400 Subject: [PATCH] Add missing tests. Make fields and primary-key options work consistently for composite keys. --- src/Console/Command/Task/ModelTask.php | 32 ++---------- .../Console/Command/Task/ModelTaskTest.php | 50 +++++++++++++++++++ 2 files changed, 53 insertions(+), 29 deletions(-) diff --git a/src/Console/Command/Task/ModelTask.php b/src/Console/Command/Task/ModelTask.php index ae0d2af5ab1..a83c8d9d73a 100644 --- a/src/Console/Command/Task/ModelTask.php +++ b/src/Console/Command/Task/ModelTask.php @@ -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; } @@ -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(); } @@ -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. * diff --git a/tests/TestCase/Console/Command/Task/ModelTaskTest.php b/tests/TestCase/Console/Command/Task/ModelTaskTest.php index 782d46eef53..58d864c357c 100644 --- a/tests/TestCase/Console/Command/Task/ModelTaskTest.php +++ b/tests/TestCase/Console/Command/Task/ModelTaskTest.php @@ -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. * @@ -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. * @@ -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. *