From 9076aef965184c39f1c45a8f7f7730ee9e3245df Mon Sep 17 00:00:00 2001 From: antograssiot Date: Sat, 30 May 2015 15:03:09 -0400 Subject: [PATCH] Add possibility to bake modeless form and tests --- src/Shell/Task/FormTask.php | 52 ++++++++++++++++ src/Shell/Task/TestTask.php | 5 +- src/Template/Bake/Form/form.ctp | 59 +++++++++++++++++++ tests/TestCase/Shell/BakeShellTest.php | 3 +- .../Shell/Task/SimpleBakeTaskTest.php | 1 + tests/TestCase/Shell/Task/TestTaskTest.php | 8 +++ 6 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 src/Shell/Task/FormTask.php create mode 100644 src/Template/Bake/Form/form.ctp diff --git a/src/Shell/Task/FormTask.php b/src/Shell/Task/FormTask.php new file mode 100644 index 000000000..e67ecc25c --- /dev/null +++ b/src/Shell/Task/FormTask.php @@ -0,0 +1,52 @@ + 'View\Helper', 'Shell' => 'Shell', 'Cell' => 'View\Cell', + 'Form' => 'Form' ]; /** @@ -71,6 +72,7 @@ class TestTask extends BakeTask 'helper' => 'Helper', 'shell' => 'Shell', 'cell' => 'Cell', + 'form' => 'Form' ]; /** @@ -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') { @@ -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.' diff --git a/src/Template/Bake/Form/form.ctp b/src/Template/Bake/Form/form.ctp new file mode 100644 index 000000000..b4351cd54 --- /dev/null +++ b/src/Template/Bake/Form/form.ctp @@ -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 +*/ +%> +\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; + } +} diff --git a/tests/TestCase/Shell/BakeShellTest.php b/tests/TestCase/Shell/BakeShellTest.php index c67fc6bdf..4d2e2f259 100644 --- a/tests/TestCase/Shell/BakeShellTest.php +++ b/tests/TestCase/Shell/BakeShellTest.php @@ -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(); @@ -143,6 +143,7 @@ public function testLoadTasksCoreAndApp() 'Bake.Component', 'Bake.Controller', 'Bake.Fixture', + 'Bake.Form', 'Bake.Helper', 'Bake.Model', 'Bake.Plugin', diff --git a/tests/TestCase/Shell/Task/SimpleBakeTaskTest.php b/tests/TestCase/Shell/Task/SimpleBakeTaskTest.php index 918fedf1f..f11d68d3e 100644 --- a/tests/TestCase/Shell/Task/SimpleBakeTaskTest.php +++ b/tests/TestCase/Shell/Task/SimpleBakeTaskTest.php @@ -191,6 +191,7 @@ public function subclassProvider() ['Bake\Shell\Task\ComponentTask'], ['Bake\Shell\Task\HelperTask'], ['Bake\Shell\Task\ShellTask'], + ['Bake\Shell\Task\FormTask'], ]; } diff --git a/tests/TestCase/Shell/Task/TestTaskTest.php b/tests/TestCase/Shell/Task/TestTaskTest.php index fcd1a7304..842ad0d0e 100644 --- a/tests/TestCase/Shell/Task/TestTaskTest.php +++ b/tests/TestCase/Shell/Task/TestTaskTest.php @@ -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); } /**