Skip to content

Commit

Permalink
feat: Support hidden commands
Browse files Browse the repository at this point in the history
Closes #194.
  • Loading branch information
theofidry committed Nov 16, 2023
1 parent e7ca2dc commit 8dacd0b
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Bridge/Command/SymfonyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ protected function configure(): void

$this
->setDescription($configuration->getDescription())
->setHelp($configuration->getHelp());
->setHelp($configuration->getHelp())
->setHidden($configuration->isHidden());

$definition = $this->getDefinition();

Expand Down
8 changes: 7 additions & 1 deletion src/Command/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function __construct(
private readonly string $description,
private readonly string $help,
private readonly array $arguments = [],
private readonly array $options = []
private readonly array $options = [],
private readonly bool $hidden = false,
) {
}

Expand Down Expand Up @@ -68,4 +69,9 @@ public function getOptions(): array
{
return $this->options;
}

public function isHidden(): bool
{
return $this->hidden;
}
}
100 changes: 100 additions & 0 deletions tests/Application/Feature/ApplicationHiddenCommandSupportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

/*
* This file is part of the Fidry\Console package.
*
* (c) Théo FIDRY <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Fidry\Console\Tests\Application\Feature;

use Fidry\Console\Application\Application;
use Fidry\Console\Application\ApplicationRunner;
use Fidry\Console\Bridge\Application\SymfonyApplication;
use Fidry\Console\Bridge\Command\SymfonyCommand;
use Fidry\Console\Tests\Application\Fixture\ConfigurableCommandsApplication;
use Fidry\Console\Tests\Application\OutputAssertions;
use Fidry\Console\Tests\Command\Fixture\HiddenCommand;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\BufferedOutput;

#[CoversClass(ApplicationRunner::class)]
#[CoversClass(SymfonyApplication::class)]
#[CoversClass(SymfonyCommand::class)]
final class ApplicationHiddenCommandSupportTest extends TestCase
{
public function test_it_can_show_the_list_of_the_available_commands(): void
{
$input = new StringInput('list');
$output = new BufferedOutput();

ApplicationRunner::runApplication(
self::createApplication(),
$input,
$output,
);

$actual = $output->fetch();
$expected = <<<'EOT'
help message
Usage:
command [options] [arguments]
Options:
-h, --help Display help for the given command. When no command is given display help for the app:foo command
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi|--no-ansi Force (or disable --no-ansi) ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
completion Dump the shell completion script
help Display help for a command
list List commands

EOT;

OutputAssertions::assertSameOutput(
$expected,
$actual,
);
}

public function test_it_can_execute_the_hidden_command(): void
{
$input = new StringInput('app:hidden');
$output = new BufferedOutput();

ApplicationRunner::runApplication(
self::createApplication(),
$input,
$output,
);

$actual = $output->fetch();

OutputAssertions::assertSameOutput(
<<<'EOF'
OK

EOF,
$actual,
);
}

private static function createApplication(): Application
{
return new ConfigurableCommandsApplication([
new HiddenCommand(),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use Fidry\Console\Application\ApplicationRunner;
use Fidry\Console\Bridge\Application\SymfonyApplication;
use Fidry\Console\Bridge\Command\SymfonyCommand;
use Fidry\Console\Command\Configuration;
use Fidry\Console\Tests\Application\Fixture\SimpleApplication;
use Fidry\Console\Tests\Application\OutputAssertions;
use PHPUnit\Framework\Attributes\CoversClass;
Expand All @@ -24,6 +26,8 @@

#[CoversClass(ApplicationRunner::class)]
#[CoversClass(SymfonyApplication::class)]
#[CoversClass(SymfonyCommand::class)]
#[CoversClass(Configuration::class)]
final class ApplicationSimpleConfigSupportTest extends TestCase
{
public function test_it_can_show_the_list_of_the_available_commands(): void
Expand Down
9 changes: 7 additions & 2 deletions tests/Application/Fixture/ConfigurableCommandsApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
use Closure;
use Fidry\Console\Application\Application;
use Fidry\Console\Command\Command;
use function is_array;

final class ConfigurableCommandsApplication implements Application
{
/**
* @param Closure(): Command[] $commandsFactory
* @param Command[]|Closure(): Command[] $commandsFactory
*/
public function __construct(
private readonly Closure $commandsFactory
private readonly Closure|array $commandsFactory
) {
}

Expand All @@ -44,6 +45,10 @@ public function getLongVersion(): string

public function getCommands(): array
{
if (is_array($this->commandsFactory)) {
return $this->commandsFactory;
}

return ($this->commandsFactory)();
}

Expand Down
43 changes: 43 additions & 0 deletions tests/Command/Fixture/HiddenCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Fidry\Console package.
*
* (c) Théo FIDRY <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Fidry\Console\Tests\Command\Fixture;

use Fidry\Console\Command\Command;
use Fidry\Console\Command\Configuration;
use Fidry\Console\ExitCode;
use Fidry\Console\IO;

/**
* Most basic command: only has a name & description and does not do anything
* during execution.
*/
final class HiddenCommand implements Command
{
public function getConfiguration(): Configuration
{
return new Configuration(
'app:hidden',
'Description content',
'Command name: "%command.name%", command full name: "%command.full_name%"',
hidden: true,
);
}

public function execute(IO $io): int
{
$io->writeln('OK');

return ExitCode::SUCCESS;
}
}

0 comments on commit 8dacd0b

Please sign in to comment.