PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/console/Symfony/Component/Console/Tests/Helper/TableTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 359 lines | 301 code | 41 blank | 17 comment | 1 complexity | 479a296e4937cda1ca0a145e94805f67 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\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. $this->assertEquals($table, $table->addRow(new TableSeparator()), 'fluent interface on addRow() with a single TableSeparator() works');
  308. }
  309. protected function getOutputStream()
  310. {
  311. return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, false);
  312. }
  313. protected function getOutputContent(StreamOutput $output)
  314. {
  315. rewind($output->getStream());
  316. return str_replace(PHP_EOL, "\n", stream_get_contents($output->getStream()));
  317. }
  318. }