Test Failed
Push — develop ( 24f682...01a8a8 )
by Marco
63:08
created

EntityManagerMock::getEM()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\Mocks;
6
7
use Doctrine\Common\EventManager;
8
use Doctrine\ORM\Configuration;
9
use Doctrine\ORM\EntityManager;
10
use Doctrine\ORM\EntityManagerInterface;
11
use Doctrine\ORM\Mapping;
12
use Doctrine\ORM\NativeQuery;
13
use Doctrine\ORM\OptimisticLockException;
14
use Doctrine\ORM\ORMException;
15
use Doctrine\ORM\PessimisticLockException;
16
use Doctrine\ORM\Query;
17
use Doctrine\ORM\Query\ResultSetMapping;
18
use Doctrine\ORM\QueryBuilder;
19
use Doctrine\ORM\Utility\IdentifierFlattener;
20
21
/**
22
 * Special EntityManager mock used for testing purposes.
23
 */
24
class EntityManagerMock implements EntityManagerInterface
25
{
26
    /**
27
     * @var \Doctrine\ORM\UnitOfWork|null
28
     */
29
    private $uowMock;
30
31
    /**
32
     * @var \Doctrine\ORM\Proxy\Factory\ProxyFactory|null
33
     */
34
    private $proxyFactoryMock;
35
36
    /** @var  EntityManagerInterface $em */
37
    private $em;
38
39
    /**
40
     * EntityManagerMock constructor.
41
     *
42
     * @param EntityManagerInterface $em
43
     */
44
    public function __construct(EntityManagerInterface $em)
45
    {
46
        $this->em = $em;
47
    }
48
49
    public function getEM()
50
    {
51
        return $this->em;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getUnitOfWork()
58
    {
59
        return $this->uowMock ?? $this->em->getUnitOfWork();
60
    }
61
62
    /* Mock API */
63
64
    /**
65
     * Sets a (mock) UnitOfWork that will be returned when getUnitOfWork() is called.
66
     *
67
     * @param \Doctrine\ORM\UnitOfWork $uow
68
     *
69
     * @return void
70
     */
71
    public function setUnitOfWork($uow)
72
    {
73
        $this->uowMock = $uow;
74
    }
75
76
    /**
77
     * @param \Doctrine\ORM\Proxy\Factory\ProxyFactory $proxyFactory
78
     *
79
     * @return void
80
     */
81
    public function setProxyFactory($proxyFactory)
82
    {
83
        $this->proxyFactoryMock = $proxyFactory;
84
    }
85
86
    /**
87
     * @return \Doctrine\ORM\Proxy\Factory\ProxyFactory
88
     */
89
    public function getProxyFactory()
90
    {
91
        return $this->proxyFactoryMock ?? $this->em->getProxyFactory();
92
    }
93
94
    /**
95
     * Mock factory method to create an EntityManager.
96
     *
97
     * {@inheritdoc}
98
     */
99
    public static function create($conn, Configuration $config = null, EventManager $eventManager = null)
100
    {
101
        if (null === $config) {
102
            $config = new Configuration();
103
            $config->setProxyDir(__DIR__ . '/../Proxies');
104
            $config->setProxyNamespace('Doctrine\Tests\Proxies');
105
            $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver([]));
106
        }
107
        if (null === $eventManager) {
108
            $eventManager = $conn->getEventManager();
109
        }
110
111
        $em = EntityManager::create($conn, $config, $eventManager);
112
        return new EntityManagerMock($em);
113
    }
114
115
    /**
116
     * Returns the cache API for managing the second level cache regions or NULL if the cache is not enabled.
117
     *
118
     * @return \Doctrine\ORM\Cache|null
119
     */
120
    public function getCache()
121
    {
122
        return $this->em->getCache();
123
    }
124
125
    /**
126
     * Gets the database connection object used by the EntityManager.
127
     *
128
     * @return \Doctrine\DBAL\Connection
129
     */
130
    public function getConnection()
131
    {
132
        return $this->em->getConnection();
133
    }
134
135
136
    /**
137
     * @return Query\Expr
138
     */
139
    public function getExpressionBuilder()
140
    {
141
        return $this->em->getExpressionBuilder();
142
    }
143
144
    /**
145
     * Gets an IdentifierFlattener used for converting Entities into an array of identifier values.
146
     *
147
     * @return IdentifierFlattener
148
     */
149
    public function getIdentifierFlattener()
150
    {
151
        return $this->em->getIdentifierFlattener();
152
    }
153
154
    /**
155
     * Starts a transaction on the underlying database connection.
156
     *
157
     * @return void
158
     */
159
    public function beginTransaction()
160
    {
161
        $this->em->beginTransaction();
162
    }
163
164
    /**
165
     * Executes a function in a transaction.
166
     *
167
     * The function gets passed this EntityManager instance as an (optional) parameter.
168
     *
169
     * {@link flush} is invoked prior to transaction commit.
170
     *
171
     * If an exception occurs during execution of the function or flushing or transaction commit,
172
     * the transaction is rolled back, the EntityManager closed and the exception re-thrown.
173
     *
174
     * @param callable $func The function to execute transactionally.
175
     *
176
     * @return mixed The value returned from the closure.
177
     *
178
     * @throws \Throwable
179
     */
180
    public function transactional(callable $func)
181
    {
182
        return $this->em->transactional($func);
183
    }
184
185
    /**
186
     * Commits a transaction on the underlying database connection.
187
     *
188
     * @return void
189
     */
190
    public function commit()
191
    {
192
        $this->em->commit();
193
    }
194
195
    /**
196
     * Performs a rollback on the underlying database connection.
197
     *
198
     * @return void
199
     */
200
    public function rollback()
201
    {
202
        $this->em->rollback();
203
    }
204
205
    /**
206
     * Creates a new Query object.
207
     *
208
     * @param string $dql The DQL string.
209
     *
210
     * @return Query
211
     */
212
    public function createQuery($dql = '')
213
    {
214
        return $this->em->createQuery($dql);
215
    }
216
217
    /**
218
     * Creates a Query from a named query.
219
     *
220
     * @param string $name
221
     *
222
     * @return Query
223
     */
224
    public function createNamedQuery($name)
225
    {
226
        return $this->em->createNamedQuery($name);
227
    }
228
229
    /**
230
     * Creates a native SQL query.
231
     *
232
     * @param string $sql
233
     * @param ResultSetMapping $rsm The ResultSetMapping to use.
234
     *
235
     * @return NativeQuery
236
     */
237
    public function createNativeQuery($sql, ResultSetMapping $rsm)
238
    {
239
        return $this->em->createNativeQuery($sql, $rsm);
240
    }
241
242
    /**
243
     * Creates a NativeQuery from a named native query.
244
     *
245
     * @param string $name
246
     *
247
     * @return NativeQuery
248
     */
249
    public function createNamedNativeQuery($name)
250
    {
251
        return $this->em->createNamedNativeQuery($name);
252
    }
253
254
    /**
255
     * Create a QueryBuilder instance
256
     *
257
     * @return QueryBuilder
258
     */
259
    public function createQueryBuilder()
260
    {
261
        return $this->em->createQueryBuilder();
262
    }
263
264
    /**
265
     * Gets a reference to the entity identified by the given type and identifier
266
     * without actually loading it, if the entity is not yet loaded.
267
     *
268
     * @param string $entityName The name of the entity type.
269
     * @param mixed $id          The entity identifier.
270
     *
271
     * @return object The entity reference.
272
     *
273
     * @throws ORMException
274
     */
275
    public function getReference($entityName, $id)
276
    {
277
        return $this->em->getReference($entityName, $id);
278
    }
279
280
    /**
281
     * Gets a partial reference to the entity identified by the given type and identifier
282
     * without actually loading it, if the entity is not yet loaded.
283
     *
284
     * The returned reference may be a partial object if the entity is not yet loaded/managed.
285
     * If it is a partial object it will not initialize the rest of the entity state on access.
286
     * Thus you can only ever safely access the identifier of an entity obtained through
287
     * this method.
288
     *
289
     * The use-cases for partial references involve maintaining bidirectional associations
290
     * without loading one side of the association or to update an entity without loading it.
291
     * Note, however, that in the latter case the original (persistent) entity data will
292
     * never be visible to the application (especially not event listeners) as it will
293
     * never be loaded in the first place.
294
     *
295
     * @param string $entityName The name of the entity type.
296
     * @param mixed $identifier  The entity identifier.
297
     *
298
     * @return object The (partial) entity reference.
299
     */
300
    public function getPartialReference($entityName, $identifier)
301
    {
302
        return $this->em->getPartialReference($entityName, $identifier);
303
    }
304
305
    /**
306
     * Closes the EntityManager. All entities that are currently managed
307
     * by this EntityManager become detached. The EntityManager may no longer
308
     * be used after it is closed.
309
     *
310
     * @return void
311
     */
312
    public function close()
313
    {
314
        $this->em->close();
315
    }
316
317
    /**
318
     * Creates a copy of the given entity. Can create a shallow or a deep copy.
319
     *
320
     * @param object $entity The entity to copy.
321
     * @param boolean $deep  FALSE for a shallow copy, TRUE for a deep copy.
322
     *
323
     * @return object The new entity.
324
     *
325
     * @throws \BadMethodCallException
326
     */
327
    public function copy($entity, $deep = false)
328
    {
329
        return $this->em->copy($entity, $deep);
330
    }
331
332
    /**
333
     * Acquire a lock on the given entity.
334
     *
335
     * @param object $entity
336
     * @param int $lockMode
337
     * @param int|null $lockVersion
338
     *
339
     * @return void
340
     *
341
     * @throws OptimisticLockException
342
     * @throws PessimisticLockException
343
     */
344
    public function lock($entity, $lockMode, $lockVersion = null)
345
    {
346
        $this->em->lock($entity, $lockMode, $lockVersion);
347
    }
348
349
    /**
350
     * Gets the EventManager used by the EntityManager.
351
     *
352
     * @return \Doctrine\Common\EventManager
353
     */
354
    public function getEventManager()
355
    {
356
        return $this->em->getEventManager();
357
    }
358
359
    /**
360
     * Gets the Configuration used by the EntityManager.
361
     *
362
     * @return Configuration
363
     */
364
    public function getConfiguration()
365
    {
366
        return $this->em->getConfiguration();
367
    }
368
369
    /**
370
     * Check if the Entity manager is open or closed.
371
     *
372
     * @return bool
373
     */
374
    public function isOpen()
375
    {
376
        return $this->em->isOpen();
377
    }
378
379
    /**
380
     * Gets a hydrator for the given hydration mode.
381
     *
382
     * This method caches the hydrator instances which is used for all queries that don't
383
     * selectively iterate over the result.
384
     *
385
     * @deprecated
386
     *
387
     * @param int $hydrationMode
388
     *
389
     * @return \Doctrine\ORM\Internal\Hydration\AbstractHydrator
390
     */
391
    public function getHydrator($hydrationMode)
392
    {
393
        return $this->em->getHydrator($hydrationMode);
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\ORM\EntityManagerInterface::getHydrator() has been deprecated.

This method has been deprecated.

Loading history...
394
    }
395
396
    /**
397
     * Create a new instance for the given hydration mode.
398
     *
399
     * @param int $hydrationMode
400
     *
401
     * @return \Doctrine\ORM\Internal\Hydration\AbstractHydrator
402
     *
403
     * @throws ORMException
404
     */
405
    public function newHydrator($hydrationMode)
406
    {
407
        return $this->em->newHydrator($hydrationMode);
408
    }
409
410
    /**
411
     * Gets the enabled filters.
412
     *
413
     * @return \Doctrine\ORM\Query\FilterCollection The active filter collection.
414
     */
415
    public function getFilters()
416
    {
417
        return $this->em->getFilters();
418
    }
419
420
    /**
421
     * Checks whether the state of the filter collection is clean.
422
     *
423
     * @return boolean True, if the filter collection is clean.
424
     */
425
    public function isFiltersStateClean()
426
    {
427
        return $this->em->isFiltersStateClean();
428
    }
429
430
    /**
431
     * Checks whether the Entity Manager has filters.
432
     *
433
     * @return boolean True, if the EM has a filter collection.
434
     */
435
    public function hasFilters()
436
    {
437
        return $this->em->hasFilters();
438
    }
439
440
    /**
441
     * Finds an object by its identifier.
442
     *
443
     * This is just a convenient shortcut for getRepository($className)->find($id).
444
     *
445
     * @param string $className The class name of the object to find.
446
     * @param mixed $id         The identity of the object to find.
447
     *
448
     * @return object The found object.
449
     */
450
    public function find($className, $id)
451
    {
452
        return $this->em->find($className, $id);
453
    }
454
455
    /**
456
     * Tells the ObjectManager to make an instance managed and persistent.
457
     *
458
     * The object will be entered into the database as a result of the flush operation.
459
     *
460
     * NOTE: The persist operation always considers objects that are not yet known to
461
     * this ObjectManager as NEW. Do not pass detached objects to the persist operation.
462
     *
463
     * @param object $object The instance to make managed and persistent.
464
     *
465
     * @return void
466
     */
467
    public function persist($object)
468
    {
469
        $this->em->persist($object);
470
    }
471
472
    /**
473
     * Removes an object instance.
474
     *
475
     * A removed object will be removed from the database as a result of the flush operation.
476
     *
477
     * @param object $object The object instance to remove.
478
     *
479
     * @return void
480
     */
481
    public function remove($object)
482
    {
483
        $this->em->remove($object);
484
    }
485
486
    /**
487
     * Merges the state of a detached object into the persistence context
488
     * of this ObjectManager and returns the managed copy of the object.
489
     * The object passed to merge will not become associated/managed with this ObjectManager.
490
     *
491
     * @param object $object
492
     *
493
     * @return object
494
     */
495
    public function merge($object)
496
    {
497
        return $this->em->merge($object);
498
    }
499
500
    /**
501
     * Clears the ObjectManager. All objects that are currently managed
502
     * by this ObjectManager become detached.
503
     *
504
     * @param string|null $objectName if given, only objects of this type will get detached.
505
     *
506
     * @return void
507
     */
508
    public function clear($objectName = null)
509
    {
510
        $this->em->clear($objectName);
511
    }
512
513
    /**
514
     * Detaches an object from the ObjectManager, causing a managed object to
515
     * become detached. Unflushed changes made to the object if any
516
     * (including removal of the object), will not be synchronized to the database.
517
     * Objects which previously referenced the detached object will continue to
518
     * reference it.
519
     *
520
     * @param object $object The object to detach.
521
     *
522
     * @return void
523
     */
524
    public function detach($object)
525
    {
526
        $this->em->detach($object);
527
    }
528
529
    /**
530
     * Refreshes the persistent state of an object from the database,
531
     * overriding any local changes that have not yet been persisted.
532
     *
533
     * @param object $object The object to refresh.
534
     *
535
     * @return void
536
     */
537
    public function refresh($object)
538
    {
539
        $this->em->refresh($object);
540
    }
541
542
    /**
543
     * Flushes all changes to objects that have been queued up to now to the database.
544
     * This effectively synchronizes the in-memory state of managed objects with the
545
     * database.
546
     *
547
     * @return void
548
     */
549
    public function flush()
550
    {
551
        $this->em->flush();
552
    }
553
554
    /**
555
     * Gets the repository for a class.
556
     *
557
     * @param string $className
558
     *
559
     * @return \Doctrine\Common\Persistence\ObjectRepository
560
     */
561
    public function getRepository($className)
562
    {
563
        return $this->em->getRepository($className);
564
    }
565
566
    /**
567
     * Gets the metadata factory used to gather the metadata of classes.
568
     *
569
     * @return \Doctrine\Common\Persistence\Mapping\ClassMetadataFactory
570
     */
571
    public function getMetadataFactory()
572
    {
573
        return $this->em->getMetadataFactory();
574
    }
575
576
    /**
577
     * Helper method to initialize a lazy loading proxy or persistent collection.
578
     *
579
     * This method is a no-op for other objects.
580
     *
581
     * @param object $obj
582
     *
583
     * @return void
584
     */
585
    public function initializeObject($obj)
586
    {
587
        $this->em->initializeObject($obj);
588
    }
589
590
    /**
591
     * Checks if the object is part of the current UnitOfWork and therefore managed.
592
     *
593
     * @param object $object
594
     *
595
     * @return bool
596
     */
597
    public function contains($object)
598
    {
599
        return $this->em->contains($object);
600
    }
601
602
    /**
603
     * @param string $className
604
     * @return Mapping\ClassMetadata
605
     */
606
    public function getClassMetadata($className) : Mapping\ClassMetadata
607
    {
608
        return $this->em->getClassMetadata($className);
609
    }
610
}
611