TestBase.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace League\CLImate\Tests;
  3. use League\CLImate\CLImate;
  4. use PHPUnit\Framework\TestCase;
  5. use Mockery;
  6. class TestBase extends TestCase
  7. {
  8. public static $functions;
  9. /** @var League\CLImate\CLImate */
  10. public $cli;
  11. /** @var League\CLImate\Util\Output|Mockery\MockInterface */
  12. public $output;
  13. /** @var League\CLImate\Util\Reader\Stdin|Mockery\MockInterface */
  14. public $reader;
  15. /** @var League\CLImate\Util\UtilFactory */
  16. public $util;
  17. protected $record_it = false;
  18. public function setUp(): void
  19. {
  20. self::$functions = Mockery::mock();
  21. $system = Mockery::mock('League\CLImate\Util\System\Linux');
  22. $system->shouldReceive('hasAnsiSupport')->andReturn(true);
  23. $system->shouldReceive('width')->andReturn(80);
  24. $this->util = new \League\CLImate\Util\UtilFactory($system);
  25. $this->output = Mockery::mock('League\CLImate\Util\Output');
  26. $this->reader = Mockery::mock('League\CLImate\Util\Reader\Stdin');
  27. $this->cli = new CLImate();
  28. $this->cli->setOutput($this->output);
  29. $this->cli->setUtil($this->util);
  30. if (method_exists($this, 'internalSetup')) {
  31. $this->internalSetup();
  32. }
  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. if ($this->record_it) {
  47. file_put_contents('test-log', $content, FILE_APPEND);
  48. }
  49. return $this->output->shouldReceive('write')->times($count)->with($content);
  50. }
  51. /**
  52. * Helper for reader mock
  53. *
  54. * @param string $response
  55. */
  56. protected function shouldReadAndReturn($response)
  57. {
  58. $this->reader->shouldReceive('line')->once()->andReturn($response);
  59. }
  60. /**
  61. * Helper for reader mock
  62. *
  63. * @param string $response
  64. */
  65. protected function shouldReadCharAndReturn($response, $char_count = 1)
  66. {
  67. $this->reader->shouldReceive('char')->with($char_count)->once()->andReturn($response);
  68. }
  69. /**
  70. * Helper for reader mock
  71. *
  72. * @param string $response
  73. */
  74. protected function shouldReadMultipleLinesAndReturn($response)
  75. {
  76. $this->reader->shouldReceive('multiLine')->once()->andReturn($response);
  77. }
  78. /**
  79. * Helper for same line output mock
  80. */
  81. protected function shouldReceiveSameLine()
  82. {
  83. $this->output->shouldReceive('sameLine')->andReturn($this->output);
  84. }
  85. protected function shouldHavePersisted($times = 1)
  86. {
  87. $this->shouldStartPersisting($times);
  88. $this->shouldStopPersisting($times);
  89. }
  90. protected function shouldStartPersisting($times = 1)
  91. {
  92. $this->output->shouldReceive('persist')->withNoArgs()->times($times)->andReturn($this->output);
  93. }
  94. protected function shouldStopPersisting($times = 1)
  95. {
  96. $this->output->shouldReceive('persist')->with(false)->times($times)->andReturn($this->output);
  97. }
  98. /**
  99. * @test
  100. * @doesNotPerformAssertions
  101. */
  102. public function it_does_nothing()
  103. {
  104. // nada
  105. }
  106. }