From e4a0c5d556ee8acd3abdbf7a8476e132e7ee6413 Mon Sep 17 00:00:00 2001 From: Roman Martinuk Date: Tue, 20 Jul 2021 19:36:01 +0300 Subject: [PATCH] [Console] fix table setHeaderTitle without headers --- .../Component/Console/Helper/Table.php | 14 +++++++------ .../Console/Tests/Helper/TableTest.php | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index 8f9e972408540..1d0a22baa3245 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -374,6 +374,7 @@ public function render() $isHeader = !$this->horizontal; $isFirstRow = $this->horizontal; + $hasTitle = (bool) $this->headerTitle; foreach ($rows as $row) { if ($divider === $row) { $isHeader = false; @@ -391,12 +392,13 @@ public function render() } if ($isHeader || $isFirstRow) { - if ($isFirstRow) { - $this->renderRowSeparator(self::SEPARATOR_TOP_BOTTOM); - $isFirstRow = false; - } else { - $this->renderRowSeparator(self::SEPARATOR_TOP, $this->headerTitle, $this->style->getHeaderTitleFormat()); - } + $this->renderRowSeparator( + $isHeader ? self::SEPARATOR_TOP : self::SEPARATOR_TOP_BOTTOM, + $hasTitle ? $this->headerTitle : null, + $hasTitle ? $this->style->getHeaderTitleFormat() : null + ); + $isFirstRow = false; + $hasTitle = false; } if ($this->horizontal) { $this->renderRow($row, $this->style->getCellRowFormat(), $this->style->getCellHeaderFormat()); diff --git a/src/Symfony/Component/Console/Tests/Helper/TableTest.php b/src/Symfony/Component/Console/Tests/Helper/TableTest.php index d02d6ea42e30d..eed0b166237fe 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TableTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TableTest.php @@ -1115,6 +1115,27 @@ public function renderSetTitle() ]; } + public function testSetTitleWithoutHeaders() + { + (new Table($output = $this->getOutputStream())) + ->setHeaderTitle('Reproducer') + ->setRows([ + ['Value', '123-456'], + ['Some other value', '789-0'], + ]) + ->render(); + + $expected = <<<'TABLE' ++-------- Reproducer --------+ +| Value | 123-456 | +| Some other value | 789-0 | ++------------------+---------+ + +TABLE; + + $this->assertSame($expected, $this->getOutputContent($output)); + } + public function testColumnMaxWidths() { $table = new Table($output = $this->getOutputStream());