TableHelperTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * CakePHP : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP Project
  13. * @since 3.1.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Shell\Helper;
  17. use Cake\Console\ConsoleIo;
  18. use Cake\Shell\Helper\TableHelper;
  19. use Cake\TestSuite\Stub\ConsoleOutput;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * TableHelper test.
  23. */
  24. class TableHelperTest extends TestCase
  25. {
  26. /**
  27. * @var \Cake\Console\ConsoleOutput
  28. */
  29. public $stub;
  30. /**
  31. * @var \Cake\Console\ConsoleIo
  32. */
  33. public $io;
  34. /**
  35. * @var \Cake\Shell\Helper\TableHelper
  36. */
  37. public $helper;
  38. /**
  39. * setUp method
  40. *
  41. * @return void
  42. */
  43. public function setUp()
  44. {
  45. parent::setUp();
  46. $this->stub = new ConsoleOutput();
  47. $this->io = new ConsoleIo($this->stub);
  48. $this->helper = new TableHelper($this->io);
  49. }
  50. /**
  51. * Test output
  52. *
  53. * @return void
  54. */
  55. public function testOutputDefaultOutput()
  56. {
  57. $data = [
  58. ['Header 1', 'Header', 'Long Header'],
  59. ['short', 'Longish thing', 'short'],
  60. ['Longer thing', 'short', 'Longest Value'],
  61. ];
  62. $this->helper->output($data);
  63. $expected = [
  64. '+--------------+---------------+---------------+',
  65. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  66. '+--------------+---------------+---------------+',
  67. '| short | Longish thing | short |',
  68. '| Longer thing | short | Longest Value |',
  69. '+--------------+---------------+---------------+',
  70. ];
  71. $this->assertEquals($expected, $this->stub->messages());
  72. }
  73. /**
  74. * Test output with inconsistent keys.
  75. *
  76. * When outputting entities or other structured data,
  77. * headers shouldn't need to have the same keys as it is
  78. * annoying to use.
  79. *
  80. * @return void
  81. */
  82. public function testOutputInconsistentKeys()
  83. {
  84. $data = [
  85. ['Header 1', 'Header', 'Long Header'],
  86. ['a' => 'short', 'b' => 'Longish thing', 'c' => 'short'],
  87. ['c' => 'Longer thing', 'a' => 'short', 'b' => 'Longest Value'],
  88. ];
  89. $this->helper->output($data);
  90. $expected = [
  91. '+--------------+---------------+---------------+',
  92. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  93. '+--------------+---------------+---------------+',
  94. '| short | Longish thing | short |',
  95. '| Longer thing | short | Longest Value |',
  96. '+--------------+---------------+---------------+',
  97. ];
  98. $this->assertEquals($expected, $this->stub->messages());
  99. }
  100. /**
  101. * Test that output works when data contains just empty strings.
  102. *
  103. * @return void
  104. */
  105. public function testOutputEmptyStrings()
  106. {
  107. $data = [
  108. ['Header 1', 'Header', 'Empty'],
  109. ['short', 'Longish thing', ''],
  110. ['Longer thing', 'short', ''],
  111. ];
  112. $this->helper->output($data);
  113. $expected = [
  114. '+--------------+---------------+-------+',
  115. '| <info>Header 1</info> | <info>Header</info> | <info>Empty</info> |',
  116. '+--------------+---------------+-------+',
  117. '| short | Longish thing | |',
  118. '| Longer thing | short | |',
  119. '+--------------+---------------+-------+',
  120. ];
  121. $this->assertEquals($expected, $this->stub->messages());
  122. }
  123. /**
  124. * Test that output works when data contains nulls.
  125. */
  126. public function testNullValues()
  127. {
  128. $data = [
  129. ['Header 1', 'Header', 'Empty'],
  130. ['short', 'Longish thing', null],
  131. ['Longer thing', 'short', null],
  132. ];
  133. $this->helper->output($data);
  134. $expected = [
  135. '+--------------+---------------+-------+',
  136. '| <info>Header 1</info> | <info>Header</info> | <info>Empty</info> |',
  137. '+--------------+---------------+-------+',
  138. '| short | Longish thing | |',
  139. '| Longer thing | short | |',
  140. '+--------------+---------------+-------+',
  141. ];
  142. $this->assertEquals($expected, $this->stub->messages());
  143. }
  144. /**
  145. * Test output with multi-byte characters
  146. *
  147. * @return void
  148. */
  149. public function testOutputUtf8()
  150. {
  151. $data = [
  152. ['Header 1', 'Head', 'Long Header'],
  153. ['short', 'ÄÄÄÜÜÜ', 'short'],
  154. ['Longer thing', 'longerish', 'Longest Value'],
  155. ];
  156. $this->helper->output($data);
  157. $expected = [
  158. '+--------------+-----------+---------------+',
  159. '| <info>Header 1</info> | <info>Head</info> | <info>Long Header</info> |',
  160. '+--------------+-----------+---------------+',
  161. '| short | ÄÄÄÜÜÜ | short |',
  162. '| Longer thing | longerish | Longest Value |',
  163. '+--------------+-----------+---------------+',
  164. ];
  165. $this->assertEquals($expected, $this->stub->messages());
  166. }
  167. /**
  168. * Test output with multi-byte characters
  169. *
  170. * @return void
  171. */
  172. public function testOutputFullwidth()
  173. {
  174. $data = [
  175. ['Header 1', 'Head', 'Long Header'],
  176. ['short', '竜頭蛇尾', 'short'],
  177. ['Longer thing', 'longerish', 'Longest Value'],
  178. ];
  179. $this->helper->output($data);
  180. $expected = [
  181. '+--------------+-----------+---------------+',
  182. '| <info>Header 1</info> | <info>Head</info> | <info>Long Header</info> |',
  183. '+--------------+-----------+---------------+',
  184. '| short | 竜頭蛇尾 | short |',
  185. '| Longer thing | longerish | Longest Value |',
  186. '+--------------+-----------+---------------+',
  187. ];
  188. $this->assertEquals($expected, $this->stub->messages());
  189. }
  190. /**
  191. * Test output without headers
  192. *
  193. * @return void
  194. */
  195. public function testOutputWithoutHeaderStyle()
  196. {
  197. $data = [
  198. ['Header 1', 'Header', 'Long Header'],
  199. ['short', 'Longish thing', 'short'],
  200. ['Longer thing', 'short', 'Longest Value'],
  201. ];
  202. $this->helper->setConfig(['headerStyle' => false]);
  203. $this->helper->output($data);
  204. $expected = [
  205. '+--------------+---------------+---------------+',
  206. '| Header 1 | Header | Long Header |',
  207. '+--------------+---------------+---------------+',
  208. '| short | Longish thing | short |',
  209. '| Longer thing | short | Longest Value |',
  210. '+--------------+---------------+---------------+',
  211. ];
  212. $this->assertEquals($expected, $this->stub->messages());
  213. }
  214. /**
  215. * Test output with different header style
  216. *
  217. * @return void
  218. */
  219. public function testOutputWithDifferentHeaderStyle()
  220. {
  221. $data = [
  222. ['Header 1', 'Header', 'Long Header'],
  223. ['short', 'Longish thing', 'short'],
  224. ['Longer thing', 'short', 'Longest Value'],
  225. ];
  226. $this->helper->setConfig(['headerStyle' => 'error']);
  227. $this->helper->output($data);
  228. $expected = [
  229. '+--------------+---------------+---------------+',
  230. '| <error>Header 1</error> | <error>Header</error> | <error>Long Header</error> |',
  231. '+--------------+---------------+---------------+',
  232. '| short | Longish thing | short |',
  233. '| Longer thing | short | Longest Value |',
  234. '+--------------+---------------+---------------+',
  235. ];
  236. $this->assertEquals($expected, $this->stub->messages());
  237. }
  238. /**
  239. * Test output without table headers
  240. *
  241. * @return void
  242. */
  243. public function testOutputWithoutHeaders()
  244. {
  245. $data = [
  246. ['short', 'Longish thing', 'short'],
  247. ['Longer thing', 'short', 'Longest Value'],
  248. ];
  249. $this->helper->setConfig(['headers' => false]);
  250. $this->helper->output($data);
  251. $expected = [
  252. '+--------------+---------------+---------------+',
  253. '| short | Longish thing | short |',
  254. '| Longer thing | short | Longest Value |',
  255. '+--------------+---------------+---------------+',
  256. ];
  257. $this->assertEquals($expected, $this->stub->messages());
  258. }
  259. /**
  260. * Test output with row separator
  261. *
  262. * @return void
  263. */
  264. public function testOutputWithRowSeparator()
  265. {
  266. $data = [
  267. ['Header 1', 'Header', 'Long Header'],
  268. ['short', 'Longish thing', 'short'],
  269. ['Longer thing', 'short', 'Longest Value'],
  270. ];
  271. $this->helper->setConfig(['rowSeparator' => true]);
  272. $this->helper->output($data);
  273. $expected = [
  274. '+--------------+---------------+---------------+',
  275. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  276. '+--------------+---------------+---------------+',
  277. '| short | Longish thing | short |',
  278. '+--------------+---------------+---------------+',
  279. '| Longer thing | short | Longest Value |',
  280. '+--------------+---------------+---------------+',
  281. ];
  282. $this->assertEquals($expected, $this->stub->messages());
  283. }
  284. /**
  285. * Test output with row separator and no headers
  286. *
  287. * @return void
  288. */
  289. public function testOutputWithRowSeparatorAndHeaders()
  290. {
  291. $data = [
  292. ['Header 1', 'Header', 'Long Header'],
  293. ['short', 'Longish thing', 'short'],
  294. ['Longer thing', 'short', 'Longest Value'],
  295. ];
  296. $this->helper->setConfig(['rowSeparator' => true]);
  297. $this->helper->output($data);
  298. $expected = [
  299. '+--------------+---------------+---------------+',
  300. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  301. '+--------------+---------------+---------------+',
  302. '| short | Longish thing | short |',
  303. '+--------------+---------------+---------------+',
  304. '| Longer thing | short | Longest Value |',
  305. '+--------------+---------------+---------------+',
  306. ];
  307. $this->assertEquals($expected, $this->stub->messages());
  308. }
  309. /**
  310. * Test output when there is no data.
  311. */
  312. public function testOutputWithNoData()
  313. {
  314. $this->helper->output([]);
  315. $this->assertEquals([], $this->stub->messages());
  316. }
  317. /**
  318. * Test output with a header but no data.
  319. */
  320. public function testOutputWithHeaderAndNoData()
  321. {
  322. $data = [
  323. ['Header 1', 'Header', 'Long Header'],
  324. ];
  325. $this->helper->output($data);
  326. $expected = [
  327. '+----------+--------+-------------+',
  328. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  329. '+----------+--------+-------------+',
  330. ];
  331. $this->assertEquals($expected, $this->stub->messages());
  332. }
  333. /**
  334. * Test no data when headers are disabled.
  335. */
  336. public function testOutputHeaderDisabledNoData()
  337. {
  338. $this->helper->setConfig(['header' => false]);
  339. $this->helper->output([]);
  340. $this->assertEquals([], $this->stub->messages());
  341. }
  342. }