Skip to content

Commit 75ad854

Browse files
committed
Provide more usage info in store setup and drop command
1 parent ef7781f commit 75ad854

File tree

4 files changed

+67
-11
lines changed

4 files changed

+67
-11
lines changed

src/store/src/Command/DropStoreCommand.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ public function __construct(
4242
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
4343
{
4444
if ($input->mustSuggestArgumentValuesFor('store')) {
45-
$suggestions->suggestValues(array_keys($this->stores->getProvidedServices()));
45+
$suggestions->suggestValues($this->getStoreNames());
4646
}
4747
}
4848

4949
protected function configure(): void
5050
{
5151
$this
52-
->addArgument('store', InputArgument::REQUIRED, 'Name of the store to drop')
52+
->addArgument('store', InputArgument::REQUIRED, 'Service name of the store to drop')
5353
->addOption('force', 'f', InputOption::VALUE_NONE, 'Force dropping the store even if it contains messages')
5454
->setHelp(<<<EOF
5555
The <info>%command.name%</info> command drops the stores:
@@ -66,9 +66,13 @@ protected function configure(): void
6666

6767
protected function execute(InputInterface $input, OutputInterface $output): int
6868
{
69+
if (0 === \count($this->getStoreNames())) {
70+
throw new RuntimeException('No store is configured to be dropped.');
71+
}
72+
6973
$storeName = $input->getArgument('store');
7074
if (!$this->stores->has($storeName)) {
71-
throw new RuntimeException(\sprintf('The "%s" store does not exist.', $storeName));
75+
throw new RuntimeException(\sprintf('The "%s" store does not exist, use "%s".', $storeName, implode('", "', $this->getStoreNames())));
7276
}
7377

7478
$store = $this->stores->get($storeName);
@@ -97,4 +101,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
97101

98102
return Command::SUCCESS;
99103
}
104+
105+
/**
106+
* @return string[]
107+
*/
108+
private function getStoreNames(): array
109+
{
110+
return array_keys($this->stores->getProvidedServices());
111+
}
100112
}

src/store/src/Command/SetupStoreCommand.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ public function __construct(
4343
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
4444
{
4545
if ($input->mustSuggestArgumentValuesFor('store')) {
46-
$suggestions->suggestValues(array_keys($this->stores->getProvidedServices()));
46+
$suggestions->suggestValues($this->getStoreNames());
4747
}
4848
}
4949

5050
protected function configure(): void
5151
{
5252
$this
53-
->addArgument('store', InputArgument::REQUIRED, 'Name of the store to setup')
53+
->addArgument('store', InputArgument::REQUIRED, 'Service name of the store to setup')
5454
->setHelp(<<<EOF
5555
The <info>%command.name%</info> command setups the stores:
5656
@@ -66,9 +66,13 @@ protected function configure(): void
6666

6767
protected function execute(InputInterface $input, OutputInterface $output): int
6868
{
69+
if (0 === \count($this->getStoreNames())) {
70+
throw new RuntimeException('No store is configured to be set up.');
71+
}
72+
6973
$storeName = $input->getArgument('store');
7074
if (!$this->stores->has($storeName)) {
71-
throw new RuntimeException(\sprintf('The "%s" store does not exist.', $storeName));
75+
throw new RuntimeException(\sprintf('The "%s" store does not exist, use "%s".', $storeName, implode('", "', $this->getStoreNames())));
7276
}
7377

7478
$store = $this->stores->get($storeName);
@@ -91,4 +95,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9195

9296
return Command::SUCCESS;
9397
}
98+
99+
/**
100+
* @return string[]
101+
*/
102+
private function getStoreNames(): array
103+
{
104+
return array_keys($this->stores->getProvidedServices());
105+
}
94106
}

src/store/tests/Command/DropStoreCommandTest.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,34 @@ public function testCommandIsConfigured()
3232
$this->assertTrue($definition->hasArgument('store'));
3333

3434
$storeArgument = $definition->getArgument('store');
35-
$this->assertSame('Name of the store to drop', $storeArgument->getDescription());
35+
$this->assertSame('Service name of the store to drop', $storeArgument->getDescription());
3636
$this->assertTrue($storeArgument->isRequired());
3737
}
3838

39-
public function testCommandCannotDropUndefinedStore()
39+
public function testCommandCannotDropWithoutStores()
4040
{
4141
$command = new DropStoreCommand(new ServiceLocator([]));
4242

4343
$tester = new CommandTester($command);
4444

4545
$this->expectException(RuntimeException::class);
46-
$this->expectExceptionMessage('The "foo" store does not exist.');
46+
$this->expectExceptionMessage('No store is configured to be dropped.');
47+
$this->expectExceptionCode(0);
48+
$tester->execute([
49+
'store' => 'foo',
50+
]);
51+
}
52+
53+
public function testCommandCannotDropUndefinedStore()
54+
{
55+
$command = new DropStoreCommand(new ServiceLocator([
56+
'bar' => fn (): object => $this->createMock(ManagedStoreInterface::class),
57+
]));
58+
59+
$tester = new CommandTester($command);
60+
61+
$this->expectException(RuntimeException::class);
62+
$this->expectExceptionMessage('The "foo" store does not exist, use "bar".');
4763
$this->expectExceptionCode(0);
4864
$tester->execute([
4965
'store' => 'foo',

src/store/tests/Command/SetupStoreCommandTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testCommandIsConfigured()
3232
$this->assertTrue($definition->hasArgument('store'));
3333

3434
$storeArgument = $definition->getArgument('store');
35-
$this->assertSame('Name of the store to setup', $storeArgument->getDescription());
35+
$this->assertSame('Service name of the store to setup', $storeArgument->getDescription());
3636
$this->assertTrue($storeArgument->isRequired());
3737
}
3838

@@ -43,7 +43,23 @@ public function testCommandCannotSetupUndefinedStore()
4343
$tester = new CommandTester($command);
4444

4545
$this->expectException(RuntimeException::class);
46-
$this->expectExceptionMessage('The "foo" store does not exist.');
46+
$this->expectExceptionMessage('No store is configured to be set up.');
47+
$this->expectExceptionCode(0);
48+
$tester->execute([
49+
'store' => 'foo',
50+
]);
51+
}
52+
53+
public function testCommandCannotSetupWithoutStores()
54+
{
55+
$command = new SetupStoreCommand(new ServiceLocator([
56+
'bar' => fn (): object => $this->createMock(ManagedStoreInterface::class),
57+
]));
58+
59+
$tester = new CommandTester($command);
60+
61+
$this->expectException(RuntimeException::class);
62+
$this->expectExceptionMessage('The "foo" store does not exist, use "bar".');
4763
$this->expectExceptionCode(0);
4864
$tester->execute([
4965
'store' => 'foo',

0 commit comments

Comments
 (0)