mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
Fixing modeltask to use correct table
ModelTask will now bake correct primaryKey when not id
This commit is contained in:
parent
1668067740
commit
57a1a2814d
3 changed files with 125 additions and 11 deletions
|
@ -57,6 +57,13 @@ class ModelTask extends BakeTask {
|
||||||
*/
|
*/
|
||||||
protected $_tables = array();
|
protected $_tables = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Holds the model names
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $_modelNames = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds validation method map.
|
* Holds validation method map.
|
||||||
*
|
*
|
||||||
|
@ -118,9 +125,9 @@ class ModelTask extends BakeTask {
|
||||||
}
|
}
|
||||||
$modelClass = Inflector::classify($table);
|
$modelClass = Inflector::classify($table);
|
||||||
$this->out(__d('cake_console', 'Baking %s', $modelClass));
|
$this->out(__d('cake_console', 'Baking %s', $modelClass));
|
||||||
$object = $this->_getModelObject($modelClass);
|
$object = $this->_getModelObject($modelClass, $table);
|
||||||
if ($this->bake($object, false) && $unitTestExists) {
|
if ($this->bake($object, false) && $unitTestExists) {
|
||||||
$this->bakeFixture($modelClass);
|
$this->bakeFixture($modelClass, $table);
|
||||||
$this->bakeTest($modelClass);
|
$this->bakeTest($modelClass);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -133,11 +140,18 @@ class ModelTask extends BakeTask {
|
||||||
* @param string $table Table name
|
* @param string $table Table name
|
||||||
* @return Model Model instance
|
* @return Model Model instance
|
||||||
*/
|
*/
|
||||||
protected function &_getModelObject($className, $table = null) {
|
protected function _getModelObject($className, $table = null) {
|
||||||
if (!$table) {
|
if (!$table) {
|
||||||
$table = Inflector::tableize($className);
|
$table = Inflector::tableize($className);
|
||||||
}
|
}
|
||||||
$object = new Model(array('name' => $className, 'table' => $table, 'ds' => $this->connection));
|
$object = new Model(array('name' => $className, 'table' => $table, 'ds' => $this->connection));
|
||||||
|
$fields = $object->schema(true);
|
||||||
|
foreach ($fields as $name => $field) {
|
||||||
|
if (isset($field['key']) && $field['key'] == 'primary') {
|
||||||
|
$object->primaryKey = $name;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
return $object;
|
return $object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -452,7 +466,7 @@ class ModelTask extends BakeTask {
|
||||||
* Handles associations
|
* Handles associations
|
||||||
*
|
*
|
||||||
* @param Model $model
|
* @param Model $model
|
||||||
* @return array $assocaitons
|
* @return array $associations
|
||||||
*/
|
*/
|
||||||
public function doAssociations($model) {
|
public function doAssociations($model) {
|
||||||
if (!is_object($model)) {
|
if (!is_object($model)) {
|
||||||
|
@ -464,7 +478,7 @@ class ModelTask extends BakeTask {
|
||||||
|
|
||||||
$fields = $model->schema(true);
|
$fields = $model->schema(true);
|
||||||
if (empty($fields)) {
|
if (empty($fields)) {
|
||||||
return false;
|
return array();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($this->_tables)) {
|
if (empty($this->_tables)) {
|
||||||
|
@ -730,8 +744,8 @@ class ModelTask extends BakeTask {
|
||||||
public function bake($name, $data = array()) {
|
public function bake($name, $data = array()) {
|
||||||
if (is_object($name)) {
|
if (is_object($name)) {
|
||||||
if ($data == false) {
|
if ($data == false) {
|
||||||
$data = $associations = array();
|
$data = array();
|
||||||
$data['associations'] = $this->doAssociations($name, $associations);
|
$data['associations'] = $this->doAssociations($name);
|
||||||
$data['validate'] = $this->doValidation($name);
|
$data['validate'] = $this->doValidation($name);
|
||||||
}
|
}
|
||||||
$data['primaryKey'] = $name->primaryKey;
|
$data['primaryKey'] = $name->primaryKey;
|
||||||
|
@ -808,6 +822,10 @@ class ModelTask extends BakeTask {
|
||||||
|
|
||||||
$db = ConnectionManager::getDataSource($useDbConfig);
|
$db = ConnectionManager::getDataSource($useDbConfig);
|
||||||
$useTable = Inflector::tableize($modelName);
|
$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);
|
$fullTableName = $db->fullTableName($useTable, false);
|
||||||
$tableIsGood = false;
|
$tableIsGood = false;
|
||||||
|
|
||||||
|
|
|
@ -200,6 +200,39 @@ 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 = true;
|
||||||
|
|
||||||
|
$this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('articles', 'bake_odd')));
|
||||||
|
$this->Task->expects($this->any())->method('in')
|
||||||
|
->will($this->onConsecutiveCalls(
|
||||||
|
2 // bake_odd
|
||||||
|
));
|
||||||
|
|
||||||
|
$result = $this->Task->getName();
|
||||||
|
$expected = 'BakeOdd';
|
||||||
|
$this->assertEquals($expected, $result);
|
||||||
|
|
||||||
|
$result = $this->Task->getTable($result);
|
||||||
|
$expected = 'bake_odd';
|
||||||
|
$this->assertEquals($expected, $result);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* test that initializing the validations works.
|
* test that initializing the validations works.
|
||||||
*
|
*
|
||||||
|
@ -548,7 +581,7 @@ class ModelTaskTest extends CakeTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ensure that the fixutre object is correctly called.
|
* Ensure that the fixture object is correctly called.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
@ -860,9 +893,17 @@ STRINGEND;
|
||||||
$this->Task->expects($this->at(3))->method('createFile')
|
$this->Task->expects($this->at(3))->method('createFile')
|
||||||
->with($filename, $this->stringContains('class BakeComment'));
|
->with($filename, $this->stringContains('class BakeComment'));
|
||||||
|
|
||||||
|
$filename = '/my/path/BakeComment.php';
|
||||||
|
$this->Task->expects($this->at(3))->method('createFile')
|
||||||
|
->with($filename, $this->stringContains('public $primaryKey = \'otherid\';'));
|
||||||
|
|
||||||
$filename = '/my/path/BakeTag.php';
|
$filename = '/my/path/BakeTag.php';
|
||||||
$this->Task->expects($this->at(4))
|
$this->Task->expects($this->at(4))->method('createFile')
|
||||||
->method('createFile')->with($filename, $this->stringContains('class BakeTag'));
|
->with($filename, $this->stringContains('class BakeTag'));
|
||||||
|
|
||||||
|
$filename = '/my/path/BakeTag.php';
|
||||||
|
$this->Task->expects($this->at(4))->method('createFile')
|
||||||
|
->with($filename, $this->logicalNot($this->stringContains('public $primaryKey')));
|
||||||
|
|
||||||
$filename = '/my/path/CategoryThread.php';
|
$filename = '/my/path/CategoryThread.php';
|
||||||
$this->Task->expects($this->at(5))->method('createFile')
|
$this->Task->expects($this->at(5))->method('createFile')
|
||||||
|
@ -874,6 +915,61 @@ STRINGEND;
|
||||||
$this->assertEquals(count(ClassRegistry::mapKeys()), 0);
|
$this->assertEquals(count(ClassRegistry::mapKeys()), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test that odd tablenames arent inflected back from modelname
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testExecuteIntoAllOddTables() {
|
||||||
|
$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('all');
|
||||||
|
$this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
|
||||||
|
$this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('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->at(3))->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('all');
|
||||||
|
$this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
|
||||||
|
$this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('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.
|
||||||
*
|
*
|
||||||
|
|
|
@ -37,7 +37,7 @@ class BakeCommentFixture extends CakeTestFixture {
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public $fields = array(
|
public $fields = array(
|
||||||
'id' => array('type' => 'integer', 'key' => 'primary'),
|
'otherid' => array('type' => 'integer', 'key' => 'primary'),
|
||||||
'bake_article_id' => array('type' => 'integer', 'null'=>false),
|
'bake_article_id' => array('type' => 'integer', 'null'=>false),
|
||||||
'bake_user_id' => array('type' => 'integer', 'null'=>false),
|
'bake_user_id' => array('type' => 'integer', 'null'=>false),
|
||||||
'comment' => 'text',
|
'comment' => 'text',
|
||||||
|
|
Loading…
Reference in a new issue