Permalink
Browse files

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

  • Loading branch information...
ceeram committed Dec 5, 2011
1 parent a062135 commit e4e26a852b502674381906542aaebba7cf9a1f03
@@ -101,10 +101,12 @@ public function execute() {
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 @@ public function bakeTest($className) {
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 @@ public function listAll($useDbConfig = null) {
* @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;
}
@@ -716,10 +716,10 @@ protected function _controllerName($name) {
}
/**
- * 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));
@@ -205,7 +205,7 @@ public function testGetTableNameCustom() {
*
* @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 @@ public function testGetTableOddTable() {
$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 @@ public function testExecuteIntoAllOddTables() {
$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.
*

0 comments on commit e4e26a8

Please sign in to comment.