diff --git a/docs/en/usage.rst b/docs/en/usage.rst index e25b3f02..29da6dcb 100644 --- a/docs/en/usage.rst +++ b/docs/en/usage.rst @@ -31,9 +31,11 @@ You can get the list of available bake command by running ``bin/cake bake --help - bake behavior - bake cell - bake command + - bake command_helper - bake component - bake controller - bake controller all + - bake enum - bake fixture - bake fixture all - bake form diff --git a/src/Command/EnumCommand.php b/src/Command/EnumCommand.php new file mode 100644 index 00000000..2118450e --- /dev/null +++ b/src/Command/EnumCommand.php @@ -0,0 +1,93 @@ + + */ + public function templateData(Arguments $arguments): array + { + $data = parent::templateData($arguments); + $data['backingType'] = $arguments->getOption('int') ? 'int' : 'string'; + + return $data; + } + + /** + * Gets the option parser instance and configures it. + * + * @param \Cake\Console\ConsoleOptionParser $parser The option parser to update. + * @return \Cake\Console\ConsoleOptionParser + */ + public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser + { + $parser = $this->_setCommonOptions($parser); + + $parser->setDescription( + 'Bake backed enums for use in models.' + )->addOption('int', [ + 'help' => 'Using backed enums with int instead of string as return type', + 'boolean' => true, + 'short' => 'i', + ]); + + return $parser; + } +} diff --git a/templates/bake/Model/enum.twig b/templates/bake/Model/enum.twig new file mode 100644 index 00000000..fe229b11 --- /dev/null +++ b/templates/bake/Model/enum.twig @@ -0,0 +1,34 @@ +{# +/** + * CakePHP(tm) : Rapid Development Framework (https://cakephp.org) + * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org) + * @link https://cakephp.org CakePHP(tm) Project + * @since 3.1.0 + * @license https://www.opensource.org/licenses/mit-license.php MIT License + */ +#} +{{ element('Bake.file_header', { + namespace: "#{namespace}\\Model\\Enum", + classImports: [ + 'Cake\\Database\\Type\\EnumLabelInterface', + 'Cake\\Utility\\Inflector', + ], +}) }} + +{{ DocBlock.classDescription(name, 'Enum', [])|raw }} +enum {{ name }}: {{ backingType }} implements EnumLabelInterface +{ + /** + * @return string + */ + public function label(): string + { + return Inflector::humanize(Inflector::underscore($this->name)); + } +} diff --git a/tests/TestCase/Command/EnumCommandTest.php b/tests/TestCase/Command/EnumCommandTest.php new file mode 100644 index 00000000..678aaf40 --- /dev/null +++ b/tests/TestCase/Command/EnumCommandTest.php @@ -0,0 +1,71 @@ +_compareBasePath = Plugin::path('Bake') . 'tests' . DS . 'comparisons' . DS . 'Model' . DS; + $this->setAppNamespace('Bake\Test\App'); + } + + /** + * test baking an enum + * + * @return void + */ + public function testBakeEnum() + { + $this->generatedFile = APP . 'Model/Enum/FooBar.php'; + $this->exec('bake enum FooBar', ['y']); + + $this->assertExitCode(CommandInterface::CODE_SUCCESS); + $this->assertFileExists($this->generatedFile); + $result = file_get_contents($this->generatedFile); + $this->assertSameAsFile(__FUNCTION__ . '.php', $result); + } + + /** + * test baking an enum with int return type + * + * @return void + */ + public function testBakeEnumBackedInt() + { + $this->generatedFile = APP . 'Model/Enum/FooBar.php'; + $this->exec('bake enum FooBar -i', ['y']); + + $this->assertExitCode(CommandInterface::CODE_SUCCESS); + $this->assertFileExists($this->generatedFile); + $result = file_get_contents($this->generatedFile); + $this->assertSameAsFile(__FUNCTION__ . '.php', $result); + } +} diff --git a/tests/comparisons/Model/testBakeEnum.php b/tests/comparisons/Model/testBakeEnum.php new file mode 100644 index 00000000..41c6faea --- /dev/null +++ b/tests/comparisons/Model/testBakeEnum.php @@ -0,0 +1,21 @@ +name)); + } +} diff --git a/tests/comparisons/Model/testBakeEnumBackedInt.php b/tests/comparisons/Model/testBakeEnumBackedInt.php new file mode 100644 index 00000000..c0c0648c --- /dev/null +++ b/tests/comparisons/Model/testBakeEnumBackedInt.php @@ -0,0 +1,21 @@ +name)); + } +}