Failed Conditions
Push — master ( da109c...fda770 )
by Marco
19:23
created

Persister/Entity/AbstractEntityPersisterTest.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Doctrine\Tests\ORM\Cache\Persister\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Criteria;
7
use Doctrine\ORM\Cache\Persister\CachedPersister;
8
use Doctrine\ORM\Cache\Persister\Entity\CachedEntityPersister;
9
use Doctrine\ORM\Cache\Region;
10
use Doctrine\ORM\EntityManager;
11
use Doctrine\ORM\Mapping\ClassMetadata;
12
use Doctrine\ORM\PersistentCollection;
13
use Doctrine\ORM\Persisters\Entity\EntityPersister;
14
use Doctrine\ORM\Query\ResultSetMappingBuilder;
15
use Doctrine\Tests\Models\Cache\Country;
16
use Doctrine\Tests\OrmTestCase;
17
18
/**
19
 * @group DDC-2183
20
 */
21
abstract class AbstractEntityPersisterTest extends OrmTestCase
22
{
23
    /**
24
     * @var \Doctrine\ORM\Cache\Region
25
     */
26
    protected $region;
27
28
    /**
29
     * @var \Doctrine\ORM\Persisters\Entity\EntityPersister
30
     */
31
    protected $entityPersister;
32
33
    /**
34
     * @var \Doctrine\ORM\EntityManager
35
     */
36
    protected $em;
37
38
    /**
39
     * @var array
40
     */
41
    protected $regionMockMethods = [
42
        'getName',
43
        'contains',
44
        'get',
45
        'getMultiple',
46
        'put',
47
        'evict',
48
        'evictAll'
49
    ];
50
51
    /**
52
     * @var array
53
     */
54
    protected $entityPersisterMockMethods = [
55
        'getClassMetadata',
56
        'getResultSetMapping',
57
        'getInserts',
58
        'getInsertSQL',
59
        'getSelectSQL',
60
        'getCountSQL',
61
        'expandParameters',
62
        'expandCriteriaParameters',
63
        'getSelectConditionStatementSQL',
64
        'addInsert',
65
        'executeInserts',
66
        'update',
67
        'delete',
68
        'getOwningTable',
69
        'load',
70
        'loadById',
71
        'loadOneToOneEntity',
72
        'count',
73
        'refresh',
74
        'loadCriteria',
75
        'loadAll',
76
        'getManyToManyCollection',
77
        'loadManyToManyCollection',
78
        'loadOneToManyCollection',
79
        'lock',
80
        'getOneToManyCollection',
81
        'exists'
82
    ];
83
84
    /**
85
     * @param \Doctrine\ORM\EntityManager                     $em
86
     * @param \Doctrine\ORM\Persisters\Entity\EntityPersister $persister
87
     * @param \Doctrine\ORM\Cache\Region                      $region
88
     * @param \Doctrine\ORM\Mapping\ClassMetadata             $metadata
89
     *
90
     * @return \Doctrine\ORM\Cache\Persister\Entity\AbstractEntityPersister
91
     */
92
    abstract protected function createPersister(EntityManager $em, EntityPersister $persister, Region $region, ClassMetadata $metadata);
93
94 View Code Duplication
    protected function setUp()
95
    {
96
        $this->getSharedSecondLevelCacheDriverImpl()->flushAll();
97
        $this->enableSecondLevelCache();
98
        parent::setUp();
99
100
        $this->em               = $this->_getTestEntityManager();
101
        $this->region           = $this->createRegion();
102
        $this->entityPersister  = $this->getMockBuilder(EntityPersister::class)
103
                                       ->setMethods($this->entityPersisterMockMethods)
104
                                       ->getMock();
105
    }
106
107
    /**
108
     * @return \Doctrine\ORM\Cache\Region
109
     */
110
    protected function createRegion()
111
    {
112
        return $this->getMockBuilder(Region::class)
113
                    ->setMethods($this->regionMockMethods)
114
                    ->getMock();
115
    }
116
117
    /**
118
     * @return \Doctrine\ORM\Cache\Persister\AbstractEntityPersister
119
     */
120
    protected function createPersisterDefault()
121
    {
122
        return $this->createPersister($this->em, $this->entityPersister, $this->region, $this->em->getClassMetadata(Country::class));
123
    }
124
125
    public function testImplementsEntityPersister()
126
    {
127
        $persister = $this->createPersisterDefault();
128
129
        $this->assertInstanceOf(EntityPersister::class, $persister);
130
        $this->assertInstanceOf(CachedPersister::class, $persister);
131
        $this->assertInstanceOf(CachedEntityPersister::class, $persister);
132
    }
133
134 View Code Duplication
    public function testInvokeAddInsert()
135
    {
136
        $persister = $this->createPersisterDefault();
137
        $entity    = new Country("Foo");
138
139
        $this->entityPersister->expects($this->once())
140
            ->method('addInsert')
141
            ->with($this->equalTo($entity));
142
143
        $this->assertNull($persister->addInsert($entity));
144
    }
145
146
    public function testInvokeGetInserts()
147
    {
148
        $persister = $this->createPersisterDefault();
149
        $entity    = new Country("Foo");
150
151
        $this->entityPersister->expects($this->once())
152
            ->method('getInserts')
153
            ->will($this->returnValue([$entity]));
154
155
        $this->assertEquals([$entity], $persister->getInserts());
156
    }
157
158
    public function testInvokeGetSelectSQL()
159
    {
160
        $persister = $this->createPersisterDefault();
161
162
        $this->entityPersister->expects($this->once())
163
            ->method('getSelectSQL')
164
            ->with($this->equalTo(['name'=>'Foo']), $this->equalTo([0]), $this->equalTo(1), $this->equalTo(2), $this->equalTo(3), $this->equalTo(
165
                [4]
166
            ))
167
            ->will($this->returnValue('SELECT * FROM foo WERE name = ?'));
168
169
        $this->assertEquals('SELECT * FROM foo WERE name = ?', $persister->getSelectSQL(
170
            ['name'=>'Foo'], [0], 1, 2, 3, [4]
171
        ));
172
    }
173
174
    public function testInvokeGetInsertSQL()
175
    {
176
        $persister = $this->createPersisterDefault();
177
178
        $this->entityPersister->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Doctrine\ORM\Pers...Entity\EntityPersister>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
179
            ->method('getInsertSQL')
180
            ->will($this->returnValue('INSERT INTO foo (?)'));
181
182
        $this->assertEquals('INSERT INTO foo (?)', $persister->getInsertSQL());
183
    }
184
185
    public function testInvokeExpandParameters()
186
    {
187
        $persister = $this->createPersisterDefault();
188
189
        $this->entityPersister->expects($this->once())
190
            ->method('expandParameters')
191
            ->with($this->equalTo(['name'=>'Foo']))
192
            ->will($this->returnValue(['name'=>'Foo']));
193
194
        $this->assertEquals(['name'=>'Foo'], $persister->expandParameters(['name'=>'Foo']));
195
    }
196
197 View Code Duplication
    public function testInvokeExpandCriteriaParameters()
198
    {
199
        $persister = $this->createPersisterDefault();
200
        $criteria  = new Criteria();
201
202
        $this->entityPersister->expects($this->once())
203
            ->method('expandCriteriaParameters')
204
            ->with($this->equalTo($criteria))
205
            ->will($this->returnValue(['name'=>'Foo']));
206
207
        $this->assertEquals(['name'=>'Foo'], $persister->expandCriteriaParameters($criteria));
208
    }
209
210
    public function testInvokeSelectConditionStatementSQL()
211
    {
212
        $persister = $this->createPersisterDefault();
213
214
        $this->entityPersister->expects($this->once())
215
            ->method('getSelectConditionStatementSQL')
216
            ->with($this->equalTo('id'), $this->equalTo(1), $this->equalTo([]), $this->equalTo('='))
217
            ->will($this->returnValue('name = 1'));
218
219
        $this->assertEquals('name = 1', $persister->getSelectConditionStatementSQL('id', 1, [], '='));
220
    }
221
222
    public function testInvokeExecuteInserts()
223
    {
224
        $persister = $this->createPersisterDefault();
225
226
        $this->entityPersister->expects($this->once())
227
            ->method('executeInserts')
228
            ->will($this->returnValue(['id' => 1]));
229
230
        $this->assertEquals(['id' => 1], $persister->executeInserts());
231
    }
232
233
    public function testInvokeUpdate()
234
    {
235
        $persister = $this->createPersisterDefault();
236
        $entity    = new Country("Foo");
237
238
        $this->entityPersister->expects($this->once())
239
            ->method('update')
240
            ->with($this->equalTo($entity));
241
242
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
243
244
        $this->assertNull($persister->update($entity));
245
    }
246
247
    public function testInvokeDelete()
248
    {
249
        $persister = $this->createPersisterDefault();
250
        $entity    = new Country("Foo");
251
252
        $this->entityPersister->expects($this->once())
253
            ->method('delete')
254
            ->with($this->equalTo($entity));
255
256
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
257
258
        $this->assertNull($persister->delete($entity));
259
    }
260
261
    public function testInvokeGetOwningTable()
262
    {
263
        $persister = $this->createPersisterDefault();
264
265
        $this->entityPersister->expects($this->once())
266
            ->method('getOwningTable')
267
            ->with($this->equalTo('name'))
268
            ->will($this->returnValue('t'));
269
270
        $this->assertEquals('t', $persister->getOwningTable('name'));
271
    }
272
273
    public function testInvokeLoad()
274
    {
275
        $persister = $this->createPersisterDefault();
276
        $entity    = new Country("Foo");
277
278
        $this->entityPersister->expects($this->once())
279
            ->method('load')
280
            ->with($this->equalTo(['id' => 1]), $this->equalTo($entity), $this->equalTo([0]), $this->equalTo(
281
                [1]
282
            ), $this->equalTo(2), $this->equalTo(3), $this->equalTo(
283
                [4]
284
            ))
285
            ->will($this->returnValue($entity));
286
287
        $this->assertEquals($entity, $persister->load(['id' => 1], $entity, [0], [1], 2, 3, [4]));
288
    }
289
290
    public function testInvokeLoadAll()
291
    {
292
        $rsm       = new ResultSetMappingBuilder($this->em);
293
        $persister = $this->createPersisterDefault();
294
        $entity    = new Country("Foo");
295
296
        $rsm->addEntityResult(Country::class, 'c');
297
298
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
299
300
        $this->entityPersister->expects($this->once())
301
            ->method('loadAll')
302
            ->with($this->equalTo(['id' => 1]), $this->equalTo([0]), $this->equalTo(1), $this->equalTo(2))
303
            ->will($this->returnValue([$entity]));
304
305
        $this->entityPersister->expects($this->once())
306
            ->method('getResultSetMapping')
307
            ->will($this->returnValue($rsm));
308
309
        $this->assertEquals([$entity], $persister->loadAll(['id' => 1], [0], 1, 2));
310
    }
311
312 View Code Duplication
    public function testInvokeLoadById()
313
    {
314
        $persister = $this->createPersisterDefault();
315
        $entity    = new Country("Foo");
316
317
        $this->entityPersister->expects($this->once())
318
            ->method('loadById')
319
            ->with($this->equalTo(['id' => 1]), $this->equalTo($entity))
320
            ->will($this->returnValue($entity));
321
322
        $this->assertEquals($entity, $persister->loadById(['id' => 1], $entity));
323
    }
324
325 View Code Duplication
    public function testInvokeLoadOneToOneEntity()
326
    {
327
        $persister = $this->createPersisterDefault();
328
        $entity    = new Country("Foo");
329
330
        $this->entityPersister->expects($this->once())
331
            ->method('loadOneToOneEntity')
332
            ->with($this->equalTo([]), $this->equalTo('foo'), $this->equalTo(['id' => 11]))
333
            ->will($this->returnValue($entity));
334
335
        $this->assertEquals($entity, $persister->loadOneToOneEntity([], 'foo', ['id' => 11]));
336
    }
337
338 View Code Duplication
    public function testInvokeRefresh()
339
    {
340
        $persister = $this->createPersisterDefault();
341
        $entity    = new Country("Foo");
342
343
        $this->entityPersister->expects($this->once())
344
            ->method('refresh')
345
            ->with($this->equalTo(['id' => 1]), $this->equalTo($entity), $this->equalTo(0))
346
            ->will($this->returnValue($entity));
347
348
        $this->assertNull($persister->refresh(['id' => 1], $entity), 0);
349
    }
350
351
    public function testInvokeLoadCriteria()
352
    {
353
        $rsm       = new ResultSetMappingBuilder($this->em);
354
        $persister = $this->createPersisterDefault();
355
        $entity    = new Country("Foo");
356
        $criteria  = new Criteria();
357
358
        $this->em->getUnitOfWork()->registerManaged($entity, ['id'=>1], ['id'=>1, 'name'=>'Foo']);
359
        $rsm->addEntityResult(Country::class, 'c');
360
361
        $this->entityPersister->expects($this->once())
362
            ->method('getResultSetMapping')
363
            ->will($this->returnValue($rsm));
364
365
        $this->entityPersister->expects($this->once())
366
            ->method('loadCriteria')
367
            ->with($this->equalTo($criteria))
368
            ->will($this->returnValue([$entity]));
369
370
        $this->assertEquals([$entity], $persister->loadCriteria($criteria));
371
    }
372
373 View Code Duplication
    public function testInvokeGetManyToManyCollection()
374
    {
375
        $persister = $this->createPersisterDefault();
376
        $entity    = new Country("Foo");
377
378
        $this->entityPersister->expects($this->once())
379
            ->method('getManyToManyCollection')
380
            ->with($this->equalTo([]), $this->equalTo('Foo'), $this->equalTo(1), $this->equalTo(2))
381
            ->will($this->returnValue([$entity]));
382
383
        $this->assertEquals([$entity], $persister->getManyToManyCollection([], 'Foo', 1 ,2));
384
    }
385
386 View Code Duplication
    public function testInvokeGetOneToManyCollection()
387
    {
388
        $persister = $this->createPersisterDefault();
389
        $entity    = new Country("Foo");
390
391
        $this->entityPersister->expects($this->once())
392
            ->method('getOneToManyCollection')
393
            ->with($this->equalTo([]), $this->equalTo('Foo'), $this->equalTo(1), $this->equalTo(2))
394
            ->will($this->returnValue([$entity]));
395
396
        $this->assertEquals([$entity], $persister->getOneToManyCollection([], 'Foo', 1 ,2));
397
    }
398
399 View Code Duplication
    public function testInvokeLoadManyToManyCollection()
400
    {
401
        $mapping   = $this->em->getClassMetadata(Country::class);
402
        $assoc     = ['type' => 1];
403
        $coll      = new PersistentCollection($this->em, $mapping, new ArrayCollection());
404
        $persister = $this->createPersisterDefault();
405
        $entity    = new Country("Foo");
406
407
        $this->entityPersister->expects($this->once())
408
            ->method('loadManyToManyCollection')
409
            ->with($this->equalTo($assoc), $this->equalTo('Foo'), $coll)
410
            ->will($this->returnValue([$entity]));
411
412
        $this->assertEquals([$entity], $persister->loadManyToManyCollection($assoc, 'Foo', $coll));
413
    }
414
415 View Code Duplication
    public function testInvokeLoadOneToManyCollection()
416
    {
417
        $mapping   = $this->em->getClassMetadata(Country::class);
418
        $assoc     = ['type' => 1];
419
        $coll      = new PersistentCollection($this->em, $mapping, new ArrayCollection());
420
        $persister = $this->createPersisterDefault();
421
        $entity    = new Country("Foo");
422
423
        $this->entityPersister->expects($this->once())
424
            ->method('loadOneToManyCollection')
425
            ->with($this->equalTo($assoc), $this->equalTo('Foo'), $coll)
426
            ->will($this->returnValue([$entity]));
427
428
        $this->assertEquals([$entity], $persister->loadOneToManyCollection($assoc, 'Foo', $coll));
429
    }
430
431
    public function testInvokeLock()
432
    {
433
        $identifier = ['id' => 1];
434
        $persister  = $this->createPersisterDefault();
435
436
        $this->entityPersister->expects($this->once())
437
            ->method('lock')
438
            ->with($this->equalTo($identifier), $this->equalTo(1));
439
440
        $this->assertNull($persister->lock($identifier, 1));
441
    }
442
443 View Code Duplication
    public function testInvokeExists()
444
    {
445
        $entity    = new Country("Foo");
446
        $persister = $this->createPersisterDefault();
447
448
        $this->entityPersister->expects($this->once())
449
            ->method('exists')
450
            ->with($this->equalTo($entity), $this->equalTo(null));
451
452
        $this->assertNull($persister->exists($entity));
453
    }
454
}
455