Skip to content

Commit

Permalink
Add a mechanism for determining the valid formats that work with any …
Browse files Browse the repository at this point in the history
…given command.
  • Loading branch information
greg-1-anderson committed Apr 29, 2016
1 parent 81ecc09 commit 2d7d52a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/AnnotatedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class AnnotatedCommand extends Command
protected $annotationData;
protected $usesInputInterface;
protected $usesOutputInterface;
protected $returnType;

public function __construct($name = null)
{
Expand Down Expand Up @@ -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;
Expand All @@ -95,6 +106,7 @@ public function setCommandInfo($commandInfo)
}
$this->setCommandArguments($commandInfo);
$this->setCommandOptions($commandInfo);
$this->setReturnType($commandInfo->getReturnType());
}

protected function setCommandArguments($commandInfo)
Expand Down
13 changes: 13 additions & 0 deletions src/Parser/CommandDocBlockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Consolidation\AnnotatedCommand\Parser;

use phpDocumentor\Reflection\DocBlock\Tag\ParamTag;
use phpDocumentor\Reflection\DocBlock\Tag\ReturnTag;
use phpDocumentor\Reflection\DocBlock;

/**
Expand All @@ -23,6 +24,7 @@ class CommandDocBlockParser
'command' => 'processCommandTag',
'name' => 'processCommandTag',
'param' => 'processArgumentTag',
'return' => 'processReturnTag',
'option' => 'processOptionTag',
'default' => 'processDefaultTag',
'aliases' => 'processAliases',
Expand Down Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions src/Parser/CommandInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class CommandInfo
*/
protected $methodName;

/**
* @var string
*/
protected $returnType;

/**
* Create a new CommandInfo class for a particular method of a class.
*
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/src/alpha/AlphaCommandFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => ''])
{
Expand Down
7 changes: 7 additions & 0 deletions tests/testCommandInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
19 changes: 18 additions & 1 deletion tests/testFullStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -59,14 +67,23 @@ 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) {
$this->application->add($command);
}
}

// 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', '');
Expand Down

0 comments on commit 2d7d52a

Please sign in to comment.