Skip to content

Commit

Permalink
Add test for int backed.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Mar 7, 2024
1 parent 5c069e1 commit 6f45ee0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
35 changes: 26 additions & 9 deletions tests/TestCase/View/Helper/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use TestApp\Model\Enum\ArticleStatus;
use TestApp\Model\Enum\ArticleStatusLabel;
use TestApp\Model\Enum\ArticleStatusLabelInterface;
use TestApp\Model\Enum\Priority;
use TestApp\Model\Table\ContactsTable;
use TestApp\Model\Table\ValidateUsersTable;
use TestApp\View\Form\StubContext;
Expand Down Expand Up @@ -3678,20 +3679,36 @@ public function testControlSelectType(): void
EnumType::from(ArticleStatusLabelInterface::class)
);

$article = $articlesTable->newEmptyEntity();
$this->Form->create($article);
$this->Form->create($articlesTable->newEmptyEntity());
$result = $this->Form->control('published');
$this->assertHtml($expected, $result);

$articlePriosTable = $this->getTableLocator()->get('ArticlePrios');
$articlePriosTable->getSchema()->setColumnType(
'priority',
EnumType::from(Priority::class)
);

$this->Form->create($articlePriosTable->newEmptyEntity());
// Select empty by default
$result = $this->Form->control('priority', ['empty' => ['' => ' - please select - '], 'default' => '']);
$expected = [
'div' => ['class' => 'input select'],
'label' => ['for' => 'published'],
'Published',
'label' => ['for' => 'priority'],
'Priority',
'/label',
'select' => ['name' => 'published', 'id' => 'published'],
['option' => ['value' => 'Y']],
'Is Published',
'select' => ['name' => 'priority', 'id' => 'priority'],
['option' => ['value' => '', 'selected' => 'selected']],
' - please select - ',
'/option',
['option' => ['value' => 'N', 'selected' => 'selected']],
'Is Unpublished',
['option' => ['value' => '1']],
'Is Low',
'/option',
['option' => ['value' => '2']],
'Is Medium',
'/option',
['option' => ['value' => '3']],
'Is High',
'/option',
'/select',
'/div',
Expand Down
20 changes: 20 additions & 0 deletions tests/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,26 @@
],
],
],
'article_prios' => [
'columns' => [
'id' => [
'type' => 'integer',
],
'priority' => [
'type' => 'integer',
'length' => 2,
'default' => 2, // Medium
],
],
'constraints' => [
'primary' => [
'type' => 'primary',
'columns' => [
'id',
],
],
],
],
'articles_translations' => [
'columns' => [
'id' => [
Expand Down
13 changes: 12 additions & 1 deletion tests/test_app/TestApp/Model/Enum/Priority.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,20 @@
*/
namespace TestApp\Model\Enum;

enum Priority: int
use Cake\Database\Type\EnumLabelInterface;
use Cake\Utility\Inflector;

enum Priority: int implements EnumLabelInterface
{
case Low = 1;
case Medium = 2;
case High = 3;

/**
* @return string
*/
public function label(): string
{
return 'Is ' . Inflector::humanize(Inflector::underscore($this->name));
}
}

0 comments on commit 6f45ee0

Please sign in to comment.