Skip to content

Commit

Permalink
Merge 20fcb8f into e01152f
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-1-anderson authored Jun 8, 2023
2 parents e01152f + 20fcb8f commit 173ca51
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 85 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"require": {
"php": ">=7.1.3",
"consolidation/output-formatters": "^4.3.1",
"consolidation/output-formatters": "dev-table-empty-message",
"psr/log": "^1 || ^2 || ^3",
"symfony/console": "^4.4.8 || ^5 || ^6",
"symfony/event-dispatcher": "^4.4.8 || ^5 || ^6",
Expand Down
139 changes: 63 additions & 76 deletions composer.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/Attributes/DefaultFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Attribute;
use Consolidation\AnnotatedCommand\Parser\CommandInfo;
use Consolidation\OutputFormatters\Options\FormatterOptions;

#[Attribute(Attribute::TARGET_METHOD)]
class DefaultFields
Expand All @@ -20,6 +21,6 @@ public function __construct(
public static function handle(\ReflectionAttribute $attribute, CommandInfo $commandInfo)
{
$args = $attribute->getArguments();
$commandInfo->addAnnotation('default-fields', $args['fields']);
$commandInfo->addAnnotation(FormatterOptions::DEFAULT_FIELDS, $args['fields']);
}
}
3 changes: 2 additions & 1 deletion src/Attributes/DefaultTableFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Attribute;
use Consolidation\AnnotatedCommand\Parser\CommandInfo;
use Consolidation\OutputFormatters\Options\FormatterOptions;

#[Attribute(Attribute::TARGET_METHOD)]
class DefaultTableFields
Expand All @@ -20,6 +21,6 @@ public function __construct(
public static function handle(\ReflectionAttribute $attribute, CommandInfo $commandInfo)
{
$args = $attribute->getArguments();
$commandInfo->addAnnotation('default-table-fields', $args['fields']);
$commandInfo->addAnnotation(FormatterOptions::DEFAULT_TABLE_FIELDS, $args['fields']);
}
}
3 changes: 2 additions & 1 deletion src/Attributes/FieldLabels.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Attribute;
use Consolidation\AnnotatedCommand\Parser\CommandInfo;
use Consolidation\OutputFormatters\Options\FormatterOptions;

#[Attribute(Attribute::TARGET_METHOD)]
class FieldLabels
Expand All @@ -20,6 +21,6 @@ public function __construct(
public static function handle(\ReflectionAttribute $attribute, CommandInfo $commandInfo)
{
$args = $attribute->getArguments();
$commandInfo->addAnnotation('field-labels', $args['labels']);
$commandInfo->addAnnotation(FormatterOptions::FIELD_LABELS, $args['labels']);
}
}
26 changes: 26 additions & 0 deletions src/Attributes/TableEmptyMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Consolidation\AnnotatedCommand\Attributes;

use Attribute;
use Consolidation\AnnotatedCommand\Parser\CommandInfo;
use Consolidation\OutputFormatters\Options\FormatterOptions;

#[Attribute(Attribute::TARGET_METHOD)]
class TableEmptyMessage
{
/**
* @param $labels
* An associative array of field names and labels for display.
*/
public function __construct(
public array $message
) {
}

public static function handle(\ReflectionAttribute $attribute, CommandInfo $commandInfo)
{
$args = $attribute->getArguments();
$commandInfo->addAnnotation(FormatterOptions::TABLE_EMPTY_MESSAGE, $args['message']);
}
}
40 changes: 40 additions & 0 deletions tests/FullStackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,46 @@ function testCommandsAndHooksIncludeAllPublicMethods()
$this->assertRunCommandViaApplicationContains('get:both', 'Here is some data.', [], 3);
}

/**
* @requires PHP 8.0
*/
function testAttributeCommands()
{
// First, search for commandfiles in the 'alpha'
// directory. Note that this same functionality
// is tested more thoroughly in isolation in
// testCommandFileDiscovery.php
$discovery = new CommandFileDiscovery();
$discovery
->setSearchPattern('*CommandFile.php')
->setIncludeFilesAtBase(false)
->setSearchLocations(['alpha']);

chdir(__DIR__);
$commandFiles = $discovery->discover('.', '\Consolidation\TestUtils');

$formatter = new FormatterManager();
$formatter->addDefaultFormatters();
$formatter->addDefaultSimplifiers();
$hookManager = new HookManager();
$commandProcessor = new CommandProcessor($hookManager);
$commandProcessor->setFormatterManager($formatter);

// Create a new factory, and load all of the files
// discovered above. The command factory class is
// tested in isolation in testAnnotatedCommandFactory.php,
// but this is the only place where
$factory = new AnnotatedCommandFactory();
$factory->setCommandProcessor($commandProcessor);
// $factory->addListener(...);
$factory->setIncludeAllPublicMethods(true);
$this->addDiscoveredCommands($factory, $commandFiles);

// Test to see if an empty table omits the table headers (labels header)
$this->assertRunCommandViaApplicationContains('tabularify apples peaches pumpkin pie', '----');
$this->assertRunCommandViaApplicationEquals('tabularify', 'There are no items to list', 0);
}

function testCommandsAndHooksWithBetaFolder()
{
// First, search for commandfiles in the 'alpha'
Expand Down
6 changes: 1 addition & 5 deletions tests/HelpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,7 @@ function testHelp()
$htmlEncodedHelpMessage = htmlspecialchars($expectedHelpMessage);

$outputFormattersVersion = ltrim(InstalledVersions::getPrettyVersion('consolidation/output-formatters'), 'v');
if (version_compare($outputFormattersVersion, '4.1.3', '>=')) {
$expectedFieldMessage = 'Select just one field, and force format to *string*.';
} else {
$expectedFieldMessage = "Select just one field, and force format to 'string'.";
}
$expectedFieldMessage = 'Select just one field, and force format to *string*.';

$expectedXML = <<<EOT
<?xml version="1.0" encoding="UTF-8"?>
Expand Down
24 changes: 24 additions & 0 deletions tests/src/alpha/AlphaCommandFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Consolidation\OutputFormatters\Options\FormatterOptions;

use Consolidation\TestUtils\ExampleCommandFile as ExampleAliasedClass;
use Consolidation\AnnotatedCommand\Attributes as CLI;

/**
* Test file used in the testCommandDiscovery() test.
Expand All @@ -26,6 +27,29 @@ class AlphaCommandFile implements CustomEventAwareInterface
{
use CustomEventAwareTrait;

/**
* This command prints its args in a table.
*/
#[CLI\Command(name: 'tabularify')]
#[CLI\Argument(name: 'args', description: 'Any number of arguments separated by spaces.')]
#[CLI\Usage(name: 'tabularify apples peaches pumpkin pie', description: 'Show a list of things in a table.')]
#[CLI\FieldLabels(labels: ['label' => 'Label', 'value' => 'Value'])]
#[CLI\TableEmptyMessage(message: 'There are no items to list')]
public function tabularify(array $args, $options = ['format' => 'table']): RowsOfFields
{
$labels = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eigth'];

$rows = [];
foreach ($args as $arg) {
$label = array_shift($labels);

if (!empty($label)) {
$rows[$label] = ['label' => $label, 'value' => $arg];
}
}
return new RowsOfFields($rows);
}

/**
* @command always:fail
*/
Expand Down

0 comments on commit 173ca51

Please sign in to comment.