Fixes #16331: Fixed console table without headers

This commit is contained in:
rhertogh
2018-10-26 12:00:49 +02:00
committed by Alexander Makarov
parent c86f26c0a8
commit 2ef24d9c65
3 changed files with 46 additions and 14 deletions

View File

@ -62,6 +62,7 @@ Yii Framework 2 Change Log
- Bug #16301: Fixed `yii\web\User::setIdentity()` to clear access check cache while setting identity object to `null` (Izumi-kun)
- Bug #16322: Fixed strings were not were not compared using timing attack resistant approach while CSRF token validation (samdark, Felix Wiedemann)
- Chg #16192: `yii\db\Command::logQuery()` is now protected, extracted `getCacheKey()` from `queryInternal()` (drlibra)
- Bug #16331: Fixed console table without headers (rhertogh)
- Bug #16377: Fixed `yii\base\Event:off()` undefined index error when event handler does not match (razvanphp)
- Bug #16514: Fixed `yii\di\Container::resolveCallableDependencies` to support callable object (wi1dcard)
- Bug #15889: Fixed override `yii\helpers\Html::setActivePlaceholder` (lesha724)

View File

@ -174,6 +174,8 @@ class Table extends Widget
public function run()
{
$this->calculateRowsSize();
$headerCount = count($this->_headers);
$buffer = $this->renderSeparator(
$this->_chars[self::CHAR_TOP_LEFT],
$this->_chars[self::CHAR_TOP_MID],
@ -181,20 +183,24 @@ class Table extends Widget
$this->_chars[self::CHAR_TOP_RIGHT]
);
// Header
$buffer .= $this->renderRow($this->_headers,
$this->_chars[self::CHAR_LEFT],
$this->_chars[self::CHAR_MIDDLE],
$this->_chars[self::CHAR_RIGHT]
);
if ($headerCount > 0) {
$buffer .= $this->renderRow($this->_headers,
$this->_chars[self::CHAR_LEFT],
$this->_chars[self::CHAR_MIDDLE],
$this->_chars[self::CHAR_RIGHT]
);
}
// Content
foreach ($this->_rows as $row) {
$buffer .= $this->renderSeparator(
$this->_chars[self::CHAR_LEFT_MID],
$this->_chars[self::CHAR_MID_MID],
$this->_chars[self::CHAR_MID],
$this->_chars[self::CHAR_RIGHT_MID]
);
foreach ($this->_rows as $i => $row) {
if ($i > 0 || $headerCount > 0) {
$buffer .= $this->renderSeparator(
$this->_chars[self::CHAR_LEFT_MID],
$this->_chars[self::CHAR_MID_MID],
$this->_chars[self::CHAR_MID],
$this->_chars[self::CHAR_RIGHT_MID]
);
}
$buffer .= $this->renderRow($row,
$this->_chars[self::CHAR_LEFT],
$this->_chars[self::CHAR_MIDDLE],
@ -303,9 +309,14 @@ class Table extends Widget
$totalWidth = 0;
$screenWidth = $this->getScreenWidth() - self::CONSOLE_SCROLLBAR_OFFSET;
for ($i = 0, $count = count($this->_headers); $i < $count; $i++) {
$headerCount = count($this->_headers);
$rowColCount = max(array_map('count', $this->_rows));
$count = max($headerCount, $rowColCount);
for ($i = 0; $i < $count; $i++) {
$columns[] = ArrayHelper::getColumn($this->_rows, $i);
$columns[$i][] = $this->_headers[$i];
if ($i < $headerCount) {
$columns[$i][] = $this->_headers[$i];
}
}
foreach ($columns as $column) {

View File

@ -249,4 +249,24 @@ EXPECTED;
])->setScreenWidth(200)->run()
);
}
public function testEmptyHeaders()
{
$table = new Table();
$expected = <<<'EXPECTED'
╔═══════════════╤═══════════════╗
║ testcontent1 │ testcontent2 ║
╟───────────────┼───────────────╢
║ testcontent21 │ testcontent22 ║
╚═══════════════╧═══════════════╝
EXPECTED;
$this->assertEqualsWithoutLE($expected, $table->setRows([
['testcontent1', 'testcontent2'],
['testcontent21', 'testcontent22']
])->setScreenWidth(200)->run()
);
}
}