Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename option backed to backing-type defaulting to string. #970

Merged
merged 1 commit into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions src/Command/EnumCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use Cake\Console\Arguments;
use Cake\Console\ConsoleOptionParser;
use InvalidArgumentException;

Check failure on line 21 in src/Command/EnumCommand.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Type InvalidArgumentException is not used in this file.

/**
* Enum code generator.
Expand Down Expand Up @@ -66,13 +66,7 @@
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;
}
Expand All @@ -88,9 +82,11 @@
$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',
]);

Expand Down
2 changes: 1 addition & 1 deletion templates/bake/Model/enum.twig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}) }}

{{ DocBlock.classDescription(name, 'Enum', [])|raw }}
enum {{ name }}{{ backed ? ': ' ~ backed : ''}} implements EnumLabelInterface
enum {{ name }}: {{ backingType }} implements EnumLabelInterface
{
/**
* @return string
Expand Down
20 changes: 2 additions & 18 deletions tests/TestCase/Command/EnumCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/comparisons/Model/testBakeEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* FooBar Enum
*/
enum FooBar implements EnumLabelInterface
enum FooBar: string implements EnumLabelInterface
{
/**
* @return string
Expand Down
21 changes: 0 additions & 21 deletions tests/comparisons/Model/testBakeEnumBacked.php

This file was deleted.

Loading