Completed
Pull Request — master (#1146)
by Giovanni
06:29
created

ObjectSerializerPersisterTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 103
Duplicated Lines 40.78 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 42
loc 103
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testThatCanReplaceObject() 0 19 1
A testThatCanInsertObject() 21 21 1
A testThatCanDeleteObject() 21 21 1
B testThatCanInsertManyObjects() 0 25 1
A getTransformer() 0 7 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
namespace FOS\ElasticaBundle\Tests\ObjectSerializerPersister;
4
5
use FOS\ElasticaBundle\Persister\ObjectSerializerPersister;
6
use FOS\ElasticaBundle\Transformer\ModelToElasticaIdentifierTransformer;
7
use Symfony\Component\EventDispatcher\EventDispatcher;
8
use Symfony\Component\PropertyAccess\PropertyAccess;
9
10
class POPO
11
{
12
    public $id   = 123;
13
    public $name = 'popoName';
14
15
    public function getId()
16
    {
17
        return $this->id;
18
    }
19
20
    public function getName()
21
    {
22
        return $this->name;
23
    }
24
}
25
26
class ObjectSerializerPersisterTest extends \PHPUnit_Framework_TestCase
27
{
28
    public function testThatCanReplaceObject()
29
    {
30
        $transformer = $this->getTransformer();
31
32
        /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica\Type */
33
        $typeMock = $this->getMockBuilder('Elastica\Type')
34
            ->disableOriginalConstructor()
35
            ->getMock();
36
        $typeMock->expects($this->once())
37
            ->method('updateDocuments');
38
39
        $serializerMock = $this->getMockBuilder('FOS\ElasticaBundle\Serializer\Callback')->getMock();
40
        $serializerMock->expects($this->once())->method('serialize');
41
42
        $dispatcherMock = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')->getMock();
43
44
        $objectPersister = new ObjectSerializerPersister($typeMock, $transformer, 'SomeClass', array($serializerMock, 'serialize'), $dispatcherMock);
45
        $objectPersister->replaceOne(new POPO());
46
    }
47
48 View Code Duplication
    public function testThatCanInsertObject()
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...
49
    {
50
        $transformer = $this->getTransformer();
51
52
        /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica\Type */
53
        $typeMock = $this->getMockBuilder('Elastica\Type')
54
            ->disableOriginalConstructor()
55
            ->getMock();
56
        $typeMock->expects($this->never())
57
            ->method('deleteById');
58
        $typeMock->expects($this->once())
59
            ->method('addDocuments');
60
61
        $serializerMock = $this->getMockBuilder('FOS\ElasticaBundle\Serializer\Callback')->getMock();
62
        $serializerMock->expects($this->once())->method('serialize');
63
64
        $dispatcherMock = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')->getMock();
65
66
        $objectPersister = new ObjectSerializerPersister($typeMock, $transformer, 'SomeClass', array($serializerMock, 'serialize'), $dispatcherMock);
67
        $objectPersister->insertOne(new POPO());
68
    }
69
70 View Code Duplication
    public function testThatCanDeleteObject()
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...
71
    {
72
        $transformer = $this->getTransformer();
73
74
        /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica\Type */
75
        $typeMock = $this->getMockBuilder('Elastica\Type')
76
            ->disableOriginalConstructor()
77
            ->getMock();
78
        $typeMock->expects($this->once())
79
            ->method('deleteDocuments');
80
        $typeMock->expects($this->never())
81
            ->method('addDocument');
82
83
        $serializerMock = $this->getMockBuilder('FOS\ElasticaBundle\Serializer\Callback')->getMock();
84
        $serializerMock->expects($this->once())->method('serialize');
85
86
        $dispatcherMock = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')->getMock();
87
88
        $objectPersister = new ObjectSerializerPersister($typeMock, $transformer, 'SomeClass', array($serializerMock, 'serialize'), $dispatcherMock);
89
        $objectPersister->deleteOne(new POPO());
90
    }
91
92
    public function testThatCanInsertManyObjects()
93
    {
94
        $transformer = $this->getTransformer();
95
96
        /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica\Type */
97
        $typeMock = $this->getMockBuilder('Elastica\Type')
98
            ->disableOriginalConstructor()
99
            ->getMock();
100
        $typeMock->expects($this->never())
101
            ->method('deleteById');
102
        $typeMock->expects($this->never())
103
            ->method('addObject');
104
        $typeMock->expects($this->never())
105
            ->method('addObjects');
106
        $typeMock->expects($this->once())
107
            ->method('addDocuments');
108
109
        $serializerMock = $this->getMockBuilder('FOS\ElasticaBundle\Serializer\Callback')->getMock();
110
        $serializerMock->expects($this->exactly(2))->method('serialize');
111
112
        $dispatcherMock = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')->getMock();
113
114
        $objectPersister = new ObjectSerializerPersister($typeMock, $transformer, 'SomeClass', array($serializerMock, 'serialize'), $dispatcherMock);
115
        $objectPersister->insertMany(array(new POPO(), new POPO()));
116
    }
117
118
    /**
119
     * @return ModelToElasticaIdentifierTransformer
120
     */
121
    private function getTransformer()
122
    {
123
        $transformer = new ModelToElasticaIdentifierTransformer();
124
        $transformer->setPropertyAccessor(PropertyAccess::createPropertyAccessor());
125
126
        return $transformer;
127
    }
128
}
129