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

Allow baking enums. #968

Merged
merged 9 commits into from
Dec 27, 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
2 changes: 2 additions & 0 deletions docs/en/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
93 changes: 93 additions & 0 deletions src/Command/EnumCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
declare(strict_types=1);

/**
* 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
*/
namespace Bake\Command;

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

/**
* Enum code generator.
*/
class EnumCommand extends SimpleBakeCommand
{
/**
* Task name used in path generation.
*
* @var string
*/
public string $pathFragment = 'Model/Enum/';

/**
* @inheritDoc
*/
public function name(): string
{
return 'enum';
}

/**
* @inheritDoc
*/
public function fileName(string $name): string
{
return $name . '.php';
}

/**
* @inheritDoc
*/
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);
$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;
}
}
34 changes: 34 additions & 0 deletions templates/bake/Model/enum.twig
Original file line number Diff line number Diff line change
@@ -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));
}
}
71 changes: 71 additions & 0 deletions tests/TestCase/Command/EnumCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);

/**
* 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
*/
namespace Bake\Test\TestCase\Command;

use Bake\Test\TestCase\TestCase;
use Cake\Console\CommandInterface;
use Cake\Core\Plugin;

/**
* EnumCommandTest class
*/
class EnumCommandTest extends TestCase
{
/**
* setup method
*
* @return void
*/
public function setUp(): void
{
parent::setUp();
$this->_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);
}
}
21 changes: 21 additions & 0 deletions tests/comparisons/Model/testBakeEnum.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(Inflector::underscore($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(Inflector::underscore($this->name));
}
}
Loading