Skip to content

Commit 8a5dfc8

Browse files
authored
Merge pull request #12037 from stlgaits/mapping-describe-completion
Add console completion for entityName param of orm:mapping:describe c…
2 parents 79e103c + 5afadf1 commit 8a5dfc8

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/Tools/Console/Command/MappingDescribeCommand.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Doctrine\ORM\Mapping\FieldMapping;
1111
use Doctrine\Persistence\Mapping\MappingException;
1212
use InvalidArgumentException;
13+
use Symfony\Component\Console\Completion\CompletionInput;
14+
use Symfony\Component\Console\Completion\CompletionSuggestions;
1315
use Symfony\Component\Console\Input\InputArgument;
1416
use Symfony\Component\Console\Input\InputInterface;
1517
use Symfony\Component\Console\Input\InputOption;
@@ -19,6 +21,7 @@
1921
use function array_filter;
2022
use function array_map;
2123
use function array_merge;
24+
use function array_values;
2225
use function count;
2326
use function current;
2427
use function get_debug_type;
@@ -32,6 +35,7 @@
3235
use function preg_quote;
3336
use function print_r;
3437
use function sprintf;
38+
use function str_replace;
3539

3640
use const JSON_PRETTY_PRINT;
3741
use const JSON_THROW_ON_ERROR;
@@ -73,6 +77,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7377
return 0;
7478
}
7579

80+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
81+
{
82+
if ($input->mustSuggestArgumentValuesFor('entityName')) {
83+
$entityManager = $this->getEntityManager($input);
84+
85+
$entities = array_map(
86+
static fn (string $fqcn) => str_replace('\\', '\\\\', $fqcn),
87+
$this->getMappedEntities($entityManager),
88+
);
89+
90+
$suggestions->suggestValues(array_values($entities));
91+
}
92+
}
93+
7694
/**
7795
* Display all the mapping information for a single Entity.
7896
*

tests/Tests/ORM/Tools/Console/Command/MappingDescribeCommandTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
use Doctrine\Tests\OrmFunctionalTestCase;
1212
use InvalidArgumentException;
1313
use PHPUnit\Framework\Attributes\CoversClass;
14+
use PHPUnit\Framework\Attributes\DataProvider;
1415
use Symfony\Component\Console\Application;
16+
use Symfony\Component\Console\Tester\CommandCompletionTester;
1517
use Symfony\Component\Console\Tester\CommandTester;
1618

1719
/**
@@ -77,4 +79,37 @@ public function testShowSpecificNotFound(): void
7779
],
7880
);
7981
}
82+
83+
/**
84+
* @param string[] $input
85+
* @param string[] $expectedSuggestions
86+
*/
87+
#[DataProvider('provideCompletionSuggestions')]
88+
public function testComplete(array $input, array $expectedSuggestions): void
89+
{
90+
$this->useModelSet('cache');
91+
92+
parent::setUp();
93+
94+
$completionTester = new CommandCompletionTester(new MappingDescribeCommand(new SingleManagerProvider($this->_em)));
95+
96+
$suggestions = $completionTester->complete($input);
97+
98+
foreach ($expectedSuggestions as $expected) {
99+
self::assertContains($expected, $suggestions);
100+
}
101+
}
102+
103+
/** @return iterable<string, array{string[], string[]}> */
104+
public static function provideCompletionSuggestions(): iterable
105+
{
106+
yield 'entityName' => [
107+
[''],
108+
[
109+
'Doctrine\\\\Tests\\\\Models\\\\Cache\\\\Restaurant',
110+
'Doctrine\\\\Tests\\\\Models\\\\Cache\\\\Beach',
111+
'Doctrine\\\\Tests\\\\Models\\\\Cache\\\\Bar',
112+
],
113+
];
114+
}
80115
}

0 commit comments

Comments
 (0)