Skip to content

Commit

Permalink
Merge pull request #105 from antograssiot/bake-form
Browse files Browse the repository at this point in the history
Add possibility to bake modeless form
  • Loading branch information
lorenzo committed Jun 1, 2015
2 parents 9241859 + 9076aef commit da683a0
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 2 deletions.
52 changes: 52 additions & 0 deletions src/Shell/Task/FormTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 1.0.9
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Bake\Shell\Task;

/**
* ShellHelper code generator.
*/
class FormTask extends SimpleBakeTask
{
/**
* Task name used in path generation.
*
* @var string
*/
public $pathFragment = 'Form/';

/**
* {@inheritDoc}
*/
public function name()
{
return 'form';
}

/**
* {@inheritDoc}
*/
public function fileName($name)
{
return $name . 'Form.php';
}

/**
* {@inheritDoc}
*/
public function template()
{
return 'Form/form';
}
}
5 changes: 4 additions & 1 deletion src/Shell/Task/TestTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class TestTask extends BakeTask
'Helper' => 'View\Helper',
'Shell' => 'Shell',
'Cell' => 'View\Cell',
'Form' => 'Form'
];

/**
Expand All @@ -71,6 +72,7 @@ class TestTask extends BakeTask
'helper' => 'Helper',
'shell' => 'Shell',
'cell' => 'Cell',
'form' => 'Form'
];

/**
Expand Down Expand Up @@ -486,7 +488,7 @@ public function generateConstructor($type, $fullClassName)
$pre = "\$config = TableRegistry::exists('{$className}') ? [] : ['className' => '{$fullClassName}'];";
$construct = "TableRegistry::get('{$className}', \$config);";
}
if ($type === 'behavior' || $type === 'entity') {
if ($type === 'behavior' || $type === 'entity' || $type === 'form') {
$construct = "new {$className}();";
}
if ($type === 'helper') {
Expand Down Expand Up @@ -591,6 +593,7 @@ public function getOptionParser()
'Behavior', 'behavior',
'Shell', 'shell',
'Cell', 'cell',
'Form', 'form'
]
])->addArgument('name', [
'help' => 'An existing class to bake tests for.'
Expand Down
59 changes: 59 additions & 0 deletions src/Template/Bake/Form/form.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<%
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 1.0.9
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
%>
<?php
namespace <%= $namespace %>\Form;

use Cake\Form\Form;
use Cake\Form\Schema;
use Cake\Validation\Validator;

/**
* <%= $name %> Form.
*/
class <%= $name %>Form extends Form
{
/**
* Builds the schema for the modeless form
*
* @param Schema $schema From schema
* @return $this
*/
protected function _buildSchema(Schema $schema)
{
return $schema;
}

/**
* Form validation builder
*
* @param Validator $validator to use against the form
* @return Validator
*/
protected function _buildValidator(Validator $validator)
{
return $validator;
}

/**
* Defines what to execute once the From is being processed
*
* @return bool
*/
protected function _execute(array $data)
{
return true;
}
}
3 changes: 2 additions & 1 deletion tests/TestCase/Shell/BakeShellTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function testMain()
->method('out')
->with($this->stringContains('The following commands'));

$this->Shell->expects($this->exactly(16))
$this->Shell->expects($this->exactly(17))
->method('out');

$this->Shell->loadTasks();
Expand Down Expand Up @@ -143,6 +143,7 @@ public function testLoadTasksCoreAndApp()
'Bake.Component',
'Bake.Controller',
'Bake.Fixture',
'Bake.Form',
'Bake.Helper',
'Bake.Model',
'Bake.Plugin',
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase/Shell/Task/SimpleBakeTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ public function subclassProvider()
['Bake\Shell\Task\ComponentTask'],
['Bake\Shell\Task\HelperTask'],
['Bake\Shell\Task\ShellTask'],
['Bake\Shell\Task\FormTask'],
];
}

Expand Down
8 changes: 8 additions & 0 deletions tests/TestCase/Shell/Task/TestTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,14 @@ public function testGenerateConstructor()
$result = $this->Task->generateConstructor('entity', 'TestBake\Model\Entity\Article');
$expected = ["", "new Article();", ''];
$this->assertEquals($expected, $result);

$result = $this->Task->generateConstructor('form', 'TestBake\Form\ExampleForm');
$expected = [
'',
"new ExampleForm();",
''
];
$this->assertEquals($expected, $result);
}

/**
Expand Down

0 comments on commit da683a0

Please sign in to comment.