ConsoleOutputTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * ConsoleOutputTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2010, 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-2010, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake.tests.cases.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. 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. function tearDown() {
  37. unset($this->output);
  38. }
  39. /**
  40. * test writing with no new line
  41. *
  42. * @return void
  43. */
  44. function testWriteNoNewLine() {
  45. $this->output->expects($this->once())->method('_write')
  46. ->with('Some output');
  47. $this->output->write('Some output', false);
  48. }
  49. /**
  50. * test writing with no new line
  51. *
  52. * @return void
  53. */
  54. function testWriteNewLine() {
  55. $this->output->expects($this->once())->method('_write')
  56. ->with('Some output' . PHP_EOL);
  57. $this->output->write('Some output');
  58. }
  59. /**
  60. * test write() with multiple new lines
  61. *
  62. * @return void
  63. */
  64. function testWriteMultipleNewLines() {
  65. $this->output->expects($this->once())->method('_write')
  66. ->with('Some output' . PHP_EOL . PHP_EOL . PHP_EOL . PHP_EOL);
  67. $this->output->write('Some output', 4);
  68. }
  69. /**
  70. * test writing an array of messages.
  71. *
  72. * @return void
  73. */
  74. function testWriteArray() {
  75. $this->output->expects($this->once())->method('_write')
  76. ->with('Line' . PHP_EOL . 'Line' . PHP_EOL . 'Line' . PHP_EOL);
  77. $this->output->write(array('Line', 'Line', 'Line'));
  78. }
  79. /**
  80. * test getting a style.
  81. *
  82. * @return void
  83. */
  84. function testStylesGet() {
  85. $result = $this->output->styles('error');
  86. $expected = array('text' => 'red', 'underline' => true);
  87. $this->assertEqual($result, $expected);
  88. $this->assertNull($this->output->styles('made_up_goop'));
  89. $result = $this->output->styles();
  90. $this->assertNotEmpty($result, 'error', 'Error is missing');
  91. $this->assertNotEmpty($result, 'warning', 'Warning is missing');
  92. }
  93. /**
  94. * test adding a style.
  95. *
  96. * @return void
  97. */
  98. function testStylesAdding() {
  99. $this->output->styles('test', array('text' => 'red', 'background' => 'black'));
  100. $result = $this->output->styles('test');
  101. $expected = array('text' => 'red', 'background' => 'black');
  102. $this->assertEquals($expected, $result);
  103. $this->assertTrue($this->output->styles('test', false), 'Removing a style should return true.');
  104. $this->assertNull($this->output->styles('test'), 'Removed styles should be null.');
  105. }
  106. /**
  107. * test formatting text with styles.
  108. *
  109. * @return void
  110. */
  111. function testFormattingSimple() {
  112. $this->output->expects($this->once())->method('_write')
  113. ->with("\033[31;4mError:\033[0m Something bad");
  114. $this->output->write('<error>Error:</error> Something bad', false);
  115. }
  116. /**
  117. * test that formatting doesn't eat tags it doesn't know about.
  118. *
  119. * @return void
  120. */
  121. function testFormattingNotEatingTags() {
  122. $this->output->expects($this->once())->method('_write')
  123. ->with("<red> Something bad");
  124. $this->output->write('<red> Something bad', false);
  125. }
  126. /**
  127. * test formatting with custom styles.
  128. *
  129. * @return void
  130. */
  131. function testFormattingCustom() {
  132. $this->output->styles('annoying', array(
  133. 'text' => 'magenta',
  134. 'background' => 'cyan',
  135. 'blink' => true,
  136. 'underline' => true
  137. ));
  138. $this->output->expects($this->once())->method('_write')
  139. ->with("\033[35;46;5;4mAnnoy:\033[0m Something bad");
  140. $this->output->write('<annoying>Annoy:</annoying> Something bad', false);
  141. }
  142. /**
  143. * test formatting text with missing styles.
  144. *
  145. * @return void
  146. */
  147. function testFormattingMissingStyleName() {
  148. $this->output->expects($this->once())->method('_write')
  149. ->with("<not_there>Error:</not_there> Something bad");
  150. $this->output->write('<not_there>Error:</not_there> Something bad', false);
  151. }
  152. /**
  153. * test formatting text with multiple styles.
  154. *
  155. * @return void
  156. */
  157. function testFormattingMultipleStylesName() {
  158. $this->output->expects($this->once())->method('_write')
  159. ->with("\033[31;4mBad\033[0m \033[33mWarning\033[0m Regular");
  160. $this->output->write('<error>Bad</error> <warning>Warning</warning> Regular', false);
  161. }
  162. /**
  163. * test that multiple tags of the same name work in one string.
  164. *
  165. * @return void
  166. */
  167. function testFormattingMultipleSameTags() {
  168. $this->output->expects($this->once())->method('_write')
  169. ->with("\033[31;4mBad\033[0m \033[31;4mWarning\033[0m Regular");
  170. $this->output->write('<error>Bad</error> <error>Warning</error> Regular', false);
  171. }
  172. /**
  173. * test raw output not getting tags replaced.
  174. *
  175. * @return void
  176. */
  177. function testOutputAsRaw() {
  178. $this->output->outputAs(ConsoleOutput::RAW);
  179. $this->output->expects($this->once())->method('_write')
  180. ->with('<error>Bad</error> Regular');
  181. $this->output->write('<error>Bad</error> Regular', false);
  182. }
  183. /**
  184. * test plain output.
  185. *
  186. * @return void
  187. */
  188. function testOutputAsPlain() {
  189. $this->output->outputAs(ConsoleOutput::PLAIN);
  190. $this->output->expects($this->once())->method('_write')
  191. ->with('Bad Regular');
  192. $this->output->write('<error>Bad</error> Regular', false);
  193. }
  194. }