TableHelperTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. $data = [
  138. ['short', 'Longish thing', 'short'],
  139. ['Longer thing', 'short', 'Longest Value'],
  140. ];
  141. $this->helper->config(['headers' => false]);
  142. $this->helper->output($data);
  143. $expected = [
  144. '+--------------+---------------+---------------+',
  145. '| short | Longish thing | short |',
  146. '| Longer thing | short | Longest Value |',
  147. '+--------------+---------------+---------------+',
  148. ];
  149. $this->assertEquals($expected, $this->stub->messages());
  150. }
  151. /**
  152. * Test output with row separator
  153. *
  154. * @return void
  155. */
  156. public function testOutputWithRowSeparator() {
  157. $data = [
  158. ['Header 1', 'Header', 'Long Header'],
  159. ['short', 'Longish thing', 'short'],
  160. ['Longer thing', 'short', 'Longest Value']
  161. ];
  162. $this->helper->config(['rowSeparator' => true]);
  163. $this->helper->output($data);
  164. $expected = [
  165. '+--------------+---------------+---------------+',
  166. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  167. '+--------------+---------------+---------------+',
  168. '| short | Longish thing | short |',
  169. '+--------------+---------------+---------------+',
  170. '| Longer thing | short | Longest Value |',
  171. '+--------------+---------------+---------------+',
  172. ];
  173. $this->assertEquals($expected, $this->stub->messages());
  174. }
  175. /**
  176. * Test output with row separator and no headers
  177. *
  178. * @return void
  179. */
  180. public function testOutputWithRowSeparatorAndHeaders() {
  181. $data = [
  182. ['Header 1', 'Header', 'Long Header'],
  183. ['short', 'Longish thing', 'short'],
  184. ['Longer thing', 'short', 'Longest Value'],
  185. ];
  186. $this->helper->config(['rowSeparator' => true]);
  187. $this->helper->output($data);
  188. $expected = [
  189. '+--------------+---------------+---------------+',
  190. '| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
  191. '+--------------+---------------+---------------+',
  192. '| short | Longish thing | short |',
  193. '+--------------+---------------+---------------+',
  194. '| Longer thing | short | Longest Value |',
  195. '+--------------+---------------+---------------+',
  196. ];
  197. $this->assertEquals($expected, $this->stub->messages());
  198. }
  199. }