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

Fix up bake value defaults for fixtures with enums #982

Merged
merged 3 commits into from
Mar 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Command/FixtureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@
use Cake\Core\Configure;
use Cake\Core\Exception\CakeException;
use Cake\Database\Schema\TableSchemaInterface;
use Cake\Database\Type\EnumType;
use Cake\Database\TypeFactory;
use Cake\Datasource\ConnectionManager;
use Cake\Utility\Inflector;
use Cake\Utility\Text;
use DateTimeInterface;
use ReflectionEnum;

/**
* Task class for creating and updating fixtures files.
Expand Down Expand Up @@ -419,6 +422,33 @@
$insert = Text::uuid();
break;
}
if (str_starts_with($fieldInfo['type'], 'enum-')) {
$insert = null;
if ($fieldInfo['default'] || $fieldInfo['null'] === false) {
$dbType = TypeFactory::build($fieldInfo['type']);
if ($dbType instanceof EnumType) {
$class = $dbType->getEnumClassName();
$reflectionEnum = new ReflectionEnum($class);
$backingType = (string)$reflectionEnum->getBackingType();

if ($fieldInfo['default'] !== null) {
$insert = $fieldInfo['default'];
if ($backingType === 'int') {
$insert = (int)$insert;
}
} else {
$cases = $reflectionEnum->getCases();
if ($cases) {
$firstCase = array_shift($cases);

Check warning on line 442 in src/Command/FixtureCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/FixtureCommand.php#L440-L442

Added lines #L440 - L442 were not covered by tests
/** @var \BackedEnum $firstValue */
$firstValue = $firstCase->getValue();
$insert = $firstValue->value;

Check warning on line 445 in src/Command/FixtureCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/FixtureCommand.php#L444-L445

Added lines #L444 - L445 were not covered by tests
}
}
}
}
}

$record[$field] = $insert;
}
$records[] = $record;
Expand Down