/** * Sets up this test case * * @author Robert Lemke <*****@*****.**> */ protected function setUp() { $this->request = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Cli\Request::class, array('dummy')); $this->mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class); $this->mockObjectManager->expects($this->any())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Cli\Request::class)->will($this->returnValue($this->request)); $this->mockCommand = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Cli\Command::class, array(), array(), '', FALSE); $this->mockCommand->expects($this->any())->method('getControllerClassName')->will($this->returnValue('Tx_SomeExtensionName_Command_DefaultCommandController')); $this->mockCommand->expects($this->any())->method('getControllerCommandName')->will($this->returnValue('list')); $this->mockCommandManager = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Cli\CommandManager::class); $this->mockCommandManager->expects($this->any())->method('getCommandByIdentifier')->with('some_extension_name:default:list')->will($this->returnValue($this->mockCommand)); $this->mockReflectionService = $this->getMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class); $this->mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class); $this->requestBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Cli\RequestBuilder::class, array('dummy')); $this->requestBuilder->_set('objectManager', $this->mockObjectManager); $this->requestBuilder->_set('reflectionService', $this->mockReflectionService); $this->requestBuilder->_set('commandManager', $this->mockCommandManager); $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager); }
/** * @test * @author Bastian Waidelich <*****@*****.**> */ public function getArgumentDefinitionsReturnsArrayOfArgumentDefinitionIfCommandExpectsArguments() { $mockParameterReflection = $this->getMock(\TYPO3\CMS\Extbase\Reflection\ParameterReflection::class, array(), array(), '', FALSE); $mockReflectionService = $this->getMock(\TYPO3\CMS\Extbase\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->_set('reflectionService', $mockReflectionService); $this->mockMethodReflection->expects($this->atLeastOnce())->method('getParameters')->will($this->returnValue(array($mockParameterReflection))); $this->mockMethodReflection->expects($this->atLeastOnce())->method('getTagsValues')->will($this->returnValue(array('param' => array('@param $argument1 argument1 description', '@param $argument2 argument2 description')))); $mockCommandArgumentDefinition1 = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition::class, array(), array(), '', FALSE); $mockCommandArgumentDefinition2 = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition::class, array(), array(), '', FALSE); $this->mockObjectManager->expects($this->at(0))->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition::class, 'argument1', TRUE, 'argument1 description')->will($this->returnValue($mockCommandArgumentDefinition1)); $this->mockObjectManager->expects($this->at(1))->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition::class, 'argument2', FALSE, 'argument2 description')->will($this->returnValue($mockCommandArgumentDefinition2)); $expectedResult = array($mockCommandArgumentDefinition1, $mockCommandArgumentDefinition2); $actualResult = $this->command->getArgumentDefinitions(); $this->assertSame($expectedResult, $actualResult); }
/** * Render help text for a single command * * @param \TYPO3\CMS\Extbase\Mvc\Cli\Command $command * @return void */ protected function displayHelpForCommand(\TYPO3\CMS\Extbase\Mvc\Cli\Command $command) { $this->outputLine(); $this->outputLine($command->getShortDescription()); $this->outputLine(); $this->outputLine('COMMAND:'); $this->outputLine('%-2s%s', array(' ', $command->getCommandIdentifier())); $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->request->getCallingScript() . ' ' . $this->commandManager->getShortestIdentifierForCommand($command) . ($hasOptions ? ' [<options>]' : '') . $usage; $this->outputLine(); $this->outputLine('USAGE:'); $this->outputLine(' ' . $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('ARGUMENTS:'); foreach ($argumentDescriptions as $argumentDescription) { $this->outputLine($argumentDescription); } } if (count($optionDescriptions) > 0) { $this->outputLine(); $this->outputLine('OPTIONS:'); foreach ($optionDescriptions as $optionDescription) { $this->outputLine($optionDescription); } } if ($command->getDescription() !== '') { $this->outputLine(); $this->outputLine('DESCRIPTION:'); $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('SEE ALSO:'); foreach ($relatedCommandIdentifiers as $commandIdentifier) { $command = $this->commandManager->getCommandByIdentifier($commandIdentifier); $this->outputLine('%-2s%s (%s)', array(' ', $commandIdentifier, $command->getShortDescription())); } } $this->outputLine(); }
/** * Returns TRUE if the specified command identifier matches the identifier of the specified command. * This is the case, if the identifiers are the same or if at least the last two command parts match (case sensitive). * * @param \TYPO3\CMS\Extbase\Mvc\Cli\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(\TYPO3\CMS\Extbase\Mvc\Cli\Command $command, $commandIdentifier) { $commandIdentifierParts = explode(':', $command->getCommandIdentifier()); $searchedCommandIdentifierParts = explode(':', $commandIdentifier); $extensionKey = array_shift($commandIdentifierParts); if (count($searchedCommandIdentifierParts) === 3) { $searchedExtensionKey = array_shift($searchedCommandIdentifierParts); if ($searchedExtensionKey !== $extensionKey) { return FALSE; } } if (count($searchedCommandIdentifierParts) !== 2) { return FALSE; } return $searchedCommandIdentifierParts === $commandIdentifierParts; }