8000 [Console] Handle zero row count in appendRow() for Table · symfony/symfony@9b38259 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9b38259

Browse files
Adam Prickettfabpot
Adam Prickett
authored andcommitted
[Console] Handle zero row count in appendRow() for Table
1 parent 48272f0 commit 9b38259

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/Symfony/Component/Console/Helper/Table.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,9 @@ private function calculateRowCount(): int
601601
++$numberOfRows; // Add row for header separator
602602
}
603603

604-
++$numberOfRows; // Add row for footer separator
604+
if (\count($this->rows) > 0) {
605+
++$numberOfRows; // Add row for footer separator
606+
}
605607

606608
return $numberOfRows;
607609
}

src/Symfony/Component/Console/Tests/Helper/TableTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,38 @@ public function testAppendRowWithoutSectionOutput()
951951
$table->appendRow(['9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens', '139.25']);
952952
}
953953

954+
public function testSectionOutputHandlesZeroRowsAfterRender()
955+
{
956+
$sections = [];
957+
$stream = $this->getOutputStream(true);
958+
$output = new ConsoleSectionOutput($stream->getStream(), $sections, $stream->getVerbosity(), $stream->isDecorated(), new OutputFormatter());
959+
$output->writeln('My Table');
960+
$table = new Table($output);
961+
$table
962+
->setHeaders(['ISBN', 'Title', 'Author', 'Price'])
963+
->setRows([]);
964+
965+
$table->render();
966+
967+
$table->appendRow(['9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens', '139.25']);
968+
969+
$expected =
970+
<<<TABLE
971+
My Table
972+
+------+-------+--------+-------+
973+
|\033[32m ISBN \033[39m|\033[32m Title \033[39m|\033[32m Author \033[39m|\033[32m Price \033[39m|
974+
+------+-------+--------+-------+
975+
\x1b[3A\x1b[0J+---------------+----------------------+-----------------+--------+
976+
|\033[32m ISBN \033[39m|\033[32m Title \033[39m|\033[32m Author \033[39m|\033[32m Price \033[39m|
977+
+---------------+----------------------+-----------------+--------+
978+
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | 139.25 |
979+
+---------------+----------------------+-----------------+--------+
980+
981+
TABLE;
982+
983+
$this->assertEquals($expected, $this->getOutputContent($output));
984+
}
985+
954986
public function testIsNotDefinedStyleException()
955987
{
956988
$this->expectException('Symfony\Component\Console\Exception\InvalidArgumentException');

0 commit comments

Comments
 (0)
0