From 4b9d0e9fa363d64a288f72c0be024a67c0d45286 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sun, 24 Dec 2023 11:03:19 +0530 Subject: [PATCH] Rename option `backed` to `backing-type` defaulting to `string`. --- src/Command/EnumCommand.php | 16 ++++++-------- templates/bake/Model/enum.twig | 2 +- tests/TestCase/Command/EnumCommandTest.php | 20 ++---------------- tests/comparisons/Model/testBakeEnum.php | 2 +- .../comparisons/Model/testBakeEnumBacked.php | 21 ------------------- 5 files changed, 10 insertions(+), 51 deletions(-) delete mode 100644 tests/comparisons/Model/testBakeEnumBacked.php diff --git a/src/Command/EnumCommand.php b/src/Command/EnumCommand.php index 7ac9a796..fde837e6 100644 --- a/src/Command/EnumCommand.php +++ b/src/Command/EnumCommand.php @@ -66,13 +66,7 @@ public function template(): string public function templateData(Arguments $arguments): array { $data = parent::templateData($arguments); - - $backed = $arguments->getOption('backed'); - if ($backed && !in_array($backed, ['string', 'int'], true)) { - throw new InvalidArgumentException('Backed enums must be of type `string` or `int`'); - } - - $data['backed'] = $backed; + $data['backingType'] = $arguments->getOption('backing-type'); return $data; } @@ -88,9 +82,11 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar $parser = $this->_setCommonOptions($parser); $parser->setDescription( - 'Bake (backed) enums for use in models.' - )->addOption('backed', [ - 'help' => 'Valid options are `string` for string columns or `int` for tinyinteger columns.', + 'Bake enums for use in models.' + )->addOption('backing-type', [ + 'help' => 'The return type for the enum class', + 'default' => 'string', + 'choices' => ['string', 'int'], 'short' => 'b', ]); diff --git a/templates/bake/Model/enum.twig b/templates/bake/Model/enum.twig index 496780bf..fe229b11 100644 --- a/templates/bake/Model/enum.twig +++ b/templates/bake/Model/enum.twig @@ -22,7 +22,7 @@ }) }} {{ DocBlock.classDescription(name, 'Enum', [])|raw }} -enum {{ name }}{{ backed ? ': ' ~ backed : ''}} implements EnumLabelInterface +enum {{ name }}: {{ backingType }} implements EnumLabelInterface { /** * @return string diff --git a/tests/TestCase/Command/EnumCommandTest.php b/tests/TestCase/Command/EnumCommandTest.php index 8f475b78..c6d5bc72 100644 --- a/tests/TestCase/Command/EnumCommandTest.php +++ b/tests/TestCase/Command/EnumCommandTest.php @@ -54,30 +54,14 @@ public function testBakeEnum() } /** - * test baking an enum - * - * @return void - */ - public function testBakeEnumBacked() - { - $this->generatedFile = APP . 'Model/Enum/FooBar.php'; - $this->exec('bake enum FooBar --backed string', ['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 + * 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 --backed int', ['y']); + $this->exec('bake enum FooBar -b int', ['y']); $this->assertExitCode(CommandInterface::CODE_SUCCESS); $this->assertFileExists($this->generatedFile); diff --git a/tests/comparisons/Model/testBakeEnum.php b/tests/comparisons/Model/testBakeEnum.php index ec7c61ce..41c6faea 100644 --- a/tests/comparisons/Model/testBakeEnum.php +++ b/tests/comparisons/Model/testBakeEnum.php @@ -9,7 +9,7 @@ /** * FooBar Enum */ -enum FooBar implements EnumLabelInterface +enum FooBar: string implements EnumLabelInterface { /** * @return string diff --git a/tests/comparisons/Model/testBakeEnumBacked.php b/tests/comparisons/Model/testBakeEnumBacked.php deleted file mode 100644 index 41c6faea..00000000 --- a/tests/comparisons/Model/testBakeEnumBacked.php +++ /dev/null @@ -1,21 +0,0 @@ -name)); - } -}