Skip to content

Commit

Permalink
Allow baking enums.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Dec 23, 2023
1 parent 1ac4f0e commit d10bd7c
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 4 deletions.
45 changes: 45 additions & 0 deletions src/Command/EnumCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
*/
namespace Bake\Command;

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

/**
* Enum code generator.
*/
Expand Down Expand Up @@ -51,4 +55,45 @@ public function template(): string
{
return 'Bake.Model/enum';
}

/**
* Get template data.
*
* @param \Cake\Console\Arguments $arguments The arguments for the command
* @return array
* @phpstan-return array<string, mixed>
*/
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`');

Check warning on line 72 in src/Command/EnumCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/EnumCommand.php#L72

Added line #L72 was not covered by tests
}

$data['backed'] = $backed;

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('backed', [
'help' => 'If using backed enums. Set to `string` or `int`.',
'short' => 'b',
]);

return $parser;
}
}
8 changes: 6 additions & 2 deletions templates/bake/Model/enum.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@
#}
{{ element('Bake.file_header', {
namespace: "#{namespace}\\Model\\Enum",
classImports: [
'Cake\\Database\\Type\\EnumLabelInterface',
'Cake\\Utility\\Inflector',
],
}) }}

{{ DocBlock.classDescription(name, 'Enum', [])|raw }}
enum {{ name }}: int
enum {{ name }}{{ backed ? ': ' ~ backed : ''}} implements EnumLabelInterface
{
/**
* @return string
*/
public function label(): string
{
return mb_strtolower($this->name);
return Inflector::humanize(mb_strtolower($this->name));
}
}
32 changes: 32 additions & 0 deletions tests/TestCase/Command/EnumCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,36 @@ public function testBakeEnum()
$result = file_get_contents($this->generatedFile);
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

/**
* 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
*
* @return void
*/
public function testBakeEnumBackedInt()
{
$this->generatedFile = APP . 'Model/Enum/FooBar.php';
$this->exec('bake enum FooBar --backed int', ['y']);

$this->assertExitCode(CommandInterface::CODE_SUCCESS);
$this->assertFileExists($this->generatedFile);
$result = file_get_contents($this->generatedFile);
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}
}
7 changes: 5 additions & 2 deletions tests/comparisons/Model/testBakeEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@

namespace Bake\Test\App\Model\Enum;

use Cake\Database\Type\EnumLabelInterface;
use Cake\Utility\Inflector;

/**
* FooBar Enum
*/
enum FooBar: int
enum FooBar implements EnumLabelInterface
{
/**
* @return string
*/
public function label(): string
{
return mb_strtolower($this->name);
return Inflector::humanize(mb_strtolower($this->name));
}
}
21 changes: 21 additions & 0 deletions tests/comparisons/Model/testBakeEnumBacked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);

namespace Bake\Test\App\Model\Enum;

use Cake\Database\Type\EnumLabelInterface;
use Cake\Utility\Inflector;

/**
* FooBar Enum
*/
enum FooBar: string implements EnumLabelInterface
{
/**
* @return string
*/
public function label(): string
{
return Inflector::humanize(mb_strtolower($this->name));
}
}
21 changes: 21 additions & 0 deletions tests/comparisons/Model/testBakeEnumBackedInt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);

namespace Bake\Test\App\Model\Enum;

use Cake\Database\Type\EnumLabelInterface;
use Cake\Utility\Inflector;

/**
* FooBar Enum
*/
enum FooBar: int implements EnumLabelInterface
{
/**
* @return string
*/
public function label(): string
{
return Inflector::humanize(mb_strtolower($this->name));
}
}

0 comments on commit d10bd7c

Please sign in to comment.