/vendor/symfony/console/Tests/Helper/TableTest.php

https://gitlab.com/Pasantias/pasantiasASLG · PHP · 585 lines · 517 code · 48 blank · 20 comment · 0 complexity · 361bf3347cbb0c647a7785a44bc583f1 MD5 · raw file

  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. 'Row with multiple cells' => array(
  435. array(),
  436. array(
  437. array(
  438. new TableCell('1', array('colspan' => 3)),
  439. new TableCell('2', array('colspan' => 2)),
  440. new TableCell('3', array('colspan' => 2)),
  441. new TableCell('4', array('colspan' => 2)),
  442. ),
  443. ),
  444. 'default',
  445. <<<TABLE
  446. +--+--+--+--+--+--+--+--+--+
  447. | 1 | 2 | 3 | 4 |
  448. +--+--+--+--+--+--+--+--+--+
  449. TABLE
  450. ),
  451. );
  452. }
  453. /**
  454. * @requires extension mbstring
  455. */
  456. public function testRenderMultiByte()
  457. {
  458. $table = new Table($output = $this->getOutputStream());
  459. $table
  460. ->setHeaders(array('■■'))
  461. ->setRows(array(array(1234)))
  462. ->setStyle('default')
  463. ;
  464. $table->render();
  465. $expected =
  466. <<<TABLE
  467. +------+
  468. | |
  469. +------+
  470. | 1234 |
  471. +------+
  472. TABLE;
  473. $this->assertEquals($expected, $this->getOutputContent($output));
  474. }
  475. public function testStyle()
  476. {
  477. $style = new TableStyle();
  478. $style
  479. ->setHorizontalBorderChar('.')
  480. ->setVerticalBorderChar('.')
  481. ->setCrossingChar('.')
  482. ;
  483. Table::setStyleDefinition('dotfull', $style);
  484. $table = new Table($output = $this->getOutputStream());
  485. $table
  486. ->setHeaders(array('Foo'))
  487. ->setRows(array(array('Bar')))
  488. ->setStyle('dotfull');
  489. $table->render();
  490. $expected =
  491. <<<TABLE
  492. .......
  493. . Foo .
  494. .......
  495. . Bar .
  496. .......
  497. TABLE;
  498. $this->assertEquals($expected, $this->getOutputContent($output));
  499. }
  500. public function testRowSeparator()
  501. {
  502. $table = new Table($output = $this->getOutputStream());
  503. $table
  504. ->setHeaders(array('Foo'))
  505. ->setRows(array(
  506. array('Bar1'),
  507. new TableSeparator(),
  508. array('Bar2'),
  509. new TableSeparator(),
  510. array('Bar3'),
  511. ));
  512. $table->render();
  513. $expected =
  514. <<<TABLE
  515. +------+
  516. | Foo |
  517. +------+
  518. | Bar1 |
  519. +------+
  520. | Bar2 |
  521. +------+
  522. | Bar3 |
  523. +------+
  524. TABLE;
  525. $this->assertEquals($expected, $this->getOutputContent($output));
  526. $this->assertEquals($table, $table->addRow(new TableSeparator()), 'fluent interface on addRow() with a single TableSeparator() works');
  527. }
  528. protected function getOutputStream()
  529. {
  530. return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, false);
  531. }
  532. protected function getOutputContent(StreamOutput $output)
  533. {
  534. rewind($output->getStream());
  535. return str_replace(PHP_EOL, "\n", stream_get_contents($output->getStream()));
  536. }
  537. }