Skip to content

Commit

Permalink
Fix UTF8 issues in TableHelper.
Browse files Browse the repository at this point in the history
str_pad() counts bytes not visible characters. Also add a test for
output() being called with a wrapping array which happens in
the ConsoleIo::__call() code path.
  • Loading branch information
markstory committed May 19, 2015
1 parent 9791dcf commit 7bee2ae
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Shell/Helper/TableHelper.php
Expand Up @@ -69,7 +69,8 @@ protected function _render($row, $widths)
{
$out = '';
foreach ($row as $i => $column) {
$out .= '| ' . str_pad($column, $widths[$i], ' ', STR_PAD_RIGHT) . ' ';
$pad = $widths[$i] - mb_strlen($column);
$out .= '| ' . $column . str_repeat(' ', $pad) . ' ';
}
$out .= '|';
$this->_io->out($out);
Expand Down
47 changes: 47 additions & 0 deletions tests/TestCase/Shell/Helper/TableHelperTest.php
Expand Up @@ -80,4 +80,51 @@ public function testOutput()
];
$this->assertEquals($expected, $this->stub->messages());
}

/**
* Test output array shifting
*
* @return voi
*/
public function testOutputShifting()
{
$data = [
['Header 1', 'Header', 'Long Header'],
['short', 'Longish thing', 'short'],
];
$this->helper->output([$data]);
$expected = [
'+----------+---------------+-------------+',
'| Header 1 | Header | Long Header |',
'+----------+---------------+-------------+',
'| short | Longish thing | short |',
'+----------+---------------+-------------+',
];
$this->assertEquals($expected, $this->stub->messages());
}

/**
* Test output with multibyte characters
*
* @return voi
*/
public function testOutputUtf8()
{
$data = [
['Header 1', 'Head', 'Long Header'],
['short', 'ÄÄÄÜÜÜ', 'short'],
['Longer thing', 'longerish', 'Longest Value'],
];
$this->helper->output($data);
$expected = [
'+--------------+-----------+---------------+',
'| Header 1 | Head | Long Header |',
'+--------------+-----------+---------------+',
'| short | ÄÄÄÜÜÜ | short |',
'| Longer thing | longerish | Longest Value |',
'+--------------+-----------+---------------+',
];
debug($this->stub->messages());
$this->assertEquals($expected, $this->stub->messages());
}
}

0 comments on commit 7bee2ae

Please sign in to comment.