ConsoleOutputTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * ConsoleOutputTest file
  5. *
  6. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  7. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  8. *
  9. * Licensed under The MIT License
  10. * For full copyright and license information, please see the LICENSE.txt
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  14. * @link https://cakephp.org CakePHP(tm) Project
  15. * @since 1.2.0
  16. * @license https://opensource.org/licenses/mit-license.php MIT License
  17. */
  18. namespace Cake\Test\TestCase\Console;
  19. use Cake\Console\ConsoleOutput;
  20. use Cake\Console\TestSuite\StubConsoleOutput;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * ConsoleOutputTest
  24. */
  25. class ConsoleOutputTest extends TestCase
  26. {
  27. /**
  28. * @var \Cake\Console\ConsoleOutput|\PHPUnit\Framework\MockObject\MockObject
  29. */
  30. protected $output;
  31. /**
  32. * setup
  33. */
  34. public function setUp(): void
  35. {
  36. parent::setUp();
  37. $this->output = $this->getMockBuilder(ConsoleOutput::class)
  38. ->onlyMethods(['_write'])
  39. ->getMock();
  40. $this->output->setOutputAs(ConsoleOutput::COLOR);
  41. }
  42. /**
  43. * tearDown
  44. */
  45. public function tearDown(): void
  46. {
  47. parent::tearDown();
  48. unset($this->output);
  49. }
  50. public function testNoColorEnvironmentVariable(): void
  51. {
  52. $_SERVER['NO_COLOR'] = '1';
  53. $output = new ConsoleOutput();
  54. $this->assertSame(ConsoleOutput::PLAIN, $output->getOutputAs());
  55. unset($_SERVER['NO_COLOR']);
  56. }
  57. /**
  58. * test writing with no new line
  59. */
  60. public function testWriteNoNewLine(): void
  61. {
  62. $this->output->expects($this->once())->method('_write')
  63. ->with('Some output');
  64. $this->output->write('Some output', 0);
  65. }
  66. /**
  67. * test writing with no new line
  68. */
  69. public function testWriteNewLine(): void
  70. {
  71. $this->output->expects($this->once())->method('_write')
  72. ->with('Some output' . PHP_EOL);
  73. $this->output->write('Some output');
  74. }
  75. /**
  76. * test write() with multiple new lines
  77. */
  78. public function testWriteMultipleNewLines(): void
  79. {
  80. $this->output->expects($this->once())->method('_write')
  81. ->with('Some output' . PHP_EOL . PHP_EOL . PHP_EOL . PHP_EOL);
  82. $this->output->write('Some output', 4);
  83. }
  84. /**
  85. * test writing an array of messages.
  86. */
  87. public function testWriteArray(): void
  88. {
  89. $this->output->expects($this->once())->method('_write')
  90. ->with('Line' . PHP_EOL . 'Line' . PHP_EOL . 'Line' . PHP_EOL);
  91. $this->output->write(['Line', 'Line', 'Line']);
  92. }
  93. /**
  94. * test getting a style.
  95. */
  96. public function testStylesGet(): void
  97. {
  98. $result = $this->output->getStyle('error');
  99. $expected = ['text' => 'red'];
  100. $this->assertEquals($expected, $result);
  101. $this->assertSame([], $this->output->getStyle('made_up_goop'));
  102. $result = $this->output->styles();
  103. $this->assertNotEmpty($result, 'Error is missing');
  104. $this->assertNotEmpty($result, 'Warning is missing');
  105. }
  106. /**
  107. * test adding a style.
  108. */
  109. public function testStylesAdding(): void
  110. {
  111. $this->output->setStyle('test', ['text' => 'red', 'background' => 'black']);
  112. $result = $this->output->getStyle('test');
  113. $expected = ['text' => 'red', 'background' => 'black'];
  114. $this->assertEquals($expected, $result);
  115. $this->output->setStyle('test', []);
  116. $this->assertSame([], $this->output->getStyle('test'), 'Removed styles should be empty.');
  117. }
  118. /**
  119. * test formatting text with styles.
  120. */
  121. public function testFormattingSimple(): void
  122. {
  123. $this->output->expects($this->once())->method('_write')
  124. ->with("\033[31mError:\033[0m Something bad");
  125. $this->output->write('<error>Error:</error> Something bad', 0);
  126. }
  127. /**
  128. * test that formatting doesn't eat tags it doesn't know about.
  129. */
  130. public function testFormattingNotEatingTags(): void
  131. {
  132. $this->output->expects($this->once())->method('_write')
  133. ->with('<red> Something bad');
  134. $this->output->write('<red> Something bad', 0);
  135. }
  136. /**
  137. * test formatting with custom styles.
  138. */
  139. public function testFormattingCustom(): void
  140. {
  141. $this->output->setStyle('annoying', [
  142. 'text' => 'magenta',
  143. 'background' => 'cyan',
  144. 'blink' => true,
  145. 'underline' => true,
  146. ]);
  147. $this->output->expects($this->once())->method('_write')
  148. ->with("\033[35;46;5;4mAnnoy:\033[0m Something bad");
  149. $this->output->write('<annoying>Annoy:</annoying> Something bad', 0);
  150. }
  151. /**
  152. * test formatting text with missing styles.
  153. */
  154. public function testFormattingMissingStyleName(): void
  155. {
  156. $this->output->expects($this->once())->method('_write')
  157. ->with('<not_there>Error:</not_there> Something bad');
  158. $this->output->write('<not_there>Error:</not_there> Something bad', 0);
  159. }
  160. /**
  161. * test formatting text with multiple styles.
  162. */
  163. public function testFormattingMultipleStylesName(): void
  164. {
  165. $this->output->expects($this->once())->method('_write')
  166. ->with("\033[31mBad\033[0m \033[33mWarning\033[0m Regular");
  167. $this->output->write('<error>Bad</error> <warning>Warning</warning> Regular', 0);
  168. }
  169. /**
  170. * test that multiple tags of the same name work in one string.
  171. */
  172. public function testFormattingMultipleSameTags(): void
  173. {
  174. $this->output->expects($this->once())->method('_write')
  175. ->with("\033[31mBad\033[0m \033[31mWarning\033[0m Regular");
  176. $this->output->write('<error>Bad</error> <error>Warning</error> Regular', 0);
  177. }
  178. /**
  179. * test raw output not getting tags replaced.
  180. */
  181. public function testSetOutputAsRaw(): void
  182. {
  183. $this->output->setOutputAs(ConsoleOutput::RAW);
  184. $this->output->expects($this->once())->method('_write')
  185. ->with('<error>Bad</error> Regular');
  186. $this->output->write('<error>Bad</error> Regular', 0);
  187. }
  188. /**
  189. * test set/get plain output.
  190. */
  191. public function testSetOutputAsPlain(): void
  192. {
  193. $this->output->setOutputAs(ConsoleOutput::PLAIN);
  194. $this->assertSame(ConsoleOutput::PLAIN, $this->output->getOutputAs());
  195. $this->output->expects($this->once())->method('_write')
  196. ->with('Bad Regular');
  197. $this->output->write('<error>Bad</error> Regular', 0);
  198. }
  199. /**
  200. * test plain output only strips tags used for formatting.
  201. */
  202. public function testSetOutputAsPlainSelectiveTagRemoval(): void
  203. {
  204. $this->output->setOutputAs(ConsoleOutput::PLAIN);
  205. $this->output->expects($this->once())
  206. ->method('_write')
  207. ->with('Bad Regular <b>Left</b> <i>behind</i> <name>');
  208. $this->output->write('<error>Bad</error> Regular <b>Left</b> <i>behind</i> <name>', 0);
  209. }
  210. public function testWorkingWithStub(): void
  211. {
  212. $output = new StubConsoleOutput();
  213. $output->write('Test line 1.');
  214. $output->write('Test line 2.');
  215. $result = $output->messages();
  216. $expected = [
  217. 'Test line 1.',
  218. 'Test line 2.',
  219. ];
  220. $this->assertSame($expected, $result);
  221. $result = $output->output();
  222. $expected = "Test line 1.\nTest line 2.";
  223. $this->assertSame($expected, $result);
  224. }
  225. }