Completed
Push — master ( 992272...53e0af )
by Artem
07:29
created

testSerializationOfTheProxyObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 15
loc 15
rs 9.4286
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
/*
3
 * This file is part of the FreshVichUploaderSerializationBundle
4
 *
5
 * (c) Artem Genvald <[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\JmsPreSerializeListener;
18
use Fresh\VichUploaderSerializationBundle\Tests\Fixtures;
19
use JMS\Serializer\DeserializationContext;
20
use JMS\Serializer\EventDispatcher\ObjectEvent;
21
use JMS\Serializer\EventDispatcher\Events as JmsEvents;
22
use Monolog\Logger;
23
use JMS\Serializer\EventDispatcher\EventDispatcherInterface;
24
use Symfony\Component\Routing\RequestContext;
25
use Vich\UploaderBundle\Storage\StorageInterface;
26
use JMS\Serializer\EventDispatcher\EventDispatcher;
27
28
/**
29
 * JmsPreSerializeListenerTest
30
 *
31
 * @author Artem Genvald <[email protected]>
32
 */
33
class JmsPreSerializeListenerTest extends \PHPUnit_Framework_TestCase
34
{
35
    /**
36
     * @var EventDispatcherInterface $dispatcher Dispatcher
37
     */
38
    private $dispatcher;
39
40
    /**
41
     * @var StorageInterface $storage Vich storage
42
     */
43
    private $storage;
44
45
    /**
46
     * @var RequestContext $requestContext Request context
47
     */
48
    private $requestContext;
49
50
    /**
51
     * @var CachedReader $annotationReader Cached annotation reader
52
     */
53
    private $annotationReader;
54
55
    /**
56
     * @var Logger $logger Logger
57
     */
58
    private $logger;
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function setUp()
64
    {
65
        AnnotationRegistry::registerLoader('class_exists');
66
67
        // Mock storage
68
        $this->storage = $this->getMockBuilder('Vich\UploaderBundle\Storage\FileSystemStorage')
69
                              ->disableOriginalConstructor()
70
                              ->getMock();
71
        $this->storage->expects($this->any())
72
                      ->method('resolveUri')
73
                      ->will($this->onConsecutiveCalls('/uploads/photo.jpg', '/uploads/cover.jpg'));
74
75
        $this->annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache());
76
77
        // Mock logger
78
        $this->logger = $this->getMockBuilder('Monolog\Logger')
79
                             ->disableOriginalConstructor()
80
                             ->setMethods(['debug'])
81
                             ->getMock();
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    protected function tearDown()
88
    {
89
        $this->dispatcher       = null;
90
        $this->storage          = null;
91
        $this->requestContext   = null;
92
        $this->annotationReader = null;
93
        $this->logger           = null;
94
    }
95
96
    /**
97
     * Test serialization with included host in the URI
98
     */
99 View Code Duplication
    public function testSerializationWithIncludedHost()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        $this->generateRequestContext();
102
103
        $user = (new Fixtures\UserA())
104
            ->setPhotoName('photo.jpg')
105
            ->setCoverName('cover.jpg');
106
107
        $context = DeserializationContext::create();
108
        $event = new ObjectEvent($context, $user, []);
109
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserA::class, $context->getFormat(), $event);
110
111
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
112
        $this->assertEquals('http://example.com/uploads/cover.jpg', $user->getCoverName());
113
    }
114
115
    /**
116
     * Test serialization with included http host and port in the URI
117
     */
118 View Code Duplication
    public function testSerializationWithIncludedHttpHostAndPort()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
    {
120
        $this->generateRequestContext(false, true);
121
122
        $user = (new Fixtures\UserA())
123
            ->setPhotoName('photo.jpg')
124
            ->setCoverName('cover.jpg');
125
126
        $context = DeserializationContext::create();
127
        $event = new ObjectEvent($context, $user, []);
128
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserA::class, $context->getFormat(), $event);
129
130
        $this->assertEquals('http://example.com:8000/uploads/photo.jpg', $user->getPhotoName());
131
        $this->assertEquals('http://example.com:8000/uploads/cover.jpg', $user->getCoverName());
132
    }
133
134
    /**
135
     * Test serialization with included https host and port in the URI
136
     */
137 View Code Duplication
    public function testSerializationWithIncludedHttpsHostAndPort()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
    {
139
        $this->generateRequestContext(true, true);
140
141
        $user = (new Fixtures\UserA())
142
            ->setPhotoName('photo.jpg')
143
            ->setCoverName('cover.jpg');
144
145
        $context = DeserializationContext::create();
146
        $event = new ObjectEvent($context, $user, []);
147
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserA::class, $context->getFormat(), $event);
148
149
        $this->assertEquals('https://example.com:8800/uploads/photo.jpg', $user->getPhotoName());
150
        $this->assertEquals('https://example.com:8800/uploads/cover.jpg', $user->getCoverName());
151
    }
152
153
    /**
154
     * Test serialization without included host in the URI
155
     */
156 View Code Duplication
    public function testSerializationWithoutIncludedHost()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
    {
158
        $this->generateRequestContext();
159
160
        $user = (new Fixtures\UserB())
161
            ->setPhotoName('photo.jpg')
162
            ->setCoverName('cover.jpg');
163
164
        $context = DeserializationContext::create();
165
        $event = new ObjectEvent($context, $user, []);
166
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserB::class, $context->getFormat(), $event);
167
168
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
169
        $this->assertEquals('/uploads/cover.jpg', $user->getCoverName());
170
    }
171
172
    /**
173
     * Test serialization without included host in the URI
174
     *
175
     * @expectedException \Fresh\VichUploaderSerializationBundle\Exception\IncompatibleUploadableAndSerializableFieldAnnotationException
176
     */
177 View Code Duplication
    public function testExceptionForIncompatibleAnnotations()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
    {
179
        $this->generateRequestContext();
180
181
        $user = (new Fixtures\UserC())
182
            ->setPhotoName('photo.jpg')
183
            ->setCoverName('cover.jpg');
184
185
        $context = DeserializationContext::create();
186
        $event = new ObjectEvent($context, $user, []);
187
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserC::class, $context->getFormat(), $event);
188
189
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
190
        $this->assertEquals('/uploads/cover.jpg', $user->getCoverName());
191
    }
192
193
    /**
194
     * Test serialization of the same object twice
195
     */
196
    public function testSerializationOfTheSameObjectTwice()
197
    {
198
        $this->generateRequestContext();
199
200
        $user1 = (new Fixtures\UserA())
201
            ->setPhotoName('photo.jpg')
202
            ->setCoverName('cover.jpg');
203
204
        $context = DeserializationContext::create();
205
        $event = new ObjectEvent($context, $user1, []);
206
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserA::class, $context->getFormat(), $event);
207
208
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user1->getPhotoName());
209
        $this->assertEquals('http://example.com/uploads/cover.jpg', $user1->getCoverName());
210
211
        $event = new ObjectEvent($context, $user1, []);
212
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserA::class, $context->getFormat(), $event);
213
214
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user1->getPhotoName());
215
        $this->assertEquals('http://example.com/uploads/cover.jpg', $user1->getCoverName());
216
    }
217
218
    /**
219
     * Test serialization of proxy object
220
     */
221 View Code Duplication
    public function testSerializationOfTheProxyObject()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
222
    {
223
        $this->generateRequestContext();
224
225
        $picture = new Fixtures\UserPictures();
226
        $picture->setPhotoName('photo.jpg')
227
            ->setCoverName('cover.jpg');
228
229
        $context = DeserializationContext::create();
230
        $event = new ObjectEvent($context, $picture, []);
231
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserA::class, $context->getFormat(), $event);
232
233
        $this->assertEquals('http://example.com/uploads/photo.jpg', $picture->getPhotoName());
234
        $this->assertEquals('http://example.com/uploads/cover.jpg', $picture->getCoverName());
235
    }
236
237
    /**
238
     *
239
     * @param bool $https
240
     * @param bool $port
241
     */
242
    protected function generateRequestContext($https = false, $port = false)
243
    {
244
245
        // Mock Request contest
246
        $this->requestContext = $this->getMockBuilder('Symfony\Component\Routing\RequestContext')
247
            ->disableOriginalConstructor()
248
            ->getMock();
249
250
        $scheme = ($https) ? 'https':'http';
251
252
        $this->requestContext->expects($this->any())
253
            ->method('getScheme')
254
            ->willReturn($scheme);
255
256
        $this->requestContext->expects($this->any())
257
            ->method('getHost')
258
            ->willReturn('example.com');
259
260
        if ($port) {
261
            if ($https) {
262
                $this->requestContext->expects($this->any())
263
                    ->method('getHttpsPort')
264
                    ->willReturn(8800);
265
            } else {
266
                $this->requestContext->expects($this->any())
267
                    ->method('getHttpPort')
268
                    ->willReturn(8000);
269
            }
270
        }
271
272
        $this->addEventListener();
273
    }
274
275
    /**
276
     * Add pre serialize event listener
277
     */
278
    protected function addEventListener()
279
    {
280
        $this->dispatcher = new EventDispatcher();
281
        $listener = new JmsPreSerializeListener($this->storage, $this->requestContext, $this->annotationReader, $this->logger);
282
283
        $this->dispatcher->addListener(JmsEvents::PRE_SERIALIZE, [$listener, 'onPreSerialize']);
284
    }
285
}
286