HandlerTests.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. use League\Flysystem\Directory;
  3. use League\Flysystem\File;
  4. use PHPUnit\Framework\TestCase;
  5. class HandlerTests extends TestCase
  6. {
  7. public function testFileRead()
  8. {
  9. $prophecy = $this->prophesize('League\Flysystem\FilesystemInterface');
  10. $prophecy->read('path.txt')->willReturn('contents');
  11. $filesystem = $prophecy->reveal();
  12. $file = new File(null, 'path.txt');
  13. $file->setFilesystem($filesystem);
  14. $output = $file->read();
  15. $this->assertEquals('contents', $output);
  16. }
  17. public function testFileDelete()
  18. {
  19. $prophecy = $this->prophesize('League\Flysystem\FilesystemInterface');
  20. $prophecy->delete('path.txt')->willReturn(true);
  21. $filesystem = $prophecy->reveal();
  22. $file = new File(null, 'path.txt');
  23. $file->setFilesystem($filesystem);
  24. $output = $file->delete();
  25. $this->assertTrue($output);
  26. }
  27. public function testFileReadStream()
  28. {
  29. $prophecy = $this->prophesize('League\Flysystem\FilesystemInterface');
  30. $prophecy->readStream('path.txt')->willReturn('contents');
  31. $filesystem = $prophecy->reveal();
  32. $file = new File(null, 'path.txt');
  33. $file->setFilesystem($filesystem);
  34. $output = $file->readStream();
  35. $this->assertEquals('contents', $output);
  36. }
  37. public function testFileUpdate()
  38. {
  39. $prophecy = $this->prophesize('League\Flysystem\FilesystemInterface');
  40. $prophecy->update('path.txt', 'contents')->willReturn(true);
  41. $filesystem = $prophecy->reveal();
  42. $file = new File(null, 'path.txt');
  43. $file->setFilesystem($filesystem);
  44. $output = $file->update('contents');
  45. $this->assertTrue($output);
  46. }
  47. public function testFileUpdateStream()
  48. {
  49. $prophecy = $this->prophesize('League\Flysystem\FilesystemInterface');
  50. $prophecy->updateStream('path.txt', 'contents')->willReturn(true);
  51. $filesystem = $prophecy->reveal();
  52. $file = new File(null, 'path.txt');
  53. $file->setFilesystem($filesystem);
  54. $output = $file->updateStream('contents');
  55. $this->assertTrue($output);
  56. }
  57. public function getterProvider()
  58. {
  59. return [
  60. ['getTimestamp', 123],
  61. ['getMimetype', 'text/plain'],
  62. ['getVisibility', 'private'],
  63. ['getMetadata', ['some' => 'metadata']],
  64. ['getSize', 123],
  65. ];
  66. }
  67. /**
  68. * @dataProvider getterProvider
  69. *
  70. * @param $method
  71. * @param $response
  72. */
  73. public function testGetters($method, $response)
  74. {
  75. $prophecy = $this->prophesize('League\Flysystem\FilesystemInterface');
  76. $prophecy->{$method}('path.txt')->willReturn($response);
  77. $filesystem = $prophecy->reveal();
  78. $file = new File(null, 'path.txt');
  79. $file->setFilesystem($filesystem);
  80. $output = $file->{$method}();
  81. $this->assertEquals($response, $output);
  82. }
  83. public function testFileIsFile()
  84. {
  85. $response = ['type' => 'file'];
  86. $prophecy = $this->prophesize('League\Flysystem\FilesystemInterface');
  87. $prophecy->getMetadata('path.txt')->willReturn($response);
  88. $filesystem = $prophecy->reveal();
  89. $file = new File(null, 'path.txt');
  90. $file->setFilesystem($filesystem);
  91. $this->assertTrue($file->isFile());
  92. }
  93. public function testFileIsDir()
  94. {
  95. $response = ['type' => 'file'];
  96. $prophecy = $this->prophesize('League\Flysystem\FilesystemInterface');
  97. $prophecy->getMetadata('path.txt')->willReturn($response);
  98. $filesystem = $prophecy->reveal();
  99. $file = new File();
  100. $file->setPath('path.txt');
  101. $file->setFilesystem($filesystem);
  102. $this->assertFalse($file->isDir());
  103. }
  104. public function testFileGetPath()
  105. {
  106. $file = new File();
  107. $file->setPath('path.txt');
  108. $this->assertEquals('path.txt', $file->getPath());
  109. }
  110. public function testDirDelete()
  111. {
  112. $prophecy = $this->prophesize('League\Flysystem\FilesystemInterface');
  113. $prophecy->deleteDir('path')->willReturn(true);
  114. $filesystem = $prophecy->reveal();
  115. $dir = new Directory(null, 'path');
  116. $dir->setFilesystem($filesystem);
  117. $output = $dir->delete();
  118. $this->assertTrue($output);
  119. }
  120. public function testDirListContents()
  121. {
  122. $prophecy = $this->prophesize('League\Flysystem\FilesystemInterface');
  123. $prophecy->listContents('path', true)->willReturn($listing = ['listing']);
  124. $filesystem = $prophecy->reveal();
  125. $dir = new Directory(null, 'path');
  126. $dir->setFilesystem($filesystem);
  127. $output = $dir->getContents(true);
  128. $this->assertEquals($listing, $output);
  129. }
  130. public function testGetFilesystem()
  131. {
  132. $prophecy = $this->prophesize('League\Flysystem\FilesystemInterface');
  133. $filesystem = $prophecy->reveal();
  134. $dir = new Directory(null, 'path');
  135. $dir->setFilesystem($filesystem);
  136. $this->assertEquals($filesystem, $dir->getFilesystem());
  137. }
  138. }