1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DmFilemanTest\Service\FileManager; |
4
|
|
|
|
5
|
|
|
use DmFileman\Service\FileManager\FileInfo; |
6
|
|
|
use org\bovigo\vfs; |
7
|
|
|
|
8
|
|
|
class FileInfoTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
/** @var FileInfo */ |
11
|
|
|
private $sut; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
private $relativePath = ''; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
private $origBasePath = ''; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
private $thumbBasePath = ''; |
21
|
|
|
|
22
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */ |
23
|
|
|
private $formatterMock; |
24
|
|
|
|
25
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */ |
26
|
|
|
private $pathMock; |
27
|
|
|
|
28
|
|
|
/** @var \org\bovigo\vfs\vfsStreamDirectory */ |
29
|
|
|
private $uploadDir; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
private $baseDir; |
33
|
|
|
|
34
|
|
|
public function setUp() |
35
|
|
|
{ |
36
|
|
|
$this->uploadDir = vfs\vfsStream::setup('upload', 0777, []); |
37
|
|
|
$this->baseDir = vfs\vfsStream::url('upload'); |
38
|
|
|
|
39
|
|
|
$this->formatterMock = $this->getMockBuilder('DmFileman\Helper\FileInfo\Formatter') |
40
|
|
|
->disableOriginalConstructor() |
41
|
|
|
->setMethods(['formatSize', 'formatPermissions', 'formatOwner', 'formatGroup']) |
42
|
|
|
->getMock(); |
43
|
|
|
|
44
|
|
|
$pathMethods = [ |
45
|
|
|
'getExtension', |
46
|
|
|
'getRelativePath', |
47
|
|
|
'getThumbnailPath', |
48
|
|
|
'getImageThumbnailPath', |
49
|
|
|
'getTypeThumbnailPath' |
50
|
|
|
]; |
51
|
|
|
$this->pathMock = $this->getMockBuilder('DmFileman\Helper\FileInfo\Path') |
52
|
|
|
->disableOriginalConstructor() |
53
|
|
|
->setMethods($pathMethods) |
54
|
|
|
->getMock(); |
55
|
|
|
|
56
|
|
|
$this->sut = new FileInfo( |
57
|
|
|
$this->relativePath, |
58
|
|
|
$this->origBasePath, |
59
|
|
|
$this->thumbBasePath, |
60
|
|
|
$this->formatterMock, |
61
|
|
|
$this->pathMock |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function getSplFileInfoMock() |
66
|
|
|
{ |
67
|
|
|
return new \SplFileInfo($this->baseDir); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
72
|
|
|
*/ |
73
|
|
|
public function testGetSizeCallsFormatterHelper() |
74
|
|
|
{ |
75
|
|
|
$expectedResult = 654; |
76
|
|
|
|
77
|
|
|
$this->formatterMock |
78
|
|
|
->expects($this->once()) |
79
|
|
|
->method('formatSize') |
80
|
|
|
->will($this->returnValue($expectedResult)); |
81
|
|
|
|
82
|
|
|
$actualResult = $this->sut->getSize(); |
83
|
|
|
|
84
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
89
|
|
|
*/ |
90
|
|
|
public function testGetPermissionCallsFormatterHelper() |
91
|
|
|
{ |
92
|
|
|
$expectedResult = 654; |
93
|
|
|
|
94
|
|
|
$this->formatterMock |
95
|
|
|
->expects($this->once()) |
96
|
|
|
->method('formatPermissions') |
97
|
|
|
->will($this->returnValue($expectedResult)); |
98
|
|
|
|
99
|
|
|
$actualResult = $this->sut->getPermissions(); |
100
|
|
|
|
101
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
106
|
|
|
*/ |
107
|
|
|
public function testGetOwnerCallsFormatterHelper() |
108
|
|
|
{ |
109
|
|
|
$expectedResult = 654; |
110
|
|
|
|
111
|
|
|
$this->formatterMock |
112
|
|
|
->expects($this->once()) |
113
|
|
|
->method('formatOwner') |
114
|
|
|
->will($this->returnValue($expectedResult)); |
115
|
|
|
|
116
|
|
|
$actualResult = $this->sut->getOwner(); |
117
|
|
|
|
118
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
123
|
|
|
*/ |
124
|
|
|
public function testGetGroupCallsFormatterHelper() |
125
|
|
|
{ |
126
|
|
|
$expectedResult = 654; |
127
|
|
|
|
128
|
|
|
$this->formatterMock |
129
|
|
|
->expects($this->once()) |
130
|
|
|
->method('formatGroup') |
131
|
|
|
->will($this->returnValue($expectedResult)); |
132
|
|
|
|
133
|
|
|
$actualResult = $this->sut->getGroup(); |
134
|
|
|
|
135
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
140
|
|
|
*/ |
141
|
|
|
public function testGetAccessedAtReturnsEmptyStringWhenSplFileInfoIsNotSet() |
142
|
|
|
{ |
143
|
|
|
$expectedResult = ''; |
144
|
|
|
|
145
|
|
|
$actualResult = $this->sut->getAccessedAt(); |
146
|
|
|
|
147
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
152
|
|
|
*/ |
153
|
|
|
public function testGetAccessedAtCallsSplFileInfo() |
154
|
|
|
{ |
155
|
|
|
$splFileInfo = $this->getSplFileInfoMock(); |
156
|
|
|
|
157
|
|
|
$this->sut->setSplFileInfo($splFileInfo); |
158
|
|
|
|
159
|
|
|
$actualResult = $this->sut->getAccessedAt(); |
160
|
|
|
|
161
|
|
|
$this->assertLessThanOrEqual(time(), $actualResult); |
162
|
|
|
$this->assertGreaterThanOrEqual(time() - 10, $actualResult); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
167
|
|
|
*/ |
168
|
|
|
public function testGetCreatedAtReturnsEmptyStringWhenSplFileInfoIsNotSet() |
169
|
|
|
{ |
170
|
|
|
$expectedResult = ''; |
171
|
|
|
|
172
|
|
|
$actualResult = $this->sut->getCreatedAt(); |
173
|
|
|
|
174
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
179
|
|
|
*/ |
180
|
|
|
public function testGetCreatedAtCallsSplFileInfo() |
181
|
|
|
{ |
182
|
|
|
$splFileInfo = $this->getSplFileInfoMock(); |
183
|
|
|
|
184
|
|
|
$this->sut->setSplFileInfo($splFileInfo); |
185
|
|
|
|
186
|
|
|
$actualResult = $this->sut->getCreatedAt(); |
187
|
|
|
|
188
|
|
|
$this->assertLessThanOrEqual(time(), $actualResult); |
189
|
|
|
$this->assertGreaterThanOrEqual(time() - 10, $actualResult); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
194
|
|
|
*/ |
195
|
|
|
public function testGetModifiedAtReturnsEmptyStringWhenSplFileInfoIsNotSet() |
196
|
|
|
{ |
197
|
|
|
$actualResult = $this->sut->getModifiedAt(); |
198
|
|
|
|
199
|
|
|
$this->assertSame('', $actualResult); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
204
|
|
|
*/ |
205
|
|
|
public function testGetModifiedAtCallsSplFileInfo() |
206
|
|
|
{ |
207
|
|
|
$splFileInfo = $this->getSplFileInfoMock(); |
208
|
|
|
|
209
|
|
|
$this->sut->setSplFileInfo($splFileInfo); |
210
|
|
|
|
211
|
|
|
$actualResult = $this->sut->getModifiedAt(); |
212
|
|
|
|
213
|
|
|
$this->assertLessThanOrEqual(time(), $actualResult); |
214
|
|
|
$this->assertGreaterThanOrEqual(time() - 10, $actualResult); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
219
|
|
|
*/ |
220
|
|
|
public function testGetExtensionCallsPathHelper() |
221
|
|
|
{ |
222
|
|
|
$expectedResult = ''; |
223
|
|
|
|
224
|
|
|
$this->pathMock->expects($this->once())->method('getExtension')->will($this->returnValue($expectedResult)); |
225
|
|
|
|
226
|
|
|
$actualResult = $this->sut->getExtension(); |
227
|
|
|
|
228
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
233
|
|
|
*/ |
234
|
|
|
public function testGetDisplayNameReturnsEmptyStringByDefault() |
235
|
|
|
{ |
236
|
|
|
$expectedResult = ''; |
237
|
|
|
|
238
|
|
|
$actualResult = $this->sut->getDisplayName(); |
239
|
|
|
|
240
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
245
|
|
|
*/ |
246
|
|
|
public function testGetDisplayNameReturnsDisplayNameIfSet() |
247
|
|
|
{ |
248
|
|
|
$expectedResult = 'foo'; |
249
|
|
|
|
250
|
|
|
$this->sut->setDisplayName($expectedResult); |
251
|
|
|
|
252
|
|
|
$actualResult = $this->sut->getDisplayName(); |
253
|
|
|
|
254
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
259
|
|
|
*/ |
260
|
|
|
public function testGetDisplayNameCallsSplFileInfoIfDisplayNameIsNotSet() |
261
|
|
|
{ |
262
|
|
|
$expectedResult = 'upload'; |
263
|
|
|
|
264
|
|
|
$splFileInfoMock = $this->getSplFileInfoMock(); |
265
|
|
|
|
266
|
|
|
$this->sut->setSplFileInfo($splFileInfoMock); |
267
|
|
|
|
268
|
|
|
$actualResult = $this->sut->getDisplayName(); |
269
|
|
|
|
270
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
275
|
|
|
*/ |
276
|
|
|
public function testGetRelativePathCallsPathHelper() |
277
|
|
|
{ |
278
|
|
|
$expectedResult = 'foo'; |
279
|
|
|
|
280
|
|
|
$this->pathMock->expects($this->once())->method('getRelativePath')->will($this->returnValue($expectedResult)); |
281
|
|
|
|
282
|
|
|
$actualResult = $this->sut->getRelativePath(); |
283
|
|
|
|
284
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
289
|
|
|
*/ |
290
|
|
|
public function testGetOrigPathReturnsEmptyStringByDefault() |
291
|
|
|
{ |
292
|
|
|
$expectedResult = ''; |
293
|
|
|
|
294
|
|
|
$actualResult = $this->sut->getOrigPath(); |
295
|
|
|
|
296
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
301
|
|
|
*/ |
302
|
|
|
public function testGetOrigPathCallsPathHelperIfSplFileInfoIsSet() |
303
|
|
|
{ |
304
|
|
|
$expectedResult = 'foo'; |
305
|
|
|
|
306
|
|
|
$splFileInfoMock = $this->getSplFileInfoMock(); |
307
|
|
|
$this->sut->setSplFileInfo($splFileInfoMock); |
308
|
|
|
|
309
|
|
|
$this->pathMock->expects($this->once())->method('getRelativePath')->will($this->returnValue($expectedResult)); |
310
|
|
|
|
311
|
|
|
$actualResult = $this->sut->getOrigPath(); |
312
|
|
|
|
313
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
318
|
|
|
*/ |
319
|
|
|
public function testGetThumbnailPathCallsPathHelper() |
320
|
|
|
{ |
321
|
|
|
$expectedResult = 'foo'; |
322
|
|
|
|
323
|
|
|
$splFileInfoMock = $this->getSplFileInfoMock(); |
324
|
|
|
$this->sut->setSplFileInfo($splFileInfoMock); |
325
|
|
|
|
326
|
|
|
$this->pathMock |
327
|
|
|
->expects($this->once()) |
328
|
|
|
->method('getThumbnailPath') |
329
|
|
|
->will($this->returnValue($expectedResult)); |
330
|
|
|
|
331
|
|
|
$actualResult = $this->sut->getThumbnailPath(); |
332
|
|
|
|
333
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
338
|
|
|
*/ |
339
|
|
|
public function testGetImageThumbnailPathCallsPathHelper() |
340
|
|
|
{ |
341
|
|
|
$expectedResult = 'foo'; |
342
|
|
|
|
343
|
|
|
$splFileInfoMock = $this->getSplFileInfoMock(); |
344
|
|
|
$this->sut->setSplFileInfo($splFileInfoMock); |
345
|
|
|
|
346
|
|
|
$this->pathMock |
347
|
|
|
->expects($this->once()) |
348
|
|
|
->method('getImageThumbnailPath') |
349
|
|
|
->will($this->returnValue($expectedResult)); |
350
|
|
|
|
351
|
|
|
$actualResult = $this->sut->getImageThumbnailPath(); |
352
|
|
|
|
353
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @expectedException \BadMethodCallException |
358
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
359
|
|
|
*/ |
360
|
|
|
public function testGetThumbnailPathThrowsExceptionIfSplFileInfoIsNotSet() |
361
|
|
|
{ |
362
|
|
|
$this->sut->getThumbnailPath(); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @expectedException \BadMethodCallException |
367
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
368
|
|
|
*/ |
369
|
|
|
public function testGetImageThumbnailPathThrowsExceptionIfSplFileInfoIsNotSet() |
370
|
|
|
{ |
371
|
|
|
$this->sut->getImageThumbnailPath(); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @covers DmFileman\Service\FileManager\FileInfo |
376
|
|
|
*/ |
377
|
|
|
public function testGetTypeThumbnailPathCallsPathHelper() |
378
|
|
|
{ |
379
|
|
|
$expectedResult = 'foo'; |
380
|
|
|
|
381
|
|
|
$this->pathMock |
382
|
|
|
->expects($this->once()) |
383
|
|
|
->method('getTypeThumbnailPath') |
384
|
|
|
->will($this->returnValue($expectedResult)); |
385
|
|
|
|
386
|
|
|
$actualResult = $this->sut->getTypeThumbnailPath(); |
387
|
|
|
|
388
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
|