Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/export command documentation #212

50 changes: 50 additions & 0 deletions app/src/Command/ExportListCommandsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Command;
olarno marked this conversation as resolved.
Show resolved Hide resolved

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ExportListCommandsCommand extends Command
{
protected static $defaultName = 'app:list-commands';
olarno marked this conversation as resolved.
Show resolved Hide resolved

protected function configure(): void
{
$this
->setDescription('List all available Symfony commands with their documentation.')
->setHelp('This command lists all available Symfony commands and their descriptions.');
olarno marked this conversation as resolved.
Show resolved Hide resolved
}

protected function execute(InputInterface $input, OutputInterface $output)
mpoiriert marked this conversation as resolved.
Show resolved Hide resolved
{
$commands = $this->getApplication()->all();
$commandList = [];

foreach ($commands as $name => $command) {
olarno marked this conversation as resolved.
Show resolved Hide resolved
$description = $this->sanitizeText($command->getDescription());
$synopsis = $this->sanitizeText($command->getSynopsis());
$helper = $this->sanitizeText($command->getHelp());

$commandList[$name]['description'] = $description;
mpoiriert marked this conversation as resolved.
Show resolved Hide resolved
$commandList[$name]['synopsis'] = $synopsis;
$commandList[$name]['helper'] = $helper;
}

$jsonOutput = json_encode($commandList, \JSON_PRETTY_PRINT);
$output->writeln($jsonOutput);
mpoiriert marked this conversation as resolved.
Show resolved Hide resolved

return 0;
mpoiriert marked this conversation as resolved.
Show resolved Hide resolved
}

private function sanitizeText($text)
mpoiriert marked this conversation as resolved.
Show resolved Hide resolved
{
$output = strip_tags($text);
$output = preg_replace('/\s+/', ' ', $output);
$output = str_replace('"', "'", $output);
$output = trim($output);

return $output;
}
}
70 changes: 70 additions & 0 deletions tests/Command/ExportListCommandsCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace App\Tests\Command;
mpoiriert marked this conversation as resolved.
Show resolved Hide resolved

use App\Command\ExportListCommandsCommand;
use App\Tests\TestCase;
use Draw\Component\Tester\Application\CommandDataTester;
use Draw\Component\Tester\Application\CommandTestTrait;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;

/**
* @covers \App\Command\ExportListCommandsCommand
*/
class ExportListCommandsCommandTest extends TestCase
{
use CommandTestTrait;

public function createCommand(): Command
{
return static::getService(ExportListCommandsCommand::class);
mpoiriert marked this conversation as resolved.
Show resolved Hide resolved
}

public function getCommandName(): string
{
return 'app:list-commands';
}

public static function provideTestArgument(): iterable
{
return [];
}

public static function provideTestOption(): iterable
{
yield [
'draw-execution-id',
null,
InputOption::VALUE_REQUIRED,
];

yield [
'draw-execution-ignore',
null,
InputOption::VALUE_NONE,
];

yield [
'aws-newest-instance-role',
null,
InputOption::VALUE_REQUIRED,
];
}

public function testExecute(): void
{
$this->execute([])
->test(
CommandDataTester::create()
->setExpectedDisplay($this->getDefaultExpectation())
);
}

private function getDefaultExpectation(): string
{
$jsonContent = file_get_contents(__DIR__.'/result/defaultExport.json');
mpoiriert marked this conversation as resolved.
Show resolved Hide resolved

return $jsonContent;
}
}
Loading