Browse Source

Fix UTF8 issues in TableHelper.

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.
Mark Story 11 years ago
parent
commit
7bee2ae086
2 changed files with 49 additions and 1 deletions
  1. 2 1
      src/Shell/Helper/TableHelper.php
  2. 47 0
      tests/TestCase/Shell/Helper/TableHelperTest.php

+ 2 - 1
src/Shell/Helper/TableHelper.php

@@ -69,7 +69,8 @@ class TableHelper extends Helper
     {
         $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);

+ 47 - 0
tests/TestCase/Shell/Helper/TableHelperTest.php

@@ -80,4 +80,51 @@ class TableHelperTest extends TestCase
         ];
         $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());
+    }
 }