Completed
Pull Request — master (#3)
by
unknown
02:29
created

JmsPreSerializeListenerTest::addEventListener()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4286
cc 1
eloc 4
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
     *
98
     * @param bool $https
99
     * @param bool $port
100
     */
101
    protected function generateRequestContext($https = false, $port = false) {
102
103
        // Mock Request contest
104
        $this->requestContext = $this->getMockBuilder('Symfony\Component\Routing\RequestContext')
105
            ->disableOriginalConstructor()
106
            ->getMock();
107
108
        $scheme = ($https) ? 'https':'http';
109
110
        $this->requestContext->expects($this->any())
111
            ->method('getScheme')
112
            ->willReturn($scheme);
113
114
        $this->requestContext->expects($this->any())
115
            ->method('getHost')
116
            ->willReturn('example.com');
117
118
        if ($port) {
119
            if ($https) {
120
                $this->requestContext->expects($this->any())
121
                    ->method('getHttpsPort')
122
                    ->willReturn(8800);
123
            } else {
124
                $this->requestContext->expects($this->any())
125
                    ->method('getHttpPort')
126
                    ->willReturn(8000);
127
            }
128
        }
129
130
        $this->addEventListener();
131
    }
132
133
    /**
134
     * Add pre serialize event listener
135
     */
136
    protected function addEventListener() {
137
        $this->dispatcher = new EventDispatcher();
138
        $listener = new JmsPreSerializeListener($this->storage, $this->requestContext, $this->annotationReader, $this->logger);
139
140
        $this->dispatcher->addListener(JmsEvents::PRE_SERIALIZE, [$listener, 'onPreSerialize']);
141
    }
142
143
    /**
144
     * Test serialization with included host in the URI
145
     */
146 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...
147
    {
148
        $this->generateRequestContext();
149
150
        $user = (new Fixtures\UserA())
151
            ->setPhotoName('photo.jpg')
152
            ->setCoverName('cover.jpg');
153
154
        $context = DeserializationContext::create();
155
        $event = new ObjectEvent($context, $user, []);
156
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserA::class, $context->getFormat(), $event);
157
158
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
159
        $this->assertEquals('http://example.com/uploads/cover.jpg', $user->getCoverName());
160
    }
161
162
    /**
163
     * Test serialization with included http host and port in the URI
164
     */
165 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...
166
    {
167
        $this->generateRequestContext(false,true);
168
169
        $user = (new Fixtures\UserA())
170
            ->setPhotoName('photo.jpg')
171
            ->setCoverName('cover.jpg');
172
173
        $context = DeserializationContext::create();
174
        $event = new ObjectEvent($context, $user, []);
175
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserA::class, $context->getFormat(), $event);
176
177
        $this->assertEquals('http://example.com:8000/uploads/photo.jpg', $user->getPhotoName());
178
        $this->assertEquals('http://example.com:8000/uploads/cover.jpg', $user->getCoverName());
179
    }
180
181
    /**
182
     * Test serialization with included https host and port in the URI
183
     */
184 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...
185
    {
186
        $this->generateRequestContext(true,true);
187
188
        $user = (new Fixtures\UserA())
189
            ->setPhotoName('photo.jpg')
190
            ->setCoverName('cover.jpg');
191
192
        $context = DeserializationContext::create();
193
        $event = new ObjectEvent($context, $user, []);
194
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserA::class, $context->getFormat(), $event);
195
196
        $this->assertEquals('https://example.com:8800/uploads/photo.jpg', $user->getPhotoName());
197
        $this->assertEquals('https://example.com:8800/uploads/cover.jpg', $user->getCoverName());
198
    }
199
200
    /**
201
     * Test serialization without included host in the URI
202
     */
203 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...
204
    {
205
        $this->generateRequestContext();
206
207
        $user = (new Fixtures\UserB())
208
            ->setPhotoName('photo.jpg')
209
            ->setCoverName('cover.jpg');
210
211
        $context = DeserializationContext::create();
212
        $event = new ObjectEvent($context, $user, []);
213
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserB::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
    /**
220
     * Test serialization without included host in the URI
221
     *
222
     * @expectedException \Fresh\VichUploaderSerializationBundle\Exception\IncompatibleUploadableAndSerializableFieldAnnotationException
223
     */
224 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...
225
    {
226
        $this->generateRequestContext();
227
228
        $user = (new Fixtures\UserC())
229
            ->setPhotoName('photo.jpg')
230
            ->setCoverName('cover.jpg');
231
232
        $context = DeserializationContext::create();
233
        $event = new ObjectEvent($context, $user, []);
234
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserC::class, $context->getFormat(), $event);
235
236
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
237
        $this->assertEquals('/uploads/cover.jpg', $user->getCoverName());
238
    }
239
240
    /**
241
     * Test serialization of the same object twice
242
     */
243
    public function testSerializationOfTheSameObjectTwice()
244
    {
245
        $this->generateRequestContext();
246
247
        $user1 = (new Fixtures\UserA())
248
            ->setPhotoName('photo.jpg')
249
            ->setCoverName('cover.jpg');
250
251
        $context = DeserializationContext::create();
252
        $event = new ObjectEvent($context, $user1, []);
253
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserA::class, $context->getFormat(), $event);
254
255
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user1->getPhotoName());
256
        $this->assertEquals('http://example.com/uploads/cover.jpg', $user1->getCoverName());
257
258
        $event = new ObjectEvent($context, $user1, []);
259
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserA::class, $context->getFormat(), $event);
260
261
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user1->getPhotoName());
262
        $this->assertEquals('http://example.com/uploads/cover.jpg', $user1->getCoverName());
263
    }
264
}
265