FlysystemStreamTests.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. use League\Flysystem\Adapter\Local;
  3. use League\Flysystem\AdapterInterface;
  4. use League\Flysystem\Filesystem;
  5. use League\Flysystem\FilesystemInterface;
  6. use PHPUnit\Framework\TestCase;
  7. use Prophecy\Argument;
  8. class FlysystemStreamTests extends TestCase
  9. {
  10. public function testWriteStream()
  11. {
  12. $stream = tmpfile();
  13. $adapter = $this->prophesize('League\Flysystem\AdapterInterface');
  14. $adapter->has('file.txt')->willReturn(false)->shouldBeCalled();
  15. $adapter->writeStream('file.txt', $stream, Argument::type('League\Flysystem\Config'))
  16. ->willReturn(['path' => 'file.txt'], false)
  17. ->shouldBeCalled();
  18. $filesystem = new Filesystem($adapter->reveal());
  19. $this->assertTrue($filesystem->writeStream('file.txt', $stream));
  20. $this->assertFalse($filesystem->writeStream('file.txt', $stream));
  21. }
  22. public function testWriteStreamFail()
  23. {
  24. $this->expectException(InvalidArgumentException::class);
  25. $filesystem = new Filesystem(new Local(__DIR__));
  26. $filesystem->writeStream('file.txt', 'not a resource');
  27. }
  28. public function testUpdateStream()
  29. {
  30. $stream = tmpfile();
  31. $adapter = $this->prophesize('League\Flysystem\AdapterInterface');
  32. $adapter->has('file.txt')->willReturn(true)->shouldBeCalled();
  33. $adapter->updateStream('file.txt', $stream, Argument::type('League\Flysystem\Config'))
  34. ->willReturn(['path' => 'file.txt'], false)
  35. ->shouldBeCalled();
  36. $filesystem = new Filesystem($adapter->reveal());
  37. $this->assertTrue($filesystem->updateStream('file.txt', $stream));
  38. $this->assertFalse($filesystem->updateStream('file.txt', $stream));
  39. }
  40. public function testUpdateStreamFail()
  41. {
  42. $this->expectException(InvalidArgumentException::class);
  43. $filesystem = new Filesystem(new Local(__DIR__));
  44. $filesystem->updateStream('file.txt', 'not a resource');
  45. }
  46. public function testReadStream()
  47. {
  48. $adapter = $this->prophesize('League\Flysystem\AdapterInterface');
  49. $adapter->has(Argument::type('string'))->willReturn(true)->shouldBeCalled();
  50. $stream = tmpfile();
  51. $adapter->readStream('file.txt')->willReturn(['stream' => $stream])->shouldBeCalled();
  52. $adapter->readStream('other.txt')->willReturn(false)->shouldBeCalled();
  53. $filesystem = new Filesystem($adapter->reveal());
  54. $this->assertIsResource($filesystem->readStream('file.txt'));
  55. $this->assertFalse($filesystem->readStream('other.txt'));
  56. fclose($stream);
  57. $this->assertFalse($filesystem->readStream('other.txt'));
  58. }
  59. }