From 9b382590ee719a3aeaad11a91a1ff666c0da1a3a Mon Sep 17 00:00:00 2001 From: Adam Prickett Date: Tue, 11 Feb 2020 14:40:40 +0000 Subject: [PATCH] [Console] Handle zero row count in appendRow() for Table --- .../Component/Console/Helper/Table.php | 4 ++- .../Console/Tests/Helper/TableTest.php | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index 24613bb9945f..e4a7423a1886 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/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/src/Symfony/Component/Console/Tests/Helper/TableTest.php b/src/Symfony/Component/Console/Tests/Helper/TableTest.php index 124309dd5b02..c4acc3fbff9c 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TableTest.php +++ b/src/Symfony/Component/Console/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');