TestBase.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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()
  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. $this->cli->setReader($this->reader);
  31. if (method_exists($this, 'internalSetup')) {
  32. $this->internalSetup();
  33. }
  34. }
  35. public function tearDown()
  36. {
  37. Mockery::close();
  38. }
  39. /**
  40. * Helper for writer mock
  41. *
  42. * @param string $content
  43. * @param integer $count
  44. */
  45. protected function shouldWrite($content, $count = 1)
  46. {
  47. if ($this->record_it) {
  48. file_put_contents('test-log', $content, FILE_APPEND);
  49. }
  50. return $this->output->shouldReceive('write')->times($count)->with($content);
  51. }
  52. /**
  53. * Helper for reader mock
  54. *
  55. * @param string $response
  56. */
  57. protected function shouldReadAndReturn($response)
  58. {
  59. $this->reader->shouldReceive('line')->once()->andReturn($response);
  60. }
  61. /**
  62. * Helper for reader mock
  63. *
  64. * @param string $response
  65. */
  66. protected function shouldReadCharAndReturn($response, $char_count = 1)
  67. {
  68. $this->reader->shouldReceive('char')->with($char_count)->once()->andReturn($response);
  69. }
  70. /**
  71. * Helper for reader mock
  72. *
  73. * @param string $response
  74. */
  75. protected function shouldReadMultipleLinesAndReturn($response)
  76. {
  77. $this->reader->shouldReceive('multiLine')->once()->andReturn($response);
  78. }
  79. /**
  80. * Helper for same line output mock
  81. */
  82. protected function shouldReceiveSameLine()
  83. {
  84. $this->output->shouldReceive('sameLine')->andReturn($this->output);
  85. }
  86. protected function shouldHavePersisted($times = 1)
  87. {
  88. $this->shouldStartPersisting($times);
  89. $this->shouldStopPersisting($times);
  90. }
  91. protected function shouldStartPersisting($times = 1)
  92. {
  93. $this->output->shouldReceive('persist')->withNoArgs()->times($times)->andReturn($this->output);
  94. }
  95. protected function shouldStopPersisting($times = 1)
  96. {
  97. $this->output->shouldReceive('persist')->with(false)->times($times)->andReturn($this->output);
  98. }
  99. /** @test */
  100. public function it_does_nothing()
  101. {
  102. // nada
  103. }
  104. }