Browse Source

Making non-interactive behave same as interactive with odd table names

Ceeram 14 years ago
parent
commit
e4e26a852b

+ 24 - 19
lib/Cake/Console/Command/Task/ModelTask.php

@@ -101,10 +101,12 @@ class ModelTask extends BakeTask {
 				return $this->all();
 			}
 			$model = $this->_modelName($this->args[0]);
-			$object = $this->_getModelObject($model);
+			$this->listAll($this->connection);
+			$useTable = $this->getTable($model);
+			$object = $this->_getModelObject($model, $useTable);
 			if ($this->bake($object, false)) {
 				if ($this->_checkUnitTest()) {
-					$this->bakeFixture($model);
+					$this->bakeFixture($model, $useTable);
 					$this->bakeTest($model);
 				}
 			}
@@ -797,12 +799,14 @@ class ModelTask extends BakeTask {
 	public function listAll($useDbConfig = null) {
 		$this->_tables = $this->getAllTables($useDbConfig);
 
+		$this->_modelNames = array();
+		$count = count($this->_tables);
+		for ($i = 0; $i < $count; $i++) {
+			$this->_modelNames[] = $this->_modelName($this->_tables[$i]);
+		}
 		if ($this->interactive === true) {
 			$this->out(__d('cake_console', 'Possible Models based on your current database:'));
-			$this->_modelNames = array();
-			$count = count($this->_tables);
 			for ($i = 0; $i < $count; $i++) {
-				$this->_modelNames[] = $this->_modelName($this->_tables[$i]);
 				$this->out($i + 1 . ". " . $this->_modelNames[$i]);
 			}
 		}
@@ -817,26 +821,27 @@ class ModelTask extends BakeTask {
  * @return string Table name
  */
 	public function getTable($modelName, $useDbConfig = null) {
-		if (!isset($useDbConfig)) {
-			$useDbConfig = $this->connection;
-		}
-
-		$db = ConnectionManager::getDataSource($useDbConfig);
 		$useTable = Inflector::tableize($modelName);
 		if (in_array($modelName, $this->_modelNames)) {
 			$modelNames = array_flip($this->_modelNames);
 			$useTable = $this->_tables[$modelNames[$modelName]];
 		}
-		$fullTableName = $db->fullTableName($useTable, false);
-		$tableIsGood = false;
 
-		if (array_search($useTable, $this->_tables) === false) {
-			$this->out();
-			$this->out(__d('cake_console', "Given your model named '%s',\nCake would expect a database table named '%s'", $modelName, $fullTableName));
-			$tableIsGood = $this->in(__d('cake_console', 'Do you want to use this table?'), array('y', 'n'), 'y');
-		}
-		if (strtolower($tableIsGood) == 'n') {
-			$useTable = $this->in(__d('cake_console', 'What is the name of the table?'));
+		if ($this->interactive === true) {
+			if (!isset($useDbConfig)) {
+				$useDbConfig = $this->connection;
+			}
+			$db = ConnectionManager::getDataSource($useDbConfig);
+			$fullTableName = $db->fullTableName($useTable, false);
+			$tableIsGood = false;
+			if (array_search($useTable, $this->_tables) === false) {
+				$this->out();
+				$this->out(__d('cake_console', "Given your model named '%s',\nCake would expect a database table named '%s'", $modelName, $fullTableName));
+				$tableIsGood = $this->in(__d('cake_console', 'Do you want to use this table?'), array('y', 'n'), 'y');
+			}
+			if (strtolower($tableIsGood) == 'n') {
+				$useTable = $this->in(__d('cake_console', 'What is the name of the table?'));
+			}
 		}
 		return $useTable;
 	}

+ 2 - 2
lib/Cake/Console/Shell.php

@@ -716,10 +716,10 @@ class Shell extends Object {
 	}
 
 /**
- * Creates the proper controller camelized name (singularized) for the specified name
+ * Creates the proper model camelized name (singularized) for the specified name
  *
  * @param string $name Name
- * @return string Camelized and singularized controller name
+ * @return string Camelized and singularized model name
  */
 	protected function _modelName($name) {
 		return Inflector::camelize(Inflector::singularize($name));

+ 84 - 1
lib/Cake/Test/Case/Console/Command/Task/ModelTaskTest.php

@@ -205,7 +205,7 @@ class ModelTaskTest extends CakeTestCase {
  *
  * @return void
  */
-	public function testGetTableOddTable() {
+	public function testGetTableOddTableInteractive() {
 		$out = $this->getMock('ConsoleOutput', array(), array(), '', false);
 		$in = $this->getMock('ConsoleInput', array(), array(), '', false);
 		$this->Task = $this->getMock('ModelTask',
@@ -234,6 +234,34 @@ class ModelTaskTest extends CakeTestCase {
 	}
 
 /**
+ * test getTable with non-conventional tablenames
+ *
+ * @return void
+ */
+	public function testGetTableOddTable() {
+		$out = $this->getMock('ConsoleOutput', array(), array(), '', false);
+		$in = $this->getMock('ConsoleInput', array(), array(), '', false);
+		$this->Task = $this->getMock('ModelTask',
+			array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables'),
+			array($out, $out, $in)
+		);
+		$this->_setupOtherMocks();
+
+		$this->Task->connection = 'test';
+		$this->Task->path = '/my/path/';
+		$this->Task->interactive = false;
+		$this->Task->args = array('BakeOdd');
+
+		$this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('articles', 'bake_odd')));
+
+		$this->Task->listAll();
+
+		$result = $this->Task->getTable('BakeOdd');
+		$expected = 'bake_odd';
+		$this->assertEquals($expected, $result);
+	}
+
+/**
  * test that initializing the validations works.
  *
  * @return void
@@ -971,6 +999,61 @@ STRINGEND;
 	}
 
 /**
+ * test that odd tablenames arent inflected back from modelname
+ *
+ * @return void
+ */
+    public function testExecuteIntoBakeOddTables() {
+		$out = $this->getMock('ConsoleOutput', array(), array(), '', false);
+		$in = $this->getMock('ConsoleInput', array(), array(), '', false);
+		$this->Task = $this->getMock('ModelTask',
+			array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables', '_getModelObject', 'bake', 'bakeFixture'),
+			array($out, $out, $in)
+		);
+		$this->_setupOtherMocks();
+
+		$this->Task->connection = 'test';
+		$this->Task->path = '/my/path/';
+		$this->Task->args = array('BakeOdd');
+		$this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
+		$this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('articles', 'bake_odd')));
+		$object = new Model(array('name' => 'BakeOdd', 'table' => 'bake_odd', 'ds' => 'test'));
+		$this->Task->expects($this->once())->method('_getModelObject')->with('BakeOdd', 'bake_odd')->will($this->returnValue($object));
+		$this->Task->expects($this->once())->method('bake')->with($object, false)->will($this->returnValue(true));
+		$this->Task->expects($this->once())->method('bakeFixture')->with('BakeOdd', 'bake_odd');
+
+		$this->Task->execute();
+
+		$out = $this->getMock('ConsoleOutput', array(), array(), '', false);
+		$in = $this->getMock('ConsoleInput', array(), array(), '', false);
+		$this->Task = $this->getMock('ModelTask',
+			array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables', '_getModelObject', 'doAssociations', 'doValidation', 'createFile'),
+			array($out, $out, $in)
+		);
+		$this->_setupOtherMocks();
+
+		$this->Task->connection = 'test';
+		$this->Task->path = '/my/path/';
+		$this->Task->args = array('BakeOdd');
+		$this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
+		$this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('articles', 'bake_odd')));
+		$object = new Model(array('name' => 'BakeOdd', 'table' => 'bake_odd', 'ds' => 'test'));
+		$this->Task->expects($this->once())->method('_getModelObject')->will($this->returnValue($object));
+		$this->Task->expects($this->once())->method('doAssociations')->will($this->returnValue(array()));
+		$this->Task->expects($this->once())->method('doValidation')->will($this->returnValue(array()));
+
+		$filename = '/my/path/BakeOdd.php';
+		$this->Task->expects($this->once())->method('createFile')
+			->with($filename, $this->stringContains('class BakeOdd'));
+
+		$filename = '/my/path/BakeOdd.php';
+		$this->Task->expects($this->once())->method('createFile')
+			->with($filename, $this->stringContains('public $useTable = \'bake_odd\''));
+
+		$this->Task->execute();
+	}
+
+/**
  * test that skipTables changes how all() works.
  *
  * @return void