Fixing inflection of parameters when using bake. Model, View, Controller, and Fixture tasks all accept either singular or plural forms of the table name. Fixes #425

This commit is contained in:
Mark Story 2010-03-05 21:30:58 -05:00
parent 1d449e3847
commit 220fba9bba
8 changed files with 135 additions and 6 deletions

View file

@ -70,7 +70,7 @@ class ControllerTask extends BakeTask {
return $this->all();
}
$controller = Inflector::camelize($this->args[0]);
$controller = $this->_controllerName($this->args[0]);
$actions = 'scaffold';
if (!empty($this->args[1]) && ($this->args[1] == 'public' || $this->args[1] == 'scaffold')) {

View file

@ -79,7 +79,7 @@ class FixtureTask extends BakeTask {
if (strtolower($this->args[0]) == 'all') {
return $this->all();
}
$model = Inflector::camelize($this->args[0]);
$model = $this->_modelName($this->args[0]);
$this->bake($model);
}
}

View file

@ -88,7 +88,7 @@ class ModelTask extends BakeTask {
if (strtolower($this->args[0]) == 'all') {
return $this->all();
}
$model = Inflector::camelize($this->args[0]);
$model = $this->_modelName($this->args[0]);
$object = $this->_getModelObject($model);
if ($this->bake($object, false)) {
if ($this->_checkUnitTest()) {

View file

@ -109,8 +109,8 @@ class ViewTask extends BakeTask {
$this->connection = 'default';
}
$controller = $action = $alias = null;
$this->controllerName = Inflector::camelize($this->args[0]);
$this->controllerPath = Inflector::underscore($this->controllerName);
$this->controllerName = $this->_controllerName($this->args[0]);
$this->controllerPath = $this->_controllerPath($this->controllerName);
$this->Project->interactive = false;
if (strtolower($this->args[0]) == 'all') {

View file

@ -326,7 +326,7 @@ class ControllerTaskTest extends CakeTestCase {
$this->assertTrue(strpos($result, 'function add()') !== false);
$this->assertTrue(strpos($result, 'if (!empty($this->data))') !== false);
$this->assertTrue(strpos($result, 'if ($this->Article->save($this->data))') !== false);
$this->assertTrue(strpos($result, "\$this->Session->setFlash(sprintf(__('The %s has been saved', true), 'article'));") !== false);
$this->assertTrue(strpos($result, "\$this->Session->setFlash(sprintf(__('The %s has been saved', true), 'article'));") !== false);
$this->assertTrue(strpos($result, 'function edit($id = null)') !== false);
$this->assertTrue(strpos($result, "\$this->Session->setFlash(sprintf(__('The %s could not be saved. Please, try again.', true), 'article'));") !== false);
@ -474,6 +474,58 @@ class ControllerTaskTest extends CakeTestCase {
$this->Task->execute();
}
/**
* test that both plural and singular forms work for controller baking.
*
* @return void
* @access public
*/
function testExecuteWithControllerNameVariations() {
$skip = $this->skipIf(!defined('ARTICLE_MODEL_CREATED'),
'Execute with scaffold param requires no Article, Tag or Comment model to be defined. %s');
if ($skip) {
return;
}
$this->Task->connection = 'test_suite';
$this->Task->path = '/my/path/';
$this->Task->args = array('Articles');
$filename = '/my/path/articles_controller.php';
$this->Task->expectAt(0, 'createFile', array(
$filename, new PatternExpectation('/\$scaffold/')
));
$this->Task->execute();
$this->Task->args = array('Article');
$filename = '/my/path/articles_controller.php';
$this->Task->expectAt(1, 'createFile', array(
$filename, new PatternExpectation('/class ArticlesController/')
));
$this->Task->execute();
$this->Task->args = array('article');
$filename = '/my/path/articles_controller.php';
$this->Task->expectAt(2, 'createFile', array(
$filename, new PatternExpectation('/class ArticlesController/')
));
$this->Task->args = array('articles');
$filename = '/my/path/articles_controller.php';
$this->Task->expectAt(3, 'createFile', array(
$filename, new PatternExpectation('/class ArticlesController/')
));
$this->Task->execute();
$this->Task->args = array('Articles');
$filename = '/my/path/articles_controller.php';
$this->Task->expectAt(4, 'createFile', array(
$filename, new PatternExpectation('/class ArticlesController/')
));
$this->Task->execute();
$this->Task->execute();
}
/**
* test that `cake bake controller foo scaffold` works.
*

View file

@ -171,6 +171,37 @@ class FixtureTaskTest extends CakeTestCase {
$this->Task->execute();
}
/**
* test that execute passes runs bake depending with named model.
*
* @return void
* @access public
*/
function testExecuteWithNamedModelVariations() {
$this->Task->connection = 'test_suite';
$this->Task->path = '/my/path/';
$this->Task->args = array('article');
$filename = '/my/path/article_fixture.php';
$this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
$this->Task->execute();
$this->Task->args = array('articles');
$filename = '/my/path/article_fixture.php';
$this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
$this->Task->execute();
$this->Task->args = array('Articles');
$filename = '/my/path/article_fixture.php';
$this->Task->expectAt(2, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
$this->Task->execute();
$this->Task->args = array('Article');
$filename = '/my/path/article_fixture.php';
$this->Task->expectAt(3, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
$this->Task->execute();
}
/**
* test that execute runs all() when args[0] = all
*

View file

@ -715,6 +715,32 @@ STRINGEND;
$this->assertEqual(count(ClassRegistry::mapKeys()), 0);
}
/**
* test that execute passes with different inflections of the same name.
*
* @return void
* @access public
*/
function testExecuteWithNamedModelVariations() {
$this->Task->connection = 'test_suite';
$this->Task->path = '/my/path/';
$this->Task->setReturnValue('_checkUnitTest', 1);
$this->Task->args = array('article');
$filename = '/my/path/article.php';
$this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class Article extends AppModel/')));
$this->Task->execute();
$this->Task->args = array('Articles');
$this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/class Article extends AppModel/')));
$this->Task->execute();
$this->Task->args = array('articles');
$this->Task->expectAt(2, 'createFile', array($filename, new PatternExpectation('/class Article extends AppModel/')));
$this->Task->execute();
}
/**
* test that execute with a model name picks up hasMany associations.
*

View file

@ -504,6 +504,26 @@ class ViewTaskTest extends CakeTestCase {
$this->Task->execute();
}
/**
* test that both plural and singular forms can be used for baking views.
*
* @return void
* @access public
*/
function testExecuteWithControllerVariations() {
$this->Task->args = array('ViewTaskComments');
$this->Task->expectAt(0, 'createFile', array(TMP . 'view_task_comments' . DS . 'index.ctp', '*'));
$this->Task->expectAt(1, 'createFile', array(TMP . 'view_task_comments' . DS . 'add.ctp', '*'));
$this->Task->execute();
$this->Task->args = array('ViewTaskComment');
$this->Task->expectAt(0, 'createFile', array(TMP . 'view_task_comments' . DS . 'index.ctp', '*'));
$this->Task->expectAt(1, 'createFile', array(TMP . 'view_task_comments' . DS . 'add.ctp', '*'));
$this->Task->execute();
}
/**
* test `cake bake view $controller -admin`
* Which only bakes admin methods, not non-admin methods.