|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Bukashk0zzzLiipImagineSerializationBundle |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Denis Golubovskiy <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Bukashk0zzz\LiipImagineSerializationBundle\Tests\EventListener; |
|
13
|
|
|
|
|
14
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\Tests\Fixtures\UserPictures; |
|
15
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
|
16
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
|
17
|
|
|
use Doctrine\Common\Annotations\CachedReader; |
|
18
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
|
19
|
|
|
use JMS\Serializer\Construction\UnserializeObjectConstructor; |
|
20
|
|
|
use JMS\Serializer\Handler\HandlerRegistry; |
|
21
|
|
|
use JMS\Serializer\Metadata\Driver\AnnotationDriver; |
|
22
|
|
|
use JMS\Serializer\DeserializationContext; |
|
23
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
|
24
|
|
|
use JMS\Serializer\EventDispatcher\Events as JmsEvents; |
|
25
|
|
|
use JMS\Serializer\EventDispatcher\EventDispatcherInterface; |
|
26
|
|
|
use JMS\Serializer\GraphNavigator; |
|
27
|
|
|
use JMS\Serializer\JsonSerializationVisitor; |
|
28
|
|
|
use JMS\Serializer\Naming\CamelCaseNamingStrategy; |
|
29
|
|
|
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy; |
|
30
|
|
|
use JMS\Serializer\SerializerBuilder; |
|
31
|
|
|
use Metadata\MetadataFactory; |
|
32
|
|
|
use Symfony\Component\Routing\RequestContext; |
|
33
|
|
|
use JMS\Serializer\EventDispatcher\EventDispatcher; |
|
34
|
|
|
use Vich\UploaderBundle\Storage\StorageInterface; |
|
35
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
|
36
|
|
|
use Liip\ImagineBundle\Imagine\Cache\Signer; |
|
37
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\EventListener\JmsSerializeListener; |
|
38
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\Tests\Fixtures\User; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* JmsSerializeListenerTest |
|
42
|
|
|
* |
|
43
|
|
|
* @author Artem Genvald <[email protected]> |
|
44
|
|
|
*/ |
|
45
|
|
|
class JmsSerializeListenerTest extends \PHPUnit_Framework_TestCase |
|
46
|
|
|
{ |
|
47
|
|
|
/** |
|
48
|
|
|
* @var EventDispatcherInterface $dispatcher Dispatcher |
|
49
|
|
|
*/ |
|
50
|
|
|
private $dispatcher; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var RequestContext $requestContext Request context |
|
54
|
|
|
*/ |
|
55
|
|
|
private $requestContext; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var CachedReader $annotationReader Cached annotation reader |
|
59
|
|
|
*/ |
|
60
|
|
|
private $annotationReader; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var CacheManager $cacheManager LiipImagineBundle Cache Manager |
|
64
|
|
|
*/ |
|
65
|
|
|
private $cacheManager; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var StorageInterface $storage Vich storage |
|
69
|
|
|
*/ |
|
70
|
|
|
private $vichStorage; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var DeserializationContext $context JMS context |
|
74
|
|
|
*/ |
|
75
|
|
|
private $context; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @var string $filePath Image file path |
|
79
|
|
|
*/ |
|
80
|
|
|
private $filePath; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function setUp() |
|
86
|
|
|
{ |
|
87
|
|
|
AnnotationRegistry::registerLoader('class_exists'); |
|
88
|
|
|
$this->filePath = (new User())->getCoverUrl(); |
|
89
|
|
|
$this->annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache()); |
|
90
|
|
|
$this->generateVichStorage(); |
|
91
|
|
|
$this->generateContext(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function tearDown() |
|
98
|
|
|
{ |
|
99
|
|
|
$this->dispatcher = null; |
|
100
|
|
|
$this->requestContext = null; |
|
101
|
|
|
$this->annotationReader = null; |
|
102
|
|
|
$this->cacheManager = null; |
|
103
|
|
|
$this->vichStorage = null; |
|
104
|
|
|
$this->filePath = null; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Test virtualField serialization |
|
109
|
|
|
*/ |
|
110
|
|
|
public function testVirtualFieldSerialization() |
|
111
|
|
|
{ |
|
112
|
|
|
$user = new User(); |
|
113
|
|
|
$this->generateCacheManager(); |
|
114
|
|
|
$this->generateRequestContext(); |
|
115
|
|
|
$serializer = SerializerBuilder::create()->configureListeners(function (EventDispatcher $dispatcher) { |
|
116
|
|
|
$this->addEvents($dispatcher); |
|
117
|
|
|
})->build(); |
|
118
|
|
|
$result = $serializer->serialize($user, 'json'); |
|
119
|
|
|
|
|
120
|
|
|
static::assertJson($result); |
|
121
|
|
|
$data = json_decode($result, true); |
|
122
|
|
|
static::assertEquals('http://a/path/to/an/image3.png', $data['imageThumb']); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Test serialization |
|
127
|
|
|
*/ |
|
128
|
|
|
public function testSerialization() |
|
129
|
|
|
{ |
|
130
|
|
|
$user = new User(); |
|
131
|
|
|
$this->generateCacheManager(); |
|
132
|
|
|
$this->generateRequestContext(); |
|
133
|
|
|
$this->dispatchEvents($user); |
|
134
|
|
|
static::assertEquals('http://a/path/to/an/image1.png', $user->getCoverUrl()); |
|
135
|
|
|
static::assertEquals('http://a/path/to/an/image2.png', $user->getPhotoName()); |
|
136
|
|
|
|
|
137
|
|
|
// Serialize same object second time (check cache) |
|
138
|
|
|
$this->generateContext(); |
|
139
|
|
|
$this->dispatchEvents($user); |
|
140
|
|
|
|
|
141
|
|
|
static::assertEquals('http://a/path/to/an/image1.png', $user->getCoverUrl()); |
|
142
|
|
|
static::assertEquals('http://a/path/to/an/image2.png', $user->getPhotoName()); |
|
143
|
|
|
static::assertEquals($this->filePath, $user->getImageUrl()); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Test serialization of proxy object |
|
148
|
|
|
*/ |
|
149
|
|
|
public function testProxySerialization() |
|
150
|
|
|
{ |
|
151
|
|
|
$user = new UserPictures(); |
|
152
|
|
|
$this->generateCacheManager(); |
|
153
|
|
|
$this->generateRequestContext(false, true); |
|
154
|
|
|
$this->dispatchEvents($user); |
|
155
|
|
|
|
|
156
|
|
|
static::assertEquals('http://a/path/to/an/image1.png', $user->getCoverUrl()); |
|
157
|
|
|
static::assertEquals('http://example.com:8000/uploads/photo.jpg', $user->getPhotoName()); |
|
158
|
|
|
static::assertEmpty($user->getImageUrl()); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Test serialization with included http host and port in the URI |
|
163
|
|
|
*/ |
|
164
|
|
|
public function testHttpsSerialization() |
|
165
|
|
|
{ |
|
166
|
|
|
$user = new UserPictures(); |
|
167
|
|
|
$this->generateCacheManager(); |
|
168
|
|
|
$this->generateRequestContext(true, true); |
|
169
|
|
|
$this->dispatchEvents($user); |
|
170
|
|
|
|
|
171
|
|
|
static::assertEquals('https://example.com:8800/uploads/photo.jpg', $user->getPhotoName()); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param bool $https |
|
176
|
|
|
* @param bool $port |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function generateRequestContext($https = false, $port = false) |
|
179
|
|
|
{ |
|
180
|
|
|
$this->requestContext = $this->getMockBuilder('Symfony\Component\Routing\RequestContext') |
|
181
|
|
|
->disableOriginalConstructor() |
|
182
|
|
|
->getMock(); |
|
183
|
|
|
|
|
184
|
|
|
$scheme = $https ? 'https':'http'; |
|
185
|
|
|
|
|
186
|
|
|
$this->requestContext->expects(static::any()) |
|
187
|
|
|
->method('getScheme') |
|
188
|
|
|
->willReturn($scheme); |
|
189
|
|
|
|
|
190
|
|
|
$this->requestContext->expects(static::any()) |
|
191
|
|
|
->method('getHost') |
|
192
|
|
|
->willReturn('example.com'); |
|
193
|
|
|
|
|
194
|
|
|
if ($port) { |
|
195
|
|
|
if ($https) { |
|
196
|
|
|
$this->requestContext->expects(static::any()) |
|
197
|
|
|
->method('getHttpsPort') |
|
198
|
|
|
->willReturn(8800); |
|
199
|
|
|
} else { |
|
200
|
|
|
$this->requestContext->expects(static::any()) |
|
201
|
|
|
->method('getHttpPort') |
|
202
|
|
|
->willReturn(8000); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
$this->addEventListeners(); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Add post & pre serialize event listeners |
|
211
|
|
|
*/ |
|
212
|
|
|
protected function addEventListeners() |
|
213
|
|
|
{ |
|
214
|
|
|
$this->dispatcher = new EventDispatcher(); |
|
215
|
|
|
$this->addEvents($this->dispatcher); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Add post & pre serialize event to dispatcher |
|
220
|
|
|
* @param EventDispatcher $dispatcher |
|
221
|
|
|
*/ |
|
222
|
|
|
protected function addEvents(EventDispatcher $dispatcher = null) |
|
223
|
|
|
{ |
|
224
|
|
|
$listener = new JmsSerializeListener($this->requestContext, $this->annotationReader, $this->cacheManager, $this->vichStorage, [ |
|
225
|
|
|
'includeHost' => true, |
|
226
|
|
|
'vichUploaderSerialize' => true, |
|
227
|
|
|
]); |
|
228
|
|
|
|
|
229
|
|
|
$dispatcher->addListener(JmsEvents::PRE_SERIALIZE, [$listener, 'onPreSerialize']); |
|
|
|
|
|
|
230
|
|
|
$dispatcher->addListener(JmsEvents::POST_SERIALIZE, [$listener, 'onPostSerialize']); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Prepare mock of Liip cache manager |
|
235
|
|
|
*/ |
|
236
|
|
|
protected function generateCacheManager() |
|
237
|
|
|
{ |
|
238
|
|
|
$resolver = static::getMock('Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface'); |
|
239
|
|
|
$resolver |
|
240
|
|
|
->expects(static::any()) |
|
241
|
|
|
->method('isStored') |
|
242
|
|
|
->will(static::returnValue(true)) |
|
243
|
|
|
; |
|
244
|
|
|
$resolver |
|
245
|
|
|
->expects(static::any()) |
|
246
|
|
|
->method('resolve') |
|
247
|
|
|
->will(static::onConsecutiveCalls('http://a/path/to/an/image1.png', 'http://a/path/to/an/image2.png', 'http://a/path/to/an/image3.png')) |
|
248
|
|
|
; |
|
249
|
|
|
|
|
250
|
|
|
$config = static::getMock('Liip\ImagineBundle\Imagine\Filter\FilterConfiguration'); |
|
251
|
|
|
$config->expects(static::any()) |
|
252
|
|
|
->method('get') |
|
253
|
|
|
->with('thumb_filter') |
|
254
|
|
|
->will(static::returnValue(array( |
|
255
|
|
|
'size' => array(180, 180), |
|
256
|
|
|
'mode' => 'outbound', |
|
257
|
|
|
'cache' => null, |
|
258
|
|
|
))) |
|
259
|
|
|
; |
|
260
|
|
|
|
|
261
|
|
|
$router = static::getMock('Symfony\Component\Routing\RouterInterface'); |
|
262
|
|
|
$router->expects(static::never()) |
|
263
|
|
|
->method('generate') |
|
264
|
|
|
; |
|
265
|
|
|
|
|
266
|
|
|
$eventDispatcher = static::getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); |
|
267
|
|
|
|
|
268
|
|
|
/** @noinspection PhpParamsInspection */ |
|
269
|
|
|
$this->cacheManager = new CacheManager($config, $router, new Signer('secret'), $eventDispatcher); |
|
270
|
|
|
|
|
271
|
|
|
/** @noinspection PhpParamsInspection */ |
|
272
|
|
|
$this->cacheManager->addResolver('default', $resolver); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Generate vichStorage mock |
|
277
|
|
|
*/ |
|
278
|
|
|
protected function generateVichStorage() |
|
279
|
|
|
{ |
|
280
|
|
|
$this->vichStorage = $this->getMockBuilder('Vich\UploaderBundle\Storage\FileSystemStorage') |
|
281
|
|
|
->disableOriginalConstructor() |
|
282
|
|
|
->getMock(); |
|
283
|
|
|
$this->vichStorage->expects(static::any()) |
|
284
|
|
|
->method('resolveUri') |
|
285
|
|
|
->will(static::returnValue('/uploads/photo.jpg')); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Generate JMS context |
|
290
|
|
|
* @return DeserializationContext |
|
291
|
|
|
*/ |
|
292
|
|
|
protected function generateContext() |
|
293
|
|
|
{ |
|
294
|
|
|
$namingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy()); |
|
295
|
|
|
|
|
296
|
|
|
$context = DeserializationContext::create(); |
|
297
|
|
|
$factory = new MetadataFactory(new AnnotationDriver(new AnnotationReader())); |
|
298
|
|
|
$context->initialize('json', new JsonSerializationVisitor($namingStrategy), new GraphNavigator($factory, new HandlerRegistry(), new UnserializeObjectConstructor(), new EventDispatcher()), $factory); |
|
299
|
|
|
$this->context = $context; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* @param User|UserPictures $user |
|
304
|
|
|
* @return ObjectEvent |
|
305
|
|
|
*/ |
|
306
|
|
|
protected function dispatchEvents($user) |
|
307
|
|
|
{ |
|
308
|
|
|
/** @noinspection PhpParamsInspection */ |
|
309
|
|
|
$event = new ObjectEvent($this->context, $user, []); |
|
310
|
|
|
$this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, User::class, $this->context->getFormat(), $event); |
|
311
|
|
|
$this->dispatcher->dispatch(JmsEvents::POST_SERIALIZE, User::class, $this->context->getFormat(), $event); |
|
312
|
|
|
|
|
313
|
|
|
return $event; |
|
314
|
|
|
} |
|
315
|
|
|
} |
|
316
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: