TestBase.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace League\CLImate\Tests;
  3. use League\CLImate\CLImate;
  4. use League\CLImate\Util\Output;
  5. use League\CLImate\Util\Reader\Stdin;
  6. use League\CLImate\Util\System\Linux;
  7. use League\CLImate\Util\UtilFactory;
  8. use Mockery;
  9. use PHPUnit\Framework\TestCase;
  10. abstract class TestBase extends TestCase
  11. {
  12. public static $functions;
  13. /** @var CLImate */
  14. public $cli;
  15. /** @var Output|Mockery\MockInterface */
  16. public $output;
  17. /** @var Stdin|Mockery\MockInterface */
  18. public $reader;
  19. /** @var UtilFactory */
  20. public $util;
  21. public function setUp(): void
  22. {
  23. self::$functions = Mockery::mock();
  24. $system = Mockery::mock(Linux::class);
  25. $system->shouldReceive('hasAnsiSupport')->andReturn(true);
  26. $system->shouldReceive('width')->andReturn(80);
  27. $this->util = new UtilFactory($system);
  28. $this->output = Mockery::mock(Output::class);
  29. $this->reader = Mockery::mock(Stdin::class);
  30. $this->cli = new CLImate();
  31. $this->cli->setOutput($this->output);
  32. $this->cli->setUtil($this->util);
  33. }
  34. public function tearDown(): void
  35. {
  36. Mockery::close();
  37. }
  38. /**
  39. * Helper for writer mock
  40. *
  41. * @param string $content
  42. * @param integer $count
  43. */
  44. protected function shouldWrite($content, $count = 1)
  45. {
  46. return $this->output->shouldReceive('write')->times($count)->with($content);
  47. }
  48. /**
  49. * Helper for reader mock
  50. *
  51. * @param string $response
  52. */
  53. protected function shouldReadAndReturn($response)
  54. {
  55. $this->reader->shouldReceive('line')->once()->andReturn($response);
  56. }
  57. /**
  58. * Helper for reader mock
  59. *
  60. * @param string $response
  61. */
  62. protected function shouldReadCharAndReturn($response, $char_count = 1)
  63. {
  64. $this->reader->shouldReceive('char')->with($char_count)->once()->andReturn($response);
  65. }
  66. /**
  67. * Helper for reader mock
  68. *
  69. * @param string $response
  70. */
  71. protected function shouldReadMultipleLinesAndReturn($response)
  72. {
  73. $this->reader->shouldReceive('multiLine')->once()->andReturn($response);
  74. }
  75. /**
  76. * Helper for same line output mock
  77. */
  78. protected function shouldReceiveSameLine()
  79. {
  80. $this->output->shouldReceive('sameLine')->andReturn($this->output);
  81. }
  82. protected function shouldHavePersisted($times = 1)
  83. {
  84. $this->shouldStartPersisting($times);
  85. $this->shouldStopPersisting($times);
  86. }
  87. protected function shouldStartPersisting($times = 1)
  88. {
  89. $this->output->shouldReceive('persist')->withNoArgs()->times($times)->andReturn($this->output);
  90. }
  91. protected function shouldStopPersisting($times = 1)
  92. {
  93. $this->output->shouldReceive('persist')->with(false)->times($times)->andReturn($this->output);
  94. }
  95. }