FileTest.php 4.8 KB

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