TableHelperTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. * @var ConsoleOutput
  27. */
  28. public $stub;
  29. /**
  30. * @var ConsoleIo
  31. */
  32. public $io;
  33. /**
  34. * @var TableHelper
  35. */
  36. public $helper;
  37. /**
  38. * setUp method
  39. *
  40. * @return void
  41. */
  42. public function setUp()
  43. {
  44. parent::setUp();
  45. $this->stub = new ConsoleOutput();
  46. $this->io = new ConsoleIo($this->stub);
  47. $this->helper = new TableHelper($this->io);
  48. }
  49. /**
  50. * Test output
  51. *
  52. * @return void
  53. */
  54. public function testDefaultOutput()
  55. {
  56. $data = [
  57. ['Header 1', 'Header', 'Long Header'],
  58. ['short', 'Longish thing', 'short'],
  59. ['Longer thing', 'short', 'Longest Value'],
  60. ];
  61. $this->helper->output($data);
  62. $expected = [
  63. '+--------------+---------------+---------------+',
  64. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  65. '+--------------+---------------+---------------+',
  66. '| short | Longish thing | short |',
  67. '| Longer thing | short | Longest Value |',
  68. '+--------------+---------------+---------------+',
  69. ];
  70. $this->assertEquals($expected, $this->stub->messages());
  71. }
  72. /**
  73. * Test that output works when data contains just empty strings.
  74. */
  75. public function testEmptyStrings()
  76. {
  77. $data = [
  78. ['Header 1', 'Header', 'Empty'],
  79. ['short', 'Longish thing', ''],
  80. ['Longer thing', 'short', ''],
  81. ];
  82. $this->helper->output($data);
  83. $expected = [
  84. '+--------------+---------------+-------+',
  85. '| <info>Header 1</info> | <info>Header</info> | <info>Empty</info> |',
  86. '+--------------+---------------+-------+',
  87. '| short | Longish thing | |',
  88. '| Longer thing | short | |',
  89. '+--------------+---------------+-------+',
  90. ];
  91. $this->assertEquals($expected, $this->stub->messages());
  92. }
  93. /**
  94. * Test output with multi-byte characters
  95. *
  96. * @return void
  97. */
  98. public function testOutputUtf8()
  99. {
  100. $data = [
  101. ['Header 1', 'Head', 'Long Header'],
  102. ['short', 'ÄÄÄÜÜÜ', 'short'],
  103. ['Longer thing', 'longerish', 'Longest Value'],
  104. ];
  105. $this->helper->output($data);
  106. $expected = [
  107. '+--------------+-----------+---------------+',
  108. '| <info>Header 1</info> | <info>Head</info> | <info>Long Header</info> |',
  109. '+--------------+-----------+---------------+',
  110. '| short | ÄÄÄÜÜÜ | short |',
  111. '| Longer thing | longerish | Longest Value |',
  112. '+--------------+-----------+---------------+',
  113. ];
  114. $this->assertEquals($expected, $this->stub->messages());
  115. }
  116. /**
  117. * Test output with multi-byte characters
  118. *
  119. * @return void
  120. */
  121. public function testOutputFullwidth()
  122. {
  123. $data = [
  124. ['Header 1', 'Head', 'Long Header'],
  125. ['short', '竜頭蛇尾', 'short'],
  126. ['Longer thing', 'longerish', 'Longest Value'],
  127. ];
  128. $this->helper->output($data);
  129. $expected = [
  130. '+--------------+-----------+---------------+',
  131. '| <info>Header 1</info> | <info>Head</info> | <info>Long Header</info> |',
  132. '+--------------+-----------+---------------+',
  133. '| short | 竜頭蛇尾 | short |',
  134. '| Longer thing | longerish | Longest Value |',
  135. '+--------------+-----------+---------------+',
  136. ];
  137. $this->assertEquals($expected, $this->stub->messages());
  138. }
  139. /**
  140. * Test output without headers
  141. *
  142. * @return void
  143. */
  144. public function testOutputWithoutHeaderStyle()
  145. {
  146. $data = [
  147. ['Header 1', 'Header', 'Long Header'],
  148. ['short', 'Longish thing', 'short'],
  149. ['Longer thing', 'short', 'Longest Value'],
  150. ];
  151. $this->helper->config(['headerStyle' => false]);
  152. $this->helper->output($data);
  153. $expected = [
  154. '+--------------+---------------+---------------+',
  155. '| Header 1 | Header | Long Header |',
  156. '+--------------+---------------+---------------+',
  157. '| short | Longish thing | short |',
  158. '| Longer thing | short | Longest Value |',
  159. '+--------------+---------------+---------------+',
  160. ];
  161. $this->assertEquals($expected, $this->stub->messages());
  162. }
  163. /**
  164. * Test output with different header style
  165. *
  166. * @return void
  167. */
  168. public function testOutputWithDifferentHeaderStyle()
  169. {
  170. $data = [
  171. ['Header 1', 'Header', 'Long Header'],
  172. ['short', 'Longish thing', 'short'],
  173. ['Longer thing', 'short', 'Longest Value'],
  174. ];
  175. $this->helper->config(['headerStyle' => 'error']);
  176. $this->helper->output($data);
  177. $expected = [
  178. '+--------------+---------------+---------------+',
  179. '| <error>Header 1</error> | <error>Header</error> | <error>Long Header</error> |',
  180. '+--------------+---------------+---------------+',
  181. '| short | Longish thing | short |',
  182. '| Longer thing | short | Longest Value |',
  183. '+--------------+---------------+---------------+',
  184. ];
  185. $this->assertEquals($expected, $this->stub->messages());
  186. }
  187. /**
  188. * Test output without table headers
  189. *
  190. * @return void
  191. */
  192. public function testOutputWithoutHeaders()
  193. {
  194. $data = [
  195. ['short', 'Longish thing', 'short'],
  196. ['Longer thing', 'short', 'Longest Value'],
  197. ];
  198. $this->helper->config(['headers' => false]);
  199. $this->helper->output($data);
  200. $expected = [
  201. '+--------------+---------------+---------------+',
  202. '| short | Longish thing | short |',
  203. '| Longer thing | short | Longest Value |',
  204. '+--------------+---------------+---------------+',
  205. ];
  206. $this->assertEquals($expected, $this->stub->messages());
  207. }
  208. /**
  209. * Test output with row separator
  210. *
  211. * @return void
  212. */
  213. public function testOutputWithRowSeparator()
  214. {
  215. $data = [
  216. ['Header 1', 'Header', 'Long Header'],
  217. ['short', 'Longish thing', 'short'],
  218. ['Longer thing', 'short', 'Longest Value']
  219. ];
  220. $this->helper->config(['rowSeparator' => true]);
  221. $this->helper->output($data);
  222. $expected = [
  223. '+--------------+---------------+---------------+',
  224. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  225. '+--------------+---------------+---------------+',
  226. '| short | Longish thing | short |',
  227. '+--------------+---------------+---------------+',
  228. '| Longer thing | short | Longest Value |',
  229. '+--------------+---------------+---------------+',
  230. ];
  231. $this->assertEquals($expected, $this->stub->messages());
  232. }
  233. /**
  234. * Test output with row separator and no headers
  235. *
  236. * @return void
  237. */
  238. public function testOutputWithRowSeparatorAndHeaders()
  239. {
  240. $data = [
  241. ['Header 1', 'Header', 'Long Header'],
  242. ['short', 'Longish thing', 'short'],
  243. ['Longer thing', 'short', 'Longest Value'],
  244. ];
  245. $this->helper->config(['rowSeparator' => true]);
  246. $this->helper->output($data);
  247. $expected = [
  248. '+--------------+---------------+---------------+',
  249. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  250. '+--------------+---------------+---------------+',
  251. '| short | Longish thing | short |',
  252. '+--------------+---------------+---------------+',
  253. '| Longer thing | short | Longest Value |',
  254. '+--------------+---------------+---------------+',
  255. ];
  256. $this->assertEquals($expected, $this->stub->messages());
  257. }
  258. /**
  259. * Test output when there is no data.
  260. */
  261. public function testOutputWithNoData()
  262. {
  263. $this->helper->output([]);
  264. $this->assertEquals([], $this->stub->messages());
  265. }
  266. /**
  267. * Test output with a header but no data.
  268. */
  269. public function testOutputWithHeaderAndNoData()
  270. {
  271. $data = [
  272. ['Header 1', 'Header', 'Long Header']
  273. ];
  274. $this->helper->output($data);
  275. $expected = [
  276. '+----------+--------+-------------+',
  277. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  278. '+----------+--------+-------------+',
  279. ];
  280. $this->assertEquals($expected, $this->stub->messages());
  281. }
  282. /**
  283. * Test no data when headers are disabled.
  284. */
  285. public function testOutputHeaderDisabledNoData()
  286. {
  287. $this->helper->config(['header' => false]);
  288. $this->helper->output([]);
  289. $this->assertEquals([], $this->stub->messages());
  290. }
  291. }