TableTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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\Helper\TableCell;
  15. use Symfony\Component\Console\Output\StreamOutput;
  16. class TableTest 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, $style, $expected)
  32. {
  33. $table = new Table($output = $this->getOutputStream());
  34. $table
  35. ->setHeaders($headers)
  36. ->setRows($rows)
  37. ->setStyle($style)
  38. ;
  39. $table->render();
  40. $this->assertEquals($expected, $this->getOutputContent($output));
  41. }
  42. /**
  43. * @dataProvider testRenderProvider
  44. */
  45. public function testRenderAddRows($headers, $rows, $style, $expected)
  46. {
  47. $table = new Table($output = $this->getOutputStream());
  48. $table
  49. ->setHeaders($headers)
  50. ->addRows($rows)
  51. ->setStyle($style)
  52. ;
  53. $table->render();
  54. $this->assertEquals($expected, $this->getOutputContent($output));
  55. }
  56. /**
  57. * @dataProvider testRenderProvider
  58. */
  59. public function testRenderAddRowsOneByOne($headers, $rows, $style, $expected)
  60. {
  61. $table = new Table($output = $this->getOutputStream());
  62. $table
  63. ->setHeaders($headers)
  64. ->setStyle($style)
  65. ;
  66. foreach ($rows as $row) {
  67. $table->addRow($row);
  68. }
  69. $table->render();
  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. '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. '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. '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. '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. '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. '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. 'default',
  189. <<<TABLE
  190. +------+-------+
  191. | ISBN | Title |
  192. +------+-------+
  193. TABLE
  194. ),
  195. array(
  196. array(),
  197. array(),
  198. '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. '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. '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. 'Cell with colspan' => array(
  234. array('ISBN', 'Title', 'Author'),
  235. array(
  236. array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
  237. new TableSeparator(),
  238. array(new TableCell('Divine Comedy(Dante Alighieri)', array('colspan' => 3))),
  239. new TableSeparator(),
  240. array(
  241. new TableCell('Arduino: A Quick-Start Guide', array('colspan' => 2)),
  242. 'Mark Schmidt',
  243. ),
  244. new TableSeparator(),
  245. array(
  246. '9971-5-0210-0',
  247. new TableCell("A Tale of \nTwo Cities", array('colspan' => 2)),
  248. ),
  249. ),
  250. 'default',
  251. <<<TABLE
  252. +----------------+---------------+-----------------+
  253. | ISBN | Title | Author |
  254. +----------------+---------------+-----------------+
  255. | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
  256. +----------------+---------------+-----------------+
  257. | Divine Comedy(Dante Alighieri) |
  258. +----------------+---------------+-----------------+
  259. | Arduino: A Quick-Start Guide | Mark Schmidt |
  260. +----------------+---------------+-----------------+
  261. | 9971-5-0210-0 | A Tale of |
  262. | | Two Cities |
  263. +----------------+---------------+-----------------+
  264. TABLE
  265. ),
  266. 'Cell with rowspan' => array(
  267. array('ISBN', 'Title', 'Author'),
  268. array(
  269. array(
  270. new TableCell('9971-5-0210-0', array('rowspan' => 3)),
  271. 'Divine Comedy',
  272. 'Dante Alighieri',
  273. ),
  274. array('A Tale of Two Cities', 'Charles Dickens'),
  275. array("The Lord of \nthe Rings", "J. R. \nR. Tolkien"),
  276. new TableSeparator(),
  277. array('80-902734-1-6', new TableCell("And Then \nThere \nWere None", array('rowspan' => 3)), 'Agatha Christie'),
  278. array('80-902734-1-7', 'Test'),
  279. ),
  280. 'default',
  281. <<<TABLE
  282. +---------------+----------------------+-----------------+
  283. | ISBN | Title | Author |
  284. +---------------+----------------------+-----------------+
  285. | 9971-5-0210-0 | Divine Comedy | Dante Alighieri |
  286. | | A Tale of Two Cities | Charles Dickens |
  287. | | The Lord of | J. R. |
  288. | | the Rings | R. Tolkien |
  289. +---------------+----------------------+-----------------+
  290. | 80-902734-1-6 | And Then | Agatha Christie |
  291. | 80-902734-1-7 | There | Test |
  292. | | Were None | |
  293. +---------------+----------------------+-----------------+
  294. TABLE
  295. ),
  296. 'Cell with rowspan and colspan' => array(
  297. array('ISBN', 'Title', 'Author'),
  298. array(
  299. array(
  300. new TableCell('9971-5-0210-0', array('rowspan' => 2, 'colspan' => 2)),
  301. 'Dante Alighieri',
  302. ),
  303. array('Charles Dickens'),
  304. new TableSeparator(),
  305. array(
  306. 'Dante Alighieri',
  307. new TableCell('9971-5-0210-0', array('rowspan' => 3, 'colspan' => 2)),
  308. ),
  309. array('J. R. R. Tolkien'),
  310. array('J. R. R'),
  311. ),
  312. 'default',
  313. <<<TABLE
  314. +------------------+--------+-----------------+
  315. | ISBN | Title | Author |
  316. +------------------+--------+-----------------+
  317. | 9971-5-0210-0 | Dante Alighieri |
  318. | | Charles Dickens |
  319. +------------------+--------+-----------------+
  320. | Dante Alighieri | 9971-5-0210-0 |
  321. | J. R. R. Tolkien | |
  322. | J. R. R | |
  323. +------------------+--------+-----------------+
  324. TABLE
  325. ),
  326. 'Cell with rowspan and colspan contains new line break' => array(
  327. array('ISBN', 'Title', 'Author'),
  328. array(
  329. array(
  330. new TableCell("9971\n-5-\n021\n0-0", array('rowspan' => 2, 'colspan' => 2)),
  331. 'Dante Alighieri',
  332. ),
  333. array('Charles Dickens'),
  334. new TableSeparator(),
  335. array(
  336. 'Dante Alighieri',
  337. new TableCell("9971\n-5-\n021\n0-0", array('rowspan' => 2, 'colspan' => 2)),
  338. ),
  339. array('Charles Dickens'),
  340. new TableSeparator(),
  341. array(
  342. new TableCell("9971\n-5-\n021\n0-0", array('rowspan' => 2, 'colspan' => 2)),
  343. new TableCell("Dante \nAlighieri", array('rowspan' => 2, 'colspan' => 1)),
  344. ),
  345. ),
  346. 'default',
  347. <<<TABLE
  348. +-----------------+-------+-----------------+
  349. | ISBN | Title | Author |
  350. +-----------------+-------+-----------------+
  351. | 9971 | Dante Alighieri |
  352. | -5- | Charles Dickens |
  353. | 021 | |
  354. | 0-0 | |
  355. +-----------------+-------+-----------------+
  356. | Dante Alighieri | 9971 |
  357. | Charles Dickens | -5- |
  358. | | 021 |
  359. | | 0-0 |
  360. +-----------------+-------+-----------------+
  361. | 9971 | Dante |
  362. | -5- | Alighieri |
  363. | 021 | |
  364. | 0-0 | |
  365. +-----------------+-------+-----------------+
  366. TABLE
  367. ),
  368. 'Cell with rowspan and colspan without using TableSeparator' => array(
  369. array('ISBN', 'Title', 'Author'),
  370. array(
  371. array(
  372. new TableCell("9971\n-5-\n021\n0-0", array('rowspan' => 2, 'colspan' => 2)),
  373. 'Dante Alighieri',
  374. ),
  375. array('Charles Dickens'),
  376. array(
  377. 'Dante Alighieri',
  378. new TableCell("9971\n-5-\n021\n0-0", array('rowspan' => 2, 'colspan' => 2)),
  379. ),
  380. array('Charles Dickens'),
  381. ),
  382. 'default',
  383. <<<TABLE
  384. +-----------------+-------+-----------------+
  385. | ISBN | Title | Author |
  386. +-----------------+-------+-----------------+
  387. | 9971 | Dante Alighieri |
  388. | -5- | Charles Dickens |
  389. | 021 | |
  390. | 0-0 | |
  391. | Dante Alighieri | 9971 |
  392. | Charles Dickens | -5- |
  393. | | 021 |
  394. | | 0-0 |
  395. +-----------------+-------+-----------------+
  396. TABLE
  397. ),
  398. 'Cell with rowspan and colspan with separator inside a rowspan' => array(
  399. array('ISBN', 'Author'),
  400. array(
  401. array(
  402. new TableCell('9971-5-0210-0', array('rowspan' => 3, 'colspan' => 1)),
  403. 'Dante Alighieri',
  404. ),
  405. array(new TableSeparator()),
  406. array('Charles Dickens'),
  407. ),
  408. 'default',
  409. <<<TABLE
  410. +---------------+-----------------+
  411. | ISBN | Author |
  412. +---------------+-----------------+
  413. | 9971-5-0210-0 | Dante Alighieri |
  414. | |-----------------|
  415. | | Charles Dickens |
  416. +---------------+-----------------+
  417. TABLE
  418. ),
  419. 'Multiple header lines' => array(
  420. array(
  421. array(new TableCell('Main title', array('colspan' => 3))),
  422. array('ISBN', 'Title', 'Author'),
  423. ),
  424. array(),
  425. 'default',
  426. <<<TABLE
  427. +------+-------+--------+
  428. | Main title |
  429. +------+-------+--------+
  430. | ISBN | Title | Author |
  431. +------+-------+--------+
  432. TABLE
  433. ),
  434. );
  435. }
  436. public function testRenderMultiByte()
  437. {
  438. if (!function_exists('mb_strlen')) {
  439. $this->markTestSkipped('The "mbstring" extension is not available');
  440. }
  441. $table = new Table($output = $this->getOutputStream());
  442. $table
  443. ->setHeaders(array('■■'))
  444. ->setRows(array(array(1234)))
  445. ->setStyle('default')
  446. ;
  447. $table->render();
  448. $expected =
  449. <<<TABLE
  450. +------+
  451. | ■■ |
  452. +------+
  453. | 1234 |
  454. +------+
  455. TABLE;
  456. $this->assertEquals($expected, $this->getOutputContent($output));
  457. }
  458. public function testStyle()
  459. {
  460. $style = new TableStyle();
  461. $style
  462. ->setHorizontalBorderChar('.')
  463. ->setVerticalBorderChar('.')
  464. ->setCrossingChar('.')
  465. ;
  466. Table::setStyleDefinition('dotfull', $style);
  467. $table = new Table($output = $this->getOutputStream());
  468. $table
  469. ->setHeaders(array('Foo'))
  470. ->setRows(array(array('Bar')))
  471. ->setStyle('dotfull');
  472. $table->render();
  473. $expected =
  474. <<<TABLE
  475. .......
  476. . Foo .
  477. .......
  478. . Bar .
  479. .......
  480. TABLE;
  481. $this->assertEquals($expected, $this->getOutputContent($output));
  482. }
  483. public function testRowSeparator()
  484. {
  485. $table = new Table($output = $this->getOutputStream());
  486. $table
  487. ->setHeaders(array('Foo'))
  488. ->setRows(array(
  489. array('Bar1'),
  490. new TableSeparator(),
  491. array('Bar2'),
  492. new TableSeparator(),
  493. array('Bar3'),
  494. ));
  495. $table->render();
  496. $expected =
  497. <<<TABLE
  498. +------+
  499. | Foo |
  500. +------+
  501. | Bar1 |
  502. +------+
  503. | Bar2 |
  504. +------+
  505. | Bar3 |
  506. +------+
  507. TABLE;
  508. $this->assertEquals($expected, $this->getOutputContent($output));
  509. $this->assertEquals($table, $table->addRow(new TableSeparator()), 'fluent interface on addRow() with a single TableSeparator() works');
  510. }
  511. protected function getOutputStream()
  512. {
  513. return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, false);
  514. }
  515. protected function getOutputContent(StreamOutput $output)
  516. {
  517. rewind($output->getStream());
  518. return str_replace(PHP_EOL, "\n", stream_get_contents($output->getStream()));
  519. }
  520. }