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 22, 2023
1 parent 39fceee commit 75d452f
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 1 deletion.
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
54 changes: 54 additions & 0 deletions src/Command/EnumCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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;

/**
* 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';
}
}
30 changes: 30 additions & 0 deletions templates/bake/Model/enum.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{#
/**
* 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",
}) }}

{{ DocBlock.classDescription(name, 'Enum', [])|raw }}
enum {{ name }}: int
{
/**
* @return string
*/
public function label(): string
{
return mb_strtolower($this->name);
}
}
55 changes: 55 additions & 0 deletions tests/TestCase/Command/EnumCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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);
}
}
2 changes: 1 addition & 1 deletion tests/TestCase/Command/ModelCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1709,7 +1709,7 @@ public function testBakeTableConfig()
*
* @return void
*/
public function testBakeEntitySimple()
public function testBakeEnum()
{
$this->generatedFile = APP . 'Model/Entity/User.php';
$this->exec('bake model --no-test --no-fixture --no-table --no-fields --no-hidden users');
Expand Down
18 changes: 18 additions & 0 deletions tests/comparisons/Model/testBakeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);

namespace Bake\Test\App\Model\Enum;

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

0 comments on commit 75d452f

Please sign in to comment.