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

testThatCanInsertManyObjects()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 0
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