TableHelperTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 formatted cells
  261. *
  262. * @return void
  263. */
  264. public function testOutputWithFormattedCells()
  265. {
  266. $data = [
  267. ['short', 'Longish thing', '<info>short</info>'],
  268. ['Longer thing', 'short', '<warning>Longest</warning> <error>Value</error>'],
  269. ];
  270. $this->helper->setConfig(['headers' => false]);
  271. $this->helper->output($data);
  272. $expected = [
  273. '+--------------+---------------+---------------+',
  274. '| short | Longish thing | <info>short</info> |',
  275. '| Longer thing | short | <warning>Longest</warning> <error>Value</error> |',
  276. '+--------------+---------------+---------------+',
  277. ];
  278. $this->assertEquals($expected, $this->stub->messages());
  279. }
  280. /**
  281. * Test output with row separator
  282. *
  283. * @return void
  284. */
  285. public function testOutputWithRowSeparator()
  286. {
  287. $data = [
  288. ['Header 1', 'Header', 'Long Header'],
  289. ['short', 'Longish thing', 'short'],
  290. ['Longer thing', 'short', 'Longest Value'],
  291. ];
  292. $this->helper->setConfig(['rowSeparator' => true]);
  293. $this->helper->output($data);
  294. $expected = [
  295. '+--------------+---------------+---------------+',
  296. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  297. '+--------------+---------------+---------------+',
  298. '| short | Longish thing | short |',
  299. '+--------------+---------------+---------------+',
  300. '| Longer thing | short | Longest Value |',
  301. '+--------------+---------------+---------------+',
  302. ];
  303. $this->assertEquals($expected, $this->stub->messages());
  304. }
  305. /**
  306. * Test output with row separator and no headers
  307. *
  308. * @return void
  309. */
  310. public function testOutputWithRowSeparatorAndHeaders()
  311. {
  312. $data = [
  313. ['Header 1', 'Header', 'Long Header'],
  314. ['short', 'Longish thing', 'short'],
  315. ['Longer thing', 'short', 'Longest Value'],
  316. ];
  317. $this->helper->setConfig(['rowSeparator' => true]);
  318. $this->helper->output($data);
  319. $expected = [
  320. '+--------------+---------------+---------------+',
  321. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  322. '+--------------+---------------+---------------+',
  323. '| short | Longish thing | short |',
  324. '+--------------+---------------+---------------+',
  325. '| Longer thing | short | Longest Value |',
  326. '+--------------+---------------+---------------+',
  327. ];
  328. $this->assertEquals($expected, $this->stub->messages());
  329. }
  330. /**
  331. * Test output when there is no data.
  332. */
  333. public function testOutputWithNoData()
  334. {
  335. $this->helper->output([]);
  336. $this->assertEquals([], $this->stub->messages());
  337. }
  338. /**
  339. * Test output with a header but no data.
  340. */
  341. public function testOutputWithHeaderAndNoData()
  342. {
  343. $data = [
  344. ['Header 1', 'Header', 'Long Header'],
  345. ];
  346. $this->helper->output($data);
  347. $expected = [
  348. '+----------+--------+-------------+',
  349. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  350. '+----------+--------+-------------+',
  351. ];
  352. $this->assertEquals($expected, $this->stub->messages());
  353. }
  354. /**
  355. * Test no data when headers are disabled.
  356. */
  357. public function testOutputHeaderDisabledNoData()
  358. {
  359. $this->helper->setConfig(['header' => false]);
  360. $this->helper->output([]);
  361. $this->assertEquals([], $this->stub->messages());
  362. }
  363. }