ConsoleOutputTest.php 7.0 KB

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