From 5afdac5a74d746faf05a7a22ceb9a37d69fb6d45 Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Tue, 11 Feb 2020 11:52:39 +0100 Subject: [PATCH 1/2] [Console] Don't load same-namespace alternatives on exact match found --- Application.php | 20 +++++++++++++++++--- Tests/ApplicationTest.php | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/Application.php b/Application.php index 1463967ff..280a34d5c 100644 --- a/Application.php +++ b/Application.php @@ -645,7 +645,15 @@ public function find($name) // filter out aliases for commands which are already on the list if (\count($commands) > 1) { $commandList = $this->commandLoader ? array_merge(array_flip($this->commandLoader->getNames()), $this->commands) : $this->commands; - $commands = array_unique(array_filter($commands, function ($nameOrAlias) use (&$commandList, $commands, &$aliases) { + + if (isset($commandList[$name])) { + return $this->get($name); + } + + foreach ($commands as $k => $nameOrAlias) { + if ($nameOrAlias === $name) { + return $this->get($nameOrAlias); + } if (!$commandList[$nameOrAlias] instanceof Command) { $commandList[$nameOrAlias] = $this->commandLoader->get($nameOrAlias); } @@ -654,8 +662,14 @@ public function find($name) $aliases[$nameOrAlias] = $commandName; - return $commandName === $nameOrAlias || !\in_array($commandName, $commands); - })); + if ($commandName === $nameOrAlias || !\in_array($commandName, $commands)) { + continue; + } + + unset($commands[$k]); + } + + $commands = array_unique($commands); } $exact = \in_array($name, $commands, true) || isset($aliases[$name]); diff --git a/Tests/ApplicationTest.php b/Tests/ApplicationTest.php index 559c42599..8288cfd32 100644 --- a/Tests/ApplicationTest.php +++ b/Tests/ApplicationTest.php @@ -1642,6 +1642,31 @@ public function testAllExcludesDisabledLazyCommand() $this->assertArrayNotHasKey('disabled', $application->all()); } + public function testFindAlternativesDoesNotLoadSameNamespaceCommandsOnExactMatch() + { + $application = new Application(); + $application->setAutoExit(false); + + $loaded = []; + + $application->setCommandLoader(new FactoryCommandLoader([ + 'foo:bar' => function () use (&$loaded) { + $loaded['foo:bar'] = true; + + return (new Command('foo:bar'))->setCode(function () {}); + }, + 'foo' => function () use (&$loaded) { + $loaded['foo'] = true; + + return (new Command('foo'))->setCode(function () {}); + }, + ])); + + $application->run(new ArrayInput(['command' => 'foo']), new NullOutput()); + + $this->assertSame(['foo' => true], $loaded); + } + protected function getDispatcher($skipCommand = false) { $dispatcher = new EventDispatcher(); From a65caf4f3be10cde81f626491ff34861190e0260 Mon Sep 17 00:00:00 2001 From: Adam Prickett Date: Tue, 11 Feb 2020 14:40:40 +0000 Subject: [PATCH 2/2] [Console] Handle zero row count in appendRow() for Table --- Helper/Table.php | 4 +++- Tests/Helper/TableTest.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Helper/Table.php b/Helper/Table.php index 24613bb99..e4a7423a1 100644 --- a/Helper/Table.php +++ b/Helper/Table.php @@ -601,7 +601,9 @@ private function calculateRowCount(): int ++$numberOfRows; // Add row for header separator } - ++$numberOfRows; // Add row for footer separator + if (\count($this->rows) > 0) { + ++$numberOfRows; // Add row for footer separator + } return $numberOfRows; } diff --git a/Tests/Helper/TableTest.php b/Tests/Helper/TableTest.php index 124309dd5..c4acc3fbf 100644 --- a/Tests/Helper/TableTest.php +++ b/Tests/Helper/TableTest.php @@ -951,6 +951,38 @@ public function testAppendRowWithoutSectionOutput() $table->appendRow(['9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens', '139.25']); } + public function testSectionOutputHandlesZeroRowsAfterRender() + { + $sections = []; + $stream = $this->getOutputStream(true); + $output = new ConsoleSectionOutput($stream->getStream(), $sections, $stream->getVerbosity(), $stream->isDecorated(), new OutputFormatter()); + $output->writeln('My Table'); + $table = new Table($output); + $table + ->setHeaders(['ISBN', 'Title', 'Author', 'Price']) + ->setRows([]); + + $table->render(); + + $table->appendRow(['9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens', '139.25']); + + $expected = + <<assertEquals($expected, $this->getOutputContent($output)); + } + public function testIsNotDefinedStyleException() { $this->expectException('Symfony\Component\Console\Exception\InvalidArgumentException');