ConsoleOutputTest.php 7.0 KB

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