1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the FreshVichUploaderSerializationBundle |
4
|
|
|
* |
5
|
|
|
* (c) Artem Henvald <[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 Fresh\VichUploaderSerializationBundle\Tests\EventListener; |
12
|
|
|
|
13
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
14
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
15
|
|
|
use Doctrine\Common\Annotations\CachedReader; |
16
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
17
|
|
|
use Fresh\VichUploaderSerializationBundle\EventListener\JmsSerializerSubscriber; |
18
|
|
|
use Fresh\VichUploaderSerializationBundle\Tests\Fixtures\UserA; |
19
|
|
|
use Fresh\VichUploaderSerializationBundle\Tests\Fixtures\UserB; |
20
|
|
|
use Fresh\VichUploaderSerializationBundle\Tests\Fixtures\UserC; |
21
|
|
|
use Fresh\VichUploaderSerializationBundle\Tests\Fixtures\UserPicture; |
22
|
|
|
use JMS\Serializer\DeserializationContext; |
23
|
|
|
use JMS\Serializer\EventDispatcher\EventDispatcher; |
24
|
|
|
use JMS\Serializer\EventDispatcher\EventDispatcherInterface; |
25
|
|
|
use JMS\Serializer\EventDispatcher\Events as JmsEvents; |
26
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
27
|
|
|
use JMS\Serializer\EventDispatcher\PreSerializeEvent; |
28
|
|
|
use Monolog\Logger; |
29
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
30
|
|
|
use Symfony\Component\Routing\RequestContext; |
31
|
|
|
use Vich\UploaderBundle\Storage\FileSystemStorage; |
32
|
|
|
use Vich\UploaderBundle\Storage\StorageInterface; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* JmsSerializerSubscriberTest. |
36
|
|
|
* |
37
|
|
|
* @author Artem Henvald <[email protected]> |
38
|
|
|
*/ |
39
|
|
|
class JmsSerializerSubscriberTest extends \PHPUnit_Framework_TestCase |
40
|
|
|
{ |
41
|
|
|
/** @var EventDispatcherInterface */ |
42
|
|
|
private $dispatcher; |
43
|
|
|
|
44
|
|
|
/** @var StorageInterface */ |
45
|
|
|
private $storage; |
46
|
|
|
|
47
|
|
|
/** @var RequestContext */ |
48
|
|
|
private $requestContext; |
49
|
|
|
|
50
|
|
|
/** @var CachedReader */ |
51
|
|
|
private $annotationReader; |
52
|
|
|
|
53
|
|
|
/** @var PropertyAccessor */ |
54
|
|
|
private $propertyAccessor; |
55
|
|
|
|
56
|
|
|
/** @var Logger */ |
57
|
|
|
private $logger; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
protected function setUp() |
63
|
|
|
{ |
64
|
|
|
AnnotationRegistry::registerLoader('class_exists'); |
|
|
|
|
65
|
|
|
|
66
|
|
|
$this->storage = $this->getMockBuilder(FileSystemStorage::class)->disableOriginalConstructor()->getMock(); |
67
|
|
|
$this->storage->expects($this->any()) |
68
|
|
|
->method('resolveUri') |
69
|
|
|
->will($this->onConsecutiveCalls('/uploads/photo.jpg', '/uploads/cover.jpg', '/uploads/photo.jpg', '/uploads/cover.jpg')); |
70
|
|
|
|
71
|
|
|
$this->propertyAccessor = new PropertyAccessor(); |
72
|
|
|
|
73
|
|
|
$this->annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache()); |
74
|
|
|
|
75
|
|
|
$this->logger = $this->getMockBuilder(Logger::class) |
76
|
|
|
->disableOriginalConstructor() |
77
|
|
|
->setMethods(['debug']) |
78
|
|
|
->getMock(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
protected function tearDown() |
85
|
|
|
{ |
86
|
|
|
$this->dispatcher = null; |
87
|
|
|
$this->storage = null; |
88
|
|
|
$this->requestContext = null; |
89
|
|
|
$this->annotationReader = null; |
90
|
|
|
$this->logger = null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
View Code Duplication |
public function testSerializationWithIncludedHost() |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$this->generateRequestContext(); |
96
|
|
|
|
97
|
|
|
$user = (new UserA()) |
98
|
|
|
->setPhotoName('photo.jpg') |
99
|
|
|
->setCoverName('cover.jpg'); |
100
|
|
|
|
101
|
|
|
$context = DeserializationContext::create(); |
102
|
|
|
$event = new PreSerializeEvent($context, $user, []); |
103
|
|
|
$this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserA::class, $context->getFormat(), $event); |
104
|
|
|
|
105
|
|
|
$this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName()); |
106
|
|
|
$this->assertEquals('http://example.com/uploads/cover.jpg', $user->getCoverName()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testPostSerializationEvent() |
110
|
|
|
{ |
111
|
|
|
$this->generateRequestContext(); |
112
|
|
|
|
113
|
|
|
$user = (new UserA()) |
114
|
|
|
->setPhotoName('photo.jpg') |
115
|
|
|
->setCoverName('cover.jpg'); |
116
|
|
|
|
117
|
|
|
$context = DeserializationContext::create(); |
118
|
|
|
$event = new PreSerializeEvent($context, $user, []); |
119
|
|
|
$this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserA::class, $context->getFormat(), $event); |
120
|
|
|
|
121
|
|
|
$this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName()); |
122
|
|
|
$this->assertEquals('http://example.com/uploads/cover.jpg', $user->getCoverName()); |
123
|
|
|
|
124
|
|
|
$event = new ObjectEvent($context, $user, []); |
125
|
|
|
$this->dispatcher->dispatch(JmsEvents::POST_SERIALIZE, UserA::class, $context->getFormat(), $event); |
126
|
|
|
|
127
|
|
|
$this->assertNotEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName()); |
128
|
|
|
$this->assertNotEquals('http://example.com/uploads/cover.jpg', $user->getCoverName()); |
129
|
|
|
$this->assertEquals('photo.jpg', $user->getPhotoName()); |
130
|
|
|
$this->assertEquals('cover.jpg', $user->getCoverName()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
View Code Duplication |
public function testSerializationWithIncludedHttpHostAndPort() |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
$this->generateRequestContext(false, true); |
136
|
|
|
|
137
|
|
|
$user = (new UserA()) |
138
|
|
|
->setPhotoName('photo.jpg') |
139
|
|
|
->setCoverName('cover.jpg'); |
140
|
|
|
|
141
|
|
|
$context = DeserializationContext::create(); |
142
|
|
|
$event = new PreSerializeEvent($context, $user, []); |
143
|
|
|
$this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserA::class, $context->getFormat(), $event); |
144
|
|
|
|
145
|
|
|
$this->assertEquals('http://example.com:8000/uploads/photo.jpg', $user->getPhotoName()); |
146
|
|
|
$this->assertEquals('http://example.com:8000/uploads/cover.jpg', $user->getCoverName()); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
View Code Duplication |
public function testSerializationWithIncludedHttpsHostAndPort() |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$this->generateRequestContext(true, true); |
152
|
|
|
|
153
|
|
|
$user = (new UserA()) |
154
|
|
|
->setPhotoName('photo.jpg') |
155
|
|
|
->setCoverName('cover.jpg'); |
156
|
|
|
|
157
|
|
|
$context = DeserializationContext::create(); |
158
|
|
|
$event = new PreSerializeEvent($context, $user, []); |
159
|
|
|
$this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserA::class, $context->getFormat(), $event); |
160
|
|
|
|
161
|
|
|
$this->assertEquals('https://example.com:8800/uploads/photo.jpg', $user->getPhotoName()); |
162
|
|
|
$this->assertEquals('https://example.com:8800/uploads/cover.jpg', $user->getCoverName()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
View Code Duplication |
public function testSerializationWithoutIncludedHost() |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
$this->generateRequestContext(); |
168
|
|
|
|
169
|
|
|
$user = (new UserB()) |
170
|
|
|
->setPhotoName('photo.jpg') |
171
|
|
|
->setCoverName('cover.jpg'); |
172
|
|
|
|
173
|
|
|
$context = DeserializationContext::create(); |
174
|
|
|
$event = new PreSerializeEvent($context, $user, []); |
175
|
|
|
$this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserB::class, $context->getFormat(), $event); |
176
|
|
|
|
177
|
|
|
$this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName()); |
178
|
|
|
$this->assertEquals('/uploads/cover.jpg', $user->getCoverName()); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @expectedException \Fresh\VichUploaderSerializationBundle\Exception\IncompatibleUploadableAndSerializableFieldAnnotationException |
183
|
|
|
*/ |
184
|
|
View Code Duplication |
public function testExceptionForIncompatibleAnnotations() |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
$this->generateRequestContext(); |
187
|
|
|
|
188
|
|
|
$user = (new UserC()) |
189
|
|
|
->setPhotoName('photo.jpg') |
190
|
|
|
->setCoverName('cover.jpg'); |
191
|
|
|
|
192
|
|
|
$context = DeserializationContext::create(); |
193
|
|
|
$event = new PreSerializeEvent($context, $user, []); |
194
|
|
|
$this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserC::class, $context->getFormat(), $event); |
195
|
|
|
|
196
|
|
|
$this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName()); |
197
|
|
|
$this->assertEquals('/uploads/cover.jpg', $user->getCoverName()); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function testSerializationOfTheSameObjectTwice() |
201
|
|
|
{ |
202
|
|
|
$this->generateRequestContext(); |
203
|
|
|
|
204
|
|
|
$user = (new UserA()) |
205
|
|
|
->setPhotoName('photo.jpg') |
206
|
|
|
->setCoverName('cover.jpg'); |
207
|
|
|
|
208
|
|
|
$context = DeserializationContext::create(); |
209
|
|
|
$event = new PreSerializeEvent($context, $user, []); |
210
|
|
|
$this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserA::class, $context->getFormat(), $event); |
211
|
|
|
|
212
|
|
|
$this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName()); |
213
|
|
|
$this->assertEquals('http://example.com/uploads/cover.jpg', $user->getCoverName()); |
214
|
|
|
|
215
|
|
|
$event = new PreSerializeEvent($context, $user, []); |
216
|
|
|
$this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserA::class, $context->getFormat(), $event); |
217
|
|
|
|
218
|
|
|
$this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName()); |
219
|
|
|
$this->assertEquals('http://example.com/uploads/cover.jpg', $user->getCoverName()); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function testDeserializationEventOfTheSameObjectTwice() |
223
|
|
|
{ |
224
|
|
|
$this->generateRequestContext(); |
225
|
|
|
|
226
|
|
|
$user = (new UserA()) |
227
|
|
|
->setPhotoName('photo.jpg') |
228
|
|
|
->setCoverName('cover.jpg'); |
229
|
|
|
|
230
|
|
|
$context = DeserializationContext::create(); |
231
|
|
|
$event = new PreSerializeEvent($context, $user, []); |
232
|
|
|
$this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserA::class, $context->getFormat(), $event); |
233
|
|
|
|
234
|
|
|
$this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName()); |
235
|
|
|
$this->assertEquals('http://example.com/uploads/cover.jpg', $user->getCoverName()); |
236
|
|
|
|
237
|
|
|
$event = new ObjectEvent($context, $user, []); |
238
|
|
|
$this->dispatcher->dispatch(JmsEvents::POST_SERIALIZE, UserA::class, $context->getFormat(), $event); |
239
|
|
|
|
240
|
|
|
$this->assertNotEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName()); |
241
|
|
|
$this->assertNotEquals('http://example.com/uploads/cover.jpg', $user->getCoverName()); |
242
|
|
|
$this->assertEquals('photo.jpg', $user->getPhotoName()); |
243
|
|
|
$this->assertEquals('cover.jpg', $user->getCoverName()); |
244
|
|
|
|
245
|
|
|
$event = new PreSerializeEvent($context, $user, []); |
246
|
|
|
$this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserA::class, $context->getFormat(), $event); |
247
|
|
|
|
248
|
|
|
$this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName()); |
249
|
|
|
$this->assertEquals('http://example.com/uploads/cover.jpg', $user->getCoverName()); |
250
|
|
|
|
251
|
|
|
$event = new ObjectEvent($context, $user, []); |
252
|
|
|
$this->dispatcher->dispatch(JmsEvents::POST_SERIALIZE, UserA::class, $context->getFormat(), $event); |
253
|
|
|
|
254
|
|
|
$this->assertNotEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName()); |
255
|
|
|
$this->assertNotEquals('http://example.com/uploads/cover.jpg', $user->getCoverName()); |
256
|
|
|
$this->assertEquals('photo.jpg', $user->getPhotoName()); |
257
|
|
|
$this->assertEquals('cover.jpg', $user->getCoverName()); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
View Code Duplication |
public function testSerializationOfTheProxyObject() |
|
|
|
|
261
|
|
|
{ |
262
|
|
|
$this->generateRequestContext(); |
263
|
|
|
|
264
|
|
|
$picture = new UserPicture(); |
265
|
|
|
$picture->setPhotoName('photo.jpg') |
266
|
|
|
->setCoverName('cover.jpg'); |
267
|
|
|
|
268
|
|
|
$context = DeserializationContext::create(); |
269
|
|
|
$event = new PreSerializeEvent($context, $picture, []); |
270
|
|
|
$this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserA::class, $context->getFormat(), $event); |
271
|
|
|
|
272
|
|
|
$this->assertEquals('http://example.com/uploads/photo.jpg', $picture->getPhotoName()); |
273
|
|
|
$this->assertEquals('http://example.com/uploads/cover.jpg', $picture->getCoverName()); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
protected function generateRequestContext($https = false, $port = false) |
277
|
|
|
{ |
278
|
|
|
$this->requestContext = $this->getMockBuilder(RequestContext::class) |
279
|
|
|
->disableOriginalConstructor() |
280
|
|
|
->getMock(); |
281
|
|
|
|
282
|
|
|
$scheme = $https ? 'https' : 'http'; |
283
|
|
|
|
284
|
|
|
$this->requestContext->expects($this->any())->method('getScheme')->willReturn($scheme); |
285
|
|
|
$this->requestContext->expects($this->any())->method('getHost')->willReturn('example.com'); |
286
|
|
|
|
287
|
|
|
if ($port) { |
288
|
|
|
if ($https) { |
289
|
|
|
$this->requestContext->expects($this->any())->method('getHttpsPort')->willReturn(8800); |
290
|
|
|
} else { |
291
|
|
|
$this->requestContext->expects($this->any())->method('getHttpPort')->willReturn(8000); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
$this->addJmsSerializerSubscriber(); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
protected function addJmsSerializerSubscriber() |
299
|
|
|
{ |
300
|
|
|
$this->dispatcher = new EventDispatcher(); |
301
|
|
|
$this->dispatcher->addSubscriber(new JmsSerializerSubscriber( |
302
|
|
|
$this->storage, |
303
|
|
|
$this->requestContext, |
304
|
|
|
$this->annotationReader, |
305
|
|
|
$this->propertyAccessor, |
306
|
|
|
$this->logger |
307
|
|
|
)); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
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.