No Description

LegacyTableHelperTest.php 10KB

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