Yaffs site version 1.1
[yaffs-website] / vendor / symfony / console / Tests / Helper / LegacyTableHelperTest.php
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
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Helper\TableHelper;
16 use Symfony\Component\Console\Output\StreamOutput;
17
18 /**
19  * @group legacy
20  */
21 class LegacyTableHelperTest extends TestCase
22 {
23     protected $stream;
24
25     protected function setUp()
26     {
27         $this->stream = fopen('php://memory', 'r+');
28     }
29
30     protected function tearDown()
31     {
32         fclose($this->stream);
33         $this->stream = null;
34     }
35
36     /**
37      * @dataProvider renderProvider
38      */
39     public function testRender($headers, $rows, $layout, $expected)
40     {
41         $table = new TableHelper();
42         $table
43             ->setHeaders($headers)
44             ->setRows($rows)
45             ->setLayout($layout)
46         ;
47         $table->render($output = $this->getOutputStream());
48
49         $this->assertEquals($expected, $this->getOutputContent($output));
50     }
51
52     /**
53      * @dataProvider renderProvider
54      */
55     public function testRenderAddRows($headers, $rows, $layout, $expected)
56     {
57         $table = new TableHelper();
58         $table
59             ->setHeaders($headers)
60             ->addRows($rows)
61             ->setLayout($layout)
62         ;
63         $table->render($output = $this->getOutputStream());
64
65         $this->assertEquals($expected, $this->getOutputContent($output));
66     }
67
68     /**
69      * @dataProvider renderProvider
70      */
71     public function testRenderAddRowsOneByOne($headers, $rows, $layout, $expected)
72     {
73         $table = new TableHelper();
74         $table
75             ->setHeaders($headers)
76             ->setLayout($layout)
77         ;
78         foreach ($rows as $row) {
79             $table->addRow($row);
80         }
81         $table->render($output = $this->getOutputStream());
82
83         $this->assertEquals($expected, $this->getOutputContent($output));
84     }
85
86     public function renderProvider()
87     {
88         $books = array(
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'),
93         );
94
95         return array(
96             array(
97                 array('ISBN', 'Title', 'Author'),
98                 $books,
99                 TableHelper::LAYOUT_DEFAULT,
100 <<<'TABLE'
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 +---------------+--------------------------+------------------+
109
110 TABLE
111             ),
112             array(
113                 array('ISBN', 'Title', 'Author'),
114                 $books,
115                 TableHelper::LAYOUT_COMPACT,
116 <<<'TABLE'
117  ISBN          Title                    Author           
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  
122
123 TABLE
124             ),
125             array(
126                 array('ISBN', 'Title', 'Author'),
127                 $books,
128                 TableHelper::LAYOUT_BORDERLESS,
129 <<<'TABLE'
130  =============== ========================== ================== 
131   ISBN            Title                      Author            
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  =============== ========================== ================== 
138
139 TABLE
140             ),
141             array(
142                 array('ISBN', 'Title'),
143                 array(
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'),
148                 ),
149                 TableHelper::LAYOUT_DEFAULT,
150 <<<'TABLE'
151 +---------------+--------------------------+------------------+
152 | ISBN          | Title                    |                  |
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 +---------------+--------------------------+------------------+
159
160 TABLE
161             ),
162             array(
163                 array(),
164                 array(
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'),
169                 ),
170                 TableHelper::LAYOUT_DEFAULT,
171 <<<'TABLE'
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 +---------------+--------------------------+------------------+
178
179 TABLE
180             ),
181             array(
182                 array('ISBN', 'Title', 'Author'),
183                 array(
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"),
188                 ),
189                 TableHelper::LAYOUT_DEFAULT,
190 <<<'TABLE'
191 +---------------+----------------------------+-----------------+
192 | ISBN          | Title                      | Author          |
193 +---------------+----------------------------+-----------------+
194 | 99921-58-10-7 | Divine                     | Dante Alighieri |
195 |               | Comedy                     |                 |
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.        |
201 |               |                            | Tolkien         |
202 +---------------+----------------------------+-----------------+
203
204 TABLE
205             ),
206             array(
207                 array('ISBN', 'Title'),
208                 array(),
209                 TableHelper::LAYOUT_DEFAULT,
210 <<<'TABLE'
211 +------+-------+
212 | ISBN | Title |
213 +------+-------+
214
215 TABLE
216             ),
217             array(
218                 array(),
219                 array(),
220                 TableHelper::LAYOUT_DEFAULT,
221                 '',
222             ),
223             'Cell text with tags used for Output styling' => array(
224                 array('ISBN', 'Title', 'Author'),
225                 array(
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</>'),
228                 ),
229                 TableHelper::LAYOUT_DEFAULT,
230 <<<'TABLE'
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 +---------------+----------------------+-----------------+
237
238 TABLE
239             ),
240             'Cell text with tags not used for Output styling' => array(
241                 array('ISBN', 'Title', 'Author'),
242                 array(
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'),
245                 ),
246                 TableHelper::LAYOUT_DEFAULT,
247 <<<'TABLE'
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 +----------------------------------+----------------------+-----------------+
254
255 TABLE
256             ),
257         );
258     }
259
260     public function testRenderMultiByte()
261     {
262         $table = new TableHelper();
263         $table
264             ->setHeaders(array('■■'))
265             ->setRows(array(array(1234)))
266             ->setLayout(TableHelper::LAYOUT_DEFAULT)
267         ;
268         $table->render($output = $this->getOutputStream());
269
270         $expected =
271 <<<'TABLE'
272 +------+
273 | ■■   |
274 +------+
275 | 1234 |
276 +------+
277
278 TABLE;
279
280         $this->assertEquals($expected, $this->getOutputContent($output));
281     }
282
283     public function testRenderFullWidthCharacters()
284     {
285         $table = new TableHelper();
286         $table
287             ->setHeaders(array('あいうえお'))
288             ->setRows(array(array(1234567890)))
289             ->setLayout(TableHelper::LAYOUT_DEFAULT)
290         ;
291         $table->render($output = $this->getOutputStream());
292
293         $expected =
294             <<<'TABLE'
295 +------------+
296 | あいうえお |
297 +------------+
298 | 1234567890 |
299 +------------+
300
301 TABLE;
302
303         $this->assertEquals($expected, $this->getOutputContent($output));
304     }
305
306     protected function getOutputStream()
307     {
308         return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, false);
309     }
310
311     protected function getOutputContent(StreamOutput $output)
312     {
313         rewind($output->getStream());
314
315         return str_replace(PHP_EOL, "\n", stream_get_contents($output->getStream()));
316     }
317 }