TableHelperTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /**
  3. * CakePHP : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP Project
  12. * @since 3.1.0
  13. * @license https://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 \Cake\Console\ConsoleOutput
  27. */
  28. public $stub;
  29. /**
  30. * @var \Cake\Console\ConsoleIo
  31. */
  32. public $io;
  33. /**
  34. * @var \Cake\Shell\Helper\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 testOutputDefaultOutput()
  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 output with inconsistent keys.
  74. *
  75. * When outputting entities or other structured data,
  76. * headers shouldn't need to have the same keys as it is
  77. * annoying to use.
  78. *
  79. * @return void
  80. */
  81. public function testOutputInconsistentKeys()
  82. {
  83. $data = [
  84. ['Header 1', 'Header', 'Long Header'],
  85. ['a' => 'short', 'b' => 'Longish thing', 'c' => 'short'],
  86. ['c' => 'Longer thing', 'a' => 'short', 'b' => 'Longest Value'],
  87. ];
  88. $this->helper->output($data);
  89. $expected = [
  90. '+--------------+---------------+---------------+',
  91. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  92. '+--------------+---------------+---------------+',
  93. '| short | Longish thing | short |',
  94. '| Longer thing | short | Longest Value |',
  95. '+--------------+---------------+---------------+',
  96. ];
  97. $this->assertEquals($expected, $this->stub->messages());
  98. }
  99. /**
  100. * Test that output works when data contains just empty strings.
  101. *
  102. * @return void
  103. */
  104. public function testOutputEmptyStrings()
  105. {
  106. $data = [
  107. ['Header 1', 'Header', 'Empty'],
  108. ['short', 'Longish thing', ''],
  109. ['Longer thing', 'short', ''],
  110. ];
  111. $this->helper->output($data);
  112. $expected = [
  113. '+--------------+---------------+-------+',
  114. '| <info>Header 1</info> | <info>Header</info> | <info>Empty</info> |',
  115. '+--------------+---------------+-------+',
  116. '| short | Longish thing | |',
  117. '| Longer thing | short | |',
  118. '+--------------+---------------+-------+',
  119. ];
  120. $this->assertEquals($expected, $this->stub->messages());
  121. }
  122. /**
  123. * Test that output works when data contains nulls.
  124. */
  125. public function testNullValues()
  126. {
  127. $data = [
  128. ['Header 1', 'Header', 'Empty'],
  129. ['short', 'Longish thing', null],
  130. ['Longer thing', 'short', null],
  131. ];
  132. $this->helper->output($data);
  133. $expected = [
  134. '+--------------+---------------+-------+',
  135. '| <info>Header 1</info> | <info>Header</info> | <info>Empty</info> |',
  136. '+--------------+---------------+-------+',
  137. '| short | Longish thing | |',
  138. '| Longer thing | short | |',
  139. '+--------------+---------------+-------+',
  140. ];
  141. $this->assertEquals($expected, $this->stub->messages());
  142. }
  143. /**
  144. * Test output with multi-byte characters
  145. *
  146. * @return void
  147. */
  148. public function testOutputUtf8()
  149. {
  150. $data = [
  151. ['Header 1', 'Head', 'Long Header'],
  152. ['short', 'ÄÄÄÜÜÜ', 'short'],
  153. ['Longer thing', 'longerish', 'Longest Value'],
  154. ];
  155. $this->helper->output($data);
  156. $expected = [
  157. '+--------------+-----------+---------------+',
  158. '| <info>Header 1</info> | <info>Head</info> | <info>Long Header</info> |',
  159. '+--------------+-----------+---------------+',
  160. '| short | ÄÄÄÜÜÜ | short |',
  161. '| Longer thing | longerish | Longest Value |',
  162. '+--------------+-----------+---------------+',
  163. ];
  164. $this->assertEquals($expected, $this->stub->messages());
  165. }
  166. /**
  167. * Test output with multi-byte characters
  168. *
  169. * @return void
  170. */
  171. public function testOutputFullwidth()
  172. {
  173. $data = [
  174. ['Header 1', 'Head', 'Long Header'],
  175. ['short', '竜頭蛇尾', 'short'],
  176. ['Longer thing', 'longerish', 'Longest Value'],
  177. ];
  178. $this->helper->output($data);
  179. $expected = [
  180. '+--------------+-----------+---------------+',
  181. '| <info>Header 1</info> | <info>Head</info> | <info>Long Header</info> |',
  182. '+--------------+-----------+---------------+',
  183. '| short | 竜頭蛇尾 | short |',
  184. '| Longer thing | longerish | Longest Value |',
  185. '+--------------+-----------+---------------+',
  186. ];
  187. $this->assertEquals($expected, $this->stub->messages());
  188. }
  189. /**
  190. * Test output without headers
  191. *
  192. * @return void
  193. */
  194. public function testOutputWithoutHeaderStyle()
  195. {
  196. $data = [
  197. ['Header 1', 'Header', 'Long Header'],
  198. ['short', 'Longish thing', 'short'],
  199. ['Longer thing', 'short', 'Longest Value'],
  200. ];
  201. $this->helper->setConfig(['headerStyle' => false]);
  202. $this->helper->output($data);
  203. $expected = [
  204. '+--------------+---------------+---------------+',
  205. '| Header 1 | Header | Long Header |',
  206. '+--------------+---------------+---------------+',
  207. '| short | Longish thing | short |',
  208. '| Longer thing | short | Longest Value |',
  209. '+--------------+---------------+---------------+',
  210. ];
  211. $this->assertEquals($expected, $this->stub->messages());
  212. }
  213. /**
  214. * Test output with different header style
  215. *
  216. * @return void
  217. */
  218. public function testOutputWithDifferentHeaderStyle()
  219. {
  220. $data = [
  221. ['Header 1', 'Header', 'Long Header'],
  222. ['short', 'Longish thing', 'short'],
  223. ['Longer thing', 'short', 'Longest Value'],
  224. ];
  225. $this->helper->setConfig(['headerStyle' => 'error']);
  226. $this->helper->output($data);
  227. $expected = [
  228. '+--------------+---------------+---------------+',
  229. '| <error>Header 1</error> | <error>Header</error> | <error>Long Header</error> |',
  230. '+--------------+---------------+---------------+',
  231. '| short | Longish thing | short |',
  232. '| Longer thing | short | Longest Value |',
  233. '+--------------+---------------+---------------+',
  234. ];
  235. $this->assertEquals($expected, $this->stub->messages());
  236. }
  237. /**
  238. * Test output without table headers
  239. *
  240. * @return void
  241. */
  242. public function testOutputWithoutHeaders()
  243. {
  244. $data = [
  245. ['short', 'Longish thing', 'short'],
  246. ['Longer thing', 'short', 'Longest Value'],
  247. ];
  248. $this->helper->setConfig(['headers' => false]);
  249. $this->helper->output($data);
  250. $expected = [
  251. '+--------------+---------------+---------------+',
  252. '| short | Longish thing | short |',
  253. '| Longer thing | short | Longest Value |',
  254. '+--------------+---------------+---------------+',
  255. ];
  256. $this->assertEquals($expected, $this->stub->messages());
  257. }
  258. /**
  259. * Test output with formatted cells
  260. *
  261. * @return void
  262. */
  263. public function testOutputWithFormattedCells()
  264. {
  265. $data = [
  266. ['short', 'Longish thing', '<info>short</info>'],
  267. ['Longer thing', 'short', '<warning>Longest</warning> <error>Value</error>'],
  268. ];
  269. $this->helper->setConfig(['headers' => false]);
  270. $this->helper->output($data);
  271. $expected = [
  272. '+--------------+---------------+---------------+',
  273. '| short | Longish thing | <info>short</info> |',
  274. '| Longer thing | short | <warning>Longest</warning> <error>Value</error> |',
  275. '+--------------+---------------+---------------+',
  276. ];
  277. $this->assertEquals($expected, $this->stub->messages());
  278. }
  279. /**
  280. * Test output with row separator
  281. *
  282. * @return void
  283. */
  284. public function testOutputWithRowSeparator()
  285. {
  286. $data = [
  287. ['Header 1', 'Header', 'Long Header'],
  288. ['short', 'Longish thing', 'short'],
  289. ['Longer thing', 'short', 'Longest Value'],
  290. ];
  291. $this->helper->setConfig(['rowSeparator' => true]);
  292. $this->helper->output($data);
  293. $expected = [
  294. '+--------------+---------------+---------------+',
  295. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  296. '+--------------+---------------+---------------+',
  297. '| short | Longish thing | short |',
  298. '+--------------+---------------+---------------+',
  299. '| Longer thing | short | Longest Value |',
  300. '+--------------+---------------+---------------+',
  301. ];
  302. $this->assertEquals($expected, $this->stub->messages());
  303. }
  304. /**
  305. * Test output with row separator and no headers
  306. *
  307. * @return void
  308. */
  309. public function testOutputWithRowSeparatorAndHeaders()
  310. {
  311. $data = [
  312. ['Header 1', 'Header', 'Long Header'],
  313. ['short', 'Longish thing', 'short'],
  314. ['Longer thing', 'short', 'Longest Value'],
  315. ];
  316. $this->helper->setConfig(['rowSeparator' => true]);
  317. $this->helper->output($data);
  318. $expected = [
  319. '+--------------+---------------+---------------+',
  320. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  321. '+--------------+---------------+---------------+',
  322. '| short | Longish thing | short |',
  323. '+--------------+---------------+---------------+',
  324. '| Longer thing | short | Longest Value |',
  325. '+--------------+---------------+---------------+',
  326. ];
  327. $this->assertEquals($expected, $this->stub->messages());
  328. }
  329. /**
  330. * Test output when there is no data.
  331. */
  332. public function testOutputWithNoData()
  333. {
  334. $this->helper->output([]);
  335. $this->assertEquals([], $this->stub->messages());
  336. }
  337. /**
  338. * Test output with a header but no data.
  339. */
  340. public function testOutputWithHeaderAndNoData()
  341. {
  342. $data = [
  343. ['Header 1', 'Header', 'Long Header'],
  344. ];
  345. $this->helper->output($data);
  346. $expected = [
  347. '+----------+--------+-------------+',
  348. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  349. '+----------+--------+-------------+',
  350. ];
  351. $this->assertEquals($expected, $this->stub->messages());
  352. }
  353. /**
  354. * Test no data when headers are disabled.
  355. */
  356. public function testOutputHeaderDisabledNoData()
  357. {
  358. $this->helper->setConfig(['header' => false]);
  359. $this->helper->output([]);
  360. $this->assertEquals([], $this->stub->messages());
  361. }
  362. }