TableHelperTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. * setUp method
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. $this->stub = new ConsoleOutput();
  34. $this->io = new ConsoleIo($this->stub);
  35. $this->helper = new TableHelper($this->io);
  36. }
  37. /**
  38. * Test output
  39. *
  40. * @return void
  41. */
  42. public function testDefaultOutput()
  43. {
  44. $data = [
  45. ['Header 1', 'Header', 'Long Header'],
  46. ['short', 'Longish thing', 'short'],
  47. ['Longer thing', 'short', 'Longest Value'],
  48. ];
  49. $this->helper->output($data);
  50. $expected = [
  51. '+--------------+---------------+---------------+',
  52. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  53. '+--------------+---------------+---------------+',
  54. '| short | Longish thing | short |',
  55. '| Longer thing | short | Longest Value |',
  56. '+--------------+---------------+---------------+',
  57. ];
  58. $this->assertEquals($expected, $this->stub->messages());
  59. }
  60. /**
  61. * Test output with multibyte characters
  62. *
  63. * @return void
  64. */
  65. public function testOutputUtf8()
  66. {
  67. $data = [
  68. ['Header 1', 'Head', 'Long Header'],
  69. ['short', 'ÄÄÄÜÜÜ', 'short'],
  70. ['Longer thing', 'longerish', 'Longest Value'],
  71. ];
  72. $this->helper->output($data);
  73. $expected = [
  74. '+--------------+-----------+---------------+',
  75. '| <info>Header 1</info> | <info>Head</info> | <info>Long Header</info> |',
  76. '+--------------+-----------+---------------+',
  77. '| short | ÄÄÄÜÜÜ | short |',
  78. '| Longer thing | longerish | Longest Value |',
  79. '+--------------+-----------+---------------+',
  80. ];
  81. $this->assertEquals($expected, $this->stub->messages());
  82. }
  83. /**
  84. * Test output without headers
  85. *
  86. * @return void
  87. */
  88. public function testOutputWithoutHeaderStyle()
  89. {
  90. $data = [
  91. ['Header 1', 'Header', 'Long Header'],
  92. ['short', 'Longish thing', 'short'],
  93. ['Longer thing', 'short', 'Longest Value'],
  94. ];
  95. $this->helper->config(['headerStyle' => false]);
  96. $this->helper->output($data);
  97. $expected = [
  98. '+--------------+---------------+---------------+',
  99. '| Header 1 | Header | Long Header |',
  100. '+--------------+---------------+---------------+',
  101. '| short | Longish thing | short |',
  102. '| Longer thing | short | Longest Value |',
  103. '+--------------+---------------+---------------+',
  104. ];
  105. $this->assertEquals($expected, $this->stub->messages());
  106. }
  107. /**
  108. * Test output with different header style
  109. *
  110. * @return void
  111. */
  112. public function testOutputWithDifferentHeaderStyle()
  113. {
  114. $data = [
  115. ['Header 1', 'Header', 'Long Header'],
  116. ['short', 'Longish thing', 'short'],
  117. ['Longer thing', 'short', 'Longest Value'],
  118. ];
  119. $this->helper->config(['headerStyle' => 'error']);
  120. $this->helper->output($data);
  121. $expected = [
  122. '+--------------+---------------+---------------+',
  123. '| <error>Header 1</error> | <error>Header</error> | <error>Long Header</error> |',
  124. '+--------------+---------------+---------------+',
  125. '| short | Longish thing | short |',
  126. '| Longer thing | short | Longest Value |',
  127. '+--------------+---------------+---------------+',
  128. ];
  129. $this->assertEquals($expected, $this->stub->messages());
  130. }
  131. /**
  132. * Test output without table headers
  133. *
  134. * @return void
  135. */
  136. public function testOutputWithoutHeaders()
  137. {
  138. $data = [
  139. ['short', 'Longish thing', 'short'],
  140. ['Longer thing', 'short', 'Longest Value'],
  141. ];
  142. $this->helper->config(['headers' => false]);
  143. $this->helper->output($data);
  144. $expected = [
  145. '+--------------+---------------+---------------+',
  146. '| short | Longish thing | short |',
  147. '| Longer thing | short | Longest Value |',
  148. '+--------------+---------------+---------------+',
  149. ];
  150. $this->assertEquals($expected, $this->stub->messages());
  151. }
  152. /**
  153. * Test output with row separator
  154. *
  155. * @return void
  156. */
  157. public function testOutputWithRowSeparator()
  158. {
  159. $data = [
  160. ['Header 1', 'Header', 'Long Header'],
  161. ['short', 'Longish thing', 'short'],
  162. ['Longer thing', 'short', 'Longest Value']
  163. ];
  164. $this->helper->config(['rowSeparator' => true]);
  165. $this->helper->output($data);
  166. $expected = [
  167. '+--------------+---------------+---------------+',
  168. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  169. '+--------------+---------------+---------------+',
  170. '| short | Longish thing | short |',
  171. '+--------------+---------------+---------------+',
  172. '| Longer thing | short | Longest Value |',
  173. '+--------------+---------------+---------------+',
  174. ];
  175. $this->assertEquals($expected, $this->stub->messages());
  176. }
  177. /**
  178. * Test output with row separator and no headers
  179. *
  180. * @return void
  181. */
  182. public function testOutputWithRowSeparatorAndHeaders()
  183. {
  184. $data = [
  185. ['Header 1', 'Header', 'Long Header'],
  186. ['short', 'Longish thing', 'short'],
  187. ['Longer thing', 'short', 'Longest Value'],
  188. ];
  189. $this->helper->config(['rowSeparator' => true]);
  190. $this->helper->output($data);
  191. $expected = [
  192. '+--------------+---------------+---------------+',
  193. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  194. '+--------------+---------------+---------------+',
  195. '| short | Longish thing | short |',
  196. '+--------------+---------------+---------------+',
  197. '| Longer thing | short | Longest Value |',
  198. '+--------------+---------------+---------------+',
  199. ];
  200. $this->assertEquals($expected, $this->stub->messages());
  201. }
  202. }