Completed
Branch 6.0 (d30585)
by yun
02:10
created

NullDriver   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 5
Duplicated Lines 0 %

Importance

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