LegacyTableHelperTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\Helper;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Helper\TableHelper;
  13. use Symfony\Component\Console\Output\StreamOutput;
  14. /**
  15. * @group legacy
  16. */
  17. class LegacyTableHelperTest extends TestCase
  18. {
  19. protected $stream;
  20. protected function setUp()
  21. {
  22. $this->stream = fopen('php://memory', 'r+');
  23. }
  24. protected function tearDown()
  25. {
  26. fclose($this->stream);
  27. $this->stream = null;
  28. }
  29. /**
  30. * @dataProvider renderProvider
  31. */
  32. public function testRender($headers, $rows, $layout, $expected)
  33. {
  34. $table = new TableHelper();
  35. $table
  36. ->setHeaders($headers)
  37. ->setRows($rows)
  38. ->setLayout($layout)
  39. ;
  40. $table->render($output = $this->getOutputStream());
  41. $this->assertEquals($expected, $this->getOutputContent($output));
  42. }
  43. /**
  44. * @dataProvider renderProvider
  45. */
  46. public function testRenderAddRows($headers, $rows, $layout, $expected)
  47. {
  48. $table = new TableHelper();
  49. $table
  50. ->setHeaders($headers)
  51. ->addRows($rows)
  52. ->setLayout($layout)
  53. ;
  54. $table->render($output = $this->getOutputStream());
  55. $this->assertEquals($expected, $this->getOutputContent($output));
  56. }
  57. /**
  58. * @dataProvider renderProvider
  59. */
  60. public function testRenderAddRowsOneByOne($headers, $rows, $layout, $expected)
  61. {
  62. $table = new TableHelper();
  63. $table
  64. ->setHeaders($headers)
  65. ->setLayout($layout)
  66. ;
  67. foreach ($rows as $row) {
  68. $table->addRow($row);
  69. }
  70. $table->render($output = $this->getOutputStream());
  71. $this->assertEquals($expected, $this->getOutputContent($output));
  72. }
  73. public function renderProvider()
  74. {
  75. $books = array(
  76. array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
  77. array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
  78. array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
  79. array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
  80. );
  81. return array(
  82. array(
  83. array('ISBN', 'Title', 'Author'),
  84. $books,
  85. TableHelper::LAYOUT_DEFAULT,
  86. <<<'TABLE'
  87. +---------------+--------------------------+------------------+
  88. | ISBN | Title | Author |
  89. +---------------+--------------------------+------------------+
  90. | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
  91. | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
  92. | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
  93. | 80-902734-1-6 | And Then There Were None | Agatha Christie |
  94. +---------------+--------------------------+------------------+
  95. TABLE
  96. ),
  97. array(
  98. array('ISBN', 'Title', 'Author'),
  99. $books,
  100. TableHelper::LAYOUT_COMPACT,
  101. <<<'TABLE'
  102. ISBN Title Author
  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. TABLE
  108. ),
  109. array(
  110. array('ISBN', 'Title', 'Author'),
  111. $books,
  112. TableHelper::LAYOUT_BORDERLESS,
  113. <<<'TABLE'
  114. =============== ========================== ==================
  115. ISBN Title Author
  116. =============== ========================== ==================
  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'),
  126. array(
  127. array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
  128. array('9971-5-0210-0'),
  129. array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
  130. array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
  131. ),
  132. TableHelper::LAYOUT_DEFAULT,
  133. <<<'TABLE'
  134. +---------------+--------------------------+------------------+
  135. | ISBN | Title | |
  136. +---------------+--------------------------+------------------+
  137. | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
  138. | 9971-5-0210-0 | | |
  139. | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
  140. | 80-902734-1-6 | And Then There Were None | Agatha Christie |
  141. +---------------+--------------------------+------------------+
  142. TABLE
  143. ),
  144. array(
  145. array(),
  146. array(
  147. array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
  148. array('9971-5-0210-0'),
  149. array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
  150. array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
  151. ),
  152. TableHelper::LAYOUT_DEFAULT,
  153. <<<'TABLE'
  154. +---------------+--------------------------+------------------+
  155. | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
  156. | 9971-5-0210-0 | | |
  157. | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
  158. | 80-902734-1-6 | And Then There Were None | Agatha Christie |
  159. +---------------+--------------------------+------------------+
  160. TABLE
  161. ),
  162. array(
  163. array('ISBN', 'Title', 'Author'),
  164. array(
  165. array('99921-58-10-7', "Divine\nComedy", 'Dante Alighieri'),
  166. array('9971-5-0210-2', "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
  167. array('9971-5-0210-2', "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
  168. array('960-425-059-0', 'The Lord of the Rings', "J. R. R.\nTolkien"),
  169. ),
  170. TableHelper::LAYOUT_DEFAULT,
  171. <<<'TABLE'
  172. +---------------+----------------------------+-----------------+
  173. | ISBN | Title | Author |
  174. +---------------+----------------------------+-----------------+
  175. | 99921-58-10-7 | Divine | Dante Alighieri |
  176. | | Comedy | |
  177. | 9971-5-0210-2 | Harry Potter | Rowling |
  178. | | and the Chamber of Secrets | Joanne K. |
  179. | 9971-5-0210-2 | Harry Potter | Rowling |
  180. | | and the Chamber of Secrets | Joanne K. |
  181. | 960-425-059-0 | The Lord of the Rings | J. R. R. |
  182. | | | Tolkien |
  183. +---------------+----------------------------+-----------------+
  184. TABLE
  185. ),
  186. array(
  187. array('ISBN', 'Title'),
  188. array(),
  189. TableHelper::LAYOUT_DEFAULT,
  190. <<<'TABLE'
  191. +------+-------+
  192. | ISBN | Title |
  193. +------+-------+
  194. TABLE
  195. ),
  196. array(
  197. array(),
  198. array(),
  199. TableHelper::LAYOUT_DEFAULT,
  200. '',
  201. ),
  202. 'Cell text with tags used for Output styling' => array(
  203. array('ISBN', 'Title', 'Author'),
  204. array(
  205. array('<info>99921-58-10-7</info>', '<error>Divine Comedy</error>', '<fg=blue;bg=white>Dante Alighieri</fg=blue;bg=white>'),
  206. array('9971-5-0210-0', 'A Tale of Two Cities', '<info>Charles Dickens</>'),
  207. ),
  208. TableHelper::LAYOUT_DEFAULT,
  209. <<<'TABLE'
  210. +---------------+----------------------+-----------------+
  211. | ISBN | Title | Author |
  212. +---------------+----------------------+-----------------+
  213. | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
  214. | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
  215. +---------------+----------------------+-----------------+
  216. TABLE
  217. ),
  218. 'Cell text with tags not used for Output styling' => array(
  219. array('ISBN', 'Title', 'Author'),
  220. array(
  221. array('<strong>99921-58-10-700</strong>', '<f>Divine Com</f>', 'Dante Alighieri'),
  222. array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
  223. ),
  224. TableHelper::LAYOUT_DEFAULT,
  225. <<<'TABLE'
  226. +----------------------------------+----------------------+-----------------+
  227. | ISBN | Title | Author |
  228. +----------------------------------+----------------------+-----------------+
  229. | <strong>99921-58-10-700</strong> | <f>Divine Com</f> | Dante Alighieri |
  230. | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
  231. +----------------------------------+----------------------+-----------------+
  232. TABLE
  233. ),
  234. );
  235. }
  236. public function testRenderMultiByte()
  237. {
  238. $table = new TableHelper();
  239. $table
  240. ->setHeaders(array('■■'))
  241. ->setRows(array(array(1234)))
  242. ->setLayout(TableHelper::LAYOUT_DEFAULT)
  243. ;
  244. $table->render($output = $this->getOutputStream());
  245. $expected =
  246. <<<'TABLE'
  247. +------+
  248. | ■■ |
  249. +------+
  250. | 1234 |
  251. +------+
  252. TABLE;
  253. $this->assertEquals($expected, $this->getOutputContent($output));
  254. }
  255. public function testRenderFullWidthCharacters()
  256. {
  257. $table = new TableHelper();
  258. $table
  259. ->setHeaders(array('あいうえお'))
  260. ->setRows(array(array(1234567890)))
  261. ->setLayout(TableHelper::LAYOUT_DEFAULT)
  262. ;
  263. $table->render($output = $this->getOutputStream());
  264. $expected =
  265. <<<'TABLE'
  266. +------------+
  267. | あいうえお |
  268. +------------+
  269. | 1234567890 |
  270. +------------+
  271. TABLE;
  272. $this->assertEquals($expected, $this->getOutputContent($output));
  273. }
  274. protected function getOutputStream()
  275. {
  276. return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, false);
  277. }
  278. protected function getOutputContent(StreamOutput $output)
  279. {
  280. rewind($output->getStream());
  281. return str_replace(PHP_EOL, "\n", stream_get_contents($output->getStream()));
  282. }
  283. }