FileTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace League\CLImate\Tests;
  3. require_once __DIR__ . '/../vendor/mikey179/vfsstream/src/main/php/org/bovigo/vfs/vfsStream.php';
  4. require_once 'FileGlobalMock.php';
  5. use League\CLImate\Exceptions\RuntimeException;
  6. use League\CLImate\Util\Output;
  7. use org\bovigo\vfs\vfsStream;
  8. class FileTest extends TestBase
  9. {
  10. protected $file;
  11. public function setUp(): void
  12. {
  13. $root = vfsStream::setup();
  14. $this->file = vfsStream::newFile('log')->at($root);
  15. }
  16. protected function getFileMock()
  17. {
  18. return \Mockery::mock('League\CLImate\Util\Writer\File', func_get_args())->makePartial();
  19. }
  20. /** @test */
  21. public function it_can_write_to_a_file()
  22. {
  23. $file = $this->getFileMock($this->file->url());
  24. self::$functions->shouldReceive('fopen')
  25. ->once()
  26. ->with($this->file->url(), 'a')
  27. ->andReturn(fopen($this->file->url(), 'a'));
  28. $output = new Output;
  29. $output->add('file', $file);
  30. $output->defaultTo('file');
  31. $output->write("Oh, you're still here.");
  32. $this->assertSame("Oh, you're still here." . \PHP_EOL, $this->file->getContent());
  33. }
  34. /** @test */
  35. public function it_will_accept_a_resource()
  36. {
  37. $resource = fopen($this->file->url(), 'a');
  38. $file = $this->getFileMock($resource);
  39. $output = new Output;
  40. $output->add('file', $file);
  41. $output->defaultTo('file');
  42. $output->write("Oh, you're still here.");
  43. $this->assertSame("Oh, you're still here." . \PHP_EOL, $this->file->getContent());
  44. }
  45. /** @test */
  46. public function it_can_lock_the_file()
  47. {
  48. $resource = fopen($this->file->url(), 'a');
  49. $file = $this->getFileMock($resource);
  50. self::$functions->shouldReceive('flock')
  51. ->once()
  52. ->with($resource, LOCK_EX);
  53. self::$functions->shouldReceive('flock')
  54. ->once()
  55. ->with($resource, LOCK_UN);
  56. $file->lock();
  57. $output = new Output;
  58. $output->add('file', $file);
  59. $output->defaultTo('file');
  60. $output->write("Oh, you're still here.");
  61. $this->assertSame("Oh, you're still here." . \PHP_EOL, $this->file->getContent());
  62. }
  63. /**
  64. * @codeCoverageIgnore
  65. * @doesNotPerformAssertions
  66. */
  67. public function it_can_write_to_a_gzipped_file()
  68. {
  69. // $file = $this->getFileMock($this->file->url());
  70. // self::$functions->shouldReceive('gzopen')
  71. // ->once()
  72. // ->with($this->file->url(), 'a')
  73. // ->andReturn('file resource');
  74. // self::$functions->shouldReceive('gzwrite')
  75. // ->once()
  76. // ->with('file resource', "Oh, you're still here.");
  77. // $file->gzipped();
  78. // $output = new Output;
  79. // $output->add('file', $file);
  80. // $output->defaultTo('file');
  81. // $output->write("Oh, you're still here.");
  82. // $this->assertSame("Oh, you're still here." . \PHP_EOL, $this->file->getContent());
  83. }
  84. /** @test */
  85. public function it_will_yell_when_a_non_writable_resource_is_passed()
  86. {
  87. $this->file->chmod(0444);
  88. $this->expectException(RuntimeException::class);
  89. $this->expectExceptionMessage("is not writable");
  90. $file = $this->getFileMock($this->file->url());
  91. $output = new Output;
  92. $output->add('file', $file);
  93. $output->defaultTo('file');
  94. $output->write("Oh, you're still here.");
  95. }
  96. /** @test */
  97. public function it_will_yell_when_a_non_existent_resource_is_passed()
  98. {
  99. $this->expectException(RuntimeException::class);
  100. $this->expectExceptionMessage("The resource [something-that-doesnt-exist] is not writable");
  101. $file = $this->getFileMock('something-that-doesnt-exist');
  102. $output = new Output;
  103. $output->add('file', $file);
  104. $output->defaultTo('file');
  105. $output->write("Oh, you're still here.");
  106. }
  107. /** @test */
  108. public function it_will_yell_when_it_failed_to_open_a_resource()
  109. {
  110. $this->expectException(RuntimeException::class);
  111. $this->expectExceptionMessage("The resource could not be opened");
  112. $file = $this->getFileMock($this->file->url());
  113. self::$functions->shouldReceive('fopen')
  114. ->once()
  115. ->with($this->file->url(), 'a')
  116. ->andReturn(false);
  117. $output = new Output;
  118. $output->add('file', $file);
  119. $output->defaultTo('file');
  120. $output->write("Oh, you're still here.");
  121. }
  122. }