TableHelperTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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\TableHelper;
  12. use Symfony\Component\Console\Output\StreamOutput;
  13. class TableHelperTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $stream;
  16. protected function setUp()
  17. {
  18. $this->stream = fopen('php://memory', 'r+');
  19. }
  20. protected function tearDown()
  21. {
  22. fclose($this->stream);
  23. $this->stream = null;
  24. }
  25. /**
  26. * @dataProvider testRenderProvider
  27. */
  28. public function testRender($headers, $rows, $layout, $expected)
  29. {
  30. $table = new TableHelper();
  31. $table
  32. ->setHeaders($headers)
  33. ->setRows($rows)
  34. ->setLayout($layout)
  35. ;
  36. $table->render($output = $this->getOutputStream());
  37. $this->assertEquals($expected, $this->getOutputContent($output));
  38. }
  39. /**
  40. * @dataProvider testRenderProvider
  41. */
  42. public function testRenderAddRows($headers, $rows, $layout, $expected)
  43. {
  44. $table = new TableHelper();
  45. $table
  46. ->setHeaders($headers)
  47. ->addRows($rows)
  48. ->setLayout($layout)
  49. ;
  50. $table->render($output = $this->getOutputStream());
  51. $this->assertEquals($expected, $this->getOutputContent($output));
  52. }
  53. /**
  54. * @dataProvider testRenderProvider
  55. */
  56. public function testRenderAddRowsOneByOne($headers, $rows, $layout, $expected)
  57. {
  58. $table = new TableHelper();
  59. $table
  60. ->setHeaders($headers)
  61. ->setLayout($layout)
  62. ;
  63. foreach ($rows as $row) {
  64. $table->addRow($row);
  65. }
  66. $table->render($output = $this->getOutputStream());
  67. $this->assertEquals($expected, $this->getOutputContent($output));
  68. }
  69. public function testRenderProvider()
  70. {
  71. return array(
  72. array(
  73. array('ISBN', 'Title', 'Author'),
  74. 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. TableHelper::LAYOUT_DEFAULT,
  81. <<<TABLE
  82. +---------------+--------------------------+------------------+
  83. | ISBN | Title | Author |
  84. +---------------+--------------------------+------------------+
  85. | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
  86. | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
  87. | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
  88. | 80-902734-1-6 | And Then There Were None | Agatha Christie |
  89. +---------------+--------------------------+------------------+
  90. TABLE
  91. ),
  92. array(
  93. array('ISBN', 'Title', 'Author'),
  94. array(
  95. array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
  96. array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
  97. array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
  98. array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
  99. ),
  100. TableHelper::LAYOUT_BORDERLESS,
  101. " =============== ========================== ================== \n ISBN Title Author \n =============== ========================== ================== \n 99921-58-10-7 Divine Comedy Dante Alighieri \n 9971-5-0210-0 A Tale of Two Cities Charles Dickens \n 960-425-059-0 The Lord of the Rings J. R. R. Tolkien \n 80-902734-1-6 And Then There Were None Agatha Christie \n =============== ========================== ================== \n"
  102. ),
  103. array(
  104. array('ISBN', 'Title'),
  105. array(
  106. array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
  107. array('9971-5-0210-0'),
  108. array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
  109. array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
  110. ),
  111. TableHelper::LAYOUT_DEFAULT,
  112. <<<TABLE
  113. +---------------+--------------------------+------------------+
  114. | ISBN | Title | |
  115. +---------------+--------------------------+------------------+
  116. | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
  117. | 9971-5-0210-0 | | |
  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(),
  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. TableHelper::LAYOUT_DEFAULT,
  132. <<<TABLE
  133. +---------------+--------------------------+------------------+
  134. | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
  135. | 9971-5-0210-0 | | |
  136. | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
  137. | 80-902734-1-6 | And Then There Were None | Agatha Christie |
  138. +---------------+--------------------------+------------------+
  139. TABLE
  140. ),
  141. array(
  142. array('ISBN', 'Title'),
  143. array(),
  144. TableHelper::LAYOUT_DEFAULT,
  145. <<<TABLE
  146. +------+-------+
  147. | ISBN | Title |
  148. +------+-------+
  149. TABLE
  150. ),
  151. array(
  152. array(),
  153. array(),
  154. TableHelper::LAYOUT_DEFAULT,
  155. '',
  156. ),
  157. );
  158. }
  159. public function testRenderMultiByte()
  160. {
  161. if (!function_exists('mb_strlen')) {
  162. $this->markTestSkipped('The "mbstring" extension is not available');
  163. }
  164. $table = new TableHelper();
  165. $table
  166. ->setHeaders(array('■■'))
  167. ->setRows(array(array(1234)))
  168. ->setLayout(TableHelper::LAYOUT_DEFAULT)
  169. ;
  170. $table->render($output = $this->getOutputStream());
  171. $expected =
  172. <<<TABLE
  173. +------+
  174. | ■■ |
  175. +------+
  176. | 1234 |
  177. +------+
  178. TABLE;
  179. $this->assertEquals($expected, $this->getOutputContent($output));
  180. }
  181. protected function getOutputStream()
  182. {
  183. return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, false);
  184. }
  185. protected function getOutputContent(StreamOutput $output)
  186. {
  187. rewind($output->getStream());
  188. return str_replace(PHP_EOL, "\n", stream_get_contents($output->getStream()));
  189. }
  190. }