TableHelperTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 that output works when data contains nulls.
  95. */
  96. public function testNullValues()
  97. {
  98. $data = [
  99. ['Header 1', 'Header', 'Empty'],
  100. ['short', 'Longish thing', null],
  101. ['Longer thing', 'short', null],
  102. ];
  103. $this->helper->output($data);
  104. $expected = [
  105. '+--------------+---------------+-------+',
  106. '| <info>Header 1</info> | <info>Header</info> | <info>Empty</info> |',
  107. '+--------------+---------------+-------+',
  108. '| short | Longish thing | |',
  109. '| Longer thing | short | |',
  110. '+--------------+---------------+-------+',
  111. ];
  112. $this->assertEquals($expected, $this->stub->messages());
  113. }
  114. /**
  115. * Test output with multi-byte characters
  116. *
  117. * @return void
  118. */
  119. public function testOutputUtf8()
  120. {
  121. $data = [
  122. ['Header 1', 'Head', 'Long Header'],
  123. ['short', 'ÄÄÄÜÜÜ', 'short'],
  124. ['Longer thing', 'longerish', 'Longest Value'],
  125. ];
  126. $this->helper->output($data);
  127. $expected = [
  128. '+--------------+-----------+---------------+',
  129. '| <info>Header 1</info> | <info>Head</info> | <info>Long Header</info> |',
  130. '+--------------+-----------+---------------+',
  131. '| short | ÄÄÄÜÜÜ | short |',
  132. '| Longer thing | longerish | Longest Value |',
  133. '+--------------+-----------+---------------+',
  134. ];
  135. $this->assertEquals($expected, $this->stub->messages());
  136. }
  137. /**
  138. * Test output with multi-byte characters
  139. *
  140. * @return void
  141. */
  142. public function testOutputFullwidth()
  143. {
  144. $data = [
  145. ['Header 1', 'Head', 'Long Header'],
  146. ['short', '竜頭蛇尾', 'short'],
  147. ['Longer thing', 'longerish', 'Longest Value'],
  148. ];
  149. $this->helper->output($data);
  150. $expected = [
  151. '+--------------+-----------+---------------+',
  152. '| <info>Header 1</info> | <info>Head</info> | <info>Long Header</info> |',
  153. '+--------------+-----------+---------------+',
  154. '| short | 竜頭蛇尾 | short |',
  155. '| Longer thing | longerish | Longest Value |',
  156. '+--------------+-----------+---------------+',
  157. ];
  158. $this->assertEquals($expected, $this->stub->messages());
  159. }
  160. /**
  161. * Test output without headers
  162. *
  163. * @return void
  164. */
  165. public function testOutputWithoutHeaderStyle()
  166. {
  167. $data = [
  168. ['Header 1', 'Header', 'Long Header'],
  169. ['short', 'Longish thing', 'short'],
  170. ['Longer thing', 'short', 'Longest Value'],
  171. ];
  172. $this->helper->config(['headerStyle' => false]);
  173. $this->helper->output($data);
  174. $expected = [
  175. '+--------------+---------------+---------------+',
  176. '| Header 1 | Header | Long Header |',
  177. '+--------------+---------------+---------------+',
  178. '| short | Longish thing | short |',
  179. '| Longer thing | short | Longest Value |',
  180. '+--------------+---------------+---------------+',
  181. ];
  182. $this->assertEquals($expected, $this->stub->messages());
  183. }
  184. /**
  185. * Test output with different header style
  186. *
  187. * @return void
  188. */
  189. public function testOutputWithDifferentHeaderStyle()
  190. {
  191. $data = [
  192. ['Header 1', 'Header', 'Long Header'],
  193. ['short', 'Longish thing', 'short'],
  194. ['Longer thing', 'short', 'Longest Value'],
  195. ];
  196. $this->helper->config(['headerStyle' => 'error']);
  197. $this->helper->output($data);
  198. $expected = [
  199. '+--------------+---------------+---------------+',
  200. '| <error>Header 1</error> | <error>Header</error> | <error>Long Header</error> |',
  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 without table headers
  210. *
  211. * @return void
  212. */
  213. public function testOutputWithoutHeaders()
  214. {
  215. $data = [
  216. ['short', 'Longish thing', 'short'],
  217. ['Longer thing', 'short', 'Longest Value'],
  218. ];
  219. $this->helper->config(['headers' => false]);
  220. $this->helper->output($data);
  221. $expected = [
  222. '+--------------+---------------+---------------+',
  223. '| short | Longish thing | short |',
  224. '| Longer thing | short | Longest Value |',
  225. '+--------------+---------------+---------------+',
  226. ];
  227. $this->assertEquals($expected, $this->stub->messages());
  228. }
  229. /**
  230. * Test output with row separator
  231. *
  232. * @return void
  233. */
  234. public function testOutputWithRowSeparator()
  235. {
  236. $data = [
  237. ['Header 1', 'Header', 'Long Header'],
  238. ['short', 'Longish thing', 'short'],
  239. ['Longer thing', 'short', 'Longest Value']
  240. ];
  241. $this->helper->config(['rowSeparator' => true]);
  242. $this->helper->output($data);
  243. $expected = [
  244. '+--------------+---------------+---------------+',
  245. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  246. '+--------------+---------------+---------------+',
  247. '| short | Longish thing | short |',
  248. '+--------------+---------------+---------------+',
  249. '| Longer thing | short | Longest Value |',
  250. '+--------------+---------------+---------------+',
  251. ];
  252. $this->assertEquals($expected, $this->stub->messages());
  253. }
  254. /**
  255. * Test output with row separator and no headers
  256. *
  257. * @return void
  258. */
  259. public function testOutputWithRowSeparatorAndHeaders()
  260. {
  261. $data = [
  262. ['Header 1', 'Header', 'Long Header'],
  263. ['short', 'Longish thing', 'short'],
  264. ['Longer thing', 'short', 'Longest Value'],
  265. ];
  266. $this->helper->config(['rowSeparator' => true]);
  267. $this->helper->output($data);
  268. $expected = [
  269. '+--------------+---------------+---------------+',
  270. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  271. '+--------------+---------------+---------------+',
  272. '| short | Longish thing | short |',
  273. '+--------------+---------------+---------------+',
  274. '| Longer thing | short | Longest Value |',
  275. '+--------------+---------------+---------------+',
  276. ];
  277. $this->assertEquals($expected, $this->stub->messages());
  278. }
  279. /**
  280. * Test output when there is no data.
  281. */
  282. public function testOutputWithNoData()
  283. {
  284. $this->helper->output([]);
  285. $this->assertEquals([], $this->stub->messages());
  286. }
  287. /**
  288. * Test output with a header but no data.
  289. */
  290. public function testOutputWithHeaderAndNoData()
  291. {
  292. $data = [
  293. ['Header 1', 'Header', 'Long Header']
  294. ];
  295. $this->helper->output($data);
  296. $expected = [
  297. '+----------+--------+-------------+',
  298. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  299. '+----------+--------+-------------+',
  300. ];
  301. $this->assertEquals($expected, $this->stub->messages());
  302. }
  303. /**
  304. * Test no data when headers are disabled.
  305. */
  306. public function testOutputHeaderDisabledNoData()
  307. {
  308. $this->helper->config(['header' => false]);
  309. $this->helper->output([]);
  310. $this->assertEquals([], $this->stub->messages());
  311. }
  312. }