TableHelperTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * CakePHP : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP Project
  12. * @since 3.1.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Shell\Helper;
  16. use Cake\Console\ConsoleIo;
  17. use Cake\Shell\Helper\TableHelper;
  18. use Cake\TestSuite\Stub\ConsoleOutput;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * TableHelper test.
  22. */
  23. class TableHelperTest extends TestCase
  24. {
  25. /**
  26. * setUp method
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. $this->stub = new ConsoleOutput();
  34. $this->io = new ConsoleIo($this->stub);
  35. $this->helper = new TableHelper($this->io);
  36. }
  37. /**
  38. * Test output
  39. *
  40. * @return void
  41. */
  42. public function testDefaultOutput()
  43. {
  44. $data = [
  45. ['Header 1', 'Header', 'Long Header'],
  46. ['short', 'Longish thing', 'short'],
  47. ['Longer thing', 'short', 'Longest Value'],
  48. ];
  49. $this->helper->output($data);
  50. $expected = [
  51. '+--------------+---------------+---------------+',
  52. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  53. '+--------------+---------------+---------------+',
  54. '| short | Longish thing | short |',
  55. '| Longer thing | short | Longest Value |',
  56. '+--------------+---------------+---------------+',
  57. ];
  58. $this->assertEquals($expected, $this->stub->messages());
  59. }
  60. /**
  61. * Test output with multi-byte characters
  62. *
  63. * @return void
  64. */
  65. public function testOutputUtf8()
  66. {
  67. $data = [
  68. ['Header 1', 'Head', 'Long Header'],
  69. ['short', 'ÄÄÄÜÜÜ', 'short'],
  70. ['Longer thing', 'longerish', 'Longest Value'],
  71. ];
  72. $this->helper->output($data);
  73. $expected = [
  74. '+--------------+-----------+---------------+',
  75. '| <info>Header 1</info> | <info>Head</info> | <info>Long Header</info> |',
  76. '+--------------+-----------+---------------+',
  77. '| short | ÄÄÄÜÜÜ | short |',
  78. '| Longer thing | longerish | Longest Value |',
  79. '+--------------+-----------+---------------+',
  80. ];
  81. $this->assertEquals($expected, $this->stub->messages());
  82. }
  83. /**
  84. * Test output with multi-byte characters
  85. *
  86. * @return void
  87. */
  88. public function testOutputFullwidth()
  89. {
  90. $data = [
  91. ['Header 1', 'Head', 'Long Header'],
  92. ['short', '竜頭蛇尾', 'short'],
  93. ['Longer thing', 'longerish', 'Longest Value'],
  94. ];
  95. $this->helper->output($data);
  96. $expected = [
  97. '+--------------+-----------+---------------+',
  98. '| <info>Header 1</info> | <info>Head</info> | <info>Long Header</info> |',
  99. '+--------------+-----------+---------------+',
  100. '| short | 竜頭蛇尾 | short |',
  101. '| Longer thing | longerish | Longest Value |',
  102. '+--------------+-----------+---------------+',
  103. ];
  104. $this->assertEquals($expected, $this->stub->messages());
  105. }
  106. /**
  107. * Test output without headers
  108. *
  109. * @return void
  110. */
  111. public function testOutputWithoutHeaderStyle()
  112. {
  113. $data = [
  114. ['Header 1', 'Header', 'Long Header'],
  115. ['short', 'Longish thing', 'short'],
  116. ['Longer thing', 'short', 'Longest Value'],
  117. ];
  118. $this->helper->config(['headerStyle' => false]);
  119. $this->helper->output($data);
  120. $expected = [
  121. '+--------------+---------------+---------------+',
  122. '| Header 1 | Header | Long Header |',
  123. '+--------------+---------------+---------------+',
  124. '| short | Longish thing | short |',
  125. '| Longer thing | short | Longest Value |',
  126. '+--------------+---------------+---------------+',
  127. ];
  128. $this->assertEquals($expected, $this->stub->messages());
  129. }
  130. /**
  131. * Test output with different header style
  132. *
  133. * @return void
  134. */
  135. public function testOutputWithDifferentHeaderStyle()
  136. {
  137. $data = [
  138. ['Header 1', 'Header', 'Long Header'],
  139. ['short', 'Longish thing', 'short'],
  140. ['Longer thing', 'short', 'Longest Value'],
  141. ];
  142. $this->helper->config(['headerStyle' => 'error']);
  143. $this->helper->output($data);
  144. $expected = [
  145. '+--------------+---------------+---------------+',
  146. '| <error>Header 1</error> | <error>Header</error> | <error>Long Header</error> |',
  147. '+--------------+---------------+---------------+',
  148. '| short | Longish thing | short |',
  149. '| Longer thing | short | Longest Value |',
  150. '+--------------+---------------+---------------+',
  151. ];
  152. $this->assertEquals($expected, $this->stub->messages());
  153. }
  154. /**
  155. * Test output without table headers
  156. *
  157. * @return void
  158. */
  159. public function testOutputWithoutHeaders()
  160. {
  161. $data = [
  162. ['short', 'Longish thing', 'short'],
  163. ['Longer thing', 'short', 'Longest Value'],
  164. ];
  165. $this->helper->config(['headers' => false]);
  166. $this->helper->output($data);
  167. $expected = [
  168. '+--------------+---------------+---------------+',
  169. '| short | Longish thing | short |',
  170. '| Longer thing | short | Longest Value |',
  171. '+--------------+---------------+---------------+',
  172. ];
  173. $this->assertEquals($expected, $this->stub->messages());
  174. }
  175. /**
  176. * Test output with row separator
  177. *
  178. * @return void
  179. */
  180. public function testOutputWithRowSeparator()
  181. {
  182. $data = [
  183. ['Header 1', 'Header', 'Long Header'],
  184. ['short', 'Longish thing', 'short'],
  185. ['Longer thing', 'short', 'Longest Value']
  186. ];
  187. $this->helper->config(['rowSeparator' => true]);
  188. $this->helper->output($data);
  189. $expected = [
  190. '+--------------+---------------+---------------+',
  191. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  192. '+--------------+---------------+---------------+',
  193. '| short | Longish thing | short |',
  194. '+--------------+---------------+---------------+',
  195. '| Longer thing | short | Longest Value |',
  196. '+--------------+---------------+---------------+',
  197. ];
  198. $this->assertEquals($expected, $this->stub->messages());
  199. }
  200. /**
  201. * Test output with row separator and no headers
  202. *
  203. * @return void
  204. */
  205. public function testOutputWithRowSeparatorAndHeaders()
  206. {
  207. $data = [
  208. ['Header 1', 'Header', 'Long Header'],
  209. ['short', 'Longish thing', 'short'],
  210. ['Longer thing', 'short', 'Longest Value'],
  211. ];
  212. $this->helper->config(['rowSeparator' => true]);
  213. $this->helper->output($data);
  214. $expected = [
  215. '+--------------+---------------+---------------+',
  216. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  217. '+--------------+---------------+---------------+',
  218. '| short | Longish thing | short |',
  219. '+--------------+---------------+---------------+',
  220. '| Longer thing | short | Longest Value |',
  221. '+--------------+---------------+---------------+',
  222. ];
  223. $this->assertEquals($expected, $this->stub->messages());
  224. }
  225. /**
  226. * Test output when there is no data.
  227. */
  228. public function testOutputWithNoData()
  229. {
  230. $this->helper->output([]);
  231. $this->assertEquals([], $this->stub->messages());
  232. }
  233. /**
  234. * Test output with a header but no data.
  235. */
  236. public function testOutputWithHeaderAndNoData()
  237. {
  238. $data = [
  239. ['Header 1', 'Header', 'Long Header']
  240. ];
  241. $this->helper->output($data);
  242. $expected = [
  243. '+----------+--------+-------------+',
  244. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  245. '+----------+--------+-------------+',
  246. ];
  247. $this->assertEquals($expected, $this->stub->messages());
  248. }
  249. /**
  250. * Test no data when headers are disabled.
  251. */
  252. public function testOutputHeaderDisabledNoData()
  253. {
  254. $this->helper->config(['header' => false]);
  255. $this->helper->output([]);
  256. $this->assertEquals([], $this->stub->messages());
  257. }
  258. }