TableTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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. new TableSeparator(),
  250. array(
  251. new TableCell('Cupiditate dicta atque porro, tempora exercitationem modi animi nulla nemo vel nihil!', array('colspan' => 3)),
  252. ),
  253. ),
  254. 'default',
  255. <<<'TABLE'
  256. +-------------------------------+-------------------------------+-----------------------------+
  257. | ISBN | Title | Author |
  258. +-------------------------------+-------------------------------+-----------------------------+
  259. | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
  260. +-------------------------------+-------------------------------+-----------------------------+
  261. | Divine Comedy(Dante Alighieri) |
  262. +-------------------------------+-------------------------------+-----------------------------+
  263. | Arduino: A Quick-Start Guide | Mark Schmidt |
  264. +-------------------------------+-------------------------------+-----------------------------+
  265. | 9971-5-0210-0 | A Tale of |
  266. | | Two Cities |
  267. +-------------------------------+-------------------------------+-----------------------------+
  268. | Cupiditate dicta atque porro, tempora exercitationem modi animi nulla nemo vel nihil! |
  269. +-------------------------------+-------------------------------+-----------------------------+
  270. TABLE
  271. ),
  272. 'Cell with rowspan' => array(
  273. array('ISBN', 'Title', 'Author'),
  274. array(
  275. array(
  276. new TableCell('9971-5-0210-0', array('rowspan' => 3)),
  277. 'Divine Comedy',
  278. 'Dante Alighieri',
  279. ),
  280. array('A Tale of Two Cities', 'Charles Dickens'),
  281. array("The Lord of \nthe Rings", "J. R. \nR. Tolkien"),
  282. new TableSeparator(),
  283. array('80-902734-1-6', new TableCell("And Then \nThere \nWere None", array('rowspan' => 3)), 'Agatha Christie'),
  284. array('80-902734-1-7', 'Test'),
  285. ),
  286. 'default',
  287. <<<'TABLE'
  288. +---------------+----------------------+-----------------+
  289. | ISBN | Title | Author |
  290. +---------------+----------------------+-----------------+
  291. | 9971-5-0210-0 | Divine Comedy | Dante Alighieri |
  292. | | A Tale of Two Cities | Charles Dickens |
  293. | | The Lord of | J. R. |
  294. | | the Rings | R. Tolkien |
  295. +---------------+----------------------+-----------------+
  296. | 80-902734-1-6 | And Then | Agatha Christie |
  297. | 80-902734-1-7 | There | Test |
  298. | | Were None | |
  299. +---------------+----------------------+-----------------+
  300. TABLE
  301. ),
  302. 'Cell with rowspan and colspan' => array(
  303. array('ISBN', 'Title', 'Author'),
  304. array(
  305. array(
  306. new TableCell('9971-5-0210-0', array('rowspan' => 2, 'colspan' => 2)),
  307. 'Dante Alighieri',
  308. ),
  309. array('Charles Dickens'),
  310. new TableSeparator(),
  311. array(
  312. 'Dante Alighieri',
  313. new TableCell('9971-5-0210-0', array('rowspan' => 3, 'colspan' => 2)),
  314. ),
  315. array('J. R. R. Tolkien'),
  316. array('J. R. R'),
  317. ),
  318. 'default',
  319. <<<'TABLE'
  320. +------------------+---------+-----------------+
  321. | ISBN | Title | Author |
  322. +------------------+---------+-----------------+
  323. | 9971-5-0210-0 | Dante Alighieri |
  324. | | Charles Dickens |
  325. +------------------+---------+-----------------+
  326. | Dante Alighieri | 9971-5-0210-0 |
  327. | J. R. R. Tolkien | |
  328. | J. R. R | |
  329. +------------------+---------+-----------------+
  330. TABLE
  331. ),
  332. 'Cell with rowspan and colspan contains new line break' => array(
  333. array('ISBN', 'Title', 'Author'),
  334. array(
  335. array(
  336. new TableCell("9971\n-5-\n021\n0-0", array('rowspan' => 2, 'colspan' => 2)),
  337. 'Dante Alighieri',
  338. ),
  339. array('Charles Dickens'),
  340. new TableSeparator(),
  341. array(
  342. 'Dante Alighieri',
  343. new TableCell("9971\n-5-\n021\n0-0", array('rowspan' => 2, 'colspan' => 2)),
  344. ),
  345. array('Charles Dickens'),
  346. new TableSeparator(),
  347. array(
  348. new TableCell("9971\n-5-\n021\n0-0", array('rowspan' => 2, 'colspan' => 2)),
  349. new TableCell("Dante \nAlighieri", array('rowspan' => 2, 'colspan' => 1)),
  350. ),
  351. ),
  352. 'default',
  353. <<<'TABLE'
  354. +-----------------+-------+-----------------+
  355. | ISBN | Title | Author |
  356. +-----------------+-------+-----------------+
  357. | 9971 | Dante Alighieri |
  358. | -5- | Charles Dickens |
  359. | 021 | |
  360. | 0-0 | |
  361. +-----------------+-------+-----------------+
  362. | Dante Alighieri | 9971 |
  363. | Charles Dickens | -5- |
  364. | | 021 |
  365. | | 0-0 |
  366. +-----------------+-------+-----------------+
  367. | 9971 | Dante |
  368. | -5- | Alighieri |
  369. | 021 | |
  370. | 0-0 | |
  371. +-----------------+-------+-----------------+
  372. TABLE
  373. ),
  374. 'Cell with rowspan and colspan without using TableSeparator' => array(
  375. array('ISBN', 'Title', 'Author'),
  376. array(
  377. array(
  378. new TableCell("9971\n-5-\n021\n0-0", array('rowspan' => 2, 'colspan' => 2)),
  379. 'Dante Alighieri',
  380. ),
  381. array('Charles Dickens'),
  382. array(
  383. 'Dante Alighieri',
  384. new TableCell("9971\n-5-\n021\n0-0", array('rowspan' => 2, 'colspan' => 2)),
  385. ),
  386. array('Charles Dickens'),
  387. ),
  388. 'default',
  389. <<<'TABLE'
  390. +-----------------+-------+-----------------+
  391. | ISBN | Title | Author |
  392. +-----------------+-------+-----------------+
  393. | 9971 | Dante Alighieri |
  394. | -5- | Charles Dickens |
  395. | 021 | |
  396. | 0-0 | |
  397. | Dante Alighieri | 9971 |
  398. | Charles Dickens | -5- |
  399. | | 021 |
  400. | | 0-0 |
  401. +-----------------+-------+-----------------+
  402. TABLE
  403. ),
  404. 'Cell with rowspan and colspan with separator inside a rowspan' => array(
  405. array('ISBN', 'Author'),
  406. array(
  407. array(
  408. new TableCell('9971-5-0210-0', array('rowspan' => 3, 'colspan' => 1)),
  409. 'Dante Alighieri',
  410. ),
  411. array(new TableSeparator()),
  412. array('Charles Dickens'),
  413. ),
  414. 'default',
  415. <<<'TABLE'
  416. +---------------+-----------------+
  417. | ISBN | Author |
  418. +---------------+-----------------+
  419. | 9971-5-0210-0 | Dante Alighieri |
  420. | |-----------------|
  421. | | Charles Dickens |
  422. +---------------+-----------------+
  423. TABLE
  424. ),
  425. 'Multiple header lines' => array(
  426. array(
  427. array(new TableCell('Main title', array('colspan' => 3))),
  428. array('ISBN', 'Title', 'Author'),
  429. ),
  430. array(),
  431. 'default',
  432. <<<'TABLE'
  433. +------+-------+--------+
  434. | Main title |
  435. +------+-------+--------+
  436. | ISBN | Title | Author |
  437. +------+-------+--------+
  438. TABLE
  439. ),
  440. 'Row with multiple cells' => array(
  441. array(),
  442. array(
  443. array(
  444. new TableCell('1', array('colspan' => 3)),
  445. new TableCell('2', array('colspan' => 2)),
  446. new TableCell('3', array('colspan' => 2)),
  447. new TableCell('4', array('colspan' => 2)),
  448. ),
  449. ),
  450. 'default',
  451. <<<'TABLE'
  452. +---+--+--+---+--+---+--+---+--+
  453. | 1 | 2 | 3 | 4 |
  454. +---+--+--+---+--+---+--+---+--+
  455. TABLE
  456. ),
  457. );
  458. }
  459. public function testRenderMultiByte()
  460. {
  461. $table = new Table($output = $this->getOutputStream());
  462. $table
  463. ->setHeaders(array('■■'))
  464. ->setRows(array(array(1234)))
  465. ->setStyle('default')
  466. ;
  467. $table->render();
  468. $expected =
  469. <<<'TABLE'
  470. +------+
  471. | ■■ |
  472. +------+
  473. | 1234 |
  474. +------+
  475. TABLE;
  476. $this->assertEquals($expected, $this->getOutputContent($output));
  477. }
  478. public function testStyle()
  479. {
  480. $style = new TableStyle();
  481. $style
  482. ->setHorizontalBorderChar('.')
  483. ->setVerticalBorderChar('.')
  484. ->setCrossingChar('.')
  485. ;
  486. Table::setStyleDefinition('dotfull', $style);
  487. $table = new Table($output = $this->getOutputStream());
  488. $table
  489. ->setHeaders(array('Foo'))
  490. ->setRows(array(array('Bar')))
  491. ->setStyle('dotfull');
  492. $table->render();
  493. $expected =
  494. <<<'TABLE'
  495. .......
  496. . Foo .
  497. .......
  498. . Bar .
  499. .......
  500. TABLE;
  501. $this->assertEquals($expected, $this->getOutputContent($output));
  502. }
  503. public function testRowSeparator()
  504. {
  505. $table = new Table($output = $this->getOutputStream());
  506. $table
  507. ->setHeaders(array('Foo'))
  508. ->setRows(array(
  509. array('Bar1'),
  510. new TableSeparator(),
  511. array('Bar2'),
  512. new TableSeparator(),
  513. array('Bar3'),
  514. ));
  515. $table->render();
  516. $expected =
  517. <<<'TABLE'
  518. +------+
  519. | Foo |
  520. +------+
  521. | Bar1 |
  522. +------+
  523. | Bar2 |
  524. +------+
  525. | Bar3 |
  526. +------+
  527. TABLE;
  528. $this->assertEquals($expected, $this->getOutputContent($output));
  529. $this->assertEquals($table, $table->addRow(new TableSeparator()), 'fluent interface on addRow() with a single TableSeparator() works');
  530. }
  531. public function testRenderMultiCalls()
  532. {
  533. $table = new Table($output = $this->getOutputStream());
  534. $table->setRows(array(
  535. array(new TableCell('foo', array('colspan' => 2))),
  536. ));
  537. $table->render();
  538. $table->render();
  539. $table->render();
  540. $expected =
  541. <<<TABLE
  542. +----+---+
  543. | foo |
  544. +----+---+
  545. +----+---+
  546. | foo |
  547. +----+---+
  548. +----+---+
  549. | foo |
  550. +----+---+
  551. TABLE;
  552. $this->assertEquals($expected, $this->getOutputContent($output));
  553. }
  554. public function testColumnStyle()
  555. {
  556. $table = new Table($output = $this->getOutputStream());
  557. $table
  558. ->setHeaders(array('ISBN', 'Title', 'Author', 'Price'))
  559. ->setRows(array(
  560. array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri', '9.95'),
  561. array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens', '139.25'),
  562. ));
  563. $style = new TableStyle();
  564. $style->setPadType(STR_PAD_LEFT);
  565. $table->setColumnStyle(3, $style);
  566. $table->render();
  567. $expected =
  568. <<<TABLE
  569. +---------------+----------------------+-----------------+--------+
  570. | ISBN | Title | Author | Price |
  571. +---------------+----------------------+-----------------+--------+
  572. | 99921-58-10-7 | Divine Comedy | Dante Alighieri | 9.95 |
  573. | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | 139.25 |
  574. +---------------+----------------------+-----------------+--------+
  575. TABLE;
  576. $this->assertEquals($expected, $this->getOutputContent($output));
  577. }
  578. public function testColumnWith()
  579. {
  580. $table = new Table($output = $this->getOutputStream());
  581. $table
  582. ->setHeaders(array('ISBN', 'Title', 'Author', 'Price'))
  583. ->setRows(array(
  584. array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri', '9.95'),
  585. array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens', '139.25'),
  586. ))
  587. ->setColumnWidth(0, 15)
  588. ->setColumnWidth(3, 10);
  589. $style = new TableStyle();
  590. $style->setPadType(STR_PAD_LEFT);
  591. $table->setColumnStyle(3, $style);
  592. $table->render();
  593. $expected =
  594. <<<TABLE
  595. +-----------------+----------------------+-----------------+------------+
  596. | ISBN | Title | Author | Price |
  597. +-----------------+----------------------+-----------------+------------+
  598. | 99921-58-10-7 | Divine Comedy | Dante Alighieri | 9.95 |
  599. | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | 139.25 |
  600. +-----------------+----------------------+-----------------+------------+
  601. TABLE;
  602. $this->assertEquals($expected, $this->getOutputContent($output));
  603. }
  604. public function testColumnWiths()
  605. {
  606. $table = new Table($output = $this->getOutputStream());
  607. $table
  608. ->setHeaders(array('ISBN', 'Title', 'Author', 'Price'))
  609. ->setRows(array(
  610. array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri', '9.95'),
  611. array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens', '139.25'),
  612. ))
  613. ->setColumnWidths(array(15, 0, -1, 10));
  614. $style = new TableStyle();
  615. $style->setPadType(STR_PAD_LEFT);
  616. $table->setColumnStyle(3, $style);
  617. $table->render();
  618. $expected =
  619. <<<TABLE
  620. +-----------------+----------------------+-----------------+------------+
  621. | ISBN | Title | Author | Price |
  622. +-----------------+----------------------+-----------------+------------+
  623. | 99921-58-10-7 | Divine Comedy | Dante Alighieri | 9.95 |
  624. | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | 139.25 |
  625. +-----------------+----------------------+-----------------+------------+
  626. TABLE;
  627. $this->assertEquals($expected, $this->getOutputContent($output));
  628. }
  629. /**
  630. * @expectedException \Symfony\Component\Console\Exception\InvalidArgumentException
  631. * @expectedExceptionMessage Style "absent" is not defined.
  632. */
  633. public function testIsNotDefinedStyleException()
  634. {
  635. $table = new Table($this->getOutputStream());
  636. $table->setStyle('absent');
  637. }
  638. /**
  639. * @expectedException \Symfony\Component\Console\Exception\InvalidArgumentException
  640. * @expectedExceptionMessage Style "absent" is not defined.
  641. */
  642. public function testGetStyleDefinition()
  643. {
  644. Table::getStyleDefinition('absent');
  645. }
  646. protected function getOutputStream()
  647. {
  648. return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, false);
  649. }
  650. protected function getOutputContent(StreamOutput $output)
  651. {
  652. rewind($output->getStream());
  653. return str_replace(PHP_EOL, "\n", stream_get_contents($output->getStream()));
  654. }
  655. }