Skip to content

Commit effb684

Browse files
TreggatsBapawebrendt
authored
fix: duplicate command completion (#600)
Signed-off-by: Tonko Mulder <[email protected]> Co-authored-by: Frank Kemps <[email protected]> Co-authored-by: Brent Roose <[email protected]>
1 parent 580c325 commit effb684

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

src/Tempest/Console/src/Middleware/ResolveOrRescueMiddleware.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ private function getSimilarCommands(string $name): array
6565
$similarCommands = [];
6666

6767
foreach ($this->consoleConfig->commands as $consoleCommand) {
68+
if (in_array($consoleCommand->getName(), $similarCommands, strict: true)) {
69+
continue;
70+
}
71+
6872
if (str_starts_with($consoleCommand->getName(), $name)) {
6973
$similarCommands[] = $consoleCommand->getName();
7074

src/Tempest/Console/src/Testing/ConsoleTester.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,19 @@ public function printFormatted(): self
160160
return $this;
161161
}
162162

163+
public function getBuffer(?callable $callback = null): array
164+
{
165+
$buffer = array_map('trim', $this->output->getBufferWithoutFormatting());
166+
167+
$this->output->clear();
168+
169+
if ($callback !== null) {
170+
return $callback($buffer);
171+
}
172+
173+
return $buffer;
174+
}
175+
163176
public function useInteractiveTerminal(): self
164177
{
165178
$this->componentRenderer = new InteractiveComponentRenderer();
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Tempest\Integration\Console\Middleware;
6+
7+
use PHPUnit\Framework\Attributes\Test;
8+
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
9+
10+
/**
11+
* @internal
12+
*/
13+
final class ResolveOrRescueMiddlewareTest extends FrameworkIntegrationTestCase
14+
{
15+
#[Test]
16+
public function it_can_find_a_single_similar_command(): void
17+
{
18+
$this->console
19+
->call('discovery:sta')
20+
->assertSee('Did you mean discovery:status?');
21+
22+
$this->console
23+
->call('bascovery:status')
24+
->assertSee('Did you mean discovery:status?');
25+
}
26+
27+
#[Test]
28+
public function it_does_not_duplicate_completed_commands(): void
29+
{
30+
$formatOutput = static fn (string $buffer) => str($buffer)
31+
->trim()
32+
->remove(['[',']'])
33+
->explode('/')
34+
->all();
35+
36+
$output = $this->console
37+
->call('discovery')
38+
->getBuffer(fn (array $buffer) => $formatOutput(array_pop($buffer)));
39+
40+
$this->assertContains('discovery:status', $output);
41+
$this->assertContains('discovery:clear', $output);
42+
43+
$this->assertCount(2, $output);
44+
}
45+
}

0 commit comments

Comments
 (0)