ConsoleOutputTest.php 6.1 KB

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