-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from novaway/feature-listing-command
Add a command to list features state
- Loading branch information
Showing
9 changed files
with
310 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,4 @@ | |
<directory suffix=".php">src</directory> | ||
</include> | ||
</coverage> | ||
</phpunit> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the NovawayFeatureFlagBundle package. | ||
* (c) Novaway <https://github.com/novaway/NovawayFeatureFlagBundle> | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Novaway\Bundle\FeatureFlagBundle\Command; | ||
|
||
use Novaway\Bundle\FeatureFlagBundle\Model\Feature; | ||
use Novaway\Bundle\FeatureFlagBundle\Storage\StorageInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\Table; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
final class ListFeatureCommand extends Command | ||
{ | ||
private const FORMAT_CSV = 'csv'; | ||
private const FORMAT_JSON = 'json'; | ||
private const FORMAT_TABLE = 'table'; | ||
|
||
/** @var StorageInterface */ | ||
private $storage; | ||
|
||
public function __construct(StorageInterface $storage) | ||
{ | ||
parent::__construct('novaway:feature-flag:list'); | ||
|
||
$this->storage = $storage; | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->setDescription('List all features with their state'); | ||
$this->addOption('format', 'f', InputOption::VALUE_OPTIONAL, 'Output format', self::FORMAT_TABLE); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$features = $this->storage->all(); | ||
|
||
switch ($input->getOption('format')) { | ||
case self::FORMAT_CSV: | ||
$this->renderCsv($output, $features); | ||
break; | ||
|
||
case self::FORMAT_JSON: | ||
$this->renderJson($output, $features); | ||
break; | ||
|
||
case self::FORMAT_TABLE: | ||
$this->renderTable($output, $features); | ||
break; | ||
|
||
default: | ||
/* @phpstan-ignore-next-line */ | ||
$output->writeln("<error>Invalid format: {$input->getOption('format')}</error>"); | ||
|
||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
private function renderTable(OutputInterface $output, array $features): void | ||
{ | ||
$table = new Table($output); | ||
$table->setHeaders(['Name', 'Enabled', 'Description']); | ||
foreach ($features as $feature) { | ||
$table->addRow([ | ||
$feature->getKey(), | ||
$feature->isEnabled() ? 'Yes' : 'No', | ||
$feature->getDescription(), | ||
]); | ||
} | ||
|
||
$table->render(); | ||
} | ||
|
||
private function renderJson(OutputInterface $output, array $features): void | ||
{ | ||
$json = json_encode( | ||
array_map(static function (Feature $feature): array { | ||
return $feature->toArray(); | ||
}, $features), | ||
JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR | ||
); | ||
|
||
$output->writeln($json); | ||
} | ||
|
||
private function renderCsv(OutputInterface $output, array $features): void | ||
{ | ||
$output->writeln($this->getCsvLine(['Name', 'Enabled', 'Description'])); | ||
|
||
foreach ($features as $feature) { | ||
$output->writeln($this->getCsvLine($feature->toArray())); | ||
} | ||
} | ||
|
||
private function getCsvLine(array $columns): string | ||
{ | ||
$fp = fopen('php://temp', 'w+'); | ||
if (false === $fp) { | ||
throw new \RuntimeException('Unable to open temporary file'); | ||
} | ||
|
||
fputcsv($fp, $columns); | ||
|
||
rewind($fp); | ||
$data = fread($fp, 1048576); // 1MB | ||
if (false === $data) { | ||
throw new \RuntimeException('Unable to read temporary file'); | ||
} | ||
|
||
fclose($fp); | ||
|
||
return rtrim($data, PHP_EOL); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the NovawayFeatureFlagBundle package. | ||
* (c) Novaway <https://github.com/novaway/NovawayFeatureFlagBundle> | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Novaway\Bundle\FeatureFlagBundle\Tests\Functional\Command; | ||
|
||
use Novaway\Bundle\FeatureFlagBundle\Tests\Functional\WebTestCase; | ||
use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
use Symfony\Component\Console\Command\Command; | ||
|
||
final class ListFeatureCommandTest extends WebTestCase | ||
{ | ||
public function testCommandIsAvailable(): void | ||
{ | ||
$application = new Application(static::bootKernel()); | ||
$command = $application->find('novaway:feature-flag:list'); | ||
|
||
static::assertInstanceOf(Command::class, $command); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the NovawayFeatureFlagBundle package. | ||
* (c) Novaway <https://github.com/novaway/NovawayFeatureFlagBundle> | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Novaway\Bundle\FeatureFlagBundle\Tests\Unit\Command; | ||
|
||
use Novaway\Bundle\FeatureFlagBundle\Command\ListFeatureCommand; | ||
use Novaway\Bundle\FeatureFlagBundle\Storage\ArrayStorage; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
final class ListFeatureCommandTest extends TestCase | ||
{ | ||
private const TEST_DATA = [ | ||
'empty-features' => [ | ||
'features' => [], | ||
'output' => [ | ||
'table' => <<<OUTPUT | ||
+------+---------+-------------+ | ||
| Name | Enabled | Description | | ||
+------+---------+-------------+ | ||
OUTPUT, | ||
'json' => <<<JSON | ||
[] | ||
JSON, | ||
'csv' => <<<CSV | ||
Name,Enabled,Description | ||
CSV, | ||
], | ||
], | ||
'with-features' => [ | ||
'features' => [ | ||
'feature1' => [ | ||
'enabled' => true, | ||
'description' => 'Feature 1 description', | ||
], | ||
'feature2' => [ | ||
'enabled' => false, | ||
'description' => 'Feature 2 description', | ||
], | ||
'feature3' => [ | ||
'enabled' => true, | ||
'description' => 'Feature 3 description', | ||
], | ||
], | ||
'output' => [ | ||
'table' => <<<OUTPUT | ||
+----------+---------+-----------------------+ | ||
| Name | Enabled | Description | | ||
+----------+---------+-----------------------+ | ||
| feature1 | Yes | Feature 1 description | | ||
| feature2 | No | Feature 2 description | | ||
| feature3 | Yes | Feature 3 description | | ||
+----------+---------+-----------------------+ | ||
OUTPUT, | ||
'json' => <<<JSON | ||
{ | ||
"feature1": { | ||
"key": "feature1", | ||
"enabled": true, | ||
"description": "Feature 1 description" | ||
}, | ||
"feature2": { | ||
"key": "feature2", | ||
"enabled": false, | ||
"description": "Feature 2 description" | ||
}, | ||
"feature3": { | ||
"key": "feature3", | ||
"enabled": true, | ||
"description": "Feature 3 description" | ||
} | ||
} | ||
JSON, | ||
'csv' => <<<CSV | ||
Name,Enabled,Description | ||
feature1,1,"Feature 1 description" | ||
feature2,,"Feature 2 description" | ||
feature3,1,"Feature 3 description" | ||
CSV, | ||
], | ||
], | ||
]; | ||
|
||
public function testAnErrorOccuredIfInvalidFormatIsProvided(): void | ||
{ | ||
$commandTester = $this->createCommandTester(); | ||
$commandTester->execute(['--format' => 'invalid']); | ||
|
||
static::assertNotSame(0, $commandTester->getStatusCode()); | ||
static::assertSame(<<<OUTPUT | ||
Invalid format: invalid | ||
OUTPUT, $commandTester->getDisplay()); | ||
} | ||
|
||
/** | ||
* @dataProvider featuresProvider | ||
*/ | ||
public function testConfiguredFeaturesAreDisplayedInAskedFormat(array $features, string $outputFormat, string $expectedOutput): void | ||
{ | ||
$commandTester = $this->createCommandTester($features); | ||
$commandTester->execute(['--format' => $outputFormat]); | ||
|
||
static::assertSame(0, $commandTester->getStatusCode()); | ||
static::assertSame($expectedOutput, $commandTester->getDisplay()); | ||
} | ||
|
||
public function featuresProvider(): iterable | ||
{ | ||
foreach (self::TEST_DATA as $caseDescription => $testData) { | ||
foreach ($testData['output'] as $format => $expectedOutput) { | ||
yield "$caseDescription in $format format" => [$testData['features'], $format, $expectedOutput]; | ||
} | ||
} | ||
} | ||
|
||
private function createCommandTester(array $features = []): CommandTester | ||
{ | ||
$storage = new ArrayStorage($features); | ||
$command = new ListFeatureCommand($storage); | ||
|
||
return new CommandTester($command); | ||
} | ||
} |