No Description

TableTest.php 11KB

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