TYPO3  7.6
LegacyTableHelperTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Symfony\Component\Console\Tests\Helper;
13 
16 
20 class LegacyTableHelperTest extends \PHPUnit_Framework_TestCase
21 {
22  protected $stream;
23 
24  protected function setUp()
25  {
26  $this->stream = fopen('php://memory', 'r+');
27  }
28 
29  protected function tearDown()
30  {
31  fclose($this->stream);
32  $this->stream = null;
33  }
34 
38  public function testRender($headers, $rows, $layout, $expected)
39  {
40  $table = new TableHelper();
41  $table
42  ->setHeaders($headers)
43  ->setRows($rows)
44  ->setLayout($layout)
45  ;
46  $table->render($output = $this->getOutputStream());
47 
48  $this->assertEquals($expected, $this->getOutputContent($output));
49  }
50 
54  public function testRenderAddRows($headers, $rows, $layout, $expected)
55  {
56  $table = new TableHelper();
57  $table
58  ->setHeaders($headers)
59  ->addRows($rows)
60  ->setLayout($layout)
61  ;
62  $table->render($output = $this->getOutputStream());
63 
64  $this->assertEquals($expected, $this->getOutputContent($output));
65  }
66 
70  public function testRenderAddRowsOneByOne($headers, $rows, $layout, $expected)
71  {
72  $table = new TableHelper();
73  $table
74  ->setHeaders($headers)
75  ->setLayout($layout)
76  ;
77  foreach ($rows as $row) {
78  $table->addRow($row);
79  }
80  $table->render($output = $this->getOutputStream());
81 
82  $this->assertEquals($expected, $this->getOutputContent($output));
83  }
84 
85  public function testRenderProvider()
86  {
87  $books = array(
88  array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
89  array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
90  array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
91  array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
92  );
93 
94  return array(
95  array(
96  array('ISBN', 'Title', 'Author'),
97  $books,
99 <<<TABLE
100 +---------------+--------------------------+------------------+
101 | ISBN | Title | Author |
102 +---------------+--------------------------+------------------+
103 | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
104 | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
105 | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
106 | 80-902734-1-6 | And Then There Were None | Agatha Christie |
107 +---------------+--------------------------+------------------+
108 
109 TABLE
110  ),
111  array(
112  array('ISBN', 'Title', 'Author'),
113  $books,
115 <<<TABLE
116  ISBN Title Author
117  99921-58-10-7 Divine Comedy Dante Alighieri
118  9971-5-0210-0 A Tale of Two Cities Charles Dickens
119  960-425-059-0 The Lord of the Rings J. R. R. Tolkien
120  80-902734-1-6 And Then There Were None Agatha Christie
121 
122 TABLE
123  ),
124  array(
125  array('ISBN', 'Title', 'Author'),
126  $books,
128 <<<TABLE
129  =============== ========================== ==================
130  ISBN Title Author
131  =============== ========================== ==================
132  99921-58-10-7 Divine Comedy Dante Alighieri
133  9971-5-0210-0 A Tale of Two Cities Charles Dickens
134  960-425-059-0 The Lord of the Rings J. R. R. Tolkien
135  80-902734-1-6 And Then There Were None Agatha Christie
136  =============== ========================== ==================
137 
138 TABLE
139  ),
140  array(
141  array('ISBN', 'Title'),
142  array(
143  array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
144  array('9971-5-0210-0'),
145  array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
146  array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
147  ),
149 <<<TABLE
150 +---------------+--------------------------+------------------+
151 | ISBN | Title | |
152 +---------------+--------------------------+------------------+
153 | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
154 | 9971-5-0210-0 | | |
155 | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
156 | 80-902734-1-6 | And Then There Were None | Agatha Christie |
157 +---------------+--------------------------+------------------+
158 
159 TABLE
160  ),
161  array(
162  array(),
163  array(
164  array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
165  array('9971-5-0210-0'),
166  array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
167  array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
168  ),
170 <<<TABLE
171 +---------------+--------------------------+------------------+
172 | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
173 | 9971-5-0210-0 | | |
174 | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
175 | 80-902734-1-6 | And Then There Were None | Agatha Christie |
176 +---------------+--------------------------+------------------+
177 
178 TABLE
179  ),
180  array(
181  array('ISBN', 'Title', 'Author'),
182  array(
183  array('99921-58-10-7', "Divine\nComedy", 'Dante Alighieri'),
184  array('9971-5-0210-2', "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
185  array('9971-5-0210-2', "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
186  array('960-425-059-0', 'The Lord of the Rings', "J. R. R.\nTolkien"),
187  ),
189 <<<TABLE
190 +---------------+----------------------------+-----------------+
191 | ISBN | Title | Author |
192 +---------------+----------------------------+-----------------+
193 | 99921-58-10-7 | Divine | Dante Alighieri |
194 | | Comedy | |
195 | 9971-5-0210-2 | Harry Potter | Rowling |
196 | | and the Chamber of Secrets | Joanne K. |
197 | 9971-5-0210-2 | Harry Potter | Rowling |
198 | | and the Chamber of Secrets | Joanne K. |
199 | 960-425-059-0 | The Lord of the Rings | J. R. R. |
200 | | | Tolkien |
201 +---------------+----------------------------+-----------------+
202 
203 TABLE
204  ),
205  array(
206  array('ISBN', 'Title'),
207  array(),
209 <<<TABLE
210 +------+-------+
211 | ISBN | Title |
212 +------+-------+
213 
214 TABLE
215  ),
216  array(
217  array(),
218  array(),
220  '',
221  ),
222  'Cell text with tags used for Output styling' => array(
223  array('ISBN', 'Title', 'Author'),
224  array(
225  array('<info>99921-58-10-7</info>', '<error>Divine Comedy</error>', '<fg=blue;bg=white>Dante Alighieri</fg=blue;bg=white>'),
226  array('9971-5-0210-0', 'A Tale of Two Cities', '<info>Charles Dickens</>'),
227  ),
229 <<<TABLE
230 +---------------+----------------------+-----------------+
231 | ISBN | Title | Author |
232 +---------------+----------------------+-----------------+
233 | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
234 | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
235 +---------------+----------------------+-----------------+
236 
237 TABLE
238  ),
239  'Cell text with tags not used for Output styling' => array(
240  array('ISBN', 'Title', 'Author'),
241  array(
242  array('<strong>99921-58-10-700</strong>', '<f>Divine Com</f>', 'Dante Alighieri'),
243  array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
244  ),
246 <<<TABLE
247 +----------------------------------+----------------------+-----------------+
248 | ISBN | Title | Author |
249 +----------------------------------+----------------------+-----------------+
250 | <strong>99921-58-10-700</strong> | <f>Divine Com</f> | Dante Alighieri |
251 | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
252 +----------------------------------+----------------------+-----------------+
253 
254 TABLE
255  ),
256  );
257  }
258 
259  public function testRenderMultiByte()
260  {
261  if (!function_exists('mb_strwidth')) {
262  $this->markTestSkipped('The "mbstring" extension is not available');
263  }
264 
265  $table = new TableHelper();
266  $table
267  ->setHeaders(array('■■'))
268  ->setRows(array(array(1234)))
269  ->setLayout(TableHelper::LAYOUT_DEFAULT)
270  ;
271  $table->render($output = $this->getOutputStream());
272 
273  $expected =
274 <<<TABLE
275 +------+
276 | ■■ |
277 +------+
278 | 1234 |
279 +------+
280 
281 TABLE;
282 
283  $this->assertEquals($expected, $this->getOutputContent($output));
284  }
285 
287  {
288  if (!function_exists('mb_strwidth')) {
289  $this->markTestSkipped('The "mbstring" extension is not available');
290  }
291 
292  $table = new TableHelper();
293  $table
294  ->setHeaders(array('あいうえお'))
295  ->setRows(array(array(1234567890)))
296  ->setLayout(TableHelper::LAYOUT_DEFAULT)
297  ;
298  $table->render($output = $this->getOutputStream());
299 
300  $expected =
301  <<<TABLE
302 +------------+
303 | あいうえお |
304 +------------+
305 | 1234567890 |
306 +------------+
307 
308 TABLE;
309 
310  $this->assertEquals($expected, $this->getOutputContent($output));
311  }
312 
313  protected function getOutputStream()
314  {
315  return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, false);
316  }
317 
318  protected function getOutputContent(StreamOutput $output)
319  {
320  rewind($output->getStream());
321 
322  return str_replace(PHP_EOL, "\n", stream_get_contents($output->getStream()));
323  }
324 }