Skip to content

Commit

Permalink
bug #1448 [make:entity] only show supported types in cli wizard (#1448)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrushlow committed Feb 14, 2024
1 parent d848873 commit 415332d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 22 deletions.
61 changes: 39 additions & 22 deletions src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,27 +430,14 @@ private function printAvailableTypes(ConsoleStyle $io): void
{
$allTypes = $this->getTypesMap();

if ('Hyper' === getenv('TERM_PROGRAM')) {
$wizard = 'wizard 🧙';
} else {
$wizard = '\\' === \DIRECTORY_SEPARATOR ? 'wizard' : 'wizard 🧙';
}

$typesTable = [
'main' => [
'string' => [],
'string' => ['ascii_string'],
'text' => [],
'boolean' => [],
'integer' => ['smallint', 'bigint'],
'float' => [],
],
'relation' => [
'relation' => 'a '.$wizard.' will help you build the relation',
EntityRelation::MANY_TO_ONE => [],
EntityRelation::ONE_TO_MANY => [],
EntityRelation::MANY_TO_MANY => [],
EntityRelation::ONE_TO_ONE => [],
],
'array_object' => [
'array' => ['simple_array'],
'json' => [],
Expand All @@ -469,19 +456,30 @@ private function printAvailableTypes(ConsoleStyle $io): void

$printSection = static function (array $sectionTypes) use ($io, &$allTypes) {
foreach ($sectionTypes as $mainType => $subTypes) {
if (!\array_key_exists($mainType, $allTypes)) {
// The type is not a valid DBAL Type - don't show it as an option
continue;
}

foreach ($subTypes as $key => $potentialType) {
if (!\array_key_exists($potentialType, $allTypes)) {
// The type is not a valid DBAL Type - don't show it as an "or" option
unset($subTypes[$key]);
}

// Remove type as not to show it again in "Other Types"
unset($allTypes[$potentialType]);
}

// Remove type as not to show it again in "Other Types"
unset($allTypes[$mainType]);

$line = sprintf(' * <comment>%s</comment>', $mainType);

if (\is_string($subTypes) && $subTypes) {
$line .= sprintf(' or %s', $subTypes);
} elseif (\is_array($subTypes) && !empty($subTypes)) {
if (!empty($subTypes)) {
$line .= sprintf(' or %s', implode(' or ', array_map(
static fn ($subType) => sprintf('<comment>%s</comment>', $subType), $subTypes))
);

foreach ($subTypes as $subType) {
unset($allTypes[$subType]);
}
}

$io->writeln($line);
Expand All @@ -490,11 +488,30 @@ private function printAvailableTypes(ConsoleStyle $io): void
$io->writeln('');
};

$printRelationsSection = static function () use ($io) {
if ('Hyper' === getenv('TERM_PROGRAM')) {
$wizard = 'wizard 🧙';
} else {
$wizard = '\\' === \DIRECTORY_SEPARATOR ? 'wizard' : 'wizard 🧙';
}

$io->writeln(sprintf(' * <comment>relation</comment> a %s will help you build the relation', $wizard));

$relations = [EntityRelation::MANY_TO_ONE, EntityRelation::ONE_TO_MANY, EntityRelation::MANY_TO_MANY, EntityRelation::ONE_TO_ONE];
foreach ($relations as $relation) {
$line = sprintf(' * <comment>%s</comment>', $relation);

$io->writeln($line);
}

$io->writeln('');
};

$io->writeln('<info>Main Types</info>');
$printSection($typesTable['main']);

$io->writeln('<info>Relationships/Associations</info>');
$printSection($typesTable['relation']);
$printRelationsSection();

$io->writeln('<info>Array/Object Types</info>');
$printSection($typesTable['array_object']);
Expand Down
34 changes: 34 additions & 0 deletions tests/Maker/MakeEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,40 @@ public function getTestDetails(): \Generator
}),
];

yield 'it_only_shows_supported_types' => [$this->createMakeEntityTest()
->run(function (MakerTestRunner $runner) {
$output = $runner->runMaker([
// entity class name
'Developer',
// property name
'keyboards',
// field type
'?',
// use default type
'',
// default length
'',
// nullable
'',
// no more properties
'',
]);

self::assertStringContainsString('Main Types', $output);
self::assertStringContainsString('* string or ascii_string', $output);
self::assertStringContainsString('* ManyToOne', $output);

// get the dependencies installed in the test project (tmp/cache/TEST)
$installedVersions = require $runner->getPath('vendor/composer/installed.php');

if (!str_starts_with($installedVersions['versions']['doctrine/dbal']['version'], '3.')) {
self::assertStringNotContainsString('* object', $output);
} else {
self::assertStringContainsString('* object', $output);
}
}),
];

yield 'it_creates_a_new_class_and_api_resource' => [$this->createMakeEntityTest()
->addExtraDependencies('api')
->run(function (MakerTestRunner $runner) {
Expand Down

0 comments on commit 415332d

Please sign in to comment.