FilesystemTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace think\tests;
  3. use League\Flysystem\Adapter\NullAdapter;
  4. use League\Flysystem\AdapterInterface;
  5. use Mockery as m;
  6. use Mockery\MockInterface;
  7. use org\bovigo\vfs\vfsStream;
  8. use org\bovigo\vfs\vfsStreamDirectory;
  9. use PHPUnit\Framework\TestCase;
  10. use think\App;
  11. use think\Cache;
  12. use think\cache\driver\File;
  13. use think\Config;
  14. use think\Container;
  15. use think\Filesystem;
  16. use think\filesystem\Driver;
  17. use think\filesystem\driver\Local;
  18. class FilesystemTest extends TestCase
  19. {
  20. /** @var App|MockInterface */
  21. protected $app;
  22. /** @var Filesystem */
  23. protected $filesystem;
  24. /** @var Config|MockInterface */
  25. protected $config;
  26. /** @var vfsStreamDirectory */
  27. protected $root;
  28. protected function setUp()
  29. {
  30. $this->app = m::mock(App::class)->makePartial();
  31. Container::setInstance($this->app);
  32. $this->app->shouldReceive('make')->with(App::class)->andReturn($this->app);
  33. $this->config = m::mock(Config::class);
  34. $this->config->shouldReceive('get')->with('filesystem.default', null)->andReturn('local');
  35. $this->app->shouldReceive('get')->with('config')->andReturn($this->config);
  36. $this->filesystem = new Filesystem($this->app);
  37. $this->root = vfsStream::setup('rootDir');
  38. }
  39. protected function tearDown(): void
  40. {
  41. m::close();
  42. }
  43. public function testDisk()
  44. {
  45. $this->config->shouldReceive('get')->with('filesystem.disks.local', null)->andReturn([
  46. 'type' => 'local',
  47. 'root' => $this->root->url(),
  48. ]);
  49. $this->config->shouldReceive('get')->with('filesystem.disks.foo', null)->andReturn([
  50. 'type' => 'local',
  51. 'root' => $this->root->url(),
  52. ]);
  53. $this->assertInstanceOf(Local::class, $this->filesystem->disk());
  54. $this->assertInstanceOf(Local::class, $this->filesystem->disk('foo'));
  55. }
  56. public function testCache()
  57. {
  58. $this->config->shouldReceive('get')->with('filesystem.disks.local', null)->andReturn([
  59. 'type' => 'local',
  60. 'root' => $this->root->url(),
  61. 'cache' => true,
  62. ]);
  63. $this->assertInstanceOf(Local::class, $this->filesystem->disk());
  64. $this->config->shouldReceive('get')->with('filesystem.disks.cache', null)->andReturn([
  65. 'type' => NullDriver::class,
  66. 'root' => $this->root->url(),
  67. 'cache' => [
  68. 'store' => 'flysystem',
  69. ],
  70. ]);
  71. $cache = m::mock(Cache::class);
  72. $cacheDriver = m::mock(File::class);
  73. $cache->shouldReceive('store')->once()->with('flysystem')->andReturn($cacheDriver);
  74. $this->app->shouldReceive('make')->with(Cache::class)->andReturn($cache);
  75. $cacheDriver->shouldReceive('get')->with('flysystem')->once()->andReturn(null);
  76. $cacheDriver->shouldReceive('set')->withAnyArgs();
  77. $this->filesystem->disk('cache')->put('test.txt', 'aa');
  78. }
  79. public function testPutFile()
  80. {
  81. $root = vfsStream::setup('rootDir', null, [
  82. 'foo.jpg' => 'hello',
  83. ]);
  84. $this->config->shouldReceive('get')->with('filesystem.disks.local', null)->andReturn([
  85. 'type' => NullDriver::class,
  86. 'root' => $root->url(),
  87. 'cache' => true,
  88. ]);
  89. $file = m::mock(\think\File::class);
  90. $file->shouldReceive('hashName')->with(null)->once()->andReturn('foo.jpg');
  91. $file->shouldReceive('getRealPath')->once()->andReturn($root->getChild('foo.jpg')->url());
  92. $this->filesystem->putFile('test', $file);
  93. }
  94. }
  95. class NullDriver extends Driver
  96. {
  97. protected function createAdapter(): AdapterInterface
  98. {
  99. return new NullAdapter();
  100. }
  101. }