Completed
Push — master ( ca3bac...db7ab3 )
by David de
26:54 queued 16:58
created

DoctrineWriterTest::getMongoDocumentManager()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 90
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 90
rs 8.5454
c 0
b 0
f 0
cc 1
eloc 67
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Port\Doctrine\Tests;
4
5
use Port\Doctrine\DoctrineWriter;
6
use Port\Doctrine\Tests\Fixtures\Entity\TestEntity;
7
8
class DoctrineWriterTest extends \PHPUnit_Framework_TestCase
9
{
10 View Code Duplication
    public function testWriteItem()
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...
11
    {
12
        $em = $this->getEntityManager();
13
14
        $em->expects($this->once())
15
                ->method('persist');
16
17
        $writer = new DoctrineWriter($em, 'DdeboerPort:TestEntity');
18
19
        $association = new TestEntity();
20
        $item = array(
21
            'firstProperty'   => 'some value',
22
            'secondProperty'  => 'some other value',
23
            'firstAssociation'=> $association
24
        );
25
        $writer->writeItem($item);
26
    }
27
    
28 View Code Duplication
    public function testWriteItemMongodb()
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...
29
    {
30
        $em = $this->getMongoDocumentManager();
31
32
        $em->expects($this->once())
33
                ->method('persist');
34
35
        $writer = new DoctrineWriter($em, 'DdeboerPort:TestEntity');
36
37
        $association = new TestEntity();
38
        $item = array(
39
            'firstProperty'   => 'some value',
40
            'secondProperty'  => 'some other value',
41
            'firstAssociation'=> $association
42
        );
43
        $writer->prepare();
44
        $writer->writeItem($item);
45
    }
46
    
47
    public function testUnsupportedDatabaseTypeException()
48
    {
49
        $this->setExpectedException('Port\Doctrine\Exception\UnsupportedDatabaseTypeException');
50
        $em = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
51
        new DoctrineWriter($em, 'DdeboerPort:TestEntity');
52
    }
53
54
    protected function getEntityManager()
55
    {
56
        $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
57
            ->setMethods(array('getRepository', 'getClassMetadata', 'persist', 'flush', 'clear', 'getConnection', 'getReference'))
58
            ->disableOriginalConstructor()
59
            ->getMock();
60
61
        $repo = $this->getMockBuilder('Doctrine\ORM\EntityRepository')
62
            ->disableOriginalConstructor()
63
            ->getMock();
64
65
        $metadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')
66
            ->setMethods(array('getName', 'getFieldNames', 'getAssociationNames', 'setFieldValue', 'getAssociationMappings'))
67
            ->disableOriginalConstructor()
68
            ->getMock();
69
70
        $metadata->expects($this->any())
71
            ->method('getName')
72
            ->will($this->returnValue('Port\Tests\Fixtures\Entity\TestEntity'));
73
74
        $metadata->expects($this->any())
75
            ->method('getFieldNames')
76
            ->will($this->returnValue(array('firstProperty', 'secondProperty')));
77
78
        $metadata->expects($this->any())
79
            ->method('getAssociationNames')
80
            ->will($this->returnValue(array('firstAssociation')));
81
82
        $metadata->expects($this->any())
83
            ->method('getAssociationMappings')
84
            ->will($this->returnValue(array(array('fieldName' => 'firstAssociation','targetEntity' => 'Port\Tests\Fixtures\Entity\TestEntity'))));
85
86
        $configuration = $this->getMockBuilder('Doctrine\DBAL\Configuration')
87
            ->setMethods(array('getConnection'))
88
            ->disableOriginalConstructor()
89
            ->getMock();
90
91
        $connection = $this->getMockBuilder('Doctrine\DBAL\Connection')
92
            ->setMethods(array('getConfiguration', 'getDatabasePlatform', 'getTruncateTableSQL', 'executeQuery'))
93
            ->disableOriginalConstructor()
94
            ->getMock();
95
96
        $connection->expects($this->any())
97
            ->method('getConfiguration')
98
            ->will($this->returnValue($configuration));
99
100
        $connection->expects($this->any())
101
            ->method('getDatabasePlatform')
102
            ->will($this->returnSelf());
103
104
        $connection->expects($this->any())
105
            ->method('getTruncateTableSQL')
106
            ->will($this->returnValue('TRUNCATE SQL'));
107
108
        $connection->expects($this->any())
109
            ->method('executeQuery')
110
            ->with('TRUNCATE SQL');
111
            
112
        $em->expects($this->never())
113
            ->method('getDocumentCollection');
114
            
115
        $em->expects($this->once())
116
            ->method('getRepository')
117
            ->will($this->returnValue($repo));
118
119
        $em->expects($this->once())
120
            ->method('getClassMetadata')
121
            ->will($this->returnValue($metadata));
122
123
        $em->expects($this->any())
124
            ->method('getConnection')
125
            ->will($this->returnValue($connection));
126
127
        $self = $this;
128
        $em->expects($this->any())
129
            ->method('persist')
130
            ->will($this->returnCallback(function ($argument) use ($self) {
131
                $self->assertNotNull($argument->getFirstAssociation());
132
                return true;
133
            }));
134
135
        return $em;
136
    }
137
    
138
139
    protected function getMongoDocumentManager()
140
    {
141
        $dm = $this->getMockBuilder('Doctrine\ODM\MongoDB\DocumentManager')
142
            ->setMethods(array('getRepository', 'getClassMetadata', 'persist', 'flush', 'clear', 'getConnection', 'getDocumentCollection'))
143
            ->disableOriginalConstructor()
144
            ->getMock();
145
146
        $repo = $this->getMockBuilder('Doctrine\ODM\MongoDB\DocumentRepository')
147
            ->disableOriginalConstructor()
148
            ->getMock();
149
150
        $metadata = $this->getMockBuilder('Doctrine\ODM\MongoDB\Mapping\ClassMetadata')
151
            ->setMethods(array('getName', 'getFieldNames', 'getAssociationNames', 'setFieldValue', 'getAssociationMappings'))
152
            ->disableOriginalConstructor()
153
            ->getMock();
154
155
        $metadata->expects($this->any())
156
            ->method('getName')
157
            ->will($this->returnValue('Port\Tests\Fixtures\Entity\TestEntity'));
158
159
        $metadata->expects($this->any())
160
            ->method('getFieldNames')
161
            ->will($this->returnValue(array('firstProperty', 'secondProperty')));
162
163
        $metadata->expects($this->any())
164
            ->method('getAssociationNames')
165
            ->will($this->returnValue(array('firstAssociation')));
166
            
167
        $metadata->expects($this->any())
168
            ->method('getAssociationMappings')
169
            ->will($this->returnValue(array(array('fieldName' => 'firstAssociation','targetEntity' => 'Port\Tests\Fixtures\Entity\TestEntity'))));
170
171
        $configuration = $this->getMockBuilder('Doctrine\DBAL\Configuration')
172
            ->setMethods(array('getConnection'))
173
            ->disableOriginalConstructor()
174
            ->getMock();
175
176
        $connection = $this->getMockBuilder('Doctrine\DBAL\Connection')
177
            ->setMethods(array('getConfiguration', 'getDatabasePlatform', 'getTruncateTableSQL', 'executeQuery'))
178
            ->disableOriginalConstructor()
179
            ->getMock();
180
181
        $connection->expects($this->any())
182
            ->method('getConfiguration')
183
            ->will($this->returnValue($configuration));
184
185
        $connection->expects($this->any())
186
            ->method('getDatabasePlatform')
187
            ->will($this->returnSelf());
188
189
        $connection->expects($this->never())
190
            ->method('getTruncateTableSQL');
191
192
        $connection->expects($this->never())
193
            ->method('executeQuery');
194
195
        $dm->expects($this->once())
196
            ->method('getRepository')
197
            ->will($this->returnValue($repo));
198
199
        $dm->expects($this->once())
200
            ->method('getClassMetadata')
201
            ->will($this->returnValue($metadata));
202
203
        $dm->expects($this->any())
204
            ->method('getConnection')
205
            ->will($this->returnValue($connection));
206
207
        $documentCollection = $this->getMockBuilder('\MongoCollection')
208
            ->disableOriginalConstructor()
209
            ->getMock();
210
211
        $documentCollection
212
            ->expects($this->once())
213
            ->method('remove');
214
215
        $dm->expects($this->once())
216
            ->method('getDocumentCollection')
217
            ->will($this->returnValue($documentCollection));
218
219
        $self = $this;
220
        $dm->expects($this->any())
221
            ->method('persist')
222
            ->will($this->returnCallback(function ($argument) use ($self) {
223
                $self->assertNotNull($argument->getFirstAssociation());
224
                return true;
225
            }));
226
227
        return $dm;
228
    }
229
230 View Code Duplication
    public function testLoadAssociationWithoutObject()
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...
231
    {
232
        $em = $this->getEntityManager();
233
234
        $em->expects($this->once())
235
            ->method('persist');
236
237
        $em->expects($this->once())
238
            ->method('getReference');
239
240
        $writer = new DoctrineWriter($em, 'DdeboerPort:TestEntity');
241
242
        $item   = array(
243
            'firstProperty'    => 'some value',
244
            'secondProperty'   => 'some other value',
245
            'firstAssociation' => 'firstAssociationId'
246
        );
247
248
        $writer->writeItem($item);
249
    }
250
251 View Code Duplication
    public function testLoadAssociationWithPresetObject()
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...
252
    {
253
        $em = $this->getEntityManager();
254
255
        $em->expects($this->once())
256
            ->method('persist');
257
258
        $em->expects($this->never())
259
            ->method('getReference');
260
261
        $writer = new DoctrineWriter($em, 'DdeboerPort:TestEntity');
262
263
        $association = new TestEntity();
264
        $item        = array(
265
            'firstProperty'    => 'some value',
266
            'secondProperty'   => 'some other value',
267
            'firstAssociation' => $association,
268
        );
269
270
        $writer->writeItem($item);
271
    }
272
273
    /**
274
     * Test to make sure that we are clearing the write entity
275
     */
276
    public function testFlushAndClear()
277
    {
278
        $em = $this->getEntityManager();
279
280
        $em->expects($this->once())
281
            ->method('clear')
282
            ->with($this->equalTo('Port\Tests\Fixtures\Entity\TestEntity'));
283
284
        $writer = new DoctrineWriter($em, 'DdeboerPort:TestEntity');
285
        $writer->finish();
286
    }
287
}
288