Skip to content

Commit 169034f

Browse files
authored
[FRAM-190] allow to setup all queues using queue:setup --all (#23)
* feat: allow to setup all queues using queue:setup --all (#FRAM-190) * ci: run code coverage on PHP 8.0 * ci: remove gearman on coverage tests
1 parent 9bf7199 commit 169034f

4 files changed

Lines changed: 66 additions & 15 deletions

File tree

.github/workflows/php.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,11 @@ jobs:
8686
with:
8787
timezoneLinux: "Europe/Paris"
8888

89-
- name: Install gearman PECL
90-
run: sudo apt-get install -y libgearman-dev
91-
9289
- name: Install PHP
9390
uses: shivammathur/setup-php@v2
9491
with:
9592
php-version: 7.4
96-
extensions: json, gearman, redis
93+
extensions: json, redis
9794
ini-values: date.timezone=Europe/Paris
9895

9996
- name: Install dependencies

src/Console/Command/Extension/DestinationExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public function createDestination(DestinationManager $destinationManager, InputI
4444
*
4545
* @param InputDefinition $definition
4646
*/
47-
public function configureDestinationOptions(InputDefinition $definition)
47+
public function configureDestinationOptions(InputDefinition $definition, bool $connectionIsRequired = true)
4848
{
49-
$definition->addArgument(new InputArgument('connection', InputArgument::REQUIRED, 'The name of connection'));
49+
$definition->addArgument(new InputArgument('connection', $connectionIsRequired ? InputArgument::REQUIRED : InputArgument::OPTIONAL, 'The name of connection'));
5050
$definition->addOption(new InputOption('queue', null, InputOption::VALUE_REQUIRED, 'The queues to listen on. can be separated by comma (only for reading).'));
5151
$definition->addOption(new InputOption('topic', null, InputOption::VALUE_REQUIRED, 'The topic to subscribe.'));
5252
}

src/Console/Command/SetupCommand.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Bdf\Queue\Console\Command;
44

55
use Bdf\Queue\Console\Command\Extension\DestinationExtension;
6+
use Bdf\Queue\Destination\DestinationInterface;
67
use Bdf\Queue\Destination\DestinationManager;
78
use Symfony\Component\Console\Attribute\AsCommand;
89
use Symfony\Component\Console\Command\Command;
@@ -44,11 +45,12 @@ public function __construct(DestinationManager $manager)
4445
*/
4546
protected function configure(): void
4647
{
47-
$this->configureDestinationOptions($this->getDefinition());
48+
$this->configureDestinationOptions($this->getDefinition(), false);
4849

4950
$this
5051
->setDescription('Declare or delete a queue from a connection.')
5152
->addOption('drop', null, InputOption::VALUE_NONE, 'Delete the queue from connection.')
53+
->addOption('all', null, InputOption::VALUE_NONE, 'Apply on all declared destinations.')
5254
;
5355
}
5456

@@ -57,19 +59,21 @@ protected function configure(): void
5759
*/
5860
protected function execute(InputInterface $input, OutputInterface $output): int
5961
{
60-
$destination = $this->createDestination($this->manager, $input);
62+
$isDrop = $input->getOption('drop');
6163

62-
if ($input->getOption('drop')) {
63-
$destination->destroy();
64+
foreach ($this->destinations($input) as $name => $destination) {
65+
if ($isDrop) {
66+
$destination->destroy();
6467

65-
$output->writeln(sprintf('The destination "<info>%s</info>" has been deleted.', $input->getArgument('connection')));
66-
} else {
67-
$destination->declare();
68+
$output->writeln(sprintf('The destination "<info>%s</info>" has been deleted.', $name));
69+
} else {
70+
$destination->declare();
6871

69-
$output->writeln(sprintf('The destination "<info>%s</info>" has been declared.', $input->getArgument('connection')));
72+
$output->writeln(sprintf('The destination "<info>%s</info>" has been declared.', $name));
73+
}
7074
}
7175

72-
return 0;
76+
return self::SUCCESS;
7377
}
7478

7579
/**
@@ -79,4 +83,19 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
7983
{
8084
$this->createAutocomplete($this->manager, $input, $suggestions);
8185
}
86+
87+
/**
88+
* @return iterable<string, DestinationInterface>
89+
*/
90+
private function destinations(InputInterface $input): iterable
91+
{
92+
if (!$input->getOption('all')) {
93+
yield $input->getArgument('connection') => $this->createDestination($this->manager, $input);
94+
return;
95+
}
96+
97+
foreach ($this->manager->destinationNames() as $name) {
98+
yield $name => $this->manager->create($name);
99+
}
100+
}
82101
}

tests/Console/Command/SetupCommandTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ public function test_declare_with_destination()
7979
$this->assertArrayHasKey('bar', $this->connection->storage()->queues);
8080
}
8181

82+
/**
83+
*
84+
*/
85+
public function test_declare_all()
86+
{
87+
$command = new SetupCommand($this->manager);
88+
$tester = new CommandTester($command);
89+
90+
$tester->execute([
91+
'--all' => true,
92+
]);
93+
94+
$this->assertMatchesRegularExpression('/^The destination "foo" has been declared/', $tester->getDisplay());
95+
$this->assertArrayHasKey('bar', $this->connection->storage()->queues);
96+
}
97+
8298
/**
8399
*
84100
*/
@@ -116,6 +132,25 @@ public function test_drop_queue()
116132
$this->assertArrayNotHasKey('bar', $this->connection->storage()->queues);
117133
}
118134

135+
/**
136+
*
137+
*/
138+
public function test_drop_all()
139+
{
140+
$this->connection->storage()->queues['bar'] = [];
141+
142+
$command = new SetupCommand($this->manager);
143+
$tester = new CommandTester($command);
144+
145+
$tester->execute([
146+
'--drop' => true,
147+
'--all' => true,
148+
]);
149+
150+
$this->assertMatchesRegularExpression('/^The destination "foo" has been deleted/', $tester->getDisplay());
151+
$this->assertArrayNotHasKey('bar', $this->connection->storage()->queues);
152+
}
153+
119154
/**
120155
* @dataProvider provideCompletionSuggestions
121156
*/

0 commit comments

Comments
 (0)