FileTest.php 4.4 KB

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