Browse Source

Add missing tests.

Make fields and primary-key options work consistently for composite
keys.
mark_story 12 years ago
parent
commit
e1e9772b66

+ 3 - 29
src/Console/Command/Task/ModelTask.php

@@ -96,7 +96,7 @@ class ModelTask extends BakeTask {
 		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 @@ class ModelTask extends BakeTask {
  */
 	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();
 	}
@@ -465,33 +466,6 @@ class ModelTask extends BakeTask {
 	}
 
 /**
- * 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.
  *
  * @param Cake\ORM\Table $model Model name or object

+ 50 - 0
tests/TestCase/Console/Command/Task/ModelTaskTest.php

@@ -335,6 +335,22 @@ class ModelTaskTest extends TestCase {
 	}
 
 /**
+ * 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.
  *
  * @return void
@@ -380,6 +396,21 @@ class ModelTaskTest extends TestCase {
 	}
 
 /**
+ * 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.
  *
  * @return void
@@ -612,6 +643,25 @@ class ModelTaskTest extends TestCase {
 	}
 
 /**
+ * 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.
  *
  * @return void