AnsiTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace League\CLImate\Tests;
  3. use League\CLImate\Decorator\Parser\Ansi;
  4. use League\CLImate\Decorator\Parser\NonAnsi;
  5. use League\CLImate\Decorator\Parser\ParserFactory;
  6. use League\CLImate\Decorator\Style;
  7. use League\CLImate\Decorator\Tags;
  8. use League\CLImate\TerminalObject\Router\BasicRouter;
  9. use League\CLImate\Util\System\Linux;
  10. use League\CLImate\Util\System\Windows;
  11. use League\CLImate\Util\UtilFactory;
  12. use Mockery;
  13. class AnsiTest extends TestBase
  14. {
  15. /** @test */
  16. public function it_can_output_with_ansi()
  17. {
  18. $router = new BasicRouter();
  19. $router->output($this->output);
  20. $style = new Style();
  21. $parser = new Ansi($style->current(), new Tags($style->all()));
  22. $obj = Mockery::mock('League\CLImate\TerminalObject');
  23. $obj->shouldReceive('result')->once()->andReturn("<green>I am green</green>");
  24. $obj->shouldReceive('sameLine')->once()->andReturn(false);
  25. $obj->shouldReceive('getParser')->once()->andReturn($parser);
  26. $this->shouldWrite("\e[m\e[32mI am green\e[0m\e[0m");
  27. $this->shouldHavePersisted();
  28. $router->execute($obj);
  29. }
  30. /** @test */
  31. public function it_can_output_without_ansi()
  32. {
  33. $router = new BasicRouter();
  34. $router->output($this->output);
  35. $style = new Style();
  36. $parser = new NonAnsi($style->current(), new Tags($style->all()));
  37. $obj = Mockery::mock('League\CLImate\TerminalObject');
  38. $obj->shouldReceive('result')->once()->andReturn("<green>I am not green</green>");
  39. $obj->shouldReceive('sameLine')->once()->andReturn(false);
  40. $obj->shouldReceive('getParser')->once()->andReturn($parser);
  41. $this->shouldWrite("I am not green");
  42. $this->shouldHavePersisted();
  43. $router->execute($obj);
  44. }
  45. /** @test */
  46. public function it_will_recognize_non_ansi_systems()
  47. {
  48. $system = Mockery::mock('League\CLImate\Util\System\Windows');
  49. $system->shouldReceive('hasAnsiSupport')->andReturn(false);
  50. $parser = ParserFactory::getInstance($system, [], new Tags([]));
  51. $this->assertInstanceOf('League\CLImate\Decorator\Parser\NonAnsi', $parser);
  52. }
  53. /** @test */
  54. public function it_will_force_ansi_on_a_non_ansi_system()
  55. {
  56. $system = new Windows();
  57. $util = new UtilFactory($system);
  58. $this->cli->setUtil($util);
  59. $this->cli->forceAnsiOn();
  60. $this->shouldWrite("\e[m\e[32mI am green\e[0m\e[0m");
  61. $this->shouldHavePersisted();
  62. $this->cli->out("<green>I am green</green>");
  63. }
  64. /** @test */
  65. public function it_will_force_ansi_off_on_an_ansi_system()
  66. {
  67. $system = new Linux();
  68. $util = new UtilFactory($system);
  69. $this->cli->setUtil($util);
  70. $this->cli->forceAnsiOff();
  71. $this->shouldWrite("I am green");
  72. $this->shouldHavePersisted();
  73. $this->cli->out("<green>I am green</green>");
  74. }
  75. }