StyleTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace League\CLImate\Tests;
  3. class StyleTest extends TestBase
  4. {
  5. /** @test */
  6. public function it_can_use_a_foreground_color_method()
  7. {
  8. $this->shouldWrite("\e[31mThis would go out to the console.\e[0m");
  9. $this->shouldHavePersisted();
  10. $this->cli->red('This would go out to the console.');
  11. }
  12. /** @test */
  13. public function it_resets_itself_after_styled_output()
  14. {
  15. $this->shouldWrite("\e[31mThis would go out to the console.\e[0m");
  16. $this->shouldWrite("\e[mThis is plain.\e[0m");
  17. $this->shouldHavePersisted(2);
  18. $this->cli->red('This would go out to the console.');
  19. $this->cli->out('This is plain.');
  20. }
  21. /** @test */
  22. public function it_can_use_a_background_color_method()
  23. {
  24. $this->shouldWrite("\e[41mThis would go out to the console.\e[0m");
  25. $this->shouldHavePersisted();
  26. $this->cli->backgroundRed('This would go out to the console.');
  27. }
  28. /** @test */
  29. public function it_can_use_a_background_color_method_chained()
  30. {
  31. $this->shouldWrite("\e[41mThis would go out to the console.\e[0m");
  32. $this->shouldHavePersisted();
  33. $this->cli->backgroundRed()->out('This would go out to the console.');
  34. }
  35. /** @test */
  36. public function it_can_apply_a_format()
  37. {
  38. $this->shouldWrite("\e[5mThis would go out to the console.\e[0m");
  39. $this->shouldHavePersisted();
  40. $this->cli->blink('This would go out to the console.');
  41. }
  42. /** @test */
  43. public function it_can_apply_multiple_formats()
  44. {
  45. $this->shouldWrite("\e[4;5mThis would go out to the console.\e[0m");
  46. $this->shouldHavePersisted();
  47. $this->cli->underline()->blink('This would go out to the console.');
  48. }
  49. /** @test */
  50. public function it_can_chain_a_foreground_color_method()
  51. {
  52. $this->shouldWrite("\e[31mThis would go out to the console.\e[0m");
  53. $this->shouldHavePersisted();
  54. $this->cli->red()->out('This would go out to the console.');
  55. }
  56. /** @test */
  57. public function it_can_use_a_background_color_and_foreground_color_method()
  58. {
  59. $this->shouldWrite("\e[31;41mThis would go out to the console.\e[0m");
  60. $this->shouldHavePersisted();
  61. $this->cli->backgroundRed()->red('This would go out to the console.');
  62. }
  63. /** @test */
  64. public function it_can_use_a_background_color_and_foreground_color_and_format_method()
  65. {
  66. $this->shouldWrite("\e[5;31;41mThis would go out to the console.\e[0m");
  67. $this->shouldHavePersisted();
  68. $this->cli->backgroundRed()->blink()->red('This would go out to the console.');
  69. }
  70. /** @test */
  71. public function it_can_parse_foreground_color_tags()
  72. {
  73. $this->shouldWrite("\e[mThis \e[31mwould\e[0m go out to the console.\e[0m");
  74. $this->shouldHavePersisted();
  75. $this->cli->out('This <red>would</red> go out to the console.');
  76. }
  77. /** @test */
  78. public function it_can_parse_background_color_tags()
  79. {
  80. $this->shouldWrite("\e[mThis \e[41mwould\e[0m go out to the console.\e[0m");
  81. $this->shouldHavePersisted();
  82. $this->cli->out('This <background_red>would</background_red> go out to the console.');
  83. }
  84. /** @test */
  85. public function it_can_parse_formatting_tags()
  86. {
  87. $this->shouldWrite("\e[mThis \e[5mwould\e[0m go out to the console.\e[0m");
  88. $this->shouldHavePersisted();
  89. $this->cli->out('This <blink>would</blink> go out to the console.');
  90. }
  91. /** @test */
  92. public function it_can_parse_nested_tags()
  93. {
  94. $this->shouldWrite("\e[mThis \e[31m\e[5mwould\e[0;31m (still red)\e[0m go out to the console.\e[0m");
  95. $this->shouldHavePersisted();
  96. $this->cli->out('This <red><blink>would</blink> (still red)</red> go out to the console.');
  97. }
  98. /** @test */
  99. public function it_can_parse_tags_and_return_to_current_style()
  100. {
  101. $this->shouldWrite("\e[31mThis \e[5mwould\e[0;31m go out to the console.\e[0m");
  102. $this->shouldHavePersisted();
  103. $this->cli->red('This <blink>would</blink> go out to the console.');
  104. }
  105. /** @test */
  106. public function it_can_add_a_color_and_use_it()
  107. {
  108. $this->shouldWrite("\e[900mThis is the new color.\e[0m");
  109. $this->shouldHavePersisted();
  110. $this->cli->style->addColor('new_custom_color', 900);
  111. $this->cli->newCustomColor('This is the new color.');
  112. }
  113. /** @test */
  114. public function it_can_add_a_color_and_use_it_as_a_tag()
  115. {
  116. $this->shouldWrite("\e[mThis \e[900mis\e[0m the new color.\e[0m");
  117. $this->shouldHavePersisted();
  118. $this->cli->style->addColor('new_custom_color', 900);
  119. $this->cli->out('This <new_custom_color>is</new_custom_color> the new color.');
  120. }
  121. /** @test */
  122. public function it_can_add_a_color_and_use_it_as_a_background()
  123. {
  124. $this->shouldWrite("\e[910mThis is the new color.\e[0m");
  125. $this->shouldHavePersisted();
  126. $this->cli->style->addColor('new_custom_color', 900);
  127. $this->cli->backgroundNewCustomColor()->out('This is the new color.');
  128. }
  129. /** @test */
  130. public function it_can_use_a_color_command()
  131. {
  132. $this->shouldWrite("\e[91mThis would go out to the console.\e[0m");
  133. $this->shouldHavePersisted();
  134. $this->cli->error('This would go out to the console.');
  135. }
  136. /** @test */
  137. public function it_can_chain_a_color_command()
  138. {
  139. $this->shouldWrite("\e[91mThis would go out to the console.\e[0m");
  140. $this->shouldHavePersisted();
  141. $this->cli->error()->out('This would go out to the console.');
  142. }
  143. /** @test */
  144. public function it_can_use_add_a_command_via_a_string()
  145. {
  146. $this->shouldWrite("\e[94mThis would go out to the console.\e[0m");
  147. $this->shouldHavePersisted();
  148. $this->cli->style->addCommand('holler', 'light_blue');
  149. $this->cli->holler('This would go out to the console.');
  150. }
  151. /** @test */
  152. public function it_can_use_a_string_command_as_a_tag()
  153. {
  154. $this->shouldWrite("\e[mThis would go \e[94mout\e[0m to the console.\e[0m");
  155. $this->shouldHavePersisted();
  156. $this->cli->style->addCommand('holler', 'light_blue');
  157. $this->cli->out('This would go <holler>out</holler> to the console.');
  158. }
  159. /** @test */
  160. public function it_can_use_add_a_command_via_an_array()
  161. {
  162. $this->shouldWrite("\e[1;4;41;94mThis would go out to the console.\e[0m");
  163. $this->shouldHavePersisted();
  164. $command = ['light_blue', 'background_red', 'bold', 'underline'];
  165. $this->cli->style->addCommand('holler', $command);
  166. $this->cli->holler('This would go out to the console.');
  167. }
  168. /** @test */
  169. public function it_can_use_an_array_command_as_a_tag()
  170. {
  171. $this->shouldWrite("\e[mThis would go \e[1;4;41;94mout\e[0m to the console.\e[0m");
  172. $this->shouldHavePersisted();
  173. $command = ['light_blue', 'background_red', 'bold', 'underline'];
  174. $this->cli->style->addCommand('holler', $command);
  175. $this->cli->out('This would go <holler>out</holler> to the console.');
  176. }
  177. }