BannerHelperTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 5.1.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Command\Helper;
  17. use Cake\Command\Helper\BannerHelper;
  18. use Cake\Console\ConsoleIo;
  19. use Cake\Console\TestSuite\StubConsoleOutput;
  20. use Cake\TestSuite\TestCase;
  21. use InvalidArgumentException;
  22. /**
  23. * BannerHelper test.
  24. */
  25. class BannerHelperTest extends TestCase
  26. {
  27. /**
  28. * @var \Cake\Command\Helper\BannerHelper
  29. */
  30. protected BannerHelper $helper;
  31. /**
  32. * @var \Cake\Console\TestSuite\StubConsoleOutput
  33. */
  34. protected StubConsoleOutput $stub;
  35. /**
  36. * @var \Cake\Console\ConsoleIo
  37. */
  38. protected ConsoleIo $io;
  39. /**
  40. * setUp method
  41. */
  42. public function setUp(): void
  43. {
  44. parent::setUp();
  45. $this->stub = new StubConsoleOutput();
  46. $this->io = new ConsoleIo($this->stub);
  47. $this->helper = new BannerHelper($this->io);
  48. }
  49. /**
  50. * Test that the callback is invoked until 100 is reached.
  51. */
  52. public function testOutputInvalidPadding(): void
  53. {
  54. $this->expectException(InvalidArgumentException::class);
  55. $this->helper->withPadding(-1);
  56. }
  57. /**
  58. * Test output with all options
  59. */
  60. public function testOutputSuccess(): void
  61. {
  62. $this->helper
  63. ->withPadding(5)
  64. ->withStyle('info.bg')
  65. ->output(['All done']);
  66. $expected = [
  67. '',
  68. '<info.bg> </info.bg>',
  69. '<info.bg> All done </info.bg>',
  70. '<info.bg> </info.bg>',
  71. '',
  72. ];
  73. $this->assertEquals($expected, $this->stub->messages());
  74. }
  75. /**
  76. * Test that width is respected
  77. */
  78. public function testOutputPadding(): void
  79. {
  80. $this->helper
  81. ->withPadding(1)
  82. ->withStyle('info.bg')
  83. ->output(['All done']);
  84. $expected = [
  85. '',
  86. '<info.bg> </info.bg>',
  87. '<info.bg> All done </info.bg>',
  88. '<info.bg> </info.bg>',
  89. '',
  90. ];
  91. $this->assertEquals($expected, $this->stub->messages());
  92. }
  93. /**
  94. * Test that width is respected
  95. */
  96. public function testOutputLongestLine(): void
  97. {
  98. $this->helper
  99. ->withPadding(1)
  100. ->withStyle('info.bg')
  101. ->output(['All done', 'This line is longer', 'tiny']);
  102. $expected = [
  103. '',
  104. '<info.bg> </info.bg>',
  105. '<info.bg> All done </info.bg>',
  106. '<info.bg> This line is longer </info.bg>',
  107. '<info.bg> tiny </info.bg>',
  108. '<info.bg> </info.bg>',
  109. '',
  110. ];
  111. $this->assertEquals($expected, $this->stub->messages());
  112. }
  113. }