Completed
Push — master ( 232324...f3371f )
by Artem
12s
created

JmsSerializerSubscriberTest   B

Complexity

Total Complexity 16

Size/Duplication

Total Lines 271
Duplicated Lines 33.21 %

Coupling/Cohesion

Components 1
Dependencies 17

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 16
c 2
b 0
f 0
lcom 1
cbo 17
dl 90
loc 271
rs 7.8571

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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->propertyAccessor = null;
91
        $this->logger = null;
92
    }
93
94
    public function testSerializationWithIncludedHost()
95
    {
96
        $this->generateRequestContext();
97
98
        $user = (new UserA())
99
            ->setPhotoName('photo.jpg')
100
            ->setCoverName('cover.jpg');
101
102
        $context = DeserializationContext::create();
103
        $event = new PreSerializeEvent($context, $user, []);
104
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserA::class, $context->getFormat(), $event);
105
106
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
107
        $this->assertEquals('http://example.com/uploads/cover.jpg', $user->getCoverName());
108
    }
109
110
    public function testPostSerializationEvent()
111
    {
112
        $this->generateRequestContext();
113
114
        $user = (new UserA())
115
            ->setPhotoName('photo.jpg')
116
            ->setCoverName('cover.jpg');
117
118
        $context = DeserializationContext::create();
119
        $event = new PreSerializeEvent($context, $user, []);
120
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserA::class, $context->getFormat(), $event);
121
122
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
123
        $this->assertEquals('http://example.com/uploads/cover.jpg', $user->getCoverName());
124
125
        $event = new ObjectEvent($context, $user, []);
126
        $this->dispatcher->dispatch(JmsEvents::POST_SERIALIZE, UserA::class, $context->getFormat(), $event);
127
128
        $this->assertNotEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
129
        $this->assertNotEquals('http://example.com/uploads/cover.jpg', $user->getCoverName());
130
        $this->assertEquals('photo.jpg', $user->getPhotoName());
131
        $this->assertEquals('cover.jpg', $user->getCoverName());
132
    }
133
134
    public function testPostSerializationEventWithoutPreviousSerialization()
135
    {
136
        $this->generateRequestContext();
137
138
        $user = (new UserA())
139
            ->setPhotoName('photo.jpg')
140
            ->setCoverName('cover.jpg');
141
142
        $context = DeserializationContext::create();
143
        $event = new ObjectEvent($context, $user, []);
144
        $this->dispatcher->dispatch(JmsEvents::POST_SERIALIZE, UserA::class, $context->getFormat(), $event);
145
146
        $this->assertNotEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
147
        $this->assertNotEquals('http://example.com/uploads/cover.jpg', $user->getCoverName());
148
        $this->assertEquals('photo.jpg', $user->getPhotoName());
149
        $this->assertEquals('cover.jpg', $user->getCoverName());
150
    }
151
152
    public function testSerializationWithIncludedHttpHostAndPort()
153
    {
154
        $this->generateRequestContext(false, true);
155
156
        $user = (new UserA())
157
            ->setPhotoName('photo.jpg')
158
            ->setCoverName('cover.jpg');
159
160
        $context = DeserializationContext::create();
161
        $event = new PreSerializeEvent($context, $user, []);
162
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserA::class, $context->getFormat(), $event);
163
164
        $this->assertEquals('http://example.com:8000/uploads/photo.jpg', $user->getPhotoName());
165
        $this->assertEquals('http://example.com:8000/uploads/cover.jpg', $user->getCoverName());
166
    }
167
168
    public function testSerializationWithIncludedHttpsHostAndPort()
169
    {
170
        $this->generateRequestContext(true, true);
171
172
        $user = (new UserA())
173
            ->setPhotoName('photo.jpg')
174
            ->setCoverName('cover.jpg');
175
176
        $context = DeserializationContext::create();
177
        $event = new PreSerializeEvent($context, $user, []);
178
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserA::class, $context->getFormat(), $event);
179
180
        $this->assertEquals('https://example.com:8800/uploads/photo.jpg', $user->getPhotoName());
181
        $this->assertEquals('https://example.com:8800/uploads/cover.jpg', $user->getCoverName());
182
    }
183
184
    public function testSerializationWithoutIncludedHost()
185
    {
186
        $this->generateRequestContext();
187
188
        $user = (new UserB())
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, UserB::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
    /**
201
     * @expectedException \Fresh\VichUploaderSerializationBundle\Exception\IncompatibleUploadableAndSerializableFieldAnnotationException
202
     */
203
    public function testExceptionForIncompatibleAnnotations()
204
    {
205
        $this->generateRequestContext();
206
207
        $user = (new UserC())
208
            ->setPhotoName('photo.jpg')
209
            ->setCoverName('cover.jpg');
210
211
        $context = DeserializationContext::create();
212
        $event = new PreSerializeEvent($context, $user, []);
213
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserC::class, $context->getFormat(), $event);
214
215
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
216
        $this->assertEquals('/uploads/cover.jpg', $user->getCoverName());
217
    }
218
219
    public function testSerializationOfTheSameObjectTwice()
220
    {
221
        $this->generateRequestContext();
222
223
        $user = (new UserA())
224
            ->setPhotoName('photo.jpg')
225
            ->setCoverName('cover.jpg');
226
227
        $context = DeserializationContext::create();
228
        $event = new PreSerializeEvent($context, $user, []);
229
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserA::class, $context->getFormat(), $event);
230
231
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
232
        $this->assertEquals('http://example.com/uploads/cover.jpg', $user->getCoverName());
233
234
        $event = new PreSerializeEvent($context, $user, []);
235
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserA::class, $context->getFormat(), $event);
236
237
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
238
        $this->assertEquals('http://example.com/uploads/cover.jpg', $user->getCoverName());
239
    }
240
241
    public function testDeserializationEventOfTheSameObjectTwice()
242
    {
243
        $this->generateRequestContext();
244
245
        $user = (new UserA())
246
            ->setPhotoName('photo.jpg')
247
            ->setCoverName('cover.jpg');
248
249
        $context = DeserializationContext::create();
250
        $event = new PreSerializeEvent($context, $user, []);
251
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserA::class, $context->getFormat(), $event);
252
253
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
254
        $this->assertEquals('http://example.com/uploads/cover.jpg', $user->getCoverName());
255
256
        $event = new ObjectEvent($context, $user, []);
257
        $this->dispatcher->dispatch(JmsEvents::POST_SERIALIZE, UserA::class, $context->getFormat(), $event);
258
259
        $this->assertNotEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
260
        $this->assertNotEquals('http://example.com/uploads/cover.jpg', $user->getCoverName());
261
        $this->assertEquals('photo.jpg', $user->getPhotoName());
262
        $this->assertEquals('cover.jpg', $user->getCoverName());
263
264
        $event = new PreSerializeEvent($context, $user, []);
265
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserA::class, $context->getFormat(), $event);
266
267
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
268
        $this->assertEquals('http://example.com/uploads/cover.jpg', $user->getCoverName());
269
270
        $event = new ObjectEvent($context, $user, []);
271
        $this->dispatcher->dispatch(JmsEvents::POST_SERIALIZE, UserA::class, $context->getFormat(), $event);
272
273
        $this->assertNotEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
274
        $this->assertNotEquals('http://example.com/uploads/cover.jpg', $user->getCoverName());
275
        $this->assertEquals('photo.jpg', $user->getPhotoName());
276
        $this->assertEquals('cover.jpg', $user->getCoverName());
277
    }
278
279
    public function testSerializationOfTheProxyObject()
280
    {
281
        $this->generateRequestContext();
282
283
        $picture = new UserPicture();
284
        $picture->setPhotoName('photo.jpg')
285
                ->setCoverName('cover.jpg');
286
287
        $context = DeserializationContext::create();
288
        $event = new PreSerializeEvent($context, $picture, []);
289
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, UserPicture::class, $context->getFormat(), $event);
290
291
        $this->assertEquals('http://example.com/uploads/photo.jpg', $picture->getPhotoName());
292
        $this->assertEquals('http://example.com/uploads/cover.jpg', $picture->getCoverName());
293
294
        $picture->setStatus(false);
295
        $event = new ObjectEvent($context, $picture, []);
296
        $this->dispatcher->dispatch(JmsEvents::POST_SERIALIZE, UserPicture::class, $context->getFormat(), $event);
297
298
        $this->assertNotEquals('http://example.com/uploads/photo.jpg', $picture->getPhotoName());
299
        $this->assertNotEquals('http://example.com/uploads/cover.jpg', $picture->getCoverName());
300
        $this->assertEquals('photo.jpg', $picture->getPhotoName());
301
        $this->assertEquals('cover.jpg', $picture->getCoverName());
302
    }
303
304
    protected function generateRequestContext($https = false, $port = false)
305
    {
306
        $this->requestContext = $this->getMockBuilder(RequestContext::class)
307
                                     ->disableOriginalConstructor()
308
                                     ->getMock();
309
310
        $scheme = $https ? 'https' : 'http';
311
312
        $this->requestContext->expects($this->any())->method('getScheme')->willReturn($scheme);
313
        $this->requestContext->expects($this->any())->method('getHost')->willReturn('example.com');
314
315
        if ($port) {
316
            if ($https) {
317
                $this->requestContext->expects($this->any())->method('getHttpsPort')->willReturn(8800);
318
            } else {
319
                $this->requestContext->expects($this->any())->method('getHttpPort')->willReturn(8000);
320
            }
321
        }
322
323
        $this->addJmsSerializerSubscriber();
324
    }
325
326
    protected function addJmsSerializerSubscriber()
327
    {
328
        $this->dispatcher = new EventDispatcher();
329
        $this->dispatcher->addSubscriber(new JmsSerializerSubscriber(
330
            $this->storage,
331
            $this->requestContext,
332
            $this->annotationReader,
333
            $this->propertyAccessor,
334
            $this->logger
335
        ));
336
    }
337
}
338