/** * @test */ public function ifCommandCantBeResolvedTheHelpScreenIsShown() { // The following call is only made to satisfy PHPUnit. For some weird reason PHPUnit complains that the // mocked method ("getObjectNameByClassName") does not exist _if the mock object is not used_. $this->mockObjectManager->getObjectNameByClassName('Acme\\Test\\Command\\DefaultCommandController'); $this->mockCommandManager->getCommandByIdentifier('acme.test:default:list'); $mockCommandManager = $this->getMock('TYPO3\\FLOW3\\Cli\\CommandManager'); $mockCommandManager->expects($this->any())->method('getCommandByIdentifier')->with('test:default:list')->will($this->throwException(new \TYPO3\FLOW3\Mvc\Exception\NoSuchCommandException())); $this->requestBuilder->injectCommandManager($mockCommandManager); $request = $this->requestBuilder->build('test:default:list'); $this->assertSame('TYPO3\\FLOW3\\Command\\HelpCommandController', $request->getControllerObjectName()); }
/** * Builds a CLI request object from a command line. * * The given command line may be a string (e.g. "mypackage:foo do-that-thing --force") or * an array consisting of the individual parts. The array must not include the script * name (like in $argv) but start with command right away. * * @param mixed $commandLine The command line, either as a string or as an array * @return \TYPO3\FLOW3\Cli\Request The CLI request as an object */ public function build($commandLine) { $request = new Request(); $request->setControllerObjectName('TYPO3\\FLOW3\\Command\\HelpCommandController'); $rawCommandLineArguments = is_array($commandLine) ? $commandLine : explode(' ', $commandLine); if (count($rawCommandLineArguments) === 0) { $request->setControllerCommandName('helpStub'); return $request; } $commandIdentifier = trim(array_shift($rawCommandLineArguments)); try { $command = $this->commandManager->getCommandByIdentifier($commandIdentifier); } catch (\TYPO3\FLOW3\Mvc\Exception\CommandException $exception) { $request->setArgument('exception', $exception); $request->setControllerCommandName('error'); return $request; } $controllerObjectName = $this->objectManager->getObjectNameByClassName($command->getControllerClassName()); $controllerCommandName = $command->getControllerCommandName(); $request->setControllerObjectName($controllerObjectName); $request->setControllerCommandName($controllerCommandName); list($commandLineArguments, $exceedingCommandLineArguments) = $this->parseRawCommandLineArguments($rawCommandLineArguments, $controllerObjectName, $controllerCommandName); $request->setArguments($commandLineArguments); $request->setExceedingArguments($exceedingCommandLineArguments); return $request; }
/** * @test */ public function getShortestIdentifierForCommandReturnsCompleteCommandIdentifierForCommandsWithTheSameControllerAndCommandName() { $mockCommand1 = $this->getMock('TYPO3\\FLOW3\\Cli\\Command', array(), array(), '', FALSE); $mockCommand1->expects($this->atLeastOnce())->method('getCommandIdentifier')->will($this->returnValue('package.key:controller:command')); $mockCommand2 = $this->getMock('TYPO3\\FLOW3\\Cli\\Command', array(), array(), '', FALSE); $mockCommand2->expects($this->atLeastOnce())->method('getCommandIdentifier')->will($this->returnValue('otherpackage.key:controller:command')); $mockCommands = array($mockCommand1, $mockCommand2); $this->commandManager->expects($this->atLeastOnce())->method('getAvailableCommands')->will($this->returnValue($mockCommands)); $this->assertSame('package.key:controller:command', $this->commandManager->getShortestIdentifierForCommand($mockCommand1)); $this->assertSame('otherpackage.key:controller:command', $this->commandManager->getShortestIdentifierForCommand($mockCommand2)); }
/** * Render help text for a single command * * @param \TYPO3\FLOW3\Cli\Command $command * @return void */ protected function displayHelpForCommand(\TYPO3\FLOW3\Cli\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; 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->getFlow3InvocationString(), $usage)); $argumentDescriptions = array(); $optionDescriptions = array(); if ($command->hasArguments()) { foreach ($commandArgumentDefinitions as $commandArgumentDefinition) { $argumentDescription = $commandArgumentDefinition->getDescription(); $argumentDescription = wordwrap($argumentDescription, self::MAXIMUM_LINE_LENGTH - 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 (\TYPO3\FLOW3\Mvc\Exception\CommandException $exception) { $this->outputLine('%-2s%s (%s)', array(' ', $commandIdentifier, '<i>Command not available</i>')); } } } $this->outputLine(); }