testGetRelativePathReturnsGivenPathWithAppendedFileNameAndSlashForDirectories()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
rs 9.4286
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace DmFilemanTest\Helper\FileInfo;
4
5
use DmFileman\Helper\FileInfo\Path;
6
use org\bovigo\vfs;
7
8
class PathTest extends \PHPUnit_Framework_TestCase
9
{
10
    /** @var Path */
11
    protected $sut;
12
13
    /**
14
     * @var vfs\vfsStreamDirectory
15
     */
16
    private $uploadDir;
17
18
    protected function setUp()
19
    {
20
        $this->sut = new Path();
21
22
        $structure = [
23
            'orig'  => [
24
                'img.jpg' => 'abcd'
25
            ],
26
            'thumb' => [
27
                'img.jpg' => 'bcde'
28
            ]
29
        ];
30
31
        $this->uploadDir = vfs\vfsStream::setup('upload', 0777, $structure);
32
    }
33
34
    /**
35
     * @covers DmFileman\Helper\FileInfo\Path
36
     */
37
    public function testGetRelativePathReturnsGivenPathIfSplInfoIsMissing()
38
    {
39
        $relativePath = 'foo';
40
41
        $actualResult = $this->sut->getRelativePath(true, false, null, $relativePath);
42
43
        $this->assertEquals($relativePath, $actualResult);
44
    }
45
46
    /**
47
     * @covers   DmFileman\Helper\FileInfo\Path
48
     * @requires PHP 5.6
49
     */
50
    public function testGetRelativePathReturnsGivenPathIfWithFilenameIsFalse()
51
    {
52
        $relativePath = 'foo';
53
54
        $splFileInfoMock = $this->getMock('\SplFileInfo', [], [], '', false);
55
56
        $actualResult = $this->sut->getRelativePath(false, false, $splFileInfoMock, $relativePath);
57
58
        $this->assertEquals($relativePath, $actualResult);
59
    }
60
61
    /**
62
     * @covers   DmFileman\Helper\FileInfo\Path
63
     * @requires PHP 5.6
64
     */
65
    public function testGetRelativePathReturnsGivenPathIfWithFilenameIsDisabled()
66
    {
67
        $relativePath = 'foo';
68
69
        $splFileInfoMock = $this->getMock('\SplFileInfo', [], [], '', false);
70
71
        $actualResult = $this->sut->getRelativePath(true, true, $splFileInfoMock, $relativePath);
72
73
        $this->assertEquals($relativePath, $actualResult);
74
    }
75
76
    /**
77
     * @covers   DmFileman\Helper\FileInfo\Path
78
     * @requires PHP 5.6
79
     */
80
    public function testGetRelativePathReturnsGivenPathWithAppendedFileName()
81
    {
82
        $relativePath = 'foo/';
83
        $filename     = 'bar.jpg';
84
85
        $splFileInfoMock = $this->getMock('\SplFileInfo', ['getFilename', 'isDir'], [], '', false);
86
87
        $splFileInfoMock->expects($this->any())->method('getFilename')->will($this->returnValue($filename));
88
        $splFileInfoMock->expects($this->any())->method('isDir')->will($this->returnValue(false));
89
90
        $actualResult = $this->sut->getRelativePath(true, false, $splFileInfoMock, $relativePath);
91
92
        $this->assertEquals($relativePath . $filename, $actualResult);
93
    }
94
95
    /**
96
     * @covers   DmFileman\Helper\FileInfo\Path
97
     * @requires PHP 5.6
98
     */
99
    public function testGetRelativePathReturnsGivenPathWithAppendedFileNameAndSlashForDirectories()
100
    {
101
        $relativePath = 'foo/';
102
        $filename     = 'bar';
103
104
        $splFileInfoMock = $this->getMock('\SplFileInfo', ['getFilename', 'isDir'], [], '', false);
105
106
        $splFileInfoMock->expects($this->any())->method('getFilename')->will($this->returnValue($filename));
107
        $splFileInfoMock->expects($this->any())->method('isDir')->will($this->returnValue(true));
108
109
        $actualResult = $this->sut->getRelativePath(true, false, $splFileInfoMock, $relativePath);
110
111
        $this->assertEquals($relativePath . $filename . '/', $actualResult);
112
    }
113
114
    /**
115
     * @covers DmFileman\Helper\FileInfo\Path
116
     */
117
    public function testGetThumbnailPathReturnEmptyOnMissingSplFileInfo()
118
    {
119
        $actualResult = $this->sut->getThumbnailPath();
120
121
        $this->assertEquals('', $actualResult);
122
    }
123
124
    /**
125
     * @covers   DmFileman\Helper\FileInfo\Path
126
     * @requires PHP 5.6
127
     */
128
    public function testGetThumbnailPathTriesToGetImageThumbnailPathForImages()
129
    {
130
        $image = vfs\vfsStream::url('upload/orig/img.jpg');
131
132
        $splFileInfoMock = $this->getMock('\SplFileInfo', ['isFile', 'getExtension'], [], '', false);
133
134
        $splFileInfoMock->expects($this->any())->method('isFile')->will($this->returnValue(true));
135
        $splFileInfoMock->expects($this->any())->method('isDir')->will($this->returnValue(false));
136
        $splFileInfoMock->expects($this->any())->method('getExtension')->will($this->returnValue('jpg'));
137
138
        $pathname      = $image;
139
        $displayName   = basename($image);
140
        $thumbBasePath = '/upload/thumb';
141
        $relativePath  = '/';
142
143
        $actualResult = $this->sut
144
            ->getThumbnailPath($splFileInfoMock, $pathname, $displayName, $thumbBasePath, $relativePath);
145
146
        $this->assertEquals('/upload/thumb/img.jpg', $actualResult);
147
    }
148
149
    /**
150
     * @covers   DmFileman\Helper\FileInfo\Path
151
     * @requires PHP 5.6
152
     */
153
    public function testGetThumbnailPathReturnsDirectoryThumbnailForDirectories()
154
    {
155
        $splFileInfoMock = $this->getMock('\SplFileInfo', ['isFile', 'isDir'], [], '', false);
156
157
        $splFileInfoMock->expects($this->any())->method('isFile')->will($this->returnValue(false));
158
        $splFileInfoMock->expects($this->any())->method('isDir')->will($this->returnValue(true));
159
160
        $actualResult = $this->sut->getThumbnailPath($splFileInfoMock);
161
162
        $this->assertEquals('/img/filemanager/folder.png', $actualResult);
163
    }
164
165
    /**
166
     * @covers   DmFileman\Helper\FileInfo\Path
167
     * @requires PHP 5.6
168
     */
169
    public function testGetThumbnailPathReturnsDirectoryThumbnailForDirectoriesWhenThumbnailCreationFails()
170
    {
171
        $splFileInfoMock = $this->getMock('\SplFileInfo', ['isFile', 'isDir', 'getExtension'], [], '', false);
172
173
        $splFileInfoMock->expects($this->any())->method('isFile')->will($this->returnValue(true));
174
        $splFileInfoMock->expects($this->any())->method('isDir')->will($this->returnValue(false));
175
        $splFileInfoMock->expects($this->any())->method('getExtension')->will($this->returnValue('jpg'));
176
177
        $pathname      = '';
178
        $displayName   = '';
179
        $thumbBasePath = '/upload/thumb';
180
        $relativePath  = '/';
181
182
        $actualResult = $this->sut
183
            ->getThumbnailPath($splFileInfoMock, $pathname, $displayName, $thumbBasePath, $relativePath);
184
185
        $this->assertEquals('/img/filemanager/image.png', $actualResult);
186
    }
187
188
    /**
189
     * @covers   DmFileman\Helper\FileInfo\Path
190
     * @requires PHP 5.6
191
     */
192
    public function testGetThumbnailPathGetsTypeThumbnailPathIfNoSpecialImagePathWasFound()
193
    {
194
        $image = vfs\vfsStream::url('upload/orig/img.jpg');
195
196
        $splFileInfoMock = $this->getMock('\SplFileInfo', ['isFile', 'getExtension', 'isDir'], [], '', false);
197
198
        $splFileInfoMock->expects($this->any())->method('isFile')->will($this->returnValue(true));
199
        $splFileInfoMock->expects($this->any())->method('isDir')->will($this->returnValue(false));
200
        $splFileInfoMock->expects($this->any())->method('getExtension')->will($this->returnValue('xyz'));
201
202
        $pathname      = $image;
203
        $displayName   = basename($image);
204
        $thumbBasePath = '/upload/thumb';
205
        $relativePath  = '/';
206
207
        $actualResult = $this->sut
208
            ->getThumbnailPath($splFileInfoMock, $pathname, $displayName, $thumbBasePath, $relativePath);
209
210
        $this->assertEquals('/img/filemanager/fileicon_bg.png', $actualResult);
211
    }
212
213
    /**
214
     * @covers DmFileman\Helper\FileInfo\Path
215
     */
216
    public function testGetExtensionReturnsEmptyOnMissingSplFileInfo()
217
    {
218
        $actualResult = $this->sut->getExtension(null, 'prefix');
219
220
        $this->assertEquals('', $actualResult);
221
    }
222
223
    /**
224
     * @covers   DmFileman\Helper\FileInfo\Path
225
     * @requires PHP 5.6
226
     */
227
    public function testGetExtensionReturnsExtensionAndPrependsPrepend()
228
    {
229
        $splFileInfoMock = $this->getMock('\SplFileInfo', ['getExtension'], [], '', false);
230
        $splFileInfoMock->expects($this->any())->method('getExtension')->will($this->returnValue('foo'));
231
232
        $actualResult = $this->sut->getExtension($splFileInfoMock, '.');
233
234
        $this->assertEquals('.foo', $actualResult);
235
    }
236
237
    /**
238
     * @covers DmFileman\Helper\FileInfo\Path
239
     */
240
    public function testGetImageThumbnailPathReturnsEmptyStringOnMissingSplFileInfo()
241
    {
242
        $actualResult = $this->sut->getImageThumbnailPath();
243
244
        $this->assertEquals('', $actualResult);
245
    }
246
247
    /**
248
     * @covers   DmFileman\Helper\FileInfo\Path
249
     * @requires PHP 5.6
250
     */
251
    public function testGetImageThumbnailPathReturnsEmptyStringIfThumbnailIsNotFound()
252
    {
253
        $image = vfs\vfsStream::url('upload/orig/img2.jpg');
254
255
        $splFileInfoMock = $this->getMock('\SplFileInfo', [], [], '', false);
256
257
        $pathname      = $image;
258
        $displayName   = basename($image);
259
        $thumbBasePath = '/upload/thumb';
260
        $relativePath  = '/';
261
262
        $actualResult = $this->sut
263
            ->getImageThumbnailPath($splFileInfoMock, $pathname, $displayName, $thumbBasePath, $relativePath);
264
265
        $this->assertEquals('', $actualResult);
266
    }
267
268
    /**
269
     * @covers   DmFileman\Helper\FileInfo\Path
270
     * @requires PHP 5.6
271
     */
272
    public function testGetImageThumbnailPath()
273
    {
274
        $image = vfs\vfsStream::url('upload/orig/img.jpg');
275
276
        $splFileInfoMock = $this->getMock('\SplFileInfo', [], [], '', false);
277
278
        $pathname      = $image;
279
        $displayName   = basename($image);
280
        $thumbBasePath = '/upload/thumb';
281
        $relativePath  = '/';
282
283
        $actualResult = $this->sut
284
            ->getImageThumbnailPath($splFileInfoMock, $pathname, $displayName, $thumbBasePath, $relativePath);
285
286
        $this->assertEquals('/upload/thumb/img.jpg', $actualResult);
287
    }
288
289
    /**
290
     * @covers DmFileman\Helper\FileInfo\Path
291
     */
292
    public function testGetTypeThumbnailPathReturnsFileIconOnMissingSplFileInfo()
293
    {
294
        $actualResult = $this->sut->getTypeThumbnailPath();
295
296
        $this->assertEquals('/img/filemanager/fileicon_bg.png', $actualResult);
297
    }
298
299
    /**
300
     * @covers   DmFileman\Helper\FileInfo\Path
301
     * @requires PHP 5.6
302
     */
303
    public function testGetTypeThumbnailPathReturnsDirectoryIconForDirectories()
304
    {
305
        $splFileInfoMock = $this->getMock('\SplFileInfo', ['isDir'], [], '', false);
306
307
        $splFileInfoMock->expects($this->any())->method('isDir')->will($this->returnValue(true));
308
309
        $actualResult = $this->sut->getTypeThumbnailPath($splFileInfoMock);
310
311
        $this->assertEquals('/img/filemanager/folder.png', $actualResult);
312
    }
313
314
    /**
315
     * @covers   DmFileman\Helper\FileInfo\Path
316
     * @requires PHP 5.6
317
     */
318
    public function testGetTypeThumbnailPathReturnsFileIconOnUnknownExtension()
319
    {
320
        $splFileInfoMock = $this->getMock('\SplFileInfo', ['getExtension', 'isDir'], [], '', false);
321
322
        $splFileInfoMock->expects($this->any())->method('isDir')->will($this->returnValue(false));
323
        $splFileInfoMock->expects($this->any())->method('getExtension')->will($this->returnValue('xyz'));
324
325
        $actualResult = $this->sut->getTypeThumbnailPath($splFileInfoMock);
326
327
        $this->assertEquals('/img/filemanager/fileicon_bg.png', $actualResult);
328
    }
329
330
    /**
331
     * @covers   DmFileman\Helper\FileInfo\Path
332
     * @requires PHP 5.6
333
     */
334
    public function testGetTypeThumbnailPathReturnsExtensionIconWhenFound()
335
    {
336
        $splFileInfoMock = $this->getMock('\SplFileInfo', ['getExtension', 'isDir'], [], '', false);
337
338
        $splFileInfoMock->expects($this->any())->method('isDir')->will($this->returnValue(false));
339
        $splFileInfoMock->expects($this->any())->method('getExtension')->will($this->returnValue('jpg'));
340
341
        $actualResult = $this->sut->getTypeThumbnailPath($splFileInfoMock);
342
343
        $this->assertEquals('/img/filemanager/image.png', $actualResult);
344
    }
345
}
346