ConsoleOutputTest.php 7.6 KB

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