-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a command to list available rules
php bin/console progressive:rules Available rules: enabled env partial roles unanimous users
- Loading branch information
Showing
5 changed files
with
118 additions
and
1 deletion.
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
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; | ||
} | ||
} |
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,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() | ||
); | ||
} | ||
} |