/**
  * Sets up this test case
  *
  */
 public function setUp()
 {
     $this->mockObjectManager = $this->createMock(\TYPO3\Flow\Object\ObjectManagerInterface::class);
     $this->mockObjectManager->expects($this->any())->method('getObjectNameByClassName')->with('Acme\\Test\\Command\\DefaultCommandController')->will($this->returnValue('Acme\\Test\\Command\\DefaultCommandController'));
     $this->mockCommand = $this->getMockBuilder(\TYPO3\Flow\Cli\Command::class)->disableOriginalConstructor()->getMock();
     $this->mockCommand->expects($this->any())->method('getControllerClassName')->will($this->returnValue('Acme\\Test\\Command\\DefaultCommandController'));
     $this->mockCommand->expects($this->any())->method('getControllerCommandName')->will($this->returnValue('list'));
     $this->mockCommandManager = $this->createMock(\TYPO3\Flow\Cli\CommandManager::class);
     $this->mockCommandManager->expects($this->any())->method('getCommandByIdentifier')->with('acme.test:default:list')->will($this->returnValue($this->mockCommand));
     $this->mockReflectionService = $this->createMock(\TYPO3\Flow\Reflection\ReflectionService::class);
     $this->requestBuilder = new \TYPO3\Flow\Cli\RequestBuilder();
     $this->requestBuilder->injectObjectManager($this->mockObjectManager);
     $this->requestBuilder->injectCommandManager($this->mockCommandManager);
 }
 /**
  * Sets up this test case
  *
  */
 public function setUp()
 {
     $this->mockObjectManager = $this->getMock('TYPO3\\Flow\\Object\\ObjectManagerInterface');
     $this->mockObjectManager->expects($this->any())->method('getObjectNameByClassName')->with('Acme\\Test\\Command\\DefaultCommandController')->will($this->returnValue('Acme\\Test\\Command\\DefaultCommandController'));
     $this->mockCommand = $this->getMock('TYPO3\\Flow\\Cli\\Command', array(), array(), '', FALSE);
     $this->mockCommand->expects($this->any())->method('getControllerClassName')->will($this->returnValue('Acme\\Test\\Command\\DefaultCommandController'));
     $this->mockCommand->expects($this->any())->method('getControllerCommandName')->will($this->returnValue('list'));
     $this->mockCommandManager = $this->getMock('TYPO3\\Flow\\Cli\\CommandManager');
     $this->mockCommandManager->expects($this->any())->method('getCommandByIdentifier')->with('acme.test:default:list')->will($this->returnValue($this->mockCommand));
     $this->mockReflectionService = $this->getMock('TYPO3\\Flow\\Reflection\\ReflectionService');
     $this->requestBuilder = new \TYPO3\Flow\Cli\RequestBuilder();
     $this->requestBuilder->injectObjectManager($this->mockObjectManager);
     $this->requestBuilder->injectReflectionService($this->mockReflectionService);
     $this->requestBuilder->injectCommandManager($this->mockCommandManager);
 }
 /**
  * @test
  */
 public function getArgumentDefinitionsReturnsArrayOfArgumentDefinitionIfCommandExpectsArguments()
 {
     $parameterReflection = $this->createMock(\TYPO3\Flow\Reflection\ParameterReflection::class, array(), array(array(__CLASS__, 'dummyMethod'), 'arg'));
     $mockReflectionService = $this->createMock(\TYPO3\Flow\Reflection\ReflectionService::class);
     $mockMethodParameters = array('argument1' => array('optional' => false), 'argument2' => array('optional' => true));
     $mockReflectionService->expects($this->atLeastOnce())->method('getMethodParameters')->will($this->returnValue($mockMethodParameters));
     $this->command->injectReflectionService($mockReflectionService);
     $this->methodReflection->expects($this->atLeastOnce())->method('getParameters')->will($this->returnValue(array($parameterReflection)));
     $this->methodReflection->expects($this->atLeastOnce())->method('getTagsValues')->will($this->returnValue(array('param' => array('@param $argument1 argument1 description', '@param $argument2 argument2 description'))));
     $expectedResult = array(new \TYPO3\Flow\Cli\CommandArgumentDefinition('argument1', true, 'argument1 description'), new \TYPO3\Flow\Cli\CommandArgumentDefinition('argument2', false, 'argument2 description'));
     $actualResult = $this->command->getArgumentDefinitions();
     $this->assertEquals($expectedResult, $actualResult);
 }
 /**
  * Returns TRUE if the specified command identifier matches the identifier of the specified command.
  * This is the case, if
  *  - the identifiers are the same
  *  - if at least the last two command parts match (case sensitive) or
  *  - if only the package key is specified and matches the commands package key
  * The first part (package key) can be reduced to the last subpackage, as long as the result is unambiguous.
  *
  * @param Command $command
  * @param string $commandIdentifier command identifier in the format foo:bar:baz (all lower case)
  * @return boolean TRUE if the specified command identifier matches this commands identifier
  */
 protected function commandMatchesIdentifier(Command $command, $commandIdentifier)
 {
     $commandIdentifierParts = explode(':', $command->getCommandIdentifier());
     $searchedCommandIdentifierParts = explode(':', $commandIdentifier);
     $packageKey = array_shift($commandIdentifierParts);
     $searchedCommandIdentifierPartsCount = count($searchedCommandIdentifierParts);
     if ($searchedCommandIdentifierPartsCount === 3 || $searchedCommandIdentifierPartsCount === 1) {
         $searchedPackageKey = array_shift($searchedCommandIdentifierParts);
         if ($searchedPackageKey !== $packageKey && substr($packageKey, -(strlen($searchedPackageKey) + 1)) !== '.' . $searchedPackageKey) {
             return false;
         }
     }
     if ($searchedCommandIdentifierPartsCount === 1) {
         return true;
     } elseif (count($searchedCommandIdentifierParts) !== 2) {
         return false;
     }
     return $searchedCommandIdentifierParts === $commandIdentifierParts;
 }
 /**
  * Calls the specified command method and passes the arguments.
  *
  * If the command returns a string, it is appended to the content in the
  * response object. If the command doesn't return anything and a valid
  * view exists, the view is rendered automatically.
  *
  * @return void
  */
 protected function callCommandMethod()
 {
     $preparedArguments = array();
     /** @var Argument $argument */
     foreach ($this->arguments as $argument) {
         $preparedArguments[] = $argument->getValue();
     }
     $command = new Command(get_class($this), $this->request->getControllerCommandName());
     if ($command->isDeprecated()) {
         $suggestedCommandMessage = '';
         $relatedCommandIdentifiers = $command->getRelatedCommandIdentifiers();
         if ($relatedCommandIdentifiers !== array()) {
             $suggestedCommandMessage = sprintf(', use the following command%s instead: %s', count($relatedCommandIdentifiers) > 1 ? 's' : '', implode(', ', $relatedCommandIdentifiers));
         }
         $this->outputLine('<b>Warning:</b> This command is <b>DEPRECATED</b>%s%s', array($suggestedCommandMessage, PHP_EOL));
     }
     $commandResult = call_user_func_array(array($this, $this->commandMethodName), $preparedArguments);
     if (is_string($commandResult) && strlen($commandResult) > 0) {
         $this->response->appendContent($commandResult);
     } elseif (is_object($commandResult) && method_exists($commandResult, '__toString')) {
         $this->response->appendContent((string) $commandResult);
     }
 }
 /**
  * Render help text for a single command
  *
  * @param Command $command
  * @return void
  */
 protected function displayHelpForCommand(Command $command)
 {
     $this->outputLine();
     $this->outputLine('<u>' . $command->getShortDescription() . '</u>');
     $this->outputLine();
     $this->outputLine('<b>COMMAND:</b>');
     $name = '<i>' . $command->getCommandIdentifier() . '</i>';
     $this->outputLine('%-2s%s', array(' ', $name));
     $commandArgumentDefinitions = $command->getArgumentDefinitions();
     $usage = '';
     $hasOptions = false;
     /** @var CommandArgumentDefinition $commandArgumentDefinition */
     foreach ($commandArgumentDefinitions as $commandArgumentDefinition) {
         if (!$commandArgumentDefinition->isRequired()) {
             $hasOptions = true;
         } else {
             $usage .= sprintf(' <%s>', strtolower(preg_replace('/([A-Z])/', ' $1', $commandArgumentDefinition->getName())));
         }
     }
     $usage = $this->commandManager->getShortestIdentifierForCommand($command) . ($hasOptions ? ' [<options>]' : '') . $usage;
     $this->outputLine();
     $this->outputLine('<b>USAGE:</b>');
     $this->outputLine('  %s %s', array($this->getFlowInvocationString(), $usage));
     $argumentDescriptions = array();
     $optionDescriptions = array();
     if ($command->hasArguments()) {
         foreach ($commandArgumentDefinitions as $commandArgumentDefinition) {
             $argumentDescription = $commandArgumentDefinition->getDescription();
             $argumentDescription = wordwrap($argumentDescription, $this->output->getMaximumLineLength() - 23, PHP_EOL . str_repeat(' ', 23), true);
             if ($commandArgumentDefinition->isRequired()) {
                 $argumentDescriptions[] = vsprintf('  %-20s %s', array($commandArgumentDefinition->getDashedName(), $argumentDescription));
             } else {
                 $optionDescriptions[] = vsprintf('  %-20s %s', array($commandArgumentDefinition->getDashedName(), $argumentDescription));
             }
         }
     }
     if (count($argumentDescriptions) > 0) {
         $this->outputLine();
         $this->outputLine('<b>ARGUMENTS:</b>');
         foreach ($argumentDescriptions as $argumentDescription) {
             $this->outputLine($argumentDescription);
         }
     }
     if (count($optionDescriptions) > 0) {
         $this->outputLine();
         $this->outputLine('<b>OPTIONS:</b>');
         foreach ($optionDescriptions as $optionDescription) {
             $this->outputLine($optionDescription);
         }
     }
     if ($command->getDescription() !== '') {
         $this->outputLine();
         $this->outputLine('<b>DESCRIPTION:</b>');
         $descriptionLines = explode(chr(10), $command->getDescription());
         foreach ($descriptionLines as $descriptionLine) {
             $this->outputLine('%-2s%s', array(' ', $descriptionLine));
         }
     }
     $relatedCommandIdentifiers = $command->getRelatedCommandIdentifiers();
     if ($relatedCommandIdentifiers !== array()) {
         $this->outputLine();
         $this->outputLine('<b>SEE ALSO:</b>');
         foreach ($relatedCommandIdentifiers as $commandIdentifier) {
             try {
                 $command = $this->commandManager->getCommandByIdentifier($commandIdentifier);
                 $this->outputLine('%-2s%s (%s)', array(' ', $commandIdentifier, $command->getShortDescription()));
             } catch (CommandException $exception) {
                 $this->outputLine('%-2s%s (%s)', array(' ', $commandIdentifier, '<i>Command not available</i>'));
             }
         }
     }
     $this->outputLine();
 }