Completed
Push — master ( fa6cfe...3512bb )
by Artem
19:15
created

JmsPreSerializeListenerTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 155
Duplicated Lines 25.16 %

Coupling/Cohesion

Components 1
Dependencies 14

Importance

Changes 4
Bugs 2 Features 2
Metric Value
wmc 6
c 4
b 2
f 2
lcom 1
cbo 14
dl 39
loc 155
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 36 1
A tearDown() 0 8 1
A testSerializationWithIncludedHost() 13 13 1
A testSerializationWithoutIncludedHost() 13 13 1
A testExceptionForIncompatibleAnnotations() 13 13 1
A testSerializationOfTheSameObjectTwice() 0 19 1

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 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
        // Mock Request contest
76
        $this->requestContext = $this->getMockBuilder('Symfony\Component\Routing\RequestContext')
77
                                     ->disableOriginalConstructor()
78
                                     ->getMock();
79
        $this->requestContext->expects($this->any())
80
                             ->method('getScheme')
81
                             ->willReturn('http');
82
        $this->requestContext->expects($this->any())
83
                             ->method('getHost')
84
                             ->willReturn('example.com');
85
86
        $this->annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache());
87
88
        // Mock logger
89
        $this->logger = $this->getMockBuilder('Monolog\Logger')
90
                             ->disableOriginalConstructor()
91
                             ->setMethods(['debug'])
92
                             ->getMock();
93
94
        $this->dispatcher = new EventDispatcher();
95
        $listener = new JmsPreSerializeListener($this->storage, $this->requestContext, $this->annotationReader, $this->logger);
96
97
        $this->dispatcher->addListener(JmsEvents::PRE_SERIALIZE, [$listener, 'onPreSerialize']);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    protected function tearDown()
104
    {
105
        $this->dispatcher       = null;
106
        $this->storage          = null;
107
        $this->requestContext   = null;
108
        $this->annotationReader = null;
109
        $this->logger           = null;
110
    }
111
112
    /**
113
     * Test serialization with included host in the URI
114
     */
115 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...
116
    {
117
        $user = (new Fixtures\UserA())
118
            ->setPhotoName('photo.jpg')
119
            ->setCoverName('cover.jpg');
120
121
        $context = DeserializationContext::create();
122
        $event = new ObjectEvent($context, $user, []);
123
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserA::class, $context->getFormat(), $event);
124
125
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
126
        $this->assertEquals('http://example.com/uploads/cover.jpg', $user->getCoverName());
127
    }
128
129
    /**
130
     * Test serialization without included host in the URI
131
     */
132 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...
133
    {
134
        $user = (new Fixtures\UserB())
135
            ->setPhotoName('photo.jpg')
136
            ->setCoverName('cover.jpg');
137
138
        $context = DeserializationContext::create();
139
        $event = new ObjectEvent($context, $user, []);
140
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserB::class, $context->getFormat(), $event);
141
142
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
143
        $this->assertEquals('/uploads/cover.jpg', $user->getCoverName());
144
    }
145
146
    /**
147
     * Test serialization without included host in the URI
148
     *
149
     * @expectedException \Fresh\VichUploaderSerializationBundle\Exception\IncompatibleUploadableAndSerializableFieldAnnotationException
150
     */
151 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...
152
    {
153
        $user = (new Fixtures\UserC())
154
            ->setPhotoName('photo.jpg')
155
            ->setCoverName('cover.jpg');
156
157
        $context = DeserializationContext::create();
158
        $event = new ObjectEvent($context, $user, []);
159
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserC::class, $context->getFormat(), $event);
160
161
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user->getPhotoName());
162
        $this->assertEquals('/uploads/cover.jpg', $user->getCoverName());
163
    }
164
165
    /**
166
     * Test serialization of the same object twice
167
     */
168
    public function testSerializationOfTheSameObjectTwice()
169
    {
170
        $user1 = (new Fixtures\UserA())
171
            ->setPhotoName('photo.jpg')
172
            ->setCoverName('cover.jpg');
173
174
        $context = DeserializationContext::create();
175
        $event = new ObjectEvent($context, $user1, []);
176
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserA::class, $context->getFormat(), $event);
177
178
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user1->getPhotoName());
179
        $this->assertEquals('http://example.com/uploads/cover.jpg', $user1->getCoverName());
180
181
        $event = new ObjectEvent($context, $user1, []);
182
        $this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, Fixtures\UserA::class, $context->getFormat(), $event);
183
184
        $this->assertEquals('http://example.com/uploads/photo.jpg', $user1->getPhotoName());
185
        $this->assertEquals('http://example.com/uploads/cover.jpg', $user1->getCoverName());
186
    }
187
}
188