diff --git a/src/Tools/Console/Command/MappingDescribeCommand.php b/src/Tools/Console/Command/MappingDescribeCommand.php index bba382ad8da..f37639889ce 100644 --- a/src/Tools/Console/Command/MappingDescribeCommand.php +++ b/src/Tools/Console/Command/MappingDescribeCommand.php @@ -10,6 +10,8 @@ use Doctrine\ORM\Mapping\FieldMapping; use Doctrine\Persistence\Mapping\MappingException; use InvalidArgumentException; +use Symfony\Component\Console\Completion\CompletionInput; +use Symfony\Component\Console\Completion\CompletionSuggestions; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -19,6 +21,7 @@ use function array_filter; use function array_map; use function array_merge; +use function array_values; use function count; use function current; use function get_debug_type; @@ -32,6 +35,7 @@ use function preg_quote; use function print_r; use function sprintf; +use function str_replace; use const JSON_PRETTY_PRINT; use const JSON_THROW_ON_ERROR; @@ -73,6 +77,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 0; } + public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void + { + if ($input->mustSuggestArgumentValuesFor('entityName')) { + $entityManager = $this->getEntityManager($input); + + $entities = array_map( + static fn (string $fqcn) => str_replace('\\', '\\\\', $fqcn), + $this->getMappedEntities($entityManager), + ); + + $suggestions->suggestValues(array_values($entities)); + } + } + /** * Display all the mapping information for a single Entity. * diff --git a/tests/Tests/ORM/Tools/Console/Command/MappingDescribeCommandTest.php b/tests/Tests/ORM/Tools/Console/Command/MappingDescribeCommandTest.php index 30a1e43b90d..124874e0106 100644 --- a/tests/Tests/ORM/Tools/Console/Command/MappingDescribeCommandTest.php +++ b/tests/Tests/ORM/Tools/Console/Command/MappingDescribeCommandTest.php @@ -11,7 +11,9 @@ use Doctrine\Tests\OrmFunctionalTestCase; use InvalidArgumentException; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use Symfony\Component\Console\Application; +use Symfony\Component\Console\Tester\CommandCompletionTester; use Symfony\Component\Console\Tester\CommandTester; /** @@ -77,4 +79,37 @@ public function testShowSpecificNotFound(): void ], ); } + + /** + * @param string[] $input + * @param string[] $expectedSuggestions + */ + #[DataProvider('provideCompletionSuggestions')] + public function testComplete(array $input, array $expectedSuggestions): void + { + $this->useModelSet('cache'); + + parent::setUp(); + + $completionTester = new CommandCompletionTester(new MappingDescribeCommand(new SingleManagerProvider($this->_em))); + + $suggestions = $completionTester->complete($input); + + foreach ($expectedSuggestions as $expected) { + self::assertContains($expected, $suggestions); + } + } + + /** @return iterable */ + public static function provideCompletionSuggestions(): iterable + { + yield 'entityName' => [ + [''], + [ + 'Doctrine\\\\Tests\\\\Models\\\\Cache\\\\Restaurant', + 'Doctrine\\\\Tests\\\\Models\\\\Cache\\\\Beach', + 'Doctrine\\\\Tests\\\\Models\\\\Cache\\\\Bar', + ], + ]; + } }