From e4e26a852b502674381906542aaebba7cf9a1f03 Mon Sep 17 00:00:00 2001 From: Ceeram Date: Mon, 5 Dec 2011 12:21:18 +0100 Subject: [PATCH] Making non-interactive behave same as interactive with odd table names --- lib/Cake/Console/Command/Task/ModelTask.php | 43 +++++----- lib/Cake/Console/Shell.php | 4 +- .../Console/Command/Task/ModelTaskTest.php | 85 ++++++++++++++++++- 3 files changed, 110 insertions(+), 22 deletions(-) diff --git a/lib/Cake/Console/Command/Task/ModelTask.php b/lib/Cake/Console/Command/Task/ModelTask.php index f44dfca9c..3fb6c2e6a 100644 --- a/lib/Cake/Console/Command/Task/ModelTask.php +++ b/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; } diff --git a/lib/Cake/Console/Shell.php b/lib/Cake/Console/Shell.php index c748b8226..b8b4919e2 100644 --- a/lib/Cake/Console/Shell.php +++ b/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)); diff --git a/lib/Cake/Test/Case/Console/Command/Task/ModelTaskTest.php b/lib/Cake/Test/Case/Console/Command/Task/ModelTaskTest.php index 4824577ed..9a1240258 100644 --- a/lib/Cake/Test/Case/Console/Command/Task/ModelTaskTest.php +++ b/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', @@ -233,6 +233,34 @@ class ModelTaskTest extends CakeTestCase { $this->assertEquals($expected, $result); } +/** + * 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. * @@ -970,6 +998,61 @@ STRINGEND; $this->Task->execute(); } +/** + * 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. *