AnsiTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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;
  9. use League\CLImate\TerminalObject\Router\BasicRouter;
  10. use League\CLImate\Util\System\Linux;
  11. use League\CLImate\Util\System\Windows;
  12. use League\CLImate\Util\UtilFactory;
  13. use Mockery;
  14. class AnsiTest extends TestBase
  15. {
  16. /**
  17. * @test
  18. * @doesNotPerformAssertions
  19. */
  20. public function it_can_output_with_ansi()
  21. {
  22. $router = new BasicRouter();
  23. $router->output($this->output);
  24. $style = new Style();
  25. $parser = new Ansi($style->current(), new Tags($style->all()));
  26. $obj = Mockery::mock('League\CLImate\TerminalObject');
  27. $obj->shouldReceive('result')->once()->andReturn("<green>I am green</green>");
  28. $obj->shouldReceive('sameLine')->once()->andReturn(false);
  29. $obj->shouldReceive('getParser')->once()->andReturn($parser);
  30. $this->shouldWrite("\e[m\e[32mI am green\e[0m\e[0m");
  31. $this->shouldHavePersisted();
  32. $router->execute($obj);
  33. }
  34. /**
  35. * @test
  36. * @doesNotPerformAssertions
  37. */
  38. public function it_can_output_without_ansi()
  39. {
  40. $router = new BasicRouter();
  41. $router->output($this->output);
  42. $style = new Style();
  43. $parser = new NonAnsi($style->current(), new Tags($style->all()));
  44. $obj = Mockery::mock('League\CLImate\TerminalObject');
  45. $obj->shouldReceive('result')->once()->andReturn("<green>I am not green</green>");
  46. $obj->shouldReceive('sameLine')->once()->andReturn(false);
  47. $obj->shouldReceive('getParser')->once()->andReturn($parser);
  48. $this->shouldWrite("I am not green");
  49. $this->shouldHavePersisted();
  50. $router->execute($obj);
  51. }
  52. /** @test */
  53. public function it_will_recognize_non_ansi_systems()
  54. {
  55. $system = Mockery::mock('League\CLImate\Util\System\Windows');
  56. $system->shouldReceive('hasAnsiSupport')->andReturn(false);
  57. $parser = ParserFactory::getInstance($system, [], new Tags([]));
  58. $this->assertInstanceOf('League\CLImate\Decorator\Parser\NonAnsi', $parser);
  59. }
  60. /**
  61. * @test
  62. * @doesNotPerformAssertions
  63. */
  64. public function it_will_force_ansi_on_a_non_ansi_system()
  65. {
  66. $system = new Windows();
  67. $util = new UtilFactory($system);
  68. $this->cli->setUtil($util);
  69. $this->cli->forceAnsiOn();
  70. $this->shouldWrite("\e[m\e[32mI am green\e[0m\e[0m");
  71. $this->shouldHavePersisted();
  72. $this->cli->out("<green>I am green</green>");
  73. }
  74. /**
  75. * @test
  76. * @doesNotPerformAssertions
  77. */
  78. public function it_will_force_ansi_off_on_an_ansi_system()
  79. {
  80. $system = new Linux();
  81. $util = new UtilFactory($system);
  82. $this->cli->setUtil($util);
  83. $this->cli->forceAnsiOff();
  84. $this->shouldWrite("I am green");
  85. $this->shouldHavePersisted();
  86. $this->cli->out("<green>I am green</green>");
  87. }
  88. /**
  89. * Ensure we can check if stdout is defined safely.
  90. * https://github.com/thephpleague/climate/issues/185
  91. */
  92. public function test6()
  93. {
  94. $system = new Linux();
  95. $util = new UtilFactory($system);
  96. $this->cli->setUtil($util);
  97. $this->output->shouldReceive('write')->times(1);
  98. $this->shouldHavePersisted();
  99. $this->cli->out("<green>I am green</green>");
  100. self::assertTrue(true);
  101. }
  102. }