LegacyTableHelperTest.php 11 KB

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