1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Bukashk0zzzLiipImagineSerializationBundle |
4
|
|
|
* |
5
|
|
|
* (c) Denis Golubovskiy <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Bukashk0zzz\LiipImagineSerializationBundle\Tests\EventListener; |
12
|
|
|
|
13
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\Tests\Fixtures\User; |
14
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\Tests\Fixtures\UserPhotos; |
15
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\Tests\Fixtures\UserPictures; |
16
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\Tests\Normalizer\FilteredUrlNormalizer; |
17
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\Tests\Normalizer\OriginUrlNormalizer; |
18
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
19
|
|
|
use JMS\Serializer\DeserializationContext; |
20
|
|
|
use JMS\Serializer\EventDispatcher\EventDispatcher; |
21
|
|
|
use JMS\Serializer\SerializerBuilder; |
22
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
23
|
|
|
use Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface; |
24
|
|
|
use Liip\ImagineBundle\Imagine\Cache\Signer; |
25
|
|
|
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration; |
26
|
|
|
use PHPUnit\Framework\TestCase; |
27
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
28
|
|
|
use Symfony\Component\Routing\RequestContext; |
29
|
|
|
use Symfony\Component\Routing\RouterInterface; |
30
|
|
|
use Vich\UploaderBundle\Storage\FileSystemStorage; |
31
|
|
|
use Vich\UploaderBundle\Storage\StorageInterface; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* JmsSerializeListenerTest |
35
|
|
|
*/ |
36
|
|
|
class JmsSerializeListenerTest extends TestCase |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var RequestContext Request context |
40
|
|
|
*/ |
41
|
|
|
private $requestContext; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var CacheManager LiipImagineBundle Cache Manager |
45
|
|
|
*/ |
46
|
|
|
private $cacheManager; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var StorageInterface Vich storage |
50
|
|
|
*/ |
51
|
|
|
private $vichStorage; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var JmsSerializeEventsManager JMS Serialize test event manager |
55
|
|
|
*/ |
56
|
|
|
private $eventManager; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var DeserializationContext JMS context |
60
|
|
|
*/ |
61
|
|
|
private $context; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
protected function setUp() |
67
|
|
|
{ |
68
|
|
|
AnnotationRegistry::registerLoader('class_exists'); |
|
|
|
|
69
|
|
|
$this->generateVichStorage(); |
70
|
|
|
$this->context = (new JmsSerializeContextGenerator())->generateContext(); |
71
|
|
|
$this->eventManager = new JmsSerializeEventsManager(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
protected function tearDown() |
78
|
|
|
{ |
79
|
|
|
$this->requestContext = null; |
80
|
|
|
$this->eventManager = null; |
81
|
|
|
$this->cacheManager = null; |
82
|
|
|
$this->vichStorage = null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Test virtualField serialization |
87
|
|
|
*/ |
88
|
|
|
public function testVirtualFieldSerialization(): void |
89
|
|
|
{ |
90
|
|
|
$user = new User(); |
91
|
|
|
$this->generateCacheManager(); |
92
|
|
|
$this->generateRequestContext(); |
93
|
|
|
$this->eventManager->addEventListeners($this->requestContext, $this->cacheManager, $this->vichStorage); |
94
|
|
|
$serializer = SerializerBuilder::create()->configureListeners(function (EventDispatcher $dispatcher): void { |
95
|
|
|
$this->eventManager->addEvents($dispatcher, $this->requestContext, $this->cacheManager, $this->vichStorage); |
96
|
|
|
})->build(); |
97
|
|
|
$result = $serializer->serialize($user, 'json'); |
98
|
|
|
|
99
|
|
|
static::assertJson($result); |
100
|
|
|
$data = \json_decode($result, true); |
101
|
|
|
static::assertEquals('http://example.com:8800/a/path/to/an/image3.png', $data['imageThumb']); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Test serialization |
106
|
|
|
*/ |
107
|
|
|
public function testSerialization(): void |
108
|
|
|
{ |
109
|
|
|
$user = new User(); |
110
|
|
|
$this->generateCacheManager(); |
111
|
|
|
$this->generateRequestContext(); |
112
|
|
|
$this->eventManager->addEventListeners($this->requestContext, $this->cacheManager, $this->vichStorage); |
113
|
|
|
$this->eventManager->dispatchEvents($this->context, $user); |
114
|
|
|
static::assertEquals('http://example.com:8800/a/path/to/an/image1.png', $user->getCoverUrl()); |
115
|
|
|
static::assertEquals('http://example.com:8800/a/path/to/an/image2.png', $user->getPhotoName()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Test serialization twice same value |
120
|
|
|
*/ |
121
|
|
View Code Duplication |
public function testSerializationMultipleTimeSameValue(): void |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$user = new User(); |
124
|
|
|
$userTwo = new User(); |
125
|
|
|
$this->generateCacheManager(); |
126
|
|
|
$this->generateRequestContext(); |
127
|
|
|
$this->eventManager->addEventListeners($this->requestContext, $this->cacheManager, $this->vichStorage); |
128
|
|
|
$this->eventManager->dispatchEvents($this->context, $user); |
129
|
|
|
$this->eventManager->dispatchEvents($this->context, $userTwo); |
130
|
|
|
static::assertEquals('http://example.com:8800/a/path/to/an/image2.png', $user->getPhotoName()); |
131
|
|
|
static::assertEquals('http://example.com:8800/a/path/to/an/image2.png', $userTwo->getPhotoName()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Test serialization twice same value |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
public function testSerializationMultipleDifferentValue(): void |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$user = new User('test_user_1.png'); |
140
|
|
|
$userTwo = new User('test_user_2.png'); |
141
|
|
|
$this->generateCacheManager(); |
142
|
|
|
$this->generateRequestContext(); |
143
|
|
|
$this->eventManager->addEventListeners($this->requestContext, $this->cacheManager, $this->vichStorage); |
144
|
|
|
$this->eventManager->dispatchEvents($this->context, $user); |
145
|
|
|
$this->eventManager->dispatchEvents($this->context, $userTwo); |
146
|
|
|
static::assertEquals('http://example.com:8800/a/path/to/an/image2.png', $user->getPhotoName()); |
147
|
|
|
static::assertEquals('http://example.com:8800/a/path/to/an/image5.png', $userTwo->getPhotoName()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Test serialization of proxy object and field with array of filters |
152
|
|
|
*/ |
153
|
|
|
public function testProxySerialization(): void |
154
|
|
|
{ |
155
|
|
|
$userPictures = new UserPictures(); |
156
|
|
|
$this->generateCacheManager(); |
157
|
|
|
$this->generateRequestContext(false, true); |
158
|
|
|
$this->eventManager->addEventListeners($this->requestContext, $this->cacheManager, $this->vichStorage); |
159
|
|
|
$data = $this->serializeObject($userPictures); |
160
|
|
|
|
161
|
|
|
static::assertEquals('http://example.com:8800/a/path/to/an/image1.png', $data['cover']['big']); |
162
|
|
|
static::assertEquals('http://example.com:8800/a/path/to/an/image2.png', $data['cover']['small']); |
163
|
|
|
static::assertEquals('http://example.com:8000/uploads/photo.jpg', $data['photo']); |
164
|
|
|
static::assertEquals('http://example.com:8800/a/path/to/an/image3.png', $data['photoThumb']); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Test serialization with origin normalizer |
169
|
|
|
*/ |
170
|
|
View Code Duplication |
public function testSerializationWithOriginNormalizer(): void |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
$userPictures = new UserPictures(); |
173
|
|
|
$this->generateCacheManager(); |
174
|
|
|
$this->generateRequestContext(); |
175
|
|
|
$data = $this->serializeObject($userPictures, [ |
176
|
|
|
'includeHost' => false, |
177
|
|
|
'vichUploaderSerialize' => true, |
178
|
|
|
'includeOriginal' => true, |
179
|
|
|
'originUrlNormalizer' => OriginUrlNormalizer::class, |
180
|
|
|
]); |
181
|
|
|
|
182
|
|
|
static::assertEquals('/uploads/newPhoto.jpg', $data['photoThumb']['original']); |
183
|
|
|
static::assertEquals('/uploads/newPhoto.jpg', $data['photo']); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Test serialization with filtered normalizer |
188
|
|
|
*/ |
189
|
|
View Code Duplication |
public function testSerializationWithFilteredNormalizer(): void |
|
|
|
|
190
|
|
|
{ |
191
|
|
|
$userPictures = new UserPictures(); |
192
|
|
|
$this->generateCacheManager(); |
193
|
|
|
$this->generateRequestContext(); |
194
|
|
|
$data = $this->serializeObject($userPictures, [ |
195
|
|
|
'includeHost' => true, |
196
|
|
|
'vichUploaderSerialize' => true, |
197
|
|
|
'includeOriginal' => true, |
198
|
|
|
'filteredUrlNormalizer' => FilteredUrlNormalizer::class, |
199
|
|
|
]); |
200
|
|
|
|
201
|
|
|
static::assertEquals('http://img.example.com:8800/a/path/to/an/image3.png', $data['photoThumb']['thumb_filter']); |
202
|
|
|
static::assertEquals('http://img.example.com:8800/a/path/to/an/image1.png', $data['cover']['big']); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Test serialization with event subscriber |
207
|
|
|
*/ |
208
|
|
|
public function testSerializationWithEventSubscriber(): void |
209
|
|
|
{ |
210
|
|
|
$userPictures = new UserPictures(); |
211
|
|
|
$this->generateCacheManager(); |
212
|
|
|
$this->generateRequestContext(); |
213
|
|
|
$this->eventManager->addNormalizerSubscriber(); |
214
|
|
|
$data = $this->serializeObject($userPictures, [ |
215
|
|
|
'includeHost' => true, |
216
|
|
|
'vichUploaderSerialize' => true, |
217
|
|
|
'includeOriginal' => true, |
218
|
|
|
]); |
219
|
|
|
|
220
|
|
|
static::assertEquals('http://img.example.com:8800/a/path/to/an/image3.png', $data['photoThumb']['thumb_filter']); |
221
|
|
|
static::assertEquals('http://img.example.com:8800/a/path/to/an/image1.png', $data['cover']['big']); |
222
|
|
|
static::assertEquals('/uploads/newPhoto.jpg', $data['photoThumb']['original']); |
223
|
|
|
static::assertEquals('http://example.com/uploads/newPhoto.jpg', $data['photo']); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Test serialization with url parse exception |
228
|
|
|
* |
229
|
|
|
* @expectedException \InvalidArgumentException |
230
|
|
|
*/ |
231
|
|
|
public function testSerializationWithUrlParseException(): void |
232
|
|
|
{ |
233
|
|
|
$userPictures = new UserPictures(); |
234
|
|
|
$this->generateCacheManager('http://blah.com:abcdef'); |
235
|
|
|
$this->generateRequestContext(); |
236
|
|
|
$this->serializeObject($userPictures, [ |
237
|
|
|
'includeHost' => false, |
238
|
|
|
]); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Test serialization with included http host and port in the URI and include original option "true" |
243
|
|
|
*/ |
244
|
|
View Code Duplication |
public function testHttpsSerialization(): void |
|
|
|
|
245
|
|
|
{ |
246
|
|
|
$userPictures = new UserPictures(); |
247
|
|
|
$this->generateCacheManager(); |
248
|
|
|
$this->generateRequestContext(true, true); |
249
|
|
|
$data = $this->serializeObject($userPictures, [ |
250
|
|
|
'includeHost' => true, |
251
|
|
|
'vichUploaderSerialize' => true, |
252
|
|
|
'includeOriginal' => true, |
253
|
|
|
]); |
254
|
|
|
|
255
|
|
|
static::assertEquals('https://example.com:8800/uploads/photo.jpg', $data['photo']); |
256
|
|
|
static::assertEquals('http://example.com:8800/a/path/to/an/image1.png', $data['cover']['big']); |
257
|
|
|
static::assertEquals('http://example.com:8800/a/path/to/an/image2.png', $data['cover']['small']); |
258
|
|
|
static::assertEquals('http://example.com:8800/a/path/to/an/image3.png', $data['photoThumb']['thumb_filter']); |
259
|
|
|
static::assertEquals('/uploads/photo.jpg', $data['photoThumb']['original']); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Test serialization without host in url and one filter |
264
|
|
|
*/ |
265
|
|
View Code Duplication |
public function testSerializationWithoutHost(): void |
|
|
|
|
266
|
|
|
{ |
267
|
|
|
$userPictures = new User(); |
268
|
|
|
$this->generateCacheManager('/'); |
269
|
|
|
$this->generateRequestContext(true, true); |
270
|
|
|
$data = $this->serializeObject($userPictures, [ |
271
|
|
|
'includeHost' => false, |
272
|
|
|
'vichUploaderSerialize' => true, |
273
|
|
|
'includeOriginal' => false, |
274
|
|
|
]); |
275
|
|
|
|
276
|
|
|
static::assertEquals('/a/path/to/an/image1.png', $data['cover']); |
277
|
|
|
static::assertEquals('/a/path/to/an/image2.png', $data['photo']); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Test serialization with host in url for original |
282
|
|
|
*/ |
283
|
|
View Code Duplication |
public function testSerializationWithHostForOriginal(): void |
|
|
|
|
284
|
|
|
{ |
285
|
|
|
$userPictures = new UserPictures(); |
286
|
|
|
$this->generateCacheManager(); |
287
|
|
|
$this->generateRequestContext(true, true); |
288
|
|
|
$data = $this->serializeObject($userPictures, [ |
289
|
|
|
'includeHost' => false, |
290
|
|
|
'vichUploaderSerialize' => true, |
291
|
|
|
'includeOriginal' => true, |
292
|
|
|
'includeHostForOriginal' => true, |
293
|
|
|
]); |
294
|
|
|
|
295
|
|
|
static::assertEquals('/uploads/photo.jpg', $data['photo']); |
296
|
|
|
static::assertFalse((bool) \mb_strpos($data['cover']['original'], 'https://example.com:8800')); |
297
|
|
|
static::assertEquals('https://example.com:8800/uploads/photo.jpg', $data['photoThumb']['original']); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Test serialization with host in url and host in url for original |
302
|
|
|
*/ |
303
|
|
View Code Duplication |
public function testSerializationWithHostAndHostForOriginal(): void |
|
|
|
|
304
|
|
|
{ |
305
|
|
|
$userPictures = new UserPictures(); |
306
|
|
|
$this->generateCacheManager(); |
307
|
|
|
$this->generateRequestContext(true, true); |
308
|
|
|
$data = $this->serializeObject($userPictures, [ |
309
|
|
|
'includeHost' => true, |
310
|
|
|
'vichUploaderSerialize' => true, |
311
|
|
|
'includeOriginal' => true, |
312
|
|
|
'includeHostForOriginal' => true, |
313
|
|
|
]); |
314
|
|
|
|
315
|
|
|
static::assertEquals('https://example.com:8800/uploads/photo.jpg', $data['photo']); |
316
|
|
|
static::assertFalse((bool) \mb_strpos($data['cover']['original'], 'https://example.com:8800')); |
317
|
|
|
static::assertEquals('https://example.com:8800/uploads/photo.jpg', $data['photoThumb']['original']); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Test serialization with host in url and host in url for original and non-stored (resolve path) images |
322
|
|
|
*/ |
323
|
|
|
public function testSerializationWithHostAndHostForOriginalAndNonStoredImages(): void |
324
|
|
|
{ |
325
|
|
|
$userPhotos = new UserPhotos(); |
326
|
|
|
$this->generateCacheManager('https://example.com:8800/', false); |
327
|
|
|
$this->generateRequestContext(true, true); |
328
|
|
|
$data = $this->serializeObject($userPhotos, [ |
329
|
|
|
'includeHost' => true, |
330
|
|
|
'vichUploaderSerialize' => true, |
331
|
|
|
'includeOriginal' => true, |
332
|
|
|
'includeHostForOriginal' => true, |
333
|
|
|
]); |
334
|
|
|
|
335
|
|
|
static::assertEquals('https://example.com:8800/a/path/to/an/resolve/image1.png', $data['cover']['big']); |
336
|
|
|
static::assertEquals('https://example.com:8800/a/path/to/an/resolve/image2.png', $data['cover']['small']); |
337
|
|
|
static::assertEquals('https://example.com:8800/uploads/photo.jpg', $data['photo']); |
338
|
|
|
static::assertFalse((bool) \mb_strpos($data['cover']['original'], 'https://example.com:8800')); |
339
|
|
|
static::assertEquals('https://example.com:8800/uploads/photo.jpg', $data['photoThumb']['original']); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Test serialization with no host in url and no host in url for original and non-stored (resolve path) images |
344
|
|
|
*/ |
345
|
|
|
public function testSerializationWithNoHostAndNoHostForOriginalAndNonStoredImages(): void |
346
|
|
|
{ |
347
|
|
|
$userPhotos = new UserPhotos(); |
348
|
|
|
$this->generateCacheManager('https://example.com:8800/', false); |
349
|
|
|
$this->generateRequestContext(true, true); |
350
|
|
|
$data = $this->serializeObject($userPhotos, [ |
351
|
|
|
'includeHost' => false, |
352
|
|
|
'vichUploaderSerialize' => true, |
353
|
|
|
'includeOriginal' => true, |
354
|
|
|
'includeHostForOriginal' => false, |
355
|
|
|
]); |
356
|
|
|
|
357
|
|
|
static::assertEquals('/a/path/to/an/resolve/image1.png', $data['cover']['big']); |
358
|
|
|
static::assertEquals('/a/path/to/an/resolve/image2.png', $data['cover']['small']); |
359
|
|
|
static::assertEquals('/uploads/photo.jpg', $data['photo']); |
360
|
|
|
static::assertEquals('/uploads/photo.jpg', $data['photoThumb']['original']); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Test serialization with no host in url and no host in url for original and ONE non-stored (resolve path) image |
365
|
|
|
*/ |
366
|
|
View Code Duplication |
public function testSerializationWithNoHostAndNoHostForOriginalAndOneNonStoredImage(): void |
|
|
|
|
367
|
|
|
{ |
368
|
|
|
$userPictures = new UserPictures(); |
369
|
|
|
$this->generateCacheManager('https://example.com:8800/', false); |
370
|
|
|
$this->generateRequestContext(true, true); |
371
|
|
|
$data = $this->serializeObject($userPictures, [ |
372
|
|
|
'includeHost' => false, |
373
|
|
|
'vichUploaderSerialize' => true, |
374
|
|
|
'includeOriginal' => true, |
375
|
|
|
'includeHostForOriginal' => false, |
376
|
|
|
]); |
377
|
|
|
|
378
|
|
|
static::assertEquals('/uploads/photo.jpg', $data['photoThumb']['original']); |
379
|
|
|
static::assertEquals('/a/path/to/an/resolve/image3.png', $data['photoThumb']['thumb_filter']); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Test serialization without host in url and array of filters |
384
|
|
|
*/ |
385
|
|
View Code Duplication |
public function testSerializationWithoutHostManyFilters(): void |
|
|
|
|
386
|
|
|
{ |
387
|
|
|
$userPhotos = new UserPhotos(); |
388
|
|
|
$this->generateCacheManager('/'); |
389
|
|
|
$this->generateRequestContext(true, true); |
390
|
|
|
$data = $this->serializeObject($userPhotos, [ |
391
|
|
|
'includeHost' => false, |
392
|
|
|
'vichUploaderSerialize' => true, |
393
|
|
|
'includeOriginal' => false, |
394
|
|
|
]); |
395
|
|
|
|
396
|
|
|
static::assertEquals('/a/path/to/an/image1.png', $data['cover']['big']); |
397
|
|
|
static::assertEquals('/a/path/to/an/image2.png', $data['cover']['small']); |
398
|
|
|
static::assertEquals('/uploads/photo.jpg', $data['photo']); |
399
|
|
|
static::assertEquals('/a/path/to/an/image3.png', $data['photoThumb']['thumb_big']); |
400
|
|
|
static::assertEquals('/a/path/to/an/image4.png', $data['photoThumb']['thumb_small']); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* @param User|UserPictures|UserPhotos $user |
405
|
|
|
* @param mixed[] $config JMS serializer listner config |
406
|
|
|
* |
407
|
|
|
* @return mixed[] |
408
|
|
|
*/ |
409
|
|
|
protected function serializeObject($user, array $config = []): array |
410
|
|
|
{ |
411
|
|
|
$serializer = SerializerBuilder::create()->configureListeners(function (EventDispatcher $dispatcher) use ($config): void { |
412
|
|
|
$this->eventManager->addEvents($dispatcher, $this->requestContext, $this->cacheManager, $this->vichStorage, $config); |
413
|
|
|
})->build(); |
414
|
|
|
$result = $serializer->serialize($user, 'json'); |
415
|
|
|
|
416
|
|
|
static::assertJson($result); |
417
|
|
|
|
418
|
|
|
return \json_decode($result, true); |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* @param bool $https |
423
|
|
|
* @param bool $port |
424
|
|
|
*/ |
425
|
|
|
protected function generateRequestContext(bool $https = false, bool $port = false): void |
426
|
|
|
{ |
427
|
|
|
$this->requestContext = $this->getMockBuilder(RequestContext::class) |
428
|
|
|
->disableOriginalConstructor() |
429
|
|
|
->getMock(); |
430
|
|
|
|
431
|
|
|
$scheme = $https ? 'https' : 'http'; |
432
|
|
|
|
433
|
|
|
$this->requestContext->expects(static::any()) |
434
|
|
|
->method('getScheme') |
435
|
|
|
->willReturn($scheme); |
436
|
|
|
|
437
|
|
|
$this->requestContext->expects(static::any()) |
438
|
|
|
->method('getHost') |
439
|
|
|
->willReturn('example.com'); |
440
|
|
|
|
441
|
|
|
if ($port) { |
442
|
|
|
if ($https) { |
443
|
|
|
$this->requestContext->expects(static::any()) |
444
|
|
|
->method('getHttpsPort') |
445
|
|
|
->willReturn(8800); |
446
|
|
|
|
447
|
|
|
return; |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
$this->requestContext->expects(static::any()) |
451
|
|
|
->method('getHttpPort') |
452
|
|
|
->willReturn(8000); |
453
|
|
|
} |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* Prepare mock of Liip cache manager |
458
|
|
|
* |
459
|
|
|
* @param string $urlPrefix |
460
|
|
|
* @param bool $isStored |
461
|
|
|
*/ |
462
|
|
|
protected function generateCacheManager(string $urlPrefix = 'http://example.com:8800/', bool $isStored = true): void |
463
|
|
|
{ |
464
|
|
|
$resolver = $this->getMockBuilder(ResolverInterface::class)->getMock(); |
465
|
|
|
$resolver |
466
|
|
|
->expects(static::any()) |
467
|
|
|
->method('isStored') |
468
|
|
|
->will(static::returnValue($isStored)) |
469
|
|
|
; |
470
|
|
|
$resolver |
471
|
|
|
->expects(static::any()) |
472
|
|
|
->method('resolve') |
473
|
|
|
->will(static::onConsecutiveCalls( |
474
|
|
|
$urlPrefix.'a/path/to/an/image1.png', |
475
|
|
|
$urlPrefix.'a/path/to/an/image2.png', |
476
|
|
|
$urlPrefix.'a/path/to/an/image3.png', |
477
|
|
|
$urlPrefix.'a/path/to/an/image4.png', |
478
|
|
|
$urlPrefix.'a/path/to/an/image5.png', |
479
|
|
|
$urlPrefix.'a/path/to/an/image6.png', |
480
|
|
|
$urlPrefix.'a/path/to/an/image7.png', |
481
|
|
|
$urlPrefix.'a/path/to/an/image8.png' |
482
|
|
|
)) |
483
|
|
|
; |
484
|
|
|
|
485
|
|
|
$config = $this->getMockBuilder(FilterConfiguration::class)->getMock(); |
486
|
|
|
$config->expects(static::any()) |
487
|
|
|
->method('get') |
488
|
|
|
->will(static::returnValue([ |
489
|
|
|
'size' => [180, 180], |
490
|
|
|
'mode' => 'outbound', |
491
|
|
|
'cache' => null, |
492
|
|
|
])) |
493
|
|
|
; |
494
|
|
|
|
495
|
|
|
$router = $this->getMockBuilder(RouterInterface::class)->getMock(); |
496
|
|
|
$router->expects(static::any()) |
497
|
|
|
->method('generate') |
498
|
|
|
->will(static::onConsecutiveCalls( |
499
|
|
|
$urlPrefix.'a/path/to/an/resolve/image1.png', |
500
|
|
|
$urlPrefix.'a/path/to/an/resolve/image2.png', |
501
|
|
|
$urlPrefix.'a/path/to/an/resolve/image3.png', |
502
|
|
|
$urlPrefix.'a/path/to/an/resole/image4.png', |
503
|
|
|
$urlPrefix.'a/path/to/an/resole/image5.png', |
504
|
|
|
$urlPrefix.'a/path/to/an/resole/image6.png', |
505
|
|
|
$urlPrefix.'a/path/to/an/resole/image7.png', |
506
|
|
|
$urlPrefix.'a/path/to/an/resole/image8.png' |
507
|
|
|
)) |
508
|
|
|
; |
509
|
|
|
|
510
|
|
|
$eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock(); |
511
|
|
|
|
512
|
|
|
/** @noinspection PhpParamsInspection */ |
513
|
|
|
$this->cacheManager = new CacheManager($config, $router, new Signer('secret'), $eventDispatcher); |
514
|
|
|
|
515
|
|
|
/** @noinspection PhpParamsInspection */ |
516
|
|
|
$this->cacheManager->addResolver('default', $resolver); |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* Generate vichStorage mock |
521
|
|
|
*/ |
522
|
|
|
protected function generateVichStorage(): void |
523
|
|
|
{ |
524
|
|
|
$this->vichStorage = $this->getMockBuilder(FileSystemStorage::class) |
525
|
|
|
->disableOriginalConstructor() |
526
|
|
|
->getMock(); |
527
|
|
|
$this->vichStorage->expects(static::any()) |
528
|
|
|
->method('resolveUri') |
529
|
|
|
->will(static::returnValue('/uploads/photo.jpg')); |
530
|
|
|
} |
531
|
|
|
} |
532
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.