ConsoleOutputTest.php 7.4 KB

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