Skip to content

Commit

Permalink
Adding files to be compared against bake generated files, fixing bake…
Browse files Browse the repository at this point in the history
… templates in the meantime.

Writing assertContains() or assertRegExp for generated code is a pain and making the test cover most part of it is very difficult.

Things like:
 ```
 } else {
 }
 ```

Are very easy to happen (which was the case) and was not being covered by the test.
  • Loading branch information
renan committed Aug 21, 2013
1 parent faa2cbd commit 703d988
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 85 deletions.
33 changes: 15 additions & 18 deletions lib/Cake/Console/Templates/default/actions/controller_actions.ctp
Expand Up @@ -55,15 +55,14 @@
$this-><?php echo $currentModelName; ?>->create();
if ($this-><?php echo $currentModelName; ?>->save($this->request->data)) {
<?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> has been saved'));
$this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> could not be saved. Please, try again.'));
<?php else: ?>
return $this->flash(__('<?php echo ucfirst(strtolower($currentModelName)); ?> saved.'), array('action' => 'index'));
return $this->flash(__('The <?php echo strtolower($singularHumanName); ?> has been saved.'), array('action' => 'index'));
<?php endif; ?>
}
<?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> could not be saved. Please, try again.'));
<?php endif; ?>
}
<?php
foreach (array('belongsTo', 'hasAndBelongsToMany') as $assoc):
Expand Down Expand Up @@ -97,15 +96,14 @@
if ($this->request->is('post') || $this->request->is('put')) {
if ($this-><?php echo $currentModelName; ?>->save($this->request->data)) {
<?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> has been saved'));
$this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> could not be saved. Please, try again.'));
<?php else: ?>
return $this->flash(__('The <?php echo strtolower($singularHumanName); ?> has been saved.'), array('action' => 'index'));
<?php endif; ?>
}
<?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> could not be saved. Please, try again.'));
<?php endif; ?>
} else {
$options = array('conditions' => array('<?php echo $currentModelName; ?>.' . $this-><?php echo $currentModelName; ?>->primaryKey => $id));
$this->request->data = $this-><?php echo $currentModelName; ?>->find('first', $options);
Expand Down Expand Up @@ -142,16 +140,15 @@
$this->request->onlyAllow('post', 'delete');
if ($this-><?php echo $currentModelName; ?>->delete()) {
<?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('<?php echo ucfirst(strtolower($singularHumanName)); ?> deleted'));
return $this->redirect(array('action' => 'index'));
<?php else: ?>
return $this->flash(__('<?php echo ucfirst(strtolower($singularHumanName)); ?> deleted'), array('action' => 'index'));
<?php endif; ?>
$this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> has been deleted.'));
} else {
$this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> could not be deleted. Please, try again.'));
}
<?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('<?php echo ucfirst(strtolower($singularHumanName)); ?> was not deleted'));
return $this->redirect(array('action' => 'index'));
<?php else: ?>
return $this->flash(__('<?php echo ucfirst(strtolower($singularHumanName)); ?> was not deleted'), array('action' => 'index'));
return $this->flash(__('The <?php echo strtolower($singularHumanName); ?> has been deleted.'), array('action' => 'index'));
} else {
return $this->flash(__('The <?php echo strtolower($singularHumanName); ?> could not be deleted. Please, try again.'), array('action' => 'index'));
}
<?php endif; ?>
return $this->redirect(array('action' => 'index'));
}
2 changes: 1 addition & 1 deletion lib/Cake/Console/Templates/default/classes/controller.ctp
Expand Up @@ -76,7 +76,7 @@ class <?php echo $controllerName; ?>Controller extends <?php echo $plugin; ?>App
echo ");\n\n";
endif;

echo trim($actions) . "\n";
echo trim($actions);

endif; ?>
}
79 changes: 13 additions & 66 deletions lib/Cake/Test/Case/Console/Command/Task/ControllerTaskTest.php
Expand Up @@ -282,29 +282,17 @@ public function testBake() {
$components = array('Acl', 'Auth');
$this->Task->expects($this->any())->method('createFile')->will($this->returnValue(true));

$result = $this->Task->bake('Articles', '--actions--', $helpers, $components);
$this->assertContains(' * @property Article $Article', $result);
$this->assertContains(' * @property AclComponent $Acl', $result);
$this->assertContains(' * @property AuthComponent $Auth', $result);
$this->assertContains(' * @property PaginatorComponent $Paginator', $result);
$this->assertContains('class ArticlesController extends AppController', $result);
$this->assertContains("public \$components = array('Acl', 'Auth', 'Paginator')", $result);
$this->assertContains("public \$helpers = array('Js', 'Time')", $result);
$this->assertContains("--actions--", $result);
$result = $this->Task->bake('Articles', null, $helpers, $components);
$expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'Controller' . DS . 'NoActions.ctp');
$this->assertTextEquals($expected, $result);

$result = $this->Task->bake('Articles', null, array(), array());
$expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'Controller' . DS . 'NoHelpersOrComponents.ctp');
$this->assertTextEquals($expected, $result);

$result = $this->Task->bake('Articles', 'scaffold', $helpers, $components);
$this->assertContains("class ArticlesController extends AppController", $result);
$this->assertContains("public \$scaffold", $result);
$this->assertNotContains('@property', $result);
$this->assertNotContains('helpers', $result);
$this->assertNotContains('components', $result);

$result = $this->Task->bake('Articles', '--actions--', array(), array());
$this->assertContains('class ArticlesController extends AppController', $result);
$this->assertSame(substr_count($result, '@property'), 2);
$this->assertContains("public \$components = array('Paginator')", $result);
$this->assertNotContains('helpers', $result);
$this->assertContains('--actions--', $result);
$expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'Controller' . DS . 'Scaffold.ctp');
$this->assertTextEquals($expected, $result);
}

/**
Expand Down Expand Up @@ -348,30 +336,10 @@ public function testBakeWithPlugin() {
*/
public function testBakeActionsUsingSessions() {
$result = $this->Task->bakeActions('BakeArticles', null, true);

$this->assertContains('function index() {', $result);
$this->assertContains('$this->BakeArticle->recursive = 0;', $result);
$this->assertContains("\$this->set('bakeArticles', \$this->Paginator->paginate());", $result);

$this->assertContains('function view($id = null)', $result);
$this->assertContains("throw new NotFoundException(__('Invalid bake article'));", $result);
$this->assertContains("\$options = array('conditions' => array('BakeArticle.' . \$this->BakeArticle->primaryKey => \$id));", $result);
$this->assertContains("\$this->set('bakeArticle', \$this->BakeArticle->find('first', \$options));", $result);

$this->assertContains('function add()', $result);
$this->assertContains("if (\$this->request->is('post'))", $result);
$this->assertContains('if ($this->BakeArticle->save($this->request->data))', $result);
$this->assertContains("\$this->Session->setFlash(__('The bake article has been saved'));", $result);

$this->assertContains('function edit($id = null)', $result);
$this->assertContains("\$this->Session->setFlash(__('The bake article could not be saved. Please, try again.'));", $result);

$this->assertContains('function delete($id = null)', $result);
$this->assertContains('if ($this->BakeArticle->delete())', $result);
$this->assertContains("\$this->Session->setFlash(__('Bake article deleted'));", $result);
$expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'Controller' . DS . 'ActionsUsingSessions.ctp');
$this->assertTextEquals($expected, $result);

$result = $this->Task->bakeActions('BakeArticles', 'admin_', true);

$this->assertContains('function admin_index() {', $result);
$this->assertContains('function admin_add()', $result);
$this->assertContains('function admin_view($id = null)', $result);
Expand All @@ -386,29 +354,8 @@ public function testBakeActionsUsingSessions() {
*/
public function testBakeActionsWithNoSessions() {
$result = $this->Task->bakeActions('BakeArticles', null, false);

$this->assertContains('function index() {', $result);
$this->assertContains('$this->BakeArticle->recursive = 0;', $result);
$this->assertContains("\$this->set('bakeArticles', \$this->Paginator->paginate());", $result);

$this->assertContains('function view($id = null)', $result);
$this->assertContains("throw new NotFoundException(__('Invalid bake article'));", $result);
$this->assertContains("\$this->set('bakeArticle', \$this->BakeArticle->find('first', \$options));", $result);

$this->assertContains('function add()', $result);
$this->assertContains("if (\$this->request->is('post'))", $result);
$this->assertContains('if ($this->BakeArticle->save($this->request->data))', $result);

$this->assertContains("\$this->flash(__('The bake article has been saved.'), array('action' => 'index'))", $result);

$this->assertContains('function edit($id = null)', $result);
$this->assertContains("\$this->BakeArticle->BakeTag->find('list')", $result);
$this->assertContains("\$this->set(compact('bakeTags'))", $result);

$this->assertContains('function delete($id = null)', $result);
$this->assertContains("\$this->request->onlyAllow('post', 'delete')", $result);
$this->assertContains('if ($this->BakeArticle->delete())', $result);
$this->assertContains("\$this->flash(__('Bake article deleted'), array('action' => 'index'))", $result);
$expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'Controller' . DS . 'ActionsWithNoSessions.ctp');
$this->assertTextEquals($expected, $result);
}

/**
Expand Down
91 changes: 91 additions & 0 deletions lib/Cake/Test/bake_compare/Controller/ActionsUsingSessions.ctp
@@ -0,0 +1,91 @@

/**
* index method
*
* @return void
*/
public function index() {
$this->BakeArticle->recursive = 0;
$this->set('bakeArticles', $this->Paginator->paginate());
}

/**
* view method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function view($id = null) {
if (!$this->BakeArticle->exists($id)) {
throw new NotFoundException(__('Invalid bake article'));
}
$options = array('conditions' => array('BakeArticle.' . $this->BakeArticle->primaryKey => $id));
$this->set('bakeArticle', $this->BakeArticle->find('first', $options));
}

/**
* add method
*
* @return void
*/
public function add() {
if ($this->request->is('post')) {
$this->BakeArticle->create();
if ($this->BakeArticle->save($this->request->data)) {
$this->Session->setFlash(__('The bake article has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The bake article could not be saved. Please, try again.'));
}
}
$bakeTags = $this->BakeArticle->BakeTag->find('list');
$this->set(compact('bakeTags'));
}

/**
* edit method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function edit($id = null) {
if (!$this->BakeArticle->exists($id)) {
throw new NotFoundException(__('Invalid bake article'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->BakeArticle->save($this->request->data)) {
$this->Session->setFlash(__('The bake article has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The bake article could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('BakeArticle.' . $this->BakeArticle->primaryKey => $id));
$this->request->data = $this->BakeArticle->find('first', $options);
}
$bakeTags = $this->BakeArticle->BakeTag->find('list');
$this->set(compact('bakeTags'));
}

/**
* delete method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function delete($id = null) {
$this->BakeArticle->id = $id;
if (!$this->BakeArticle->exists()) {
throw new NotFoundException(__('Invalid bake article'));
}
$this->request->onlyAllow('post', 'delete');
if ($this->BakeArticle->delete()) {
$this->Session->setFlash(__('The bake article has been deleted.'));
} else {
$this->Session->setFlash(__('The bake article could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}
84 changes: 84 additions & 0 deletions lib/Cake/Test/bake_compare/Controller/ActionsWithNoSessions.ctp
@@ -0,0 +1,84 @@

/**
* index method
*
* @return void
*/
public function index() {
$this->BakeArticle->recursive = 0;
$this->set('bakeArticles', $this->Paginator->paginate());
}

/**
* view method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function view($id = null) {
if (!$this->BakeArticle->exists($id)) {
throw new NotFoundException(__('Invalid bake article'));
}
$options = array('conditions' => array('BakeArticle.' . $this->BakeArticle->primaryKey => $id));
$this->set('bakeArticle', $this->BakeArticle->find('first', $options));
}

/**
* add method
*
* @return void
*/
public function add() {
if ($this->request->is('post')) {
$this->BakeArticle->create();
if ($this->BakeArticle->save($this->request->data)) {
return $this->flash(__('The bake article has been saved.'), array('action' => 'index'));
}
}
$bakeTags = $this->BakeArticle->BakeTag->find('list');
$this->set(compact('bakeTags'));
}

/**
* edit method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function edit($id = null) {
if (!$this->BakeArticle->exists($id)) {
throw new NotFoundException(__('Invalid bake article'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->BakeArticle->save($this->request->data)) {
return $this->flash(__('The bake article has been saved.'), array('action' => 'index'));
}
} else {
$options = array('conditions' => array('BakeArticle.' . $this->BakeArticle->primaryKey => $id));
$this->request->data = $this->BakeArticle->find('first', $options);
}
$bakeTags = $this->BakeArticle->BakeTag->find('list');
$this->set(compact('bakeTags'));
}

/**
* delete method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function delete($id = null) {
$this->BakeArticle->id = $id;
if (!$this->BakeArticle->exists()) {
throw new NotFoundException(__('Invalid bake article'));
}
$this->request->onlyAllow('post', 'delete');
if ($this->BakeArticle->delete()) {
return $this->flash(__('The bake article has been deleted.'), array('action' => 'index'));
} else {
return $this->flash(__('The bake article could not be deleted. Please, try again.'), array('action' => 'index'));
}
}
27 changes: 27 additions & 0 deletions lib/Cake/Test/bake_compare/Controller/NoActions.ctp
@@ -0,0 +1,27 @@
<?php
App::uses('AppController', 'Controller');
/**
* Articles Controller
*
* @property Article $Article
* @property AclComponent $Acl
* @property AuthComponent $Auth
* @property PaginatorComponent $Paginator
*/
class ArticlesController extends AppController {

/**
* Helpers
*
* @var array
*/
public $helpers = array('Js', 'Time');

/**
* Components
*
* @var array
*/
public $components = array('Acl', 'Auth', 'Paginator');

}

0 comments on commit 703d988

Please sign in to comment.