From 2d7d52a4b585293d6f5a98ad308f7809d958980b Mon Sep 17 00:00:00 2001 From: Greg Anderson Date: Fri, 29 Apr 2016 15:44:30 -0700 Subject: [PATCH] Add a mechanism for determining the valid formats that work with any given command. --- src/AnnotatedCommand.php | 12 ++++++++++++ src/Parser/CommandDocBlockParser.php | 13 +++++++++++++ src/Parser/CommandInfo.php | 16 ++++++++++++++++ tests/src/alpha/AlphaCommandFile.php | 1 + tests/testCommandInfo.php | 7 +++++++ tests/testFullStack.php | 19 ++++++++++++++++++- 6 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/AnnotatedCommand.php b/src/AnnotatedCommand.php index 7509ed6..8f09995 100644 --- a/src/AnnotatedCommand.php +++ b/src/AnnotatedCommand.php @@ -31,6 +31,7 @@ class AnnotatedCommand extends Command protected $annotationData; protected $usesInputInterface; protected $usesOutputInterface; + protected $returnType; public function __construct($name = null) { @@ -78,6 +79,16 @@ public function getCommandProcessor() return $this->commandProcessor; } + public function getReturnType() + { + return $this->returnType; + } + + public function setReturnType($returnType) + { + $this->returnType = $returnType; + } + public function setAnnotationData($annotationData) { $this->annotationData = $annotationData; @@ -95,6 +106,7 @@ public function setCommandInfo($commandInfo) } $this->setCommandArguments($commandInfo); $this->setCommandOptions($commandInfo); + $this->setReturnType($commandInfo->getReturnType()); } protected function setCommandArguments($commandInfo) diff --git a/src/Parser/CommandDocBlockParser.php b/src/Parser/CommandDocBlockParser.php index 700f515..2dcee76 100644 --- a/src/Parser/CommandDocBlockParser.php +++ b/src/Parser/CommandDocBlockParser.php @@ -2,6 +2,7 @@ namespace Consolidation\AnnotatedCommand\Parser; use phpDocumentor\Reflection\DocBlock\Tag\ParamTag; +use phpDocumentor\Reflection\DocBlock\Tag\ReturnTag; use phpDocumentor\Reflection\DocBlock; /** @@ -23,6 +24,7 @@ class CommandDocBlockParser 'command' => 'processCommandTag', 'name' => 'processCommandTag', 'param' => 'processArgumentTag', + 'return' => 'processReturnTag', 'option' => 'processOptionTag', 'default' => 'processDefaultTag', 'aliases' => 'processAliases', @@ -104,6 +106,17 @@ protected function processArgumentTag($tag) $this->commandInfo->arguments()->add($variableName, $description); } + /** + * Store the data from a @return annotation in our argument descriptions. + */ + protected function processReturnTag($tag) + { + if (!$tag instanceof ReturnTag) { + return; + } + $this->commandInfo->setReturnType($tag->getType()); + } + /** * Given a docblock description in the form "$variable description", * return the variable name and description via the 'match' parameter. diff --git a/src/Parser/CommandInfo.php b/src/Parser/CommandInfo.php index bff224f..4cd6542 100644 --- a/src/Parser/CommandInfo.php +++ b/src/Parser/CommandInfo.php @@ -64,6 +64,11 @@ class CommandInfo */ protected $methodName; + /** + * @var string + */ + protected $returnType; + /** * Create a new CommandInfo class for a particular method of a class. * @@ -113,6 +118,17 @@ public function setName($name) $this->name = $name; } + public function getReturnType() + { + $this->parseDocBlock(); + return $this->returnType; + } + + public function setReturnType($returnType) + { + $this->returnType = $returnType; + } + /** * Get any annotations included in the docblock comment for the * implementation method of this command that are not already diff --git a/tests/src/alpha/AlphaCommandFile.php b/tests/src/alpha/AlphaCommandFile.php index 3e2a04b..bb36150 100644 --- a/tests/src/alpha/AlphaCommandFile.php +++ b/tests/src/alpha/AlphaCommandFile.php @@ -57,6 +57,7 @@ public function exampleMessage() * @usage try:formatters --format=csv * @usage try:formatters --fields=first,third * @usage try:formatters --fields=III,II + * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields */ public function exampleTable($options = ['format' => 'table', 'fields' => '']) { diff --git a/tests/testCommandInfo.php b/tests/testCommandInfo.php index d9aa68a..29fb2eb 100644 --- a/tests/testCommandInfo.php +++ b/tests/testCommandInfo.php @@ -51,4 +51,11 @@ function testParsing() $commandInfo->options()->getDescription('negate') ); } + + function testReturnValue() + { + $commandInfo = new CommandInfo('\Consolidation\TestUtils\alpha\AlphaCommandFile', 'exampleTable'); + $this->assertEquals('example:table', $commandInfo->getName()); + $this->assertEquals('\Consolidation\OutputFormatters\StructuredData\RowsOfFields', $commandInfo->getReturnType()); + } } diff --git a/tests/testFullStack.php b/tests/testFullStack.php index b367eb6..aca4a2f 100644 --- a/tests/testFullStack.php +++ b/tests/testFullStack.php @@ -30,6 +30,14 @@ function setup() { $this->application->setAutoExit(false); } + function testValidFormats() + { + $formatter = new FormatterManager(); + $commandInfo = new CommandInfo('\Consolidation\TestUtils\alpha\AlphaCommandFile', 'exampleTable'); + $this->assertEquals('example:table', $commandInfo->getName()); + $this->assertEquals('\Consolidation\OutputFormatters\StructuredData\RowsOfFields', $commandInfo->getReturnType()); + } + function testCommandsAndHooks() { // First, search for commandfiles in the 'alpha' @@ -59,7 +67,9 @@ function testCommandsAndHooks() // $factory->addListener(...); foreach ($commandFiles as $path => $commandClass) { $this->assertFileExists($path); - include $path; + if (!class_exists($commandClass)) { + include $path; + } $commandInstance = new $commandClass(); $commandList = $factory->createCommandsFromClass($commandInstance); foreach ($commandList as $command) { @@ -67,6 +77,13 @@ function testCommandsAndHooks() } } + // Fetch a reference to the 'example:table' command and test its valid format types + $exampleTableCommand = $this->application->find('example:table'); + $returnType = $exampleTableCommand->getReturnType(); + $this->assertEquals('\Consolidation\OutputFormatters\StructuredData\RowsOfFields', $returnType); + $validFormats = $formatter->validFormats($returnType); + $this->assertEquals('csv,json,list,php,print-r,sections,table,var_export,yaml', implode(',', $validFormats)); + // Control: run commands without hooks. $this->assertRunCommandViaApplicationEquals('always:fail', 'This command always fails.', 13); $this->assertRunCommandViaApplicationEquals('simulated:status', '');