Completed
Pull Request — master (#1145)
by
unknown
06:24
created

AbstractListenerTest::createListener()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace FOS\ElasticaBundle\Tests\Doctrine;
4
5
/**
6
 * See concrete MongoDB/ORM instances of this abstract test.
7
 *
8
 * @author Richard Miller <[email protected]>
9
 */
10
abstract class AbstractListenerTest extends \PHPUnit_Framework_TestCase
11
{
12
    public function testObjectInsertedOnPersist()
13
    {
14
        $entity = new Listener\Entity(1);
15
        $persister = $this->getMockPersister($entity, 'index', 'type');
16
        $eventArgs = $this->createLifecycleEventArgs($entity, $this->getMockObjectManager());
17
        $indexable = $this->getMockIndexable('index', 'type', $entity, true);
18
19
        $listener = $this->createListener($persister, $indexable, array('indexName' => 'index', 'typeName' => 'type'));
20
        $listener->postPersist($eventArgs);
21
22
        $this->assertEquals($entity, current($listener->scheduledForInsertion));
23
24
        $persister->expects($this->once())
25
            ->method('insertMany')
26
            ->with($listener->scheduledForInsertion);
27
28
        $listener->postFlush($eventArgs);
29
    }
30
31 View Code Duplication
    public function testNonIndexableObjectNotInsertedOnPersist()
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...
32
    {
33
        $entity = new Listener\Entity(1);
34
        $persister = $this->getMockPersister($entity, 'index', 'type');
35
        $eventArgs = $this->createLifecycleEventArgs($entity, $this->getMockObjectManager());
36
        $indexable = $this->getMockIndexable('index', 'type', $entity, false);
37
38
        $listener = $this->createListener($persister, $indexable, array('indexName' => 'index', 'typeName' => 'type'));
39
        $listener->postPersist($eventArgs);
40
41
        $this->assertEmpty($listener->scheduledForInsertion);
42
43
        $persister->expects($this->never())
44
            ->method('insertOne');
45
        $persister->expects($this->never())
46
            ->method('insertMany');
47
48
        $listener->postFlush($eventArgs);
49
    }
50
51 View Code Duplication
    public function testObjectReplacedOnUpdate()
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...
52
    {
53
        $entity = new Listener\Entity(1);
54
        $persister = $this->getMockPersister($entity, 'index', 'type');
55
        $eventArgs = $this->createLifecycleEventArgs($entity, $this->getMockObjectManager());
56
        $indexable = $this->getMockIndexable('index', 'type', $entity, true);
57
58
        $listener = $this->createListener($persister, $indexable, array('indexName' => 'index', 'typeName' => 'type'));
59
        $listener->postUpdate($eventArgs);
60
61
        $this->assertEquals($entity, current($listener->scheduledForUpdate));
62
63
        $persister->expects($this->once())
64
            ->method('replaceMany')
65
            ->with(array($entity));
66
        $persister->expects($this->never())
67
            ->method('deleteById');
68
69
        $listener->postFlush($eventArgs);
70
    }
71
72
    public function testNonIndexableObjectRemovedOnUpdate()
73
    {
74
        $classMetadata = $this->getMockClassMetadata();
75
        $objectManager = $this->getMockObjectManager();
76
77
        $entity = new Listener\Entity(1);
78
        $persister = $this->getMockPersister($entity, 'index', 'type');
79
        $eventArgs = $this->createLifecycleEventArgs($entity, $objectManager);
80
        $indexable = $this->getMockIndexable('index', 'type', $entity, false);
81
82
        $objectManager->expects($this->any())
83
            ->method('getClassMetadata')
84
            ->with(get_class($entity))
85
            ->will($this->returnValue($classMetadata));
86
87
        $classMetadata->expects($this->any())
88
            ->method('getFieldValue')
89
            ->with($entity, 'id')
90
            ->will($this->returnValue($entity->getId()));
91
92
        $listener = $this->createListener($persister, $indexable, array('indexName' => 'index', 'typeName' => 'type'));
93
        $listener->postUpdate($eventArgs);
94
95
        $this->assertEmpty($listener->scheduledForUpdate);
96
        $this->assertEquals($entity->getId(), current($listener->scheduledForDeletion));
97
98
        $persister->expects($this->never())
99
            ->method('replaceOne');
100
        $persister->expects($this->once())
101
            ->method('deleteManyByIdentifiers')
102
            ->with(array($entity->getId()));
103
104
        $listener->postFlush($eventArgs);
105
    }
106
107
    public function testObjectDeletedOnRemove()
108
    {
109
        $classMetadata = $this->getMockClassMetadata();
110
        $objectManager = $this->getMockObjectManager();
111
112
        $entity = new Listener\Entity(1);
113
        $persister = $this->getMockPersister($entity, 'index', 'type');
114
        $eventArgs = $this->createLifecycleEventArgs($entity, $objectManager);
115
        $indexable = $this->getMockIndexable('index', 'type', $entity);
116
117
        $objectManager->expects($this->any())
118
            ->method('getClassMetadata')
119
            ->with(get_class($entity))
120
            ->will($this->returnValue($classMetadata));
121
122
        $classMetadata->expects($this->any())
123
            ->method('getFieldValue')
124
            ->with($entity, 'id')
125
            ->will($this->returnValue($entity->getId()));
126
127
        $listener = $this->createListener($persister, $indexable, array('indexName' => 'index', 'typeName' => 'type'));
128
        $listener->preRemove($eventArgs);
129
130
        $this->assertEquals($entity->getId(), current($listener->scheduledForDeletion));
131
132
        $persister->expects($this->once())
133
            ->method('deleteManyByIdentifiers')
134
            ->with(array($entity->getId()));
135
136
        $listener->postFlush($eventArgs);
137
    }
138
139
    public function testObjectWithNonStandardIdentifierDeletedOnRemove()
140
    {
141
        $classMetadata = $this->getMockClassMetadata();
142
        $objectManager = $this->getMockObjectManager();
143
144
        $entity = new Listener\Entity(1);
145
        $entity->identifier = 'foo';
146
        $persister = $this->getMockPersister($entity, 'index', 'type');
147
        $eventArgs = $this->createLifecycleEventArgs($entity, $objectManager);
148
        $indexable = $this->getMockIndexable('index', 'type', $entity);
149
150
        $objectManager->expects($this->any())
151
            ->method('getClassMetadata')
152
            ->with(get_class($entity))
153
            ->will($this->returnValue($classMetadata));
154
155
        $classMetadata->expects($this->any())
156
            ->method('getFieldValue')
157
            ->with($entity, 'identifier')
158
            ->will($this->returnValue($entity->getId()));
159
160
        $listener = $this->createListener($persister, $indexable, array('identifier' => 'identifier', 'indexName' => 'index', 'typeName' => 'type'));
161
        $listener->preRemove($eventArgs);
162
163
        $this->assertEquals($entity->identifier, current($listener->scheduledForDeletion));
164
165
        $persister->expects($this->once())
166
            ->method('deleteManyByIdentifiers')
167
            ->with(array($entity->identifier));
168
169
        $listener->postFlush($eventArgs);
170
    }
171
172 View Code Duplication
    public function testObjectNotInsertedIfIndexingDisabled()
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...
173
    {
174
        $entity = new Listener\Entity(1);
175
        $persister = $this->getMockPersister($entity, 'index', 'type');
176
        $eventArgs = $this->createLifecycleEventArgs($entity, $this->getMockObjectManager());
177
        $indexable = $this->getMockIndexable('index', 'type', $entity, null, false);
178
179
        $listener = $this->createListener($persister, $indexable, array('indexName' => 'index', 'typeName' => 'type'));
180
        $listener->postPersist($eventArgs);
181
182
        $persister->expects($this->never())
183
            ->method('insertOne');
184
        $persister->expects($this->never())
185
            ->method('insertMany');
186
187
        $listener->postFlush($eventArgs);
188
    }
189
190 View Code Duplication
    public function testObjectNotReplacedIfIndexingDisabled()
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...
191
    {
192
        $entity = new Listener\Entity(1);
193
        $persister = $this->getMockPersister($entity, 'index', 'type');
194
        $eventArgs = $this->createLifecycleEventArgs($entity, $this->getMockObjectManager());
195
        $indexable = $this->getMockIndexable('index', 'type', $entity, true, false);
196
197
        $listener = $this->createListener($persister, $indexable, array('indexName' => 'index', 'typeName' => 'type'));
198
        $listener->postUpdate($eventArgs);
199
200
        $persister->expects($this->never())
201
            ->method('replaceMany');
202
203
        $listener->postFlush($eventArgs);
204
    }
205
206 View Code Duplication
    public function testObjectNotDeletedIfIndexingDisabled()
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...
207
    {
208
        $entity = new Listener\Entity(1);
209
        $persister = $this->getMockPersister($entity, 'index', 'type');
210
        $eventArgs = $this->createLifecycleEventArgs($entity, $this->getMockObjectManager());
211
        $indexable = $this->getMockIndexable('index', 'type', $entity, null, false);
212
213
        $listener = $this->createListener($persister, $indexable, array('indexName' => 'index', 'typeName' => 'type'));
214
        $listener->preRemove($eventArgs);
215
216
        $persister->expects($this->never())
217
            ->method('deleteManyByIdentifiers');
218
219
        $listener->postFlush($eventArgs);
220
    }
221
222
    abstract protected function getLifecycleEventArgsClass();
223
224
    abstract protected function getListenerClass();
225
226
    /**
227
     * @return string
228
     */
229
    abstract protected function getObjectManagerClass();
230
231
    /**
232
     * @return string
233
     */
234
    abstract protected function getClassMetadataClass();
235
236
    private function createLifecycleEventArgs()
237
    {
238
        $refl = new \ReflectionClass($this->getLifecycleEventArgsClass());
239
240
        return $refl->newInstanceArgs(func_get_args());
241
    }
242
243
    private function createListener()
244
    {
245
        $refl = new \ReflectionClass($this->getListenerClass());
246
247
        return $refl->newInstanceArgs(func_get_args());
248
    }
249
250
    private function getMockClassMetadata()
251
    {
252
        return $this->getMockBuilder($this->getClassMetadataClass())
253
            ->disableOriginalConstructor()
254
            ->getMock();
255
    }
256
257
    private function getMockObjectManager()
258
    {
259
        return $this->getMockBuilder($this->getObjectManagerClass())
260
            ->disableOriginalConstructor()
261
            ->getMock();
262
    }
263
264
    /**
265
     * @param Listener\Entity $object
266
     * @param string          $indexName
267
     * @param string          $typeName
268
     */
269
    private function getMockPersister($object, $indexName, $typeName)
270
    {
271
        $mock = $this->getMockBuilder('FOS\ElasticaBundle\Persister\ObjectPersister')
272
            ->disableOriginalConstructor()
273
            ->getMock();
274
275
        $mock->expects($this->any())
276
            ->method('handlesObject')
277
            ->with($object)
278
            ->will($this->returnValue(true));
279
280
        $index = $this->getMockBuilder('Elastica\Index')->disableOriginalConstructor()->getMock();
281
        $index->expects($this->any())
282
            ->method('getName')
283
            ->will($this->returnValue($indexName));
284
        $type = $this->getMockBuilder('Elastica\Type')->disableOriginalConstructor()->getMock();
285
        $type->expects($this->any())
286
            ->method('getName')
287
            ->will($this->returnValue($typeName));
288
        $type->expects($this->any())
289
            ->method('getIndex')
290
            ->will($this->returnValue($index));
291
292
        $index->expects($this->any())
293
            ->method('getType')
294
            ->will($this->returnValue($type));
295
296
        return $mock;
297
    }
298
299
    /**
300
     * @param string          $indexName
301
     * @param string          $typeName
302
     * @param Listener\Entity $object
303
     * @param boolean         $isObjectIndexable
304
     * @param boolean         $isIndexingEnabled
305
     */
306
    private function getMockIndexable($indexName, $typeName, $object, $isObjectIndexable = null, $isIndexingEnabled = true)
307
    {
308
        $mock = $this->getMockBuilder('FOS\ElasticaBundle\Provider\IndexableInterface')->getMock();
309
310
        if (null !== $isObjectIndexable) {
311
            $mock->expects($this->once())
312
                ->method('isObjectIndexable')
313
                ->with($indexName, $typeName, $object)
314
                ->will($this->returnValue($isObjectIndexable));
315
        }
316
317
        $mock->expects($this->once())
318
            ->method('isIndexingEnabled')
319
            ->will($this->returnValue($isIndexingEnabled));
320
321
        return $mock;
322
    }
323
}
324
325
namespace FOS\ElasticaBundle\Tests\Doctrine\Listener;
326
327
class Entity
328
{
329
    private $id;
330
    public $identifier;
331
332
    /**
333
     * @param integer $id
334
     */
335
    public function __construct($id)
336
    {
337
        $this->id = $id;
338
    }
339
340
    public function getId()
341
    {
342
        return $this->id;
343
    }
344
}
345