Skip to content

Commit

Permalink
Add a command to list available rules
Browse files Browse the repository at this point in the history
php bin/console progressive:rules

Available rules:
    enabled
    env
    partial
    roles
    unanimous
    users
  • Loading branch information
antfroger committed Jan 18, 2021
1 parent 9e78c16 commit ca3ecf1
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
59 changes: 59 additions & 0 deletions Command/ListRulesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Af\ProgressiveBundle\Command;

use Progressive\Rule\RuleInterface;
use Progressive\Rule\StoreInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ListRulesCommand extends Command
{
private $store;
protected static $defaultName = 'progressive:rules';

public function __construct(StoreInterface $store)
{
parent::__construct();

$this->store = $store;
}

protected function configure()
{
$this
->setDescription('List the available rules.')
->setHelp(
<<<'EOF'
The <info>%command.name%</info> command lists all the rules provided by Progressive:
<info>php %command.full_name%</info>
EOF
)
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$rules = $this->store->list();

if (empty($rules)) {
$output->writeln('<error>No available rules.</error>');

return Command::SUCCESS;
}

usort($rules, function (RuleInterface $a, RuleInterface $b) {
return strnatcmp($a->getName(), $b->getName());
});

$output->writeln('<comment>Available rules:</comment>');

foreach ($rules as $rule) {
$output->writeln(sprintf(' <info>%s</info>', $rule->getName()));
}

return Command::SUCCESS;
}
}
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,18 @@ If you specify the name of the feature, its configuration will be displayed:
env: dev, preprod
roles: ROLE_DEV
```

### `progressive:rules`

The command `progressive:rules` lists the rules provided by Progressive:

```bash
$ php bin/console progressive:rules
Available rules:
enabled
env
partial
roles
unanimous
users
```
4 changes: 4 additions & 0 deletions Resources/config/console.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
<argument type="service" id="Progressive\Progressive"/>
<tag name="console.command"/>
</service>
<service id="Af\ProgressiveBundle\Command\ListRulesCommand">
<argument type="service" id="Progressive\Rule\Store"/>
<tag name="console.command"/>
</service>
</services>
</container>
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
],
"require": {
"antfroger/progressive": "^1.0",
"antfroger/progressive": "^1.2",
"symfony/config": "^5.2",
"symfony/console": "^5.2",
"symfony/dependency-injection": "^5.2",
Expand Down
39 changes: 39 additions & 0 deletions tests/Command/ListRulesCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Af\ProgressiveBundle\Tests\Command;

use Af\ProgressiveBundle\Command\ListRulesCommand;
use PHPUnit\Framework\TestCase;
use Progressive\Rule\Store;
use Symfony\Component\Console\Tester\CommandTester;

class ListRulesCommandTest extends TestCase
{
private $tester;

protected function setUp(): void
{
$store = new Store();

$command = new ListRulesCommand($store);
$command->setName('progressive:rules');

$this->tester = new CommandTester($command);
}

public function testListRules(): void
{
$this->tester->execute([]);

$this->assertSame(
<<<EOF
Available rules:
enabled
partial
unanimous
EOF,
$this->tester->getDisplay()
);
}
}

0 comments on commit ca3ecf1

Please sign in to comment.