1<?php 2 3/* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Symfony\Component\Console\Tester; 13 14use Symfony\Component\Console\Command\Command; 15use Symfony\Component\Console\Completion\CompletionInput; 16use Symfony\Component\Console\Completion\CompletionSuggestions; 17 18/** 19 * Eases the testing of command completion. 20 * 21 * @author Jérôme Tamarelle <jerome@tamarelle.net> 22 */ 23class CommandCompletionTester 24{ 25 private $command; 26 27 public function __construct(Command $command) 28 { 29 $this->command = $command; 30 } 31 32 /** 33 * Create completion suggestions from input tokens. 34 */ 35 public function complete(array $input): array 36 { 37 $currentIndex = \count($input); 38 if ('' === end($input)) { 39 array_pop($input); 40 } 41 array_unshift($input, $this->command->getName()); 42 43 $completionInput = CompletionInput::fromTokens($input, $currentIndex); 44 $completionInput->bind($this->command->getDefinition()); 45 $suggestions = new CompletionSuggestions(); 46 47 $this->command->complete($completionInput, $suggestions); 48 49 $options = []; 50 foreach ($suggestions->getOptionSuggestions() as $option) { 51 $options[] = '--'.$option->getName(); 52 } 53 54 return array_map('strval', array_merge($options, $suggestions->getValueSuggestions())); 55 } 56} 57