FunctionalTestCase.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace Gaufrette\Functional\FileStream;
  3. use Gaufrette\StreamWrapper;
  4. class FunctionalTestCase extends \PHPUnit_Framework_TestCase
  5. {
  6. protected $filesystem;
  7. /**
  8. * @test
  9. */
  10. public function shouldCheckIsFile()
  11. {
  12. $this->filesystem->write('test.txt', 'some content');
  13. $this->assertTrue(is_file('gaufrette://filestream/test.txt'));
  14. $this->filesystem->delete('test.txt');
  15. $this->assertFalse(is_file('gaufrette://filestream/test.txt'));
  16. }
  17. /**
  18. * @test
  19. */
  20. public function shouldCheckFileExists()
  21. {
  22. $this->filesystem->write('test.txt', 'some content');
  23. $this->assertTrue(file_exists('gaufrette://filestream/test.txt'));
  24. $this->filesystem->delete('test.txt');
  25. $this->assertFalse(file_exists('gaufrette://filestream/test.txt'));
  26. }
  27. /**
  28. * @test
  29. */
  30. public function shouldWriteAndReadFile()
  31. {
  32. file_put_contents('gaufrette://filestream/test.txt', 'test content');
  33. $this->assertEquals('test content', file_get_contents('gaufrette://filestream/test.txt'));
  34. $this->filesystem->delete('test.txt');
  35. }
  36. /**
  37. * @test
  38. * @expectedException \LogicException
  39. * @expectedExceptionMessage The stream does not allow read.
  40. */
  41. public function shouldNotReadWhenOpenInWriteMode()
  42. {
  43. $this->filesystem->write('test.txt', 'test content');
  44. $fileHandler = fopen('gaufrette://filestream/test.txt', 'w');
  45. fread($fileHandler, 10);
  46. fclose($fileHandler);
  47. $this->filesystem->delete('test.txt');
  48. }
  49. /**
  50. * @test
  51. * @expectedException \LogicException
  52. * @expectedExceptionMessage The stream does not allow write.
  53. */
  54. public function shouldNotWriteWhenOpenInReadMode()
  55. {
  56. $this->filesystem->write('test.txt', 'test content');
  57. $fileHandler = fopen('gaufrette://filestream/test.txt', 'r');
  58. fwrite($fileHandler, 'test content2');
  59. fclose($fileHandler);
  60. $this->filesystem->delete('test.txt');
  61. }
  62. /**
  63. * @test
  64. */
  65. public function shouldWriteFromSettedPosition()
  66. {
  67. $fileHandler = fopen('gaufrette://filestream/test.txt', 'w');
  68. fseek($fileHandler, 1, SEEK_SET);
  69. fwrite($fileHandler, 'est');
  70. fseek($fileHandler, 0, SEEK_SET);
  71. fwrite($fileHandler, 't');
  72. fclose($fileHandler);
  73. $this->assertEquals('test', $this->filesystem->read('test.txt'));
  74. $fileHandler = fopen('gaufrette://filestream/test.txt', 'w');
  75. fseek($fileHandler, 0, SEEK_SET);
  76. fwrite($fileHandler, 't');
  77. fseek($fileHandler, 1, SEEK_SET);
  78. fwrite($fileHandler, 'est');
  79. fseek($fileHandler, 0, SEEK_SET);
  80. fwrite($fileHandler, 'f');
  81. fclose($fileHandler);
  82. $this->assertEquals('fest', $this->filesystem->read('test.txt'));
  83. }
  84. /**
  85. * @test
  86. */
  87. public function shouldWriteEmptyContent()
  88. {
  89. $bytes = file_put_contents('gaufrette://filestream/test.txt', '');
  90. $this->assertEquals('', file_get_contents('gaufrette://filestream/test.txt'));
  91. $this->filesystem->delete('test.txt');
  92. $this->assertSame(0, $bytes);
  93. }
  94. /**
  95. * @test
  96. */
  97. public function shouldSetAndGetPosition()
  98. {
  99. file_put_contents('gaufrette://filestream/test.txt', 'test content');
  100. $fileHandler = fopen('gaufrette://filestream/test.txt', 'r+');
  101. fseek($fileHandler, 1, SEEK_SET);
  102. $this->assertEquals(1, ftell($fileHandler));
  103. fseek($fileHandler, 1, SEEK_CUR);
  104. $this->assertEquals(2, ftell($fileHandler));
  105. fclose($fileHandler);
  106. $fileHandler = fopen('gaufrette://filestream/test.txt', 'r+');
  107. fseek($fileHandler, 1, SEEK_CUR);
  108. $this->assertEquals(1, ftell($fileHandler));
  109. fclose($fileHandler);
  110. $fileHandler = fopen('gaufrette://filestream/test.txt', 'r+');
  111. fseek($fileHandler, -2, SEEK_END);
  112. $this->assertEquals(10, ftell($fileHandler));
  113. fclose($fileHandler);
  114. }
  115. /**
  116. * @test
  117. */
  118. public function shouldNotSeekWhenWhenceParameterIsInvalid()
  119. {
  120. file_put_contents('gaufrette://filestream/test.txt', 'test content');
  121. $fileHandler = fopen('gaufrette://filestream/test.txt', 'r+');
  122. $this->assertEquals(-1, fseek($fileHandler, 1, 666));
  123. }
  124. /**
  125. * @test
  126. */
  127. public function shouldHandlesSubDir()
  128. {
  129. file_put_contents('gaufrette://filestream/subdir/test.txt', 'test content');
  130. $this->assertTrue(is_file('gaufrette://filestream/subdir/test.txt'));
  131. $this->filesystem->delete('subdir/test.txt');
  132. $this->assertFalse(is_file('gaufrette://filestream/subdir/test.txt'));
  133. }
  134. /**
  135. * @test
  136. */
  137. public function shouldUnlinkFile()
  138. {
  139. $this->filesystem->write('test.txt', 'some content');
  140. unlink('gaufrette://filestream/test.txt');
  141. $this->assertFalse($this->filesystem->has('test.txt'));
  142. }
  143. /**
  144. * @test
  145. * @dataProvider modesProvider
  146. */
  147. public function shouldCreateNewFile($mode)
  148. {
  149. $fileHandler = fopen('gaufrette://filestream/test.txt', $mode);
  150. $this->assertTrue(file_exists('gaufrette://filestream/test.txt'));
  151. }
  152. public static function modesProvider()
  153. {
  154. return array(
  155. array('w'),
  156. array('a+'),
  157. array('w+'),
  158. array('ab+'),
  159. array('wb'),
  160. array('wb+')
  161. );
  162. }
  163. protected function registerLocalFilesystemInStream()
  164. {
  165. $filesystemMap = StreamWrapper::getFilesystemMap();
  166. $filesystemMap->set('filestream', $this->filesystem);
  167. StreamWrapper::register();
  168. }
  169. }