8000 bug #35676 [Console] Handle zero row count in appendRow() for Table (… · symfony/symfony@e87b599 · GitHub
[go: up one dir, main page]

Skip to content

Commit e87b599

Browse files
committed
bug #35676 [Console] Handle zero row count in appendRow() for Table (Adam Prickett)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [Console] Handle zero row count in appendRow() for Table | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | n/a | License | MIT | Doc PR | n/a When a `Table` is created and rendered with no rows (headers only) and subsequently rows are added using `appendRow()`, the first call to `appendRow()` clears back one line too far., thus removing the last run This is caused by `calculateRowCount()` not accounting for the fact that the footer separator is also the header separator when no rows are present. This PR works around the issue by checking to ensure that at least 1 row exists before including the footer separator in the row count. ## Example Command: ```php <?php namespace App\Command; class TableTestCommand extends Command { // ... protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('My table'); $table = new Table($output->section()); $table->setHeaders(['Column', 'Another column']); $table->render(); $table->appendRow(['Value', 'Another Value']); $table->appendRow(['Value', 'Another Value']); } } ``` Before fix: ``` +--------+----------------+ | Column | Another column | +--------+----------------+ | Value | Another Value | | Value | Another Value | +--------+----------------+ ``` After fix: ``` My table +--------+----------------+ | Column | Another column | +--------+----------------+ | Value | Another Value | | Value | Another Value | +--------+----------------+ ``` Commits ------- 9b38259 [Console] Handle zero row count in appendRow() for Table
2 parents ded655b + 9b38259 commit e87b599

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