|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Liip\ImagineBundle\Tests\Imagine\Cache\Resolver; |
|
4
|
|
|
|
|
5
|
|
|
use Liip\ImagineBundle\Imagine\Cache\Resolver\AmazonS3Resolver; |
|
6
|
|
|
use Liip\ImagineBundle\Model\Binary; |
|
7
|
|
|
use Liip\ImagineBundle\Tests\AbstractTest; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @covers Liip\ImagineBundle\Imagine\Cache\Resolver\AmazonS3Resolver |
|
11
|
|
|
*/ |
|
12
|
|
|
class AmazonS3ResolverTest extends AbstractTest |
|
13
|
|
|
{ |
|
14
|
|
|
public function testImplementsResolverInterface() |
|
15
|
|
|
{ |
|
16
|
|
|
$rc = new \ReflectionClass('Liip\ImagineBundle\Imagine\Cache\Resolver\AmazonS3Resolver'); |
|
17
|
|
|
|
|
18
|
|
|
$this->assertTrue($rc->implementsInterface('Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface')); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function testNoDoubleSlashesInObjectUrlOnResolve() |
|
22
|
|
|
{ |
|
23
|
|
|
$s3 = $this->createAmazonS3Mock(); |
|
24
|
|
|
$s3 |
|
|
|
|
|
|
25
|
|
|
->expects($this->once()) |
|
26
|
|
|
->method('get_object_url') |
|
27
|
|
|
->with('images.example.com', 'thumb/some-folder/path.jpg') |
|
28
|
|
|
; |
|
29
|
|
|
|
|
30
|
|
|
$resolver = new AmazonS3Resolver($s3, 'images.example.com'); |
|
|
|
|
|
|
31
|
|
|
$resolver->resolve('/some-folder/path.jpg', 'thumb'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
View Code Duplication |
public function testObjUrlOptionsPassedToAmazonOnResolve() |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
$s3 = $this->createAmazonS3Mock(); |
|
37
|
|
|
$s3 |
|
|
|
|
|
|
38
|
|
|
->expects($this->once()) |
|
39
|
|
|
->method('get_object_url') |
|
40
|
|
|
->with('images.example.com', 'thumb/some-folder/path.jpg', 0, array('torrent' => true)) |
|
41
|
|
|
; |
|
42
|
|
|
|
|
43
|
|
|
$resolver = new AmazonS3Resolver($s3, 'images.example.com'); |
|
|
|
|
|
|
44
|
|
|
$resolver->setObjectUrlOption('torrent', true); |
|
45
|
|
|
$resolver->resolve('/some-folder/path.jpg', 'thumb'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
View Code Duplication |
public function testThrowsAndLogIfCanNotCreateObjectOnAmazon() |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
$binary = new Binary('aContent', 'image/jpeg', 'jpeg'); |
|
51
|
|
|
|
|
52
|
|
|
$s3 = $this->createAmazonS3Mock(); |
|
53
|
|
|
$s3 |
|
|
|
|
|
|
54
|
|
|
->expects($this->once()) |
|
55
|
|
|
->method('create_object') |
|
56
|
|
|
->will($this->returnValue($this->createCFResponseMock(false))) |
|
57
|
|
|
; |
|
58
|
|
|
|
|
59
|
|
|
$logger = $this->getMock('Psr\Log\LoggerInterface'); |
|
60
|
|
|
$logger |
|
61
|
|
|
->expects($this->once()) |
|
62
|
|
|
->method('error') |
|
63
|
|
|
; |
|
64
|
|
|
|
|
65
|
|
|
$resolver = new AmazonS3Resolver($s3, 'images.example.com'); |
|
|
|
|
|
|
66
|
|
|
$resolver->setLogger($logger); |
|
67
|
|
|
|
|
68
|
|
|
$this->setExpectedException( |
|
69
|
|
|
'Liip\ImagineBundle\Exception\Imagine\Cache\Resolver\NotStorableException', |
|
70
|
|
|
'The object could not be created on Amazon S3.' |
|
71
|
|
|
); |
|
72
|
|
|
$resolver->store($binary, 'foobar.jpg', 'thumb'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
View Code Duplication |
public function testCreatedObjectOnAmazon() |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
$binary = new Binary('aContent', 'image/jpeg', 'jpeg'); |
|
78
|
|
|
|
|
79
|
|
|
$s3 = $this->createAmazonS3Mock(); |
|
80
|
|
|
$s3 |
|
|
|
|
|
|
81
|
|
|
->expects($this->once()) |
|
82
|
|
|
->method('create_object') |
|
83
|
|
|
->will($this->returnValue($this->createCFResponseMock(true))) |
|
84
|
|
|
; |
|
85
|
|
|
|
|
86
|
|
|
$resolver = new AmazonS3Resolver($s3, 'images.example.com'); |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
$resolver->store($binary, 'foobar.jpg', 'thumb'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
View Code Duplication |
public function testIsStoredChecksObjectExistence() |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
$s3 = $this->createAmazonS3Mock(); |
|
94
|
|
|
$s3 |
|
|
|
|
|
|
95
|
|
|
->expects($this->once()) |
|
96
|
|
|
->method('if_object_exists') |
|
97
|
|
|
->will($this->returnValue(false)) |
|
98
|
|
|
; |
|
99
|
|
|
|
|
100
|
|
|
$resolver = new AmazonS3Resolver($s3, 'images.example.com'); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
$this->assertFalse($resolver->isStored('/some-folder/path.jpg', 'thumb')); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
View Code Duplication |
public function testReturnResolvedImageUrlOnResolve() |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
|
|
$s3 = $this->createAmazonS3Mock(); |
|
108
|
|
|
$s3 |
|
|
|
|
|
|
109
|
|
|
->expects($this->once()) |
|
110
|
|
|
->method('get_object_url') |
|
111
|
|
|
->with('images.example.com', 'thumb/some-folder/path.jpg', 0, array()) |
|
112
|
|
|
->will($this->returnValue('http://images.example.com/some-folder/path.jpg')) |
|
113
|
|
|
; |
|
114
|
|
|
|
|
115
|
|
|
$resolver = new AmazonS3Resolver($s3, 'images.example.com'); |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
$this->assertEquals( |
|
118
|
|
|
'http://images.example.com/some-folder/path.jpg', |
|
119
|
|
|
$resolver->resolve('/some-folder/path.jpg', 'thumb') |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function testDoNothingIfFiltersAndPathsEmptyOnRemove() |
|
124
|
|
|
{ |
|
125
|
|
|
$s3 = $this->createAmazonS3Mock(); |
|
126
|
|
|
$s3 |
|
|
|
|
|
|
127
|
|
|
->expects($this->never()) |
|
128
|
|
|
->method('if_object_exists') |
|
129
|
|
|
; |
|
130
|
|
|
$s3 |
|
131
|
|
|
->expects($this->never()) |
|
132
|
|
|
->method('delete_object') |
|
133
|
|
|
; |
|
134
|
|
|
$s3 |
|
135
|
|
|
->expects($this->never()) |
|
136
|
|
|
->method('delete_all_objects') |
|
137
|
|
|
; |
|
138
|
|
|
|
|
139
|
|
|
$resolver = new AmazonS3Resolver($s3, 'images.example.com'); |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
$resolver->remove(array(), array()); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function testRemoveCacheForPathAndFilterOnRemove() |
|
145
|
|
|
{ |
|
146
|
|
|
$s3 = $this->createAmazonS3Mock(); |
|
147
|
|
|
$s3 |
|
|
|
|
|
|
148
|
|
|
->expects($this->once()) |
|
149
|
|
|
->method('if_object_exists') |
|
150
|
|
|
->with('images.example.com', 'thumb/some-folder/path.jpg') |
|
151
|
|
|
->will($this->returnValue(true)) |
|
152
|
|
|
; |
|
153
|
|
|
$s3 |
|
154
|
|
|
->expects($this->once()) |
|
155
|
|
|
->method('delete_object') |
|
156
|
|
|
->with('images.example.com', 'thumb/some-folder/path.jpg') |
|
157
|
|
|
->will($this->returnValue($this->createCFResponseMock(true))) |
|
158
|
|
|
; |
|
159
|
|
|
|
|
160
|
|
|
$resolver = new AmazonS3Resolver($s3, 'images.example.com'); |
|
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
$resolver->remove(array('some-folder/path.jpg'), array('thumb')); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function testRemoveCacheForSomePathsAndFilterOnRemove() |
|
166
|
|
|
{ |
|
167
|
|
|
$s3 = $this->createAmazonS3Mock(); |
|
168
|
|
|
$s3 |
|
|
|
|
|
|
169
|
|
|
->expects($this->at(0)) |
|
170
|
|
|
->method('if_object_exists') |
|
171
|
|
|
->with('images.example.com', 'filter/pathOne.jpg') |
|
172
|
|
|
->will($this->returnValue(true)) |
|
173
|
|
|
; |
|
174
|
|
|
$s3 |
|
175
|
|
|
->expects($this->at(1)) |
|
176
|
|
|
->method('delete_object') |
|
177
|
|
|
->with('images.example.com', 'filter/pathOne.jpg') |
|
178
|
|
|
->will($this->returnValue($this->createCFResponseMock(true))) |
|
179
|
|
|
; |
|
180
|
|
|
$s3 |
|
181
|
|
|
->expects($this->at(2)) |
|
182
|
|
|
->method('if_object_exists') |
|
183
|
|
|
->with('images.example.com', 'filter/pathTwo.jpg') |
|
184
|
|
|
->will($this->returnValue(true)) |
|
185
|
|
|
; |
|
186
|
|
|
$s3 |
|
187
|
|
|
->expects($this->at(3)) |
|
188
|
|
|
->method('delete_object') |
|
189
|
|
|
->with('images.example.com', 'filter/pathTwo.jpg') |
|
190
|
|
|
->will($this->returnValue($this->createCFResponseMock(true))) |
|
191
|
|
|
; |
|
192
|
|
|
|
|
193
|
|
|
$resolver = new AmazonS3Resolver($s3, 'images.example.com'); |
|
|
|
|
|
|
194
|
|
|
|
|
195
|
|
|
$resolver->remove(array('pathOne.jpg', 'pathTwo.jpg'), array('filter')); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function testRemoveCacheForSomePathsAndSomeFiltersOnRemove() |
|
199
|
|
|
{ |
|
200
|
|
|
$s3 = $this->createAmazonS3Mock(); |
|
201
|
|
|
$s3 |
|
|
|
|
|
|
202
|
|
|
->expects($this->at(0)) |
|
203
|
|
|
->method('if_object_exists') |
|
204
|
|
|
->with('images.example.com', 'filterOne/pathOne.jpg') |
|
205
|
|
|
->will($this->returnValue(true)) |
|
206
|
|
|
; |
|
207
|
|
|
$s3 |
|
208
|
|
|
->expects($this->at(1)) |
|
209
|
|
|
->method('delete_object') |
|
210
|
|
|
->with('images.example.com', 'filterOne/pathOne.jpg') |
|
211
|
|
|
->will($this->returnValue($this->createCFResponseMock(true))) |
|
212
|
|
|
; |
|
213
|
|
|
$s3 |
|
214
|
|
|
->expects($this->at(2)) |
|
215
|
|
|
->method('if_object_exists') |
|
216
|
|
|
->with('images.example.com', 'filterOne/pathTwo.jpg') |
|
217
|
|
|
->will($this->returnValue(true)) |
|
218
|
|
|
; |
|
219
|
|
|
$s3 |
|
220
|
|
|
->expects($this->at(3)) |
|
221
|
|
|
->method('delete_object') |
|
222
|
|
|
->with('images.example.com', 'filterOne/pathTwo.jpg') |
|
223
|
|
|
->will($this->returnValue($this->createCFResponseMock(true))) |
|
224
|
|
|
; |
|
225
|
|
|
$s3 |
|
226
|
|
|
->expects($this->at(4)) |
|
227
|
|
|
->method('if_object_exists') |
|
228
|
|
|
->with('images.example.com', 'filterTwo/pathOne.jpg') |
|
229
|
|
|
->will($this->returnValue(true)) |
|
230
|
|
|
; |
|
231
|
|
|
$s3 |
|
232
|
|
|
->expects($this->at(5)) |
|
233
|
|
|
->method('delete_object') |
|
234
|
|
|
->with('images.example.com', 'filterTwo/pathOne.jpg') |
|
235
|
|
|
->will($this->returnValue($this->createCFResponseMock(true))) |
|
236
|
|
|
; |
|
237
|
|
|
$s3 |
|
238
|
|
|
->expects($this->at(6)) |
|
239
|
|
|
->method('if_object_exists') |
|
240
|
|
|
->with('images.example.com', 'filterTwo/pathTwo.jpg') |
|
241
|
|
|
->will($this->returnValue(true)) |
|
242
|
|
|
; |
|
243
|
|
|
$s3 |
|
244
|
|
|
->expects($this->at(7)) |
|
245
|
|
|
->method('delete_object') |
|
246
|
|
|
->with('images.example.com', 'filterTwo/pathTwo.jpg') |
|
247
|
|
|
->will($this->returnValue($this->createCFResponseMock(true))) |
|
248
|
|
|
; |
|
249
|
|
|
|
|
250
|
|
|
$resolver = new AmazonS3Resolver($s3, 'images.example.com'); |
|
|
|
|
|
|
251
|
|
|
|
|
252
|
|
|
$resolver->remove( |
|
253
|
|
|
array('pathOne.jpg', 'pathTwo.jpg'), |
|
254
|
|
|
array('filterOne', 'filterTwo') |
|
255
|
|
|
); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
public function testDoNothingWhenObjectNotExistForPathAndFilterOnRemove() |
|
259
|
|
|
{ |
|
260
|
|
|
$s3 = $this->createAmazonS3Mock(); |
|
261
|
|
|
$s3 |
|
|
|
|
|
|
262
|
|
|
->expects($this->once()) |
|
263
|
|
|
->method('if_object_exists') |
|
264
|
|
|
->with('images.example.com', 'filter/path.jpg') |
|
265
|
|
|
->will($this->returnValue(false)) |
|
266
|
|
|
; |
|
267
|
|
|
$s3 |
|
268
|
|
|
->expects($this->never()) |
|
269
|
|
|
->method('delete_object') |
|
270
|
|
|
; |
|
271
|
|
|
|
|
272
|
|
|
$resolver = new AmazonS3Resolver($s3, 'images.example.com'); |
|
|
|
|
|
|
273
|
|
|
|
|
274
|
|
|
$resolver->remove(array('path.jpg'), array('filter')); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
public function testLogIfNotDeletedForPathAndFilterOnRemove() |
|
278
|
|
|
{ |
|
279
|
|
|
$s3 = $this->createAmazonS3Mock(); |
|
280
|
|
|
$s3 |
|
|
|
|
|
|
281
|
|
|
->expects($this->once()) |
|
282
|
|
|
->method('if_object_exists') |
|
283
|
|
|
->with('images.example.com', 'filter/path.jpg') |
|
284
|
|
|
->will($this->returnValue(true)) |
|
285
|
|
|
; |
|
286
|
|
|
$s3 |
|
287
|
|
|
->expects($this->once()) |
|
288
|
|
|
->method('delete_object') |
|
289
|
|
|
->will($this->returnValue($this->createCFResponseMock(false))) |
|
290
|
|
|
; |
|
291
|
|
|
|
|
292
|
|
|
$logger = $this->getMock('Psr\Log\LoggerInterface'); |
|
293
|
|
|
$logger |
|
294
|
|
|
->expects($this->once()) |
|
295
|
|
|
->method('error') |
|
296
|
|
|
; |
|
297
|
|
|
|
|
298
|
|
|
$resolver = new AmazonS3Resolver($s3, 'images.example.com'); |
|
|
|
|
|
|
299
|
|
|
$resolver->setLogger($logger); |
|
300
|
|
|
|
|
301
|
|
|
$resolver->remove(array('path.jpg'), array('filter')); |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
View Code Duplication |
public function testRemoveCacheForFilterOnRemove() |
|
|
|
|
|
|
305
|
|
|
{ |
|
306
|
|
|
$s3 = $this->createAmazonS3Mock(); |
|
307
|
|
|
$s3 |
|
|
|
|
|
|
308
|
|
|
->expects($this->once()) |
|
309
|
|
|
->method('delete_all_objects') |
|
310
|
|
|
->with('images.example.com', '/filter/i') |
|
311
|
|
|
->will($this->returnValue(true)) |
|
312
|
|
|
; |
|
313
|
|
|
|
|
314
|
|
|
$resolver = new AmazonS3Resolver($s3, 'images.example.com'); |
|
|
|
|
|
|
315
|
|
|
|
|
316
|
|
|
$resolver->remove(array(), array('filter')); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
View Code Duplication |
public function testRemoveCacheForSomeFiltersOnRemove() |
|
|
|
|
|
|
320
|
|
|
{ |
|
321
|
|
|
$s3 = $this->createAmazonS3Mock(); |
|
322
|
|
|
$s3 |
|
|
|
|
|
|
323
|
|
|
->expects($this->once()) |
|
324
|
|
|
->method('delete_all_objects') |
|
325
|
|
|
->with('images.example.com', '/filterOne|filterTwo/i') |
|
326
|
|
|
->will($this->returnValue(true)) |
|
327
|
|
|
; |
|
328
|
|
|
|
|
329
|
|
|
$resolver = new AmazonS3Resolver($s3, 'images.example.com'); |
|
|
|
|
|
|
330
|
|
|
|
|
331
|
|
|
$resolver->remove(array(), array('filterOne', 'filterTwo')); |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
public function testLogIfBatchNotDeletedForFilterOnRemove() |
|
335
|
|
|
{ |
|
336
|
|
|
$s3 = $this->createAmazonS3Mock(); |
|
337
|
|
|
$s3 |
|
|
|
|
|
|
338
|
|
|
->expects($this->once()) |
|
339
|
|
|
->method('delete_all_objects') |
|
340
|
|
|
->with('images.example.com', '/filter/i') |
|
341
|
|
|
->will($this->returnValue(false)) |
|
342
|
|
|
; |
|
343
|
|
|
|
|
344
|
|
|
$logger = $this->getMock('Psr\Log\LoggerInterface'); |
|
345
|
|
|
$logger |
|
346
|
|
|
->expects($this->once()) |
|
347
|
|
|
->method('error') |
|
348
|
|
|
; |
|
349
|
|
|
|
|
350
|
|
|
$resolver = new AmazonS3Resolver($s3, 'images.example.com'); |
|
|
|
|
|
|
351
|
|
|
$resolver->setLogger($logger); |
|
352
|
|
|
|
|
353
|
|
|
$resolver->remove(array(), array('filter')); |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* @param bool $ok |
|
358
|
|
|
* |
|
359
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|\CFResponse |
|
360
|
|
|
*/ |
|
361
|
|
|
protected function createCFResponseMock($ok = true) |
|
362
|
|
|
{ |
|
363
|
|
|
$s3Response = $this->getMock('CFResponse', array('isOK'), array(), '', false); |
|
364
|
|
|
$s3Response |
|
365
|
|
|
->expects($this->once()) |
|
366
|
|
|
->method('isOK') |
|
367
|
|
|
->will($this->returnValue($ok)) |
|
368
|
|
|
; |
|
369
|
|
|
|
|
370
|
|
|
return $s3Response; |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
/** |
|
374
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|\AmazonS3 |
|
375
|
|
|
*/ |
|
376
|
|
|
protected function createAmazonS3Mock() |
|
377
|
|
|
{ |
|
378
|
|
|
$mockedMethods = array( |
|
379
|
|
|
'if_object_exists', |
|
380
|
|
|
'create_object', |
|
381
|
|
|
'get_object_url', |
|
382
|
|
|
'delete_object', |
|
383
|
|
|
'delete_all_objects', |
|
384
|
|
|
'authenticate', |
|
385
|
|
|
); |
|
386
|
|
|
|
|
387
|
|
|
return $this->getMock('AmazonS3', $mockedMethods, array(), '', false); |
|
388
|
|
|
} |
|
389
|
|
|
} |
|
390
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: