FileTests.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace League\Flysystem;
  3. use PHPUnit\Framework\TestCase;
  4. class FileTests extends TestCase
  5. {
  6. /** @var Filesystem */
  7. protected $filesystem;
  8. public function setup(): void
  9. {
  10. clearstatcache();
  11. $fs = new Adapter\Local(__DIR__ . '/');
  12. $fs->deleteDir('files');
  13. $fs->createDir('files', new Config());
  14. $fs->write('file.txt', 'contents', new Config());
  15. $this->filesystem = new Filesystem($fs);
  16. }
  17. public function tearDown(): void
  18. {
  19. try {
  20. $this->filesystem->delete('file.txt');
  21. } catch (FileNotFoundException $e) {
  22. }
  23. $this->filesystem->deleteDir('files');
  24. }
  25. /**
  26. * @return File
  27. */
  28. protected function getFile()
  29. {
  30. return $this->filesystem->get('file.txt');
  31. }
  32. public function testExists()
  33. {
  34. $file = $this->getFile();
  35. $this->assertTrue($file->exists());
  36. }
  37. public function testRead()
  38. {
  39. $file = $this->getFile();
  40. $contents = $file->read();
  41. $this->assertEquals('contents', $contents);
  42. }
  43. public function testReadSteam()
  44. {
  45. $file = $this->getFile();
  46. $this->assertIsResource($file->readStream());
  47. }
  48. public function testWrite()
  49. {
  50. $file = new File();
  51. $this->filesystem->get('files/new.txt', $file);
  52. $file->write('new contents');
  53. $this->assertEquals('new contents', $file->read());
  54. }
  55. public function testWriteStream()
  56. {
  57. $file = new File();
  58. $this->filesystem->get('files/new.txt', $file);
  59. $resource = tmpfile();
  60. fwrite($resource, 'stream contents');
  61. $file->writeStream($resource);
  62. $this->assertEquals('stream contents', $file->read());
  63. }
  64. public function testUpdate()
  65. {
  66. $file = $this->getFile();
  67. $file->update('new contents');
  68. $this->assertEquals('new contents', $file->read());
  69. }
  70. public function testUpdateStream()
  71. {
  72. $file = $this->getFile();
  73. $resource = tmpfile();
  74. fwrite($resource, 'stream contents');
  75. $file->updateStream($resource);
  76. fclose($resource);
  77. $this->assertEquals('stream contents', $file->read());
  78. }
  79. public function testPut()
  80. {
  81. $file = new File();
  82. $this->filesystem->get('files/new.txt', $file);
  83. $file->put('new contents');
  84. $this->assertEquals('new contents', $file->read());
  85. $file->put('updated content');
  86. $this->assertEquals('updated content', $file->read());
  87. }
  88. public function testPutStream()
  89. {
  90. $file = new File();
  91. $this->filesystem->get('files/new.txt', $file);
  92. $resource = tmpfile();
  93. fwrite($resource, 'stream contents');
  94. $file->putStream($resource);
  95. fclose($resource);
  96. $this->assertEquals('stream contents', $file->read());
  97. $resource = tmpfile();
  98. fwrite($resource, 'updated stream contents');
  99. $file->putStream($resource);
  100. fclose($resource);
  101. $this->assertEquals('updated stream contents', $file->read());
  102. }
  103. public function testRename()
  104. {
  105. $file = $this->getFile();
  106. $result = $file->rename('files/renamed.txt');
  107. $this->assertTrue($result);
  108. $this->assertFalse($this->filesystem->has('file.txt'));
  109. $this->assertTrue($this->filesystem->has('files/renamed.txt'));
  110. $this->assertEquals('files/renamed.txt', $file->getPath());
  111. }
  112. public function testRenameFails()
  113. {
  114. $adapter = $this->prophesize('League\Flysystem\AdapterInterface');
  115. $adapter->has('file.txt')->willReturn(true)->shouldBeCalled();
  116. $adapter->has('files/renamed.txt')->willReturn(false)->shouldBeCalled();
  117. $adapter->rename('file.txt', 'files/renamed.txt')->willReturn(false)->shouldBeCalled();
  118. $filesystem = new Filesystem($adapter->reveal());
  119. /** @var File $file */
  120. $file = $filesystem->get('file.txt', new File());
  121. $result = $file->rename('files/renamed.txt');
  122. $this->assertFalse($result);
  123. $this->assertEquals('file.txt', $file->getPath());
  124. }
  125. public function testCopy()
  126. {
  127. $file = $this->getFile();
  128. $copied = $file->copy('files/copied.txt');
  129. $this->assertTrue($this->filesystem->has('file.txt'));
  130. $this->assertTrue($this->filesystem->has('files/copied.txt'));
  131. $this->assertEquals('file.txt', $file->getPath());
  132. $this->assertEquals('files/copied.txt', $copied->getPath());
  133. }
  134. public function testCopyFails()
  135. {
  136. $adapter = $this->prophesize('League\Flysystem\AdapterInterface');
  137. $adapter->has('file.txt')->willReturn(true)->shouldBeCalled();
  138. $adapter->has('files/copied.txt')->willReturn(false)->shouldBeCalled();
  139. $adapter->copy('file.txt', 'files/copied.txt')->willReturn(false)->shouldBeCalled();
  140. $filesystem = new Filesystem($adapter->reveal());
  141. /** @var File $file */
  142. $file = $filesystem->get('file.txt', new File());
  143. $result = $file->copy('files/copied.txt');
  144. $this->assertFalse($result);
  145. }
  146. public function testTimestamp()
  147. {
  148. $file = $this->getFile();
  149. $timestamp = $this->filesystem->getTimestamp($file->getPath());
  150. $this->assertEquals($timestamp, $file->getTimestamp());
  151. }
  152. public function testMimetype()
  153. {
  154. $file = $this->getFile();
  155. $mimetype = $this->filesystem->getMimetype($file->getPath());
  156. $this->assertEquals($mimetype, $file->getMimetype());
  157. }
  158. public function testVisibility()
  159. {
  160. $file = $this->getFile();
  161. $visibility = $this->filesystem->getVisibility($file->getPath());
  162. $this->assertEquals($visibility, $file->getVisibility());
  163. }
  164. public function testMetadata()
  165. {
  166. $file = $this->getFile();
  167. $metadata = $this->filesystem->getMetadata($file->getPath());
  168. $this->assertEquals($metadata, $file->getMetadata());
  169. }
  170. public function testSize()
  171. {
  172. $file = $this->getFile();
  173. $size = $this->filesystem->getSize($file->getPath());
  174. $this->assertEquals($size, $file->getSize());
  175. }
  176. public function testDelete()
  177. {
  178. $file = $this->getFile();
  179. $file->delete();
  180. $this->assertFalse($this->filesystem->has('file.txt'));
  181. }
  182. }