AnsiTest.php 3.6 KB

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