4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Tests\Helper;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Helper\TableHelper;
16 use Symfony\Component\Console\Output\StreamOutput;
21 class LegacyTableHelperTest extends TestCase
25 protected function setUp()
27 $this->stream = fopen('php://memory', 'r+');
30 protected function tearDown()
32 fclose($this->stream);
37 * @dataProvider renderProvider
39 public function testRender($headers, $rows, $layout, $expected)
41 $table = new TableHelper();
43 ->setHeaders($headers)
47 $table->render($output = $this->getOutputStream());
49 $this->assertEquals($expected, $this->getOutputContent($output));
53 * @dataProvider renderProvider
55 public function testRenderAddRows($headers, $rows, $layout, $expected)
57 $table = new TableHelper();
59 ->setHeaders($headers)
63 $table->render($output = $this->getOutputStream());
65 $this->assertEquals($expected, $this->getOutputContent($output));
69 * @dataProvider renderProvider
71 public function testRenderAddRowsOneByOne($headers, $rows, $layout, $expected)
73 $table = new TableHelper();
75 ->setHeaders($headers)
78 foreach ($rows as $row) {
81 $table->render($output = $this->getOutputStream());
83 $this->assertEquals($expected, $this->getOutputContent($output));
86 public function renderProvider()
89 array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
90 array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
91 array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
92 array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
97 array('ISBN', 'Title', 'Author'),
99 TableHelper::LAYOUT_DEFAULT,
101 +---------------+--------------------------+------------------+
102 | ISBN | Title | Author |
103 +---------------+--------------------------+------------------+
104 | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
105 | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
106 | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
107 | 80-902734-1-6 | And Then There Were None | Agatha Christie |
108 +---------------+--------------------------+------------------+
113 array('ISBN', 'Title', 'Author'),
115 TableHelper::LAYOUT_COMPACT,
118 99921-58-10-7 Divine Comedy Dante Alighieri
119 9971-5-0210-0 A Tale of Two Cities Charles Dickens
120 960-425-059-0 The Lord of the Rings J. R. R. Tolkien
121 80-902734-1-6 And Then There Were None Agatha Christie
126 array('ISBN', 'Title', 'Author'),
128 TableHelper::LAYOUT_BORDERLESS,
130 =============== ========================== ==================
132 =============== ========================== ==================
133 99921-58-10-7 Divine Comedy Dante Alighieri
134 9971-5-0210-0 A Tale of Two Cities Charles Dickens
135 960-425-059-0 The Lord of the Rings J. R. R. Tolkien
136 80-902734-1-6 And Then There Were None Agatha Christie
137 =============== ========================== ==================
142 array('ISBN', 'Title'),
144 array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
145 array('9971-5-0210-0'),
146 array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
147 array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
149 TableHelper::LAYOUT_DEFAULT,
151 +---------------+--------------------------+------------------+
153 +---------------+--------------------------+------------------+
154 | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
155 | 9971-5-0210-0 | | |
156 | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
157 | 80-902734-1-6 | And Then There Were None | Agatha Christie |
158 +---------------+--------------------------+------------------+
165 array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
166 array('9971-5-0210-0'),
167 array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
168 array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
170 TableHelper::LAYOUT_DEFAULT,
172 +---------------+--------------------------+------------------+
173 | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
174 | 9971-5-0210-0 | | |
175 | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
176 | 80-902734-1-6 | And Then There Were None | Agatha Christie |
177 +---------------+--------------------------+------------------+
182 array('ISBN', 'Title', 'Author'),
184 array('99921-58-10-7', "Divine\nComedy", 'Dante Alighieri'),
185 array('9971-5-0210-2', "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
186 array('9971-5-0210-2', "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
187 array('960-425-059-0', 'The Lord of the Rings', "J. R. R.\nTolkien"),
189 TableHelper::LAYOUT_DEFAULT,
191 +---------------+----------------------------+-----------------+
192 | ISBN | Title | Author |
193 +---------------+----------------------------+-----------------+
194 | 99921-58-10-7 | Divine | Dante Alighieri |
196 | 9971-5-0210-2 | Harry Potter | Rowling |
197 | | and the Chamber of Secrets | Joanne K. |
198 | 9971-5-0210-2 | Harry Potter | Rowling |
199 | | and the Chamber of Secrets | Joanne K. |
200 | 960-425-059-0 | The Lord of the Rings | J. R. R. |
202 +---------------+----------------------------+-----------------+
207 array('ISBN', 'Title'),
209 TableHelper::LAYOUT_DEFAULT,
220 TableHelper::LAYOUT_DEFAULT,
223 'Cell text with tags used for Output styling' => array(
224 array('ISBN', 'Title', 'Author'),
226 array('<info>99921-58-10-7</info>', '<error>Divine Comedy</error>', '<fg=blue;bg=white>Dante Alighieri</fg=blue;bg=white>'),
227 array('9971-5-0210-0', 'A Tale of Two Cities', '<info>Charles Dickens</>'),
229 TableHelper::LAYOUT_DEFAULT,
231 +---------------+----------------------+-----------------+
232 | ISBN | Title | Author |
233 +---------------+----------------------+-----------------+
234 | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
235 | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
236 +---------------+----------------------+-----------------+
240 'Cell text with tags not used for Output styling' => array(
241 array('ISBN', 'Title', 'Author'),
243 array('<strong>99921-58-10-700</strong>', '<f>Divine Com</f>', 'Dante Alighieri'),
244 array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
246 TableHelper::LAYOUT_DEFAULT,
248 +----------------------------------+----------------------+-----------------+
249 | ISBN | Title | Author |
250 +----------------------------------+----------------------+-----------------+
251 | <strong>99921-58-10-700</strong> | <f>Divine Com</f> | Dante Alighieri |
252 | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
253 +----------------------------------+----------------------+-----------------+
260 public function testRenderMultiByte()
262 $table = new TableHelper();
264 ->setHeaders(array('■■'))
265 ->setRows(array(array(1234)))
266 ->setLayout(TableHelper::LAYOUT_DEFAULT)
268 $table->render($output = $this->getOutputStream());
280 $this->assertEquals($expected, $this->getOutputContent($output));
283 public function testRenderFullWidthCharacters()
285 $table = new TableHelper();
287 ->setHeaders(array('あいうえお'))
288 ->setRows(array(array(1234567890)))
289 ->setLayout(TableHelper::LAYOUT_DEFAULT)
291 $table->render($output = $this->getOutputStream());
303 $this->assertEquals($expected, $this->getOutputContent($output));
306 protected function getOutputStream()
308 return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, false);
311 protected function getOutputContent(StreamOutput $output)
313 rewind($output->getStream());
315 return str_replace(PHP_EOL, "\n", stream_get_contents($output->getStream()));