Skip to content

Commit b1163a5

Browse files
authored
Merge pull request #1057 from cakephp/fix-enumsupport
Fix type error when undefined fields are used
1 parent 4495dd8 commit b1163a5

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

phpcs.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0"?>
22
<ruleset name="CakePHP Bake">
3-
<config name="installed_paths" value="../../cakephp/cakephp-codesniffer" />
43
<arg value="ns"/>
54

65
<file>src/</file>

src/View/Helper/BakeHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public function columnData(string $field, TableSchema $schema): ?array
304304
public function enumSupportsLabel(string $field, TableSchema $schema): bool
305305
{
306306
$typeName = $schema->getColumnType($field);
307-
if (!str_starts_with($typeName, 'enum-')) {
307+
if (!$typeName || !str_starts_with($typeName, 'enum-')) {
308308
return false;
309309
}
310310
$type = TypeFactory::build($typeName);

tests/TestCase/View/Helper/BakeHelperTest.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
*/
1717
namespace Bake\Test\TestCase\View\Helper;
1818

19+
use Bake\Test\App\Model\Enum\BakeUserStatus;
1920
use Bake\View\BakeView;
2021
use Bake\View\Helper\BakeHelper;
22+
use Cake\Database\Type\EnumType;
2123
use Cake\Http\Response;
2224
use Cake\Http\ServerRequest as Request;
2325
use Cake\TestSuite\TestCase;
@@ -40,6 +42,7 @@ class BakeHelperTest extends TestCase
4042
'plugin.Bake.BakeComments',
4143
'plugin.Bake.BakeArticlesBakeTags',
4244
'plugin.Bake.BakeTags',
45+
'plugin.Bake.Users',
4346
];
4447

4548
/**
@@ -89,12 +92,12 @@ public function testAliasExtractorFilteredHasMany()
8992
'className' => '\Bake\Test\App\Model\Table\ArticlesTable',
9093
]);
9194
$this->BakeHelper = $this->getMockBuilder('Bake\View\Helper\BakeHelper')
92-
->disableOriginalConstructor()
93-
->onlyMethods(['_filterHasManyAssociationsAliases'])
94-
->getMock();
95+
->disableOriginalConstructor()
96+
->onlyMethods(['_filterHasManyAssociationsAliases'])
97+
->getMock();
9598
$this->BakeHelper->expects($this->once())
96-
->method('_filterHasManyAssociationsAliases')
97-
->with($table, ['ArticlesTags']);
99+
->method('_filterHasManyAssociationsAliases')
100+
->with($table, ['ArticlesTags']);
98101
$result = $this->BakeHelper->aliasExtractor($table, 'HasMany');
99102
$this->assertEmpty($result);
100103
}
@@ -107,7 +110,7 @@ public function testAliasExtractorFilteredHasMany()
107110
public function testAliasExtractorBelongsTo()
108111
{
109112
$table = $this->getTableLocator()->get('Articles', [
110-
'className' => '\Bake\Test\App\Model\Table\ArticlesTable',
113+
'className' => '\Bake\Test\App\Model\Table\ArticlesTable',
111114
]);
112115
$result = $this->BakeHelper->aliasExtractor($table, 'BelongsTo');
113116
$expected = ['authors'];
@@ -122,7 +125,7 @@ public function testAliasExtractorBelongsTo()
122125
public function testAliasExtractorBelongsToMany()
123126
{
124127
$table = $this->getTableLocator()->get('Articles', [
125-
'className' => '\Bake\Test\App\Model\Table\ArticlesTable',
128+
'className' => '\Bake\Test\App\Model\Table\ArticlesTable',
126129
]);
127130
$result = $this->BakeHelper->aliasExtractor($table, 'BelongsToMany');
128131
$expected = ['tags'];
@@ -185,4 +188,15 @@ public function testHasPlugin(): void
185188
$this->assertTrue($this->BakeHelper->hasPlugin('Bake'));
186189
$this->assertFalse($this->BakeHelper->hasPlugin('DebugKit'));
187190
}
191+
192+
public function testEnumSupportsLabel(): void
193+
{
194+
$table = $this->fetchTable('BakeUsers');
195+
$schema = $table->getSchema();
196+
$schema->setColumnType('status', EnumType::from(BakeUserStatus::class));
197+
198+
$this->assertTrue($this->BakeHelper->enumSupportsLabel('status', $schema));
199+
$this->assertFalse($this->BakeHelper->enumSupportsLabel('username', $schema));
200+
$this->assertFalse($this->BakeHelper->enumSupportsLabel('does_not_exist', $schema));
201+
}
188202
}

0 commit comments

Comments
 (0)