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

This commit is contained in:
Ceeram 2011-12-05 12:21:18 +01:00
parent a062135d87
commit e4e26a852b
3 changed files with 110 additions and 22 deletions

View file

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

View file

@ -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 * @param string $name Name
* @return string Camelized and singularized controller name * @return string Camelized and singularized model name
*/ */
protected function _modelName($name) { protected function _modelName($name) {
return Inflector::camelize(Inflector::singularize($name)); return Inflector::camelize(Inflector::singularize($name));

View file

@ -205,7 +205,7 @@ class ModelTaskTest extends CakeTestCase {
* *
* @return void * @return void
*/ */
public function testGetTableOddTable() { public function testGetTableOddTableInteractive() {
$out = $this->getMock('ConsoleOutput', array(), array(), '', false); $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
$in = $this->getMock('ConsoleInput', array(), array(), '', false); $in = $this->getMock('ConsoleInput', array(), array(), '', false);
$this->Task = $this->getMock('ModelTask', $this->Task = $this->getMock('ModelTask',
@ -233,6 +233,34 @@ class ModelTaskTest extends CakeTestCase {
$this->assertEquals($expected, $result); $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. * test that initializing the validations works.
* *
@ -970,6 +998,61 @@ STRINGEND;
$this->Task->execute(); $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. * test that skipTables changes how all() works.
* *