TableHelperTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\Console\ConsoleOutput;
  18. use Cake\Shell\Helper\TableHelper;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * StubOutput makes testing easier.
  22. */
  23. class StubOutput extends ConsoleOutput
  24. {
  25. protected $_out = [];
  26. public function write($message, $newlines = 1)
  27. {
  28. $this->_out[] = $message;
  29. }
  30. public function messages()
  31. {
  32. return $this->_out;
  33. }
  34. }
  35. /**
  36. * TableHelper test.
  37. */
  38. class TableHelperTest extends TestCase
  39. {
  40. /**
  41. * setUp method
  42. *
  43. * @return void
  44. */
  45. public function setUp()
  46. {
  47. parent::setUp();
  48. $this->stub = new StubOutput();
  49. $this->io = new ConsoleIo($this->stub);
  50. $this->helper = new TableHelper($this->io);
  51. }
  52. /**
  53. * Test output
  54. *
  55. * @return voi
  56. */
  57. public function testOutput()
  58. {
  59. $data = [
  60. ['Header 1', 'Header', 'Long Header'],
  61. ['short', 'Longish thing', 'short'],
  62. ['Longer thing', 'short', 'Longest Value'],
  63. ];
  64. $this->helper->output($data);
  65. $expected = [
  66. '+--------------+---------------+---------------+',
  67. '| Header 1 | Header | Long Header |',
  68. '+--------------+---------------+---------------+',
  69. '| short | Longish thing | short |',
  70. '| Longer thing | short | Longest Value |',
  71. '+--------------+---------------+---------------+',
  72. ];
  73. $this->assertEquals($expected, $this->stub->messages());
  74. }
  75. /**
  76. * Test output array shifting
  77. *
  78. * @return voi
  79. */
  80. public function testOutputShifting()
  81. {
  82. $data = [
  83. ['Header 1', 'Header', 'Long Header'],
  84. ['short', 'Longish thing', 'short'],
  85. ];
  86. $this->helper->output([$data]);
  87. $expected = [
  88. '+----------+---------------+-------------+',
  89. '| Header 1 | Header | Long Header |',
  90. '+----------+---------------+-------------+',
  91. '| short | Longish thing | short |',
  92. '+----------+---------------+-------------+',
  93. ];
  94. $this->assertEquals($expected, $this->stub->messages());
  95. }
  96. /**
  97. * Test output with multibyte characters
  98. *
  99. * @return voi
  100. */
  101. public function testOutputUtf8()
  102. {
  103. $data = [
  104. ['Header 1', 'Head', 'Long Header'],
  105. ['short', 'ÄÄÄÜÜÜ', 'short'],
  106. ['Longer thing', 'longerish', 'Longest Value'],
  107. ];
  108. $this->helper->output($data);
  109. $expected = [
  110. '+--------------+-----------+---------------+',
  111. '| Header 1 | Head | Long Header |',
  112. '+--------------+-----------+---------------+',
  113. '| short | ÄÄÄÜÜÜ | short |',
  114. '| Longer thing | longerish | Longest Value |',
  115. '+--------------+-----------+---------------+',
  116. ];
  117. debug($this->stub->messages());
  118. $this->assertEquals($expected, $this->stub->messages());
  119. }
  120. }