ConsoleOutputTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * ConsoleOutputTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Console
  16. * @since CakePHP(tm) v 1.2.0.5432
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ConsoleOutput', 'Console');
  20. class ConsoleOutputTest extends CakeTestCase {
  21. /**
  22. * setup
  23. *
  24. * @return void
  25. */
  26. public function setUp() {
  27. parent::setUp();
  28. $this->output = $this->getMock('ConsoleOutput', array('_write'));
  29. $this->output->outputAs(ConsoleOutput::COLOR);
  30. }
  31. /**
  32. * tearDown
  33. *
  34. * @return void
  35. */
  36. public function tearDown() {
  37. parent::tearDown();
  38. unset($this->output);
  39. }
  40. /**
  41. * test writing with no new line
  42. *
  43. * @return void
  44. */
  45. public function testWriteNoNewLine() {
  46. $this->output->expects($this->once())->method('_write')
  47. ->with('Some output');
  48. $this->output->write('Some output', false);
  49. }
  50. /**
  51. * test writing with no new line
  52. *
  53. * @return void
  54. */
  55. public function testWriteNewLine() {
  56. $this->output->expects($this->once())->method('_write')
  57. ->with('Some output' . PHP_EOL);
  58. $this->output->write('Some output');
  59. }
  60. /**
  61. * test write() with multiple new lines
  62. *
  63. * @return void
  64. */
  65. public function testWriteMultipleNewLines() {
  66. $this->output->expects($this->once())->method('_write')
  67. ->with('Some output' . PHP_EOL . PHP_EOL . PHP_EOL . PHP_EOL);
  68. $this->output->write('Some output', 4);
  69. }
  70. /**
  71. * test writing an array of messages.
  72. *
  73. * @return void
  74. */
  75. public function testWriteArray() {
  76. $this->output->expects($this->once())->method('_write')
  77. ->with('Line' . PHP_EOL . 'Line' . PHP_EOL . 'Line' . PHP_EOL);
  78. $this->output->write(array('Line', 'Line', 'Line'));
  79. }
  80. /**
  81. * test getting a style.
  82. *
  83. * @return void
  84. */
  85. public function testStylesGet() {
  86. $result = $this->output->styles('error');
  87. $expected = array('text' => 'red', 'underline' => true);
  88. $this->assertEquals($expected, $result);
  89. $this->assertNull($this->output->styles('made_up_goop'));
  90. $result = $this->output->styles();
  91. $this->assertNotEmpty($result, 'error', 'Error is missing');
  92. $this->assertNotEmpty($result, 'warning', 'Warning is missing');
  93. }
  94. /**
  95. * test adding a style.
  96. *
  97. * @return void
  98. */
  99. public function testStylesAdding() {
  100. $this->output->styles('test', array('text' => 'red', 'background' => 'black'));
  101. $result = $this->output->styles('test');
  102. $expected = array('text' => 'red', 'background' => 'black');
  103. $this->assertEquals($expected, $result);
  104. $this->assertTrue($this->output->styles('test', false), 'Removing a style should return true.');
  105. $this->assertNull($this->output->styles('test'), 'Removed styles should be null.');
  106. }
  107. /**
  108. * test formatting text with styles.
  109. *
  110. * @return void
  111. */
  112. public function testFormattingSimple() {
  113. $this->output->expects($this->once())->method('_write')
  114. ->with("\033[31;4mError:\033[0m Something bad");
  115. $this->output->write('<error>Error:</error> Something bad', false);
  116. }
  117. /**
  118. * test that formatting doesn't eat tags it doesn't know about.
  119. *
  120. * @return void
  121. */
  122. public function testFormattingNotEatingTags() {
  123. $this->output->expects($this->once())->method('_write')
  124. ->with("<red> Something bad");
  125. $this->output->write('<red> Something bad', false);
  126. }
  127. /**
  128. * test formatting with custom styles.
  129. *
  130. * @return void
  131. */
  132. public function testFormattingCustom() {
  133. $this->output->styles('annoying', array(
  134. 'text' => 'magenta',
  135. 'background' => 'cyan',
  136. 'blink' => true,
  137. 'underline' => true
  138. ));
  139. $this->output->expects($this->once())->method('_write')
  140. ->with("\033[35;46;5;4mAnnoy:\033[0m Something bad");
  141. $this->output->write('<annoying>Annoy:</annoying> Something bad', false);
  142. }
  143. /**
  144. * test formatting text with missing styles.
  145. *
  146. * @return void
  147. */
  148. public function testFormattingMissingStyleName() {
  149. $this->output->expects($this->once())->method('_write')
  150. ->with("<not_there>Error:</not_there> Something bad");
  151. $this->output->write('<not_there>Error:</not_there> Something bad', false);
  152. }
  153. /**
  154. * test formatting text with multiple styles.
  155. *
  156. * @return void
  157. */
  158. public function testFormattingMultipleStylesName() {
  159. $this->output->expects($this->once())->method('_write')
  160. ->with("\033[31;4mBad\033[0m \033[33mWarning\033[0m Regular");
  161. $this->output->write('<error>Bad</error> <warning>Warning</warning> Regular', false);
  162. }
  163. /**
  164. * test that multiple tags of the same name work in one string.
  165. *
  166. * @return void
  167. */
  168. public function testFormattingMultipleSameTags() {
  169. $this->output->expects($this->once())->method('_write')
  170. ->with("\033[31;4mBad\033[0m \033[31;4mWarning\033[0m Regular");
  171. $this->output->write('<error>Bad</error> <error>Warning</error> Regular', false);
  172. }
  173. /**
  174. * test raw output not getting tags replaced.
  175. *
  176. * @return void
  177. */
  178. public function testOutputAsRaw() {
  179. $this->output->outputAs(ConsoleOutput::RAW);
  180. $this->output->expects($this->once())->method('_write')
  181. ->with('<error>Bad</error> Regular');
  182. $this->output->write('<error>Bad</error> Regular', false);
  183. }
  184. /**
  185. * test plain output.
  186. *
  187. * @return void
  188. */
  189. public function testOutputAsPlain() {
  190. $this->output->outputAs(ConsoleOutput::PLAIN);
  191. $this->output->expects($this->once())->method('_write')
  192. ->with('Bad Regular');
  193. $this->output->write('<error>Bad</error> Regular', false);
  194. }
  195. /**
  196. * test plain output only strips tags used for formatting.
  197. *
  198. * @return void
  199. */
  200. public function testOutputAsPlainSelectiveTagRemoval() {
  201. $this->output->outputAs(ConsoleOutput::PLAIN);
  202. $this->output->expects($this->once())->method('_write')
  203. ->with('Bad Regular <b>Left</b> <i>behind</i> <name>');
  204. $this->output->write('<error>Bad</error> Regular <b>Left</b> <i>behind</i> <name>', false);
  205. }
  206. }