Completed
Pull Request — master (#15)
by Artem
05:22 queued 04:14
created

testSerializationOfTheSameObjectTwice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 1
eloc 14
nc 1
nop 0
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;
19
use JMS\Serializer\DeserializationContext;
20
use JMS\Serializer\EventDispatcher\EventDispatcher;
21
use JMS\Serializer\EventDispatcher\EventDispatcherInterface;
22
use JMS\Serializer\EventDispatcher\Events as JmsEvents;
23
use JMS\Serializer\EventDispatcher\PreSerializeEvent;
24
use Monolog\Logger;
25
use Symfony\Component\PropertyAccess\PropertyAccessor;
26
use Symfony\Component\Routing\RequestContext;
27
use Vich\UploaderBundle\Storage\FileSystemStorage;
28
use Vich\UploaderBundle\Storage\StorageInterface;
29
30
/**
31
 * JmsSerializerSubscriberTest.
32
 *
33
 * @author Artem Henvald <[email protected]>
34
 */
35
class JmsSerializerSubscriberTest extends \PHPUnit_Framework_TestCase
36
{
37
    /** @var EventDispatcherInterface */
38
    private $dispatcher;
39
40
    /** @var StorageInterface */
41
    private $storage;
42
43
    /** @var RequestContext */
44
    private $requestContext;
45
46
    /** @var CachedReader */
47
    private $annotationReader;
48
49
    /** @var PropertyAccessor */
50
    private $propertyAccessor;
51
52
    /** @var Logger */
53
    private $logger;
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    protected function setUp()
59
    {
60
        AnnotationRegistry::registerLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\Common\Annotati...istry::registerLoader() has been deprecated with message: this method is deprecated and will be removed in doctrine/annotations 2.0 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists')

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.

Loading history...
61
62
        $this->storage = $this->getMockBuilder(FileSystemStorage::class)
63
                              ->disableOriginalConstructor()
64
                              ->getMock();
65
        $this->storage->expects($this->any())
66
                      ->method('resolveUri')
67
                      ->will($this->onConsecutiveCalls('/uploads/photo.jpg', '/uploads/cover.jpg'));
68
69
        $this->propertyAccessor = $this->getMockBuilder(PropertyAccessor::class)
70
                                       ->disableOriginalConstructor()
71
                                       ->getMock();
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()
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...
94
    {
95
        $this->generateRequestContext();
96
97
        $user = (new Fixtures\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, Fixtures\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 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...
110
    {
111
        $this->generateRequestContext(false, true);
112
113
        $user = (new Fixtures\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, Fixtures\UserA::class, $context->getFormat(), $event);
120
121
        $this->assertEquals('http://example.com:8000/uploads/photo.jpg', $user->getPhotoName());
122
        $this->assertEquals('http://example.com:8000/uploads/cover.jpg', $user->getCoverName());
123
    }
124
125 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...
126
    {
127
        $this->generateRequestContext(true, true);
128
129
        $user = (new Fixtures\UserA())
130
            ->setPhotoName('photo.jpg')
131
            ->setCoverName('cover.jpg');
132
133
        $context = DeserializationContext::create();
134
        $event = new PreSerializeEvent($context, $user, []);
135
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserA::class, $context->getFormat(), $event);
136
137
        $this->assertEquals('https://example.com:8800/uploads/photo.jpg', $user->getPhotoName());
138
        $this->assertEquals('https://example.com:8800/uploads/cover.jpg', $user->getCoverName());
139
    }
140
141 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...
142
    {
143
        $this->generateRequestContext();
144
145
        $user = (new Fixtures\UserB())
146
            ->setPhotoName('photo.jpg')
147
            ->setCoverName('cover.jpg');
148
149
        $context = DeserializationContext::create();
150
        $event = new PreSerializeEvent($context, $user, []);
151
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserB::class, $context->getFormat(), $event);
152
153
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
154
        $this->assertEquals('/uploads/cover.jpg', $user->getCoverName());
155
    }
156
157
    /**
158
     * @expectedException \Fresh\VichUploaderSerializationBundle\Exception\IncompatibleUploadableAndSerializableFieldAnnotationException
159
     */
160 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...
161
    {
162
        $this->generateRequestContext();
163
164
        $user = (new Fixtures\UserC())
165
            ->setPhotoName('photo.jpg')
166
            ->setCoverName('cover.jpg');
167
168
        $context = DeserializationContext::create();
169
        $event = new PreSerializeEvent($context, $user, []);
170
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserC::class, $context->getFormat(), $event);
171
172
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
173
        $this->assertEquals('/uploads/cover.jpg', $user->getCoverName());
174
    }
175
176
    public function testSerializationOfTheSameObjectTwice()
177
    {
178
        $this->generateRequestContext();
179
180
        $user = (new Fixtures\UserA())
181
            ->setPhotoName('photo.jpg')
182
            ->setCoverName('cover.jpg');
183
184
        $context = DeserializationContext::create();
185
        $event = new PreSerializeEvent($context, $user, []);
186
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserA::class, $context->getFormat(), $event);
187
188
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
189
        $this->assertEquals('http://example.com/uploads/cover.jpg', $user->getCoverName());
190
191
        $event = new PreSerializeEvent($context, $user, []);
192
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserA::class, $context->getFormat(), $event);
193
194
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
195
        $this->assertEquals('http://example.com/uploads/cover.jpg', $user->getCoverName());
196
    }
197
198 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...
199
    {
200
        $this->generateRequestContext();
201
202
        $picture = new Fixtures\UserPicture();
203
        $picture->setPhotoName('photo.jpg')
204
                ->setCoverName('cover.jpg');
205
206
        $context = DeserializationContext::create();
207
        $event   = new PreSerializeEvent($context, $picture, []);
208
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserA::class, $context->getFormat(), $event);
209
210
        $this->assertEquals('http://example.com/uploads/photo.jpg', $picture->getPhotoName());
211
        $this->assertEquals('http://example.com/uploads/cover.jpg', $picture->getCoverName());
212
    }
213
214
    protected function generateRequestContext($https = false, $port = false)
215
    {
216
        $this->requestContext = $this->getMockBuilder(RequestContext::class)
217
                                     ->disableOriginalConstructor()
218
                                     ->getMock();
219
220
        $scheme = $https ? 'https' : 'http';
221
222
        $this->requestContext->expects($this->any())
223
                             ->method('getScheme')
224
                             ->willReturn($scheme);
225
226
        $this->requestContext->expects($this->any())
227
                             ->method('getHost')
228
                             ->willReturn('example.com');
229
230
        if ($port) {
231
            if ($https) {
232
                $this->requestContext->expects($this->any())
233
                                     ->method('getHttpsPort')
234
                                     ->willReturn(8800);
235
            } else {
236
                $this->requestContext->expects($this->any())
237
                                     ->method('getHttpPort')
238
                                     ->willReturn(8000);
239
            }
240
        }
241
242
        $this->addEventListener();
243
    }
244
245
    protected function addEventListener()
246
    {
247
        $this->dispatcher = new EventDispatcher();
248
        $subscriber = new JmsSerializerSubscriber(
249
            $this->storage,
250
            $this->requestContext,
251
            $this->annotationReader,
252
            $this->propertyAccessor,
253
            $this->logger
254
        );
255
256
        $this->dispatcher->addSubscriber($subscriber);
257
    }
258
}
259