FileManagerTest::testDeleteTree()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 7
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace DmFilemanTest\Service\FileManager;
4
5
use DmFileman\Service\FileManager\FileManager;
6
use org\bovigo\vfs;
7
8
class FileManagerTest extends \PHPUnit_Framework_TestCase
9
{
10
    /** @var FileManager */
11
    private $sut;
12
13
    /** @var \PHPUnit_Framework_MockObject_MockObject */
14
    private $factory;
15
16
    /** @var \org\bovigo\vfs\vfsStreamDirectory */
17
    private $uploadDir;
18
19
    /** @var string */
20
    private $baseDir;
21
22
    /** @var string */
23
    private $basePath;
24
25
    public function setUp()
26
    {
27
        $structure = [
28
            'orig'  => [
29
                'img.jpg' => 'abcd',
30
                'old'     => [
31
                    'img.jpg' => 'cdab'
32
                ],
33
            ],
34
            'thumb' => [
35
                'img.jpg' => 'bcde',
36
                'old'     => [
37
                    'img.jpg' => 'cdab'
38
                ],
39
            ]
40
        ];
41
42
        $this->uploadDir = vfs\vfsStream::setup('upload', 0777, $structure);
43
        $this->baseDir   = vfs\vfsStream::url('upload');
44
        $this->basePath  = '/upload';
45
46
        $this->factory = $this->getMockBuilder('DmFileman\Service\FileManager\Factory')
47
            ->setMethods(['getFilesystemIterator', 'getFileInfo', 'getSplFileInfo'])
48
            ->getMock();
49
50
        $config = [
51
            FileManager::CONFIG_BASE_DIR  => $this->baseDir,
52
            FileManager::CONFIG_BASE_PATH => $this->basePath,
53
        ];
54
55
        $this->sut = new FileManager($this->factory, $config);
56
    }
57
58
    /**
59
     * @return \PHPUnit_Framework_MockObject_MockObject
60
     */
61
    private function getFileInfo()
62
    {
63
        $fileInfoMethods = [
64
            'setFromSplFileInfo',
65
            'getOrigBasePath',
66
            'getThumbBasePath',
67
            'disableWithFilename',
68
            'getSize',
69
            'getPermissions',
70
            'getOwner',
71
            'getGroup',
72
            'getAccessedAt',
73
            'getCreatedAt',
74
            'getModifiedAt',
75
            'getExtension',
76
            'getDisplayName',
77
            'setDisplayName',
78
            'getRelativePath',
79
            'getOrigPath',
80
            'getThumbnailPath',
81
            'getTypeThumbnailPath'
82
        ];
83
84
        $fileInfoMock = $this->getMockBuilder('DmFileman\Service\FileManager\FileInfo')
85
            ->setMethods($fileInfoMethods)
86
            ->disableOriginalConstructor()
87
            ->getMock();
88
89
        return $fileInfoMock;
90
    }
91
92
    /**
93
     * @return \PHPUnit_Framework_MockObject_MockObject
94
     */
95
    private function getSplFileInfo()
96
    {
97
        $splFileInfoMock = new \SplFileInfo($this->baseDir);
98
99
        return $splFileInfoMock;
100
    }
101
102
    /**
103
     * @covers DmFileman\Service\FileManager\FileManager
104
     */
105
    public function testGetListReturnsEmptyByDefault()
106
    {
107
        $this->factory
108
            ->expects($this->any())
109
            ->method('getFileInfo')
110
            ->will($this->returnValue($this->getFileInfo()));
111
112
        $this->factory
113
            ->expects($this->any())
114
            ->method('getSplFileInfo')
115
            ->will($this->returnValue($this->getSplFileInfo()));
116
117
        $this->factory
118
            ->expects($this->any())
119
            ->method('getFilesystemIterator')
120
            ->will($this->returnValue([]));
121
122
        $result = $this->sut->getList();
123
124
        $this->assertSame([], $result);
125
    }
126
127
    /**
128
     * @covers DmFileman\Service\FileManager\FileManager
129
     */
130
    public function testGetListReturnsList()
131
    {
132
        $fileInfoMock = $this->getFileInfo();
133
134
        $this->factory
135
            ->expects($this->any())
136
            ->method('getFileInfo')
137
            ->will($this->returnValue($fileInfoMock));
138
139
        $this->factory
140
            ->expects($this->any())
141
            ->method('getSplFileInfo')
142
            ->will($this->returnValue($this->getSplFileInfo()));
143
144
        $this->factory
145
            ->expects($this->any())
146
            ->method('getFilesystemIterator')
147
            ->will($this->returnValue([]));
148
149
        $result = $this->sut->getList('ok');
150
151
        $this->assertSame([$fileInfoMock], $result);
152
    }
153
154
    /**
155
     * @covers DmFileman\Service\FileManager\FileManager
156
     */
157
    public function testGetListReturnsListWithRealFiles()
158
    {
159
        $fileInfoMock    = $this->getFileInfo();
160
        $splFileInfoMock = $this->getSplFileInfo();
161
162
        $this->factory
163
            ->expects($this->any())
164
            ->method('getFileInfo')
165
            ->will($this->returnValue($fileInfoMock));
166
167
        $this->factory
168
            ->expects($this->any())
169
            ->method('getSplFileInfo')
170
            ->will($this->returnValue($splFileInfoMock));
171
172
        $this->factory
173
            ->expects($this->any())
174
            ->method('getFilesystemIterator')
175
            ->will($this->returnValue([$splFileInfoMock, $splFileInfoMock]));
176
177
        $result = $this->sut->getList('/');
178
179
        $this->assertSame([$fileInfoMock, $fileInfoMock], $result);
180
    }
181
182
    /**
183
     * @covers DmFileman\Service\FileManager\FileManager
184
     */
185
    public function testCreateCreatesNewDirectories()
186
    {
187
        $actualResult = $this->sut->create('/new');
188
189
        $this->assertTrue($actualResult);
190
191
        $this->assertFileExists(vfs\vfsStream::url('upload/orig/new'));
192
        $this->assertFileExists(vfs\vfsStream::url('upload/thumb/new'));
193
    }
194
195
    /**
196
     * @covers DmFileman\Service\FileManager\FileManager
197
     */
198
    public function testCreateReturnsFalseIfDirectoryAlreadyExists()
199
    {
200
        $actualResult = $this->sut->create('/old');
201
202
        $this->assertFalse($actualResult);
203
    }
204
205
    /**
206
     * @covers DmFileman\Service\FileManager\FileManager
207
     */
208
    public function testDeleteDeletesFiles()
209
    {
210
        $this->assertFileExists(vfs\vfsStream::url('upload/orig/old/img.jpg'));
211
212
        $actualResult = $this->sut->delete('/old/img.jpg');
213
214
        $this->assertTrue($actualResult);
215
        $this->assertFileExists(vfs\vfsStream::url('upload/orig/old'));
216
        $this->assertFileNotExists(vfs\vfsStream::url('upload/orig/old/img.jpg'));
217
    }
218
219
    /**
220
     * @covers DmFileman\Service\FileManager\FileManager
221
     */
222
    public function testDeleteDeletesFolders()
223
    {
224
        $this->assertFileExists(vfs\vfsStream::url('upload/orig/old'));
225
226
        $actualResult = $this->sut->delete('/old');
227
228
        $this->assertTrue($actualResult);
229
        $this->assertFileExists(vfs\vfsStream::url('upload/orig'));
230
        $this->assertFileNotExists(vfs\vfsStream::url('upload/orig/old'));
231
    }
232
233
    /**
234
     * @covers DmFileman\Service\FileManager\FileManager
235
     */
236
    public function testDeleteTree()
237
    {
238
        $actualResult = $this->sut->deleteTree(vfs\vfsStream::url('upload/orig'));
239
240
        $this->assertTrue($actualResult);
241
        $this->assertFileNotExists(vfs\vfsStream::url('upload/orig'));
242
    }
243
}
244