1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Liip\ImagineBundle\Tests\Imagine\Data; |
4
|
|
|
|
5
|
|
|
use Liip\ImagineBundle\Imagine\Data\DataManager; |
6
|
|
|
use Liip\ImagineBundle\Binary\Loader\LoaderInterface; |
7
|
|
|
use Liip\ImagineBundle\Model\Binary; |
8
|
|
|
use Liip\ImagineBundle\Tests\AbstractTest; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @covers Liip\ImagineBundle\Imagine\Data\DataManager |
12
|
|
|
*/ |
13
|
|
|
class DataManagerTest extends AbstractTest |
14
|
|
|
{ |
15
|
|
|
public function testUseDefaultLoaderUsedIfNoneSet() |
16
|
|
|
{ |
17
|
|
|
$loader = $this->getMockLoader(); |
18
|
|
|
$loader |
19
|
|
|
->expects($this->once()) |
20
|
|
|
->method('find') |
21
|
|
|
->with('cats.jpeg') |
22
|
|
|
; |
23
|
|
|
|
24
|
|
|
$config = $this->createFilterConfigurationMock(); |
25
|
|
|
$config |
26
|
|
|
->expects($this->once()) |
27
|
|
|
->method('get') |
28
|
|
|
->with('thumbnail') |
29
|
|
|
->will($this->returnValue(array( |
30
|
|
|
'size' => array(180, 180), |
31
|
|
|
'mode' => 'outbound', |
32
|
|
|
'data_loader' => null, |
33
|
|
|
))) |
34
|
|
|
; |
35
|
|
|
|
36
|
|
|
$mimeTypeGuesser = $this->getMockMimeTypeGuesser(); |
37
|
|
|
$mimeTypeGuesser |
38
|
|
|
->expects($this->once()) |
39
|
|
|
->method('guess') |
40
|
|
|
->will($this->returnValue('image/png')) |
41
|
|
|
; |
42
|
|
|
|
43
|
|
|
$dataManager = new DataManager($mimeTypeGuesser, $this->getMockExtensionGuesser(), $config, 'default'); |
44
|
|
|
$dataManager->addLoader('default', $loader); |
45
|
|
|
|
46
|
|
|
$dataManager->find('thumbnail', 'cats.jpeg'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testUseLoaderRegisteredForFilterOnFind() |
50
|
|
|
{ |
51
|
|
|
$loader = $this->getMockLoader(); |
52
|
|
|
$loader |
53
|
|
|
->expects($this->once()) |
54
|
|
|
->method('find') |
55
|
|
|
->with('cats.jpeg') |
56
|
|
|
; |
57
|
|
|
|
58
|
|
|
$config = $this->createFilterConfigurationMock(); |
59
|
|
|
$config |
60
|
|
|
->expects($this->once()) |
61
|
|
|
->method('get') |
62
|
|
|
->with('thumbnail') |
63
|
|
|
->will($this->returnValue(array( |
64
|
|
|
'size' => array(180, 180), |
65
|
|
|
'mode' => 'outbound', |
66
|
|
|
'data_loader' => 'the_loader', |
67
|
|
|
))) |
68
|
|
|
; |
69
|
|
|
|
70
|
|
|
$mimeTypeGuesser = $this->getMockMimeTypeGuesser(); |
71
|
|
|
$mimeTypeGuesser |
72
|
|
|
->expects($this->once()) |
73
|
|
|
->method('guess') |
74
|
|
|
->will($this->returnValue('image/png')) |
75
|
|
|
; |
76
|
|
|
|
77
|
|
|
$dataManager = new DataManager($mimeTypeGuesser, $this->getMockExtensionGuesser(), $config); |
78
|
|
|
$dataManager->addLoader('the_loader', $loader); |
79
|
|
|
|
80
|
|
|
$dataManager->find('thumbnail', 'cats.jpeg'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testThrowsIfMimeTypeWasNotGuessedOnFind() |
84
|
|
|
{ |
85
|
|
|
$loader = $this->getMockLoader(); |
86
|
|
|
$loader |
87
|
|
|
->expects($this->once()) |
88
|
|
|
->method('find') |
89
|
|
|
->with('cats.jpeg') |
90
|
|
|
; |
91
|
|
|
|
92
|
|
|
$config = $this->createFilterConfigurationMock(); |
93
|
|
|
$config |
94
|
|
|
->expects($this->once()) |
95
|
|
|
->method('get') |
96
|
|
|
->with('thumbnail') |
97
|
|
|
->will($this->returnValue(array( |
98
|
|
|
'size' => array(180, 180), |
99
|
|
|
'mode' => 'outbound', |
100
|
|
|
'data_loader' => 'the_loader', |
101
|
|
|
))) |
102
|
|
|
; |
103
|
|
|
|
104
|
|
|
$mimeTypeGuesser = $this->getMockMimeTypeGuesser(); |
105
|
|
|
$mimeTypeGuesser |
106
|
|
|
->expects($this->once()) |
107
|
|
|
->method('guess') |
108
|
|
|
->will($this->returnValue(null)) |
109
|
|
|
; |
110
|
|
|
|
111
|
|
|
$dataManager = new DataManager($mimeTypeGuesser, $this->getMockExtensionGuesser(), $config); |
112
|
|
|
$dataManager->addLoader('the_loader', $loader); |
113
|
|
|
|
114
|
|
|
$this->setExpectedException('LogicException', 'The mime type of image cats.jpeg was not guessed.'); |
115
|
|
|
$dataManager->find('thumbnail', 'cats.jpeg'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testThrowsIfMimeTypeNotImageOneOnFind() |
119
|
|
|
{ |
120
|
|
|
$loader = $this->getMockLoader(); |
121
|
|
|
$loader |
122
|
|
|
->expects($this->once()) |
123
|
|
|
->method('find') |
124
|
|
|
->with('cats.jpeg') |
125
|
|
|
->will($this->returnValue('content')) |
126
|
|
|
; |
127
|
|
|
|
128
|
|
|
$config = $this->createFilterConfigurationMock(); |
129
|
|
|
$config |
130
|
|
|
->expects($this->once()) |
131
|
|
|
->method('get') |
132
|
|
|
->with('thumbnail') |
133
|
|
|
->will($this->returnValue(array( |
134
|
|
|
'size' => array(180, 180), |
135
|
|
|
'mode' => 'outbound', |
136
|
|
|
'data_loader' => 'the_loader', |
137
|
|
|
))) |
138
|
|
|
; |
139
|
|
|
|
140
|
|
|
$mimeTypeGuesser = $this->getMockMimeTypeGuesser(); |
141
|
|
|
$mimeTypeGuesser |
142
|
|
|
->expects($this->once()) |
143
|
|
|
->method('guess') |
144
|
|
|
->will($this->returnValue('text/plain')) |
145
|
|
|
; |
146
|
|
|
|
147
|
|
|
$dataManager = new DataManager($mimeTypeGuesser, $this->getMockExtensionGuesser(), $config); |
148
|
|
|
$dataManager->addLoader('the_loader', $loader); |
149
|
|
|
|
150
|
|
|
$this->setExpectedException('LogicException', 'The mime type of image cats.jpeg must be image/xxx got text/plain.'); |
151
|
|
|
$dataManager->find('thumbnail', 'cats.jpeg'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testThrowsIfLoaderReturnBinaryWithEmtptyMimeTypeOnFind() |
155
|
|
|
{ |
156
|
|
|
$loader = $this->getMockLoader(); |
157
|
|
|
$loader |
158
|
|
|
->expects($this->once()) |
159
|
|
|
->method('find') |
160
|
|
|
->with('cats.jpeg') |
161
|
|
|
->will($this->returnValue(new Binary('content', null))) |
162
|
|
|
; |
163
|
|
|
|
164
|
|
|
$config = $this->createFilterConfigurationMock(); |
165
|
|
|
$config |
166
|
|
|
->expects($this->once()) |
167
|
|
|
->method('get') |
168
|
|
|
->with('thumbnail') |
169
|
|
|
->will($this->returnValue(array( |
170
|
|
|
'size' => array(180, 180), |
171
|
|
|
'mode' => 'outbound', |
172
|
|
|
'data_loader' => 'the_loader', |
173
|
|
|
))) |
174
|
|
|
; |
175
|
|
|
|
176
|
|
|
$mimeTypeGuesser = $this->getMockMimeTypeGuesser(); |
177
|
|
|
$mimeTypeGuesser |
178
|
|
|
->expects($this->never()) |
179
|
|
|
->method('guess') |
180
|
|
|
; |
181
|
|
|
|
182
|
|
|
$dataManager = new DataManager($mimeTypeGuesser, $this->getMockExtensionGuesser(), $config); |
183
|
|
|
$dataManager->addLoader('the_loader', $loader); |
184
|
|
|
|
185
|
|
|
$this->setExpectedException('LogicException', 'The mime type of image cats.jpeg was not guessed.'); |
186
|
|
|
$dataManager->find('thumbnail', 'cats.jpeg'); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function testThrowsIfLoaderReturnBinaryWithMimeTypeNotImageOneOnFind() |
190
|
|
|
{ |
191
|
|
|
$binary = new Binary('content', 'text/plain'); |
192
|
|
|
|
193
|
|
|
$loader = $this->getMockLoader(); |
194
|
|
|
$loader |
195
|
|
|
->expects($this->once()) |
196
|
|
|
->method('find') |
197
|
|
|
->with('cats.jpeg') |
198
|
|
|
->will($this->returnValue($binary)) |
199
|
|
|
; |
200
|
|
|
|
201
|
|
|
$config = $this->createFilterConfigurationMock(); |
202
|
|
|
$config |
203
|
|
|
->expects($this->once()) |
204
|
|
|
->method('get') |
205
|
|
|
->with('thumbnail') |
206
|
|
|
->will($this->returnValue(array( |
207
|
|
|
'size' => array(180, 180), |
208
|
|
|
'mode' => 'outbound', |
209
|
|
|
'data_loader' => 'the_loader', |
210
|
|
|
))) |
211
|
|
|
; |
212
|
|
|
|
213
|
|
|
$mimeTypeGuesser = $this->getMockMimeTypeGuesser(); |
214
|
|
|
$mimeTypeGuesser |
215
|
|
|
->expects($this->never()) |
216
|
|
|
->method('guess') |
217
|
|
|
; |
218
|
|
|
|
219
|
|
|
$dataManager = new DataManager($mimeTypeGuesser, $this->getMockExtensionGuesser(), $config); |
220
|
|
|
$dataManager->addLoader('the_loader', $loader); |
221
|
|
|
|
222
|
|
|
$this->setExpectedException('LogicException', 'The mime type of image cats.jpeg must be image/xxx got text/plain.'); |
223
|
|
|
$dataManager->find('thumbnail', 'cats.jpeg'); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function testThrowIfLoaderNotRegisteredForGivenFilterOnFind() |
227
|
|
|
{ |
228
|
|
|
$config = $this->createFilterConfigurationMock(); |
229
|
|
|
$config |
230
|
|
|
->expects($this->once()) |
231
|
|
|
->method('get') |
232
|
|
|
->with('thumbnail') |
233
|
|
|
->will($this->returnValue(array( |
234
|
|
|
'size' => array(180, 180), |
235
|
|
|
'mode' => 'outbound', |
236
|
|
|
'data_loader' => null, |
237
|
|
|
))) |
238
|
|
|
; |
239
|
|
|
|
240
|
|
|
$dataManager = new DataManager($this->getMockMimeTypeGuesser(), $this->getMockExtensionGuesser(), $config); |
241
|
|
|
|
242
|
|
|
$this->setExpectedException('InvalidArgumentException', 'Could not find data loader "" for "thumbnail" filter type'); |
243
|
|
|
$dataManager->find('thumbnail', 'cats.jpeg'); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function testShouldReturnBinaryWithLoaderContentAndGuessedMimeTypeOnFind() |
247
|
|
|
{ |
248
|
|
|
$expectedContent = 'theImageBinaryContent'; |
249
|
|
|
$expectedMimeType = 'image/png'; |
250
|
|
|
|
251
|
|
|
$loader = $this->getMockLoader(); |
252
|
|
|
$loader |
253
|
|
|
->expects($this->once()) |
254
|
|
|
->method('find') |
255
|
|
|
->will($this->returnValue($expectedContent)) |
256
|
|
|
; |
257
|
|
|
|
258
|
|
|
$mimeTypeGuesser = $this->getMockMimeTypeGuesser(); |
259
|
|
|
$mimeTypeGuesser |
260
|
|
|
->expects($this->once()) |
261
|
|
|
->method('guess') |
262
|
|
|
->with($expectedContent) |
263
|
|
|
->will($this->returnValue($expectedMimeType)) |
264
|
|
|
; |
265
|
|
|
|
266
|
|
|
$config = $this->createFilterConfigurationMock(); |
267
|
|
|
$config |
268
|
|
|
->expects($this->once()) |
269
|
|
|
->method('get') |
270
|
|
|
->with('thumbnail') |
271
|
|
|
->will($this->returnValue(array( |
272
|
|
|
'size' => array(180, 180), |
273
|
|
|
'mode' => 'outbound', |
274
|
|
|
'data_loader' => null, |
275
|
|
|
))) |
276
|
|
|
; |
277
|
|
|
|
278
|
|
|
$dataManager = new DataManager($mimeTypeGuesser, $this->getMockExtensionGuesser(), $config, 'default'); |
279
|
|
|
$dataManager->addLoader('default', $loader); |
280
|
|
|
|
281
|
|
|
$binary = $dataManager->find('thumbnail', 'cats.jpeg'); |
282
|
|
|
|
283
|
|
|
$this->assertInstanceOf('Liip\ImagineBundle\Model\Binary', $binary); |
284
|
|
|
$this->assertEquals($expectedContent, $binary->getContent()); |
285
|
|
|
$this->assertEquals($expectedMimeType, $binary->getMimeType()); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
public function testShouldReturnBinaryWithLoaderContentAndGuessedFormatOnFind() |
289
|
|
|
{ |
290
|
|
|
$content = 'theImageBinaryContent'; |
291
|
|
|
$mimeType = 'image/png'; |
292
|
|
|
$expectedFormat = 'png'; |
293
|
|
|
|
294
|
|
|
$loader = $this->getMockLoader(); |
295
|
|
|
$loader |
296
|
|
|
->expects($this->once()) |
297
|
|
|
->method('find') |
298
|
|
|
->will($this->returnValue($content)) |
299
|
|
|
; |
300
|
|
|
|
301
|
|
|
$mimeTypeGuesser = $this->getMockMimeTypeGuesser(); |
302
|
|
|
$mimeTypeGuesser |
303
|
|
|
->expects($this->once()) |
304
|
|
|
->method('guess') |
305
|
|
|
->with($content) |
306
|
|
|
->will($this->returnValue($mimeType)) |
307
|
|
|
; |
308
|
|
|
|
309
|
|
|
$extensionGuesser = $this->getMockExtensionGuesser(); |
310
|
|
|
$extensionGuesser |
311
|
|
|
->expects($this->once()) |
312
|
|
|
->method('guess') |
313
|
|
|
->with($mimeType) |
314
|
|
|
->will($this->returnValue($expectedFormat)) |
315
|
|
|
; |
316
|
|
|
|
317
|
|
|
$config = $this->createFilterConfigurationMock(); |
318
|
|
|
$config |
319
|
|
|
->expects($this->once()) |
320
|
|
|
->method('get') |
321
|
|
|
->with('thumbnail') |
322
|
|
|
->will($this->returnValue(array( |
323
|
|
|
'size' => array(180, 180), |
324
|
|
|
'mode' => 'outbound', |
325
|
|
|
'data_loader' => null, |
326
|
|
|
))) |
327
|
|
|
; |
328
|
|
|
|
329
|
|
|
$dataManager = new DataManager($mimeTypeGuesser, $extensionGuesser, $config, 'default'); |
330
|
|
|
$dataManager->addLoader('default', $loader); |
331
|
|
|
|
332
|
|
|
$binary = $dataManager->find('thumbnail', 'cats.jpeg'); |
333
|
|
|
|
334
|
|
|
$this->assertInstanceOf('Liip\ImagineBundle\Model\Binary', $binary); |
335
|
|
|
$this->assertEquals($expectedFormat, $binary->getFormat()); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
public function testUseDefaultGlobalImageUsedIfImageNotFound() |
339
|
|
|
{ |
340
|
|
|
$loader = $this->getMockLoader(); |
341
|
|
|
|
342
|
|
|
$config = $this->createFilterConfigurationMock(); |
343
|
|
|
$config |
344
|
|
|
->expects($this->once()) |
345
|
|
|
->method('get') |
346
|
|
|
->with('thumbnail') |
347
|
|
|
->will($this->returnValue(array( |
348
|
|
|
'default_image' => null, |
349
|
|
|
))) |
350
|
|
|
; |
351
|
|
|
|
352
|
|
|
$mimeTypeGuesser = $this->getMockMimeTypeGuesser(); |
353
|
|
|
$mimeTypeGuesser |
354
|
|
|
->expects($this->never()) |
355
|
|
|
->method('guess') |
356
|
|
|
; |
357
|
|
|
|
358
|
|
|
$defaultGlobalImage = 'cats.jpeg'; |
359
|
|
|
$dataManager = new DataManager($mimeTypeGuesser, $this->getMockExtensionGuesser(), $config, 'default', 'cats.jpeg'); |
360
|
|
|
$dataManager->addLoader('default', $loader); |
361
|
|
|
|
362
|
|
|
$defaultImage = $dataManager->getDefaultImageUrl('thumbnail'); |
363
|
|
|
$this->assertEquals($defaultImage, $defaultGlobalImage); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
public function testUseDefaultFilterImageUsedIfImageNotFound() |
367
|
|
|
{ |
368
|
|
|
$loader = $this->getMockLoader(); |
369
|
|
|
|
370
|
|
|
$defaultFilterImage = 'cats.jpeg'; |
371
|
|
|
|
372
|
|
|
$config = $this->createFilterConfigurationMock(); |
373
|
|
|
$config |
374
|
|
|
->expects($this->once()) |
375
|
|
|
->method('get') |
376
|
|
|
->with('thumbnail') |
377
|
|
|
->will($this->returnValue(array( |
378
|
|
|
'default_image' => $defaultFilterImage, |
379
|
|
|
))) |
380
|
|
|
; |
381
|
|
|
|
382
|
|
|
$mimeTypeGuesser = $this->getMockMimeTypeGuesser(); |
383
|
|
|
$mimeTypeGuesser |
384
|
|
|
->expects($this->never()) |
385
|
|
|
->method('guess') |
386
|
|
|
; |
387
|
|
|
|
388
|
|
|
$dataManager = new DataManager($mimeTypeGuesser, $this->getMockExtensionGuesser(), $config, 'default', null); |
389
|
|
|
$dataManager->addLoader('default', $loader); |
390
|
|
|
|
391
|
|
|
$defaultImage = $dataManager->getDefaultImageUrl('thumbnail'); |
392
|
|
|
$this->assertEquals($defaultImage, $defaultFilterImage); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|LoaderInterface |
397
|
|
|
*/ |
398
|
|
|
protected function getMockLoader() |
399
|
|
|
{ |
400
|
|
|
return $this->getMock('Liip\ImagineBundle\Binary\Loader\LoaderInterface'); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|\Liip\ImagineBundle\Binary\MimeTypeGuesserInterface |
405
|
|
|
*/ |
406
|
|
|
protected function getMockMimeTypeGuesser() |
407
|
|
|
{ |
408
|
|
|
return $this->getMock('Liip\ImagineBundle\Binary\MimeTypeGuesserInterface'); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|\Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface |
413
|
|
|
*/ |
414
|
|
|
protected function getMockExtensionGuesser() |
415
|
|
|
{ |
416
|
|
|
return $this->getMock('Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface'); |
417
|
|
|
} |
418
|
|
|
} |
419
|
|
|
|