TableTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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\Table;
  13. use Symfony\Component\Console\Helper\TableStyle;
  14. use Symfony\Component\Console\Helper\TableSeparator;
  15. use Symfony\Component\Console\Helper\TableCell;
  16. use Symfony\Component\Console\Output\StreamOutput;
  17. class TableTest 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 testRenderProvider
  31. */
  32. public function testRender($headers, $rows, $style, $expected, $decorated = false)
  33. {
  34. $table = new Table($output = $this->getOutputStream($decorated));
  35. $table
  36. ->setHeaders($headers)
  37. ->setRows($rows)
  38. ->setStyle($style)
  39. ;
  40. $table->render();
  41. $this->assertEquals($expected, $this->getOutputContent($output));
  42. }
  43. /**
  44. * @dataProvider testRenderProvider
  45. */
  46. public function testRenderAddRows($headers, $rows, $style, $expected, $decorated = false)
  47. {
  48. $table = new Table($output = $this->getOutputStream($decorated));
  49. $table
  50. ->setHeaders($headers)
  51. ->addRows($rows)
  52. ->setStyle($style)
  53. ;
  54. $table->render();
  55. $this->assertEquals($expected, $this->getOutputContent($output));
  56. }
  57. /**
  58. * @dataProvider testRenderProvider
  59. */
  60. public function testRenderAddRowsOneByOne($headers, $rows, $style, $expected, $decorated = false)
  61. {
  62. $table = new Table($output = $this->getOutputStream($decorated));
  63. $table
  64. ->setHeaders($headers)
  65. ->setStyle($style)
  66. ;
  67. foreach ($rows as $row) {
  68. $table->addRow($row);
  69. }
  70. $table->render();
  71. $this->assertEquals($expected, $this->getOutputContent($output));
  72. }
  73. public function testRenderProvider()
  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. '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. '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. '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. '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. '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. '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. 'default',
  190. <<<'TABLE'
  191. +------+-------+
  192. | ISBN | Title |
  193. +------+-------+
  194. TABLE
  195. ),
  196. array(
  197. array(),
  198. array(),
  199. '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. '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. '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. 'Cell with colspan' => array(
  235. array('ISBN', 'Title', 'Author'),
  236. array(
  237. array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
  238. new TableSeparator(),
  239. array(new TableCell('Divine Comedy(Dante Alighieri)', array('colspan' => 3))),
  240. new TableSeparator(),
  241. array(
  242. new TableCell('Arduino: A Quick-Start Guide', array('colspan' => 2)),
  243. 'Mark Schmidt',
  244. ),
  245. new TableSeparator(),
  246. array(
  247. '9971-5-0210-0',
  248. new TableCell("A Tale of \nTwo Cities", array('colspan' => 2)),
  249. ),
  250. new TableSeparator(),
  251. array(
  252. new TableCell('Cupiditate dicta atque porro, tempora exercitationem modi animi nulla nemo vel nihil!', array('colspan' => 3)),
  253. ),
  254. ),
  255. 'default',
  256. <<<'TABLE'
  257. +-------------------------------+-------------------------------+-----------------------------+
  258. | ISBN | Title | Author |
  259. +-------------------------------+-------------------------------+-----------------------------+
  260. | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
  261. +-------------------------------+-------------------------------+-----------------------------+
  262. | Divine Comedy(Dante Alighieri) |
  263. +-------------------------------+-------------------------------+-----------------------------+
  264. | Arduino: A Quick-Start Guide | Mark Schmidt |
  265. +-------------------------------+-------------------------------+-----------------------------+
  266. | 9971-5-0210-0 | A Tale of |
  267. | | Two Cities |
  268. +-------------------------------+-------------------------------+-----------------------------+
  269. | Cupiditate dicta atque porro, tempora exercitationem modi animi nulla nemo vel nihil! |
  270. +-------------------------------+-------------------------------+-----------------------------+
  271. TABLE
  272. ),
  273. 'Cell with rowspan' => array(
  274. array('ISBN', 'Title', 'Author'),
  275. array(
  276. array(
  277. new TableCell('9971-5-0210-0', array('rowspan' => 3)),
  278. new TableCell('Divine Comedy', array('rowspan' => 2)),
  279. 'Dante Alighieri',
  280. ),
  281. array(),
  282. array("The Lord of \nthe Rings", "J. R. \nR. Tolkien"),
  283. new TableSeparator(),
  284. array('80-902734-1-6', new TableCell("And Then \nThere \nWere None", array('rowspan' => 3)), 'Agatha Christie'),
  285. array('80-902734-1-7', 'Test'),
  286. ),
  287. 'default',
  288. <<<'TABLE'
  289. +---------------+---------------+-----------------+
  290. | ISBN | Title | Author |
  291. +---------------+---------------+-----------------+
  292. | 9971-5-0210-0 | Divine Comedy | Dante Alighieri |
  293. | | | |
  294. | | The Lord of | J. R. |
  295. | | the Rings | R. Tolkien |
  296. +---------------+---------------+-----------------+
  297. | 80-902734-1-6 | And Then | Agatha Christie |
  298. | 80-902734-1-7 | There | Test |
  299. | | Were None | |
  300. +---------------+---------------+-----------------+
  301. TABLE
  302. ),
  303. 'Cell with rowspan and colspan' => array(
  304. array('ISBN', 'Title', 'Author'),
  305. array(
  306. array(
  307. new TableCell('9971-5-0210-0', array('rowspan' => 2, 'colspan' => 2)),
  308. 'Dante Alighieri',
  309. ),
  310. array('Charles Dickens'),
  311. new TableSeparator(),
  312. array(
  313. 'Dante Alighieri',
  314. new TableCell('9971-5-0210-0', array('rowspan' => 3, 'colspan' => 2)),
  315. ),
  316. array('J. R. R. Tolkien'),
  317. array('J. R. R'),
  318. ),
  319. 'default',
  320. <<<'TABLE'
  321. +------------------+---------+-----------------+
  322. | ISBN | Title | Author |
  323. +------------------+---------+-----------------+
  324. | 9971-5-0210-0 | Dante Alighieri |
  325. | | Charles Dickens |
  326. +------------------+---------+-----------------+
  327. | Dante Alighieri | 9971-5-0210-0 |
  328. | J. R. R. Tolkien | |
  329. | J. R. R | |
  330. +------------------+---------+-----------------+
  331. TABLE
  332. ),
  333. 'Cell with rowspan and colspan contains new line break' => array(
  334. array('ISBN', 'Title', 'Author'),
  335. array(
  336. array(
  337. new TableCell("9971\n-5-\n021\n0-0", array('rowspan' => 2, 'colspan' => 2)),
  338. 'Dante Alighieri',
  339. ),
  340. array('Charles Dickens'),
  341. new TableSeparator(),
  342. array(
  343. 'Dante Alighieri',
  344. new TableCell("9971\n-5-\n021\n0-0", array('rowspan' => 2, 'colspan' => 2)),
  345. ),
  346. array('Charles Dickens'),
  347. new TableSeparator(),
  348. array(
  349. new TableCell("9971\n-5-\n021\n0-0", array('rowspan' => 2, 'colspan' => 2)),
  350. new TableCell("Dante \nAlighieri", array('rowspan' => 2, 'colspan' => 1)),
  351. ),
  352. ),
  353. 'default',
  354. <<<'TABLE'
  355. +-----------------+-------+-----------------+
  356. | ISBN | Title | Author |
  357. +-----------------+-------+-----------------+
  358. | 9971 | Dante Alighieri |
  359. | -5- | Charles Dickens |
  360. | 021 | |
  361. | 0-0 | |
  362. +-----------------+-------+-----------------+
  363. | Dante Alighieri | 9971 |
  364. | Charles Dickens | -5- |
  365. | | 021 |
  366. | | 0-0 |
  367. +-----------------+-------+-----------------+
  368. | 9971 | Dante |
  369. | -5- | Alighieri |
  370. | 021 | |
  371. | 0-0 | |
  372. +-----------------+-------+-----------------+
  373. TABLE
  374. ),
  375. 'Cell with rowspan and colspan without using TableSeparator' => array(
  376. array('ISBN', 'Title', 'Author'),
  377. array(
  378. array(
  379. new TableCell("9971\n-5-\n021\n0-0", array('rowspan' => 2, 'colspan' => 2)),
  380. 'Dante Alighieri',
  381. ),
  382. array('Charles Dickens'),
  383. array(
  384. 'Dante Alighieri',
  385. new TableCell("9971\n-5-\n021\n0-0", array('rowspan' => 2, 'colspan' => 2)),
  386. ),
  387. array('Charles Dickens'),
  388. ),
  389. 'default',
  390. <<<'TABLE'
  391. +-----------------+-------+-----------------+
  392. | ISBN | Title | Author |
  393. +-----------------+-------+-----------------+
  394. | 9971 | Dante Alighieri |
  395. | -5- | Charles Dickens |
  396. | 021 | |
  397. | 0-0 | |
  398. | Dante Alighieri | 9971 |
  399. | Charles Dickens | -5- |
  400. | | 021 |
  401. | | 0-0 |
  402. +-----------------+-------+-----------------+
  403. TABLE
  404. ),
  405. 'Cell with rowspan and colspan with separator inside a rowspan' => array(
  406. array('ISBN', 'Author'),
  407. array(
  408. array(
  409. new TableCell('9971-5-0210-0', array('rowspan' => 3, 'colspan' => 1)),
  410. 'Dante Alighieri',
  411. ),
  412. array(new TableSeparator()),
  413. array('Charles Dickens'),
  414. ),
  415. 'default',
  416. <<<'TABLE'
  417. +---------------+-----------------+
  418. | ISBN | Author |
  419. +---------------+-----------------+
  420. | 9971-5-0210-0 | Dante Alighieri |
  421. | |-----------------|
  422. | | Charles Dickens |
  423. +---------------+-----------------+
  424. TABLE
  425. ),
  426. 'Multiple header lines' => array(
  427. array(
  428. array(new TableCell('Main title', array('colspan' => 3))),
  429. array('ISBN', 'Title', 'Author'),
  430. ),
  431. array(),
  432. 'default',
  433. <<<'TABLE'
  434. +------+-------+--------+
  435. | Main title |
  436. +------+-------+--------+
  437. | ISBN | Title | Author |
  438. +------+-------+--------+
  439. TABLE
  440. ),
  441. 'Row with multiple cells' => array(
  442. array(),
  443. array(
  444. array(
  445. new TableCell('1', array('colspan' => 3)),
  446. new TableCell('2', array('colspan' => 2)),
  447. new TableCell('3', array('colspan' => 2)),
  448. new TableCell('4', array('colspan' => 2)),
  449. ),
  450. ),
  451. 'default',
  452. <<<'TABLE'
  453. +---+--+--+---+--+---+--+---+--+
  454. | 1 | 2 | 3 | 4 |
  455. +---+--+--+---+--+---+--+---+--+
  456. TABLE
  457. ),
  458. 'Coslpan and table cells with comment style' => array(
  459. array(
  460. new TableCell('<comment>Long Title</comment>', array('colspan' => 3)),
  461. ),
  462. array(
  463. array(
  464. new TableCell('9971-5-0210-0', array('colspan' => 3)),
  465. ),
  466. new TableSeparator(),
  467. array(
  468. 'Dante Alighieri',
  469. 'J. R. R. Tolkien',
  470. 'J. R. R',
  471. ),
  472. ),
  473. 'default',
  474. <<<TABLE
  475. +-----------------+------------------+---------+
  476. |\033[32m \033[39m\033[33mLong Title\033[39m\033[32m \033[39m|
  477. +-----------------+------------------+---------+
  478. | 9971-5-0210-0 |
  479. +-----------------+------------------+---------+
  480. | Dante Alighieri | J. R. R. Tolkien | J. R. R |
  481. +-----------------+------------------+---------+
  482. TABLE
  483. ,
  484. true,
  485. ),
  486. );
  487. }
  488. public function testRenderMultiByte()
  489. {
  490. $table = new Table($output = $this->getOutputStream());
  491. $table
  492. ->setHeaders(array('■■'))
  493. ->setRows(array(array(1234)))
  494. ->setStyle('default')
  495. ;
  496. $table->render();
  497. $expected =
  498. <<<'TABLE'
  499. +------+
  500. | ■■ |
  501. +------+
  502. | 1234 |
  503. +------+
  504. TABLE;
  505. $this->assertEquals($expected, $this->getOutputContent($output));
  506. }
  507. public function testTableCellWithNumericIntValue()
  508. {
  509. $table = new Table($output = $this->getOutputStream());
  510. $table->setRows(array(array(new TableCell(12345))));
  511. $table->render();
  512. $expected =
  513. <<<'TABLE'
  514. +-------+
  515. | 12345 |
  516. +-------+
  517. TABLE;
  518. $this->assertEquals($expected, $this->getOutputContent($output));
  519. }
  520. public function testTableCellWithNumericFloatValue()
  521. {
  522. $table = new Table($output = $this->getOutputStream());
  523. $table->setRows(array(array(new TableCell(12345.01))));
  524. $table->render();
  525. $expected =
  526. <<<'TABLE'
  527. +----------+
  528. | 12345.01 |
  529. +----------+
  530. TABLE;
  531. $this->assertEquals($expected, $this->getOutputContent($output));
  532. }
  533. public function testStyle()
  534. {
  535. $style = new TableStyle();
  536. $style
  537. ->setHorizontalBorderChar('.')
  538. ->setVerticalBorderChar('.')
  539. ->setCrossingChar('.')
  540. ;
  541. Table::setStyleDefinition('dotfull', $style);
  542. $table = new Table($output = $this->getOutputStream());
  543. $table
  544. ->setHeaders(array('Foo'))
  545. ->setRows(array(array('Bar')))
  546. ->setStyle('dotfull');
  547. $table->render();
  548. $expected =
  549. <<<'TABLE'
  550. .......
  551. . Foo .
  552. .......
  553. . Bar .
  554. .......
  555. TABLE;
  556. $this->assertEquals($expected, $this->getOutputContent($output));
  557. }
  558. public function testRowSeparator()
  559. {
  560. $table = new Table($output = $this->getOutputStream());
  561. $table
  562. ->setHeaders(array('Foo'))
  563. ->setRows(array(
  564. array('Bar1'),
  565. new TableSeparator(),
  566. array('Bar2'),
  567. new TableSeparator(),
  568. array('Bar3'),
  569. ));
  570. $table->render();
  571. $expected =
  572. <<<'TABLE'
  573. +------+
  574. | Foo |
  575. +------+
  576. | Bar1 |
  577. +------+
  578. | Bar2 |
  579. +------+
  580. | Bar3 |
  581. +------+
  582. TABLE;
  583. $this->assertEquals($expected, $this->getOutputContent($output));
  584. $this->assertEquals($table, $table->addRow(new TableSeparator()), 'fluent interface on addRow() with a single TableSeparator() works');
  585. }
  586. public function testRenderMultiCalls()
  587. {
  588. $table = new Table($output = $this->getOutputStream());
  589. $table->setRows(array(
  590. array(new TableCell('foo', array('colspan' => 2))),
  591. ));
  592. $table->render();
  593. $table->render();
  594. $table->render();
  595. $expected =
  596. <<<TABLE
  597. +----+---+
  598. | foo |
  599. +----+---+
  600. +----+---+
  601. | foo |
  602. +----+---+
  603. +----+---+
  604. | foo |
  605. +----+---+
  606. TABLE;
  607. $this->assertEquals($expected, $this->getOutputContent($output));
  608. }
  609. public function testColumnStyle()
  610. {
  611. $table = new Table($output = $this->getOutputStream());
  612. $table
  613. ->setHeaders(array('ISBN', 'Title', 'Author', 'Price'))
  614. ->setRows(array(
  615. array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri', '9.95'),
  616. array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens', '139.25'),
  617. ));
  618. $style = new TableStyle();
  619. $style->setPadType(STR_PAD_LEFT);
  620. $table->setColumnStyle(3, $style);
  621. $table->render();
  622. $expected =
  623. <<<TABLE
  624. +---------------+----------------------+-----------------+--------+
  625. | ISBN | Title | Author | Price |
  626. +---------------+----------------------+-----------------+--------+
  627. | 99921-58-10-7 | Divine Comedy | Dante Alighieri | 9.95 |
  628. | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | 139.25 |
  629. +---------------+----------------------+-----------------+--------+
  630. TABLE;
  631. $this->assertEquals($expected, $this->getOutputContent($output));
  632. }
  633. public function testColumnWith()
  634. {
  635. $table = new Table($output = $this->getOutputStream());
  636. $table
  637. ->setHeaders(array('ISBN', 'Title', 'Author', 'Price'))
  638. ->setRows(array(
  639. array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri', '9.95'),
  640. array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens', '139.25'),
  641. ))
  642. ->setColumnWidth(0, 15)
  643. ->setColumnWidth(3, 10);
  644. $style = new TableStyle();
  645. $style->setPadType(STR_PAD_LEFT);
  646. $table->setColumnStyle(3, $style);
  647. $table->render();
  648. $expected =
  649. <<<TABLE
  650. +-----------------+----------------------+-----------------+------------+
  651. | ISBN | Title | Author | Price |
  652. +-----------------+----------------------+-----------------+------------+
  653. | 99921-58-10-7 | Divine Comedy | Dante Alighieri | 9.95 |
  654. | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | 139.25 |
  655. +-----------------+----------------------+-----------------+------------+
  656. TABLE;
  657. $this->assertEquals($expected, $this->getOutputContent($output));
  658. }
  659. public function testColumnWiths()
  660. {
  661. $table = new Table($output = $this->getOutputStream());
  662. $table
  663. ->setHeaders(array('ISBN', 'Title', 'Author', 'Price'))
  664. ->setRows(array(
  665. array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri', '9.95'),
  666. array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens', '139.25'),
  667. ))
  668. ->setColumnWidths(array(15, 0, -1, 10));
  669. $style = new TableStyle();
  670. $style->setPadType(STR_PAD_LEFT);
  671. $table->setColumnStyle(3, $style);
  672. $table->render();
  673. $expected =
  674. <<<TABLE
  675. +-----------------+----------------------+-----------------+------------+
  676. | ISBN | Title | Author | Price |
  677. +-----------------+----------------------+-----------------+------------+
  678. | 99921-58-10-7 | Divine Comedy | Dante Alighieri | 9.95 |
  679. | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | 139.25 |
  680. +-----------------+----------------------+-----------------+------------+
  681. TABLE;
  682. $this->assertEquals($expected, $this->getOutputContent($output));
  683. }
  684. /**
  685. * @expectedException \Symfony\Component\Console\Exception\InvalidArgumentException
  686. * @expectedExceptionMessage Style "absent" is not defined.
  687. */
  688. public function testIsNotDefinedStyleException()
  689. {
  690. $table = new Table($this->getOutputStream());
  691. $table->setStyle('absent');
  692. }
  693. /**
  694. * @expectedException \Symfony\Component\Console\Exception\InvalidArgumentException
  695. * @expectedExceptionMessage Style "absent" is not defined.
  696. */
  697. public function testGetStyleDefinition()
  698. {
  699. Table::getStyleDefinition('absent');
  700. }
  701. protected function getOutputStream($decorated = false)
  702. {
  703. return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, $decorated);
  704. }
  705. protected function getOutputContent(StreamOutput $output)
  706. {
  707. rewind($output->getStream());
  708. return str_replace(PHP_EOL, "\n", stream_get_contents($output->getStream()));
  709. }
  710. }