Test Failed
Pull Request — develop (#6596)
by
unknown
65:38
created

EntityManagerMock   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 580
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 49
lcom 1
cbo 4
dl 0
loc 580
rs 8.5454
c 0
b 0
f 0

45 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getUnitOfWork() 0 4 2
A setUnitOfWork() 0 4 1
A setProxyFactory() 0 4 1
A getProxyFactory() 0 4 2
A create() 0 13 3
A getCache() 0 4 1
A getConnection() 0 4 1
A getExpressionBuilder() 0 4 1
A getIdentifierFlattener() 0 4 1
A beginTransaction() 0 4 1
A transactional() 0 4 1
A commit() 0 4 1
A rollback() 0 4 1
A createQuery() 0 4 1
A createNamedQuery() 0 4 1
A createNativeQuery() 0 4 1
A createNamedNativeQuery() 0 4 1
A createQueryBuilder() 0 4 1
A getReference() 0 4 1
A getPartialReference() 0 4 1
A close() 0 4 1
A copy() 0 4 1
A lock() 0 4 1
A getEventManager() 0 4 1
A getConfiguration() 0 4 1
A isOpen() 0 4 1
A getHydrator() 0 4 1
A newHydrator() 0 4 1
A getFilters() 0 4 1
A isFiltersStateClean() 0 4 1
A hasFilters() 0 4 1
A find() 0 4 1
A persist() 0 4 1
A remove() 0 4 1
A merge() 0 4 1
A clear() 0 4 1
A detach() 0 4 1
A refresh() 0 4 1
A flush() 0 4 1
A getRepository() 0 4 1
A getMetadataFactory() 0 4 1
A initializeObject() 0 4 1
A contains() 0 4 1
A getClassMetadata() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like EntityManagerMock often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use EntityManagerMock, and based on these observations, apply Extract Interface, too.

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
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getUnitOfWork()
53
    {
54
        return isset($this->uowMock) ? $this->uowMock : $this->em->getUnitOfWork();
55
    }
56
57
    /* Mock API */
58
59
    /**
60
     * Sets a (mock) UnitOfWork that will be returned when getUnitOfWork() is called.
61
     *
62
     * @param \Doctrine\ORM\UnitOfWork $uow
63
     *
64
     * @return void
65
     */
66
    public function setUnitOfWork($uow)
67
    {
68
        $this->uowMock = $uow;
69
    }
70
71
    /**
72
     * @param \Doctrine\ORM\Proxy\Factory\ProxyFactory $proxyFactory
73
     *
74
     * @return void
75
     */
76
    public function setProxyFactory($proxyFactory)
77
    {
78
        $this->proxyFactoryMock = $proxyFactory;
79
    }
80
81
    /**
82
     * @return \Doctrine\ORM\Proxy\Factory\ProxyFactory
83
     */
84
    public function getProxyFactory()
85
    {
86
        return isset($this->proxyFactoryMock) ? $this->proxyFactoryMock : $this->em->getProxyFactory();
87
    }
88
89
    /**
90
     * Mock factory method to create an EntityManager.
91
     *
92
     * {@inheritdoc}
93
     */
94
    public static function create($conn, Configuration $config = null, EventManager $eventManager = null)
95
    {
96
        if (null === $config) {
97
            $config = new Configuration();
98
            $config->setProxyDir(__DIR__ . '/../Proxies');
99
            $config->setProxyNamespace('Doctrine\Tests\Proxies');
100
            $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver([]));
101
        }
102
        if (null === $eventManager) {
103
            $eventManager = new EventManager();
104
        }
105
        return new EntityManagerMock(new EntityManager($conn, $config, $eventManager));
106
    }
107
108
    /**
109
     * Returns the cache API for managing the second level cache regions or NULL if the cache is not enabled.
110
     *
111
     * @return \Doctrine\ORM\Cache|null
112
     */
113
    public function getCache()
114
    {
115
        return $this->em->getCache();
116
    }
117
118
    /**
119
     * Gets the database connection object used by the EntityManager.
120
     *
121
     * @return \Doctrine\DBAL\Connection
122
     */
123
    public function getConnection()
124
    {
125
        return $this->em->getConnection();
126
    }
127
128
129
    /**
130
     * @return Query\Expr
131
     */
132
    public function getExpressionBuilder()
133
    {
134
        return $this->em->getExpressionBuilder();
135
    }
136
137
    /**
138
     * Gets an IdentifierFlattener used for converting Entities into an array of identifier values.
139
     *
140
     * @return IdentifierFlattener
141
     */
142
    public function getIdentifierFlattener()
143
    {
144
        return $this->em->getIdentifierFlattener();
145
    }
146
147
    /**
148
     * Starts a transaction on the underlying database connection.
149
     *
150
     * @return void
151
     */
152
    public function beginTransaction()
153
    {
154
        $this->em->beginTransaction();
155
    }
156
157
    /**
158
     * Executes a function in a transaction.
159
     *
160
     * The function gets passed this EntityManager instance as an (optional) parameter.
161
     *
162
     * {@link flush} is invoked prior to transaction commit.
163
     *
164
     * If an exception occurs during execution of the function or flushing or transaction commit,
165
     * the transaction is rolled back, the EntityManager closed and the exception re-thrown.
166
     *
167
     * @param callable $func The function to execute transactionally.
168
     *
169
     * @return mixed The value returned from the closure.
170
     *
171
     * @throws \Throwable
172
     */
173
    public function transactional(callable $func)
174
    {
175
        return $this->em->transactional($func);
176
    }
177
178
    /**
179
     * Commits a transaction on the underlying database connection.
180
     *
181
     * @return void
182
     */
183
    public function commit()
184
    {
185
        $this->em->commit();
186
    }
187
188
    /**
189
     * Performs a rollback on the underlying database connection.
190
     *
191
     * @return void
192
     */
193
    public function rollback()
194
    {
195
        $this->em->rollback();
196
    }
197
198
    /**
199
     * Creates a new Query object.
200
     *
201
     * @param string $dql The DQL string.
202
     *
203
     * @return Query
204
     */
205
    public function createQuery($dql = '')
206
    {
207
        return $this->em->createQuery($dql);
208
    }
209
210
    /**
211
     * Creates a Query from a named query.
212
     *
213
     * @param string $name
214
     *
215
     * @return Query
216
     */
217
    public function createNamedQuery($name)
218
    {
219
        return $this->em->createNamedQuery($name);
220
    }
221
222
    /**
223
     * Creates a native SQL query.
224
     *
225
     * @param string $sql
226
     * @param ResultSetMapping $rsm The ResultSetMapping to use.
227
     *
228
     * @return NativeQuery
229
     */
230
    public function createNativeQuery($sql, ResultSetMapping $rsm)
231
    {
232
        return $this->em->createNativeQuery($sql, $rsm);
233
    }
234
235
    /**
236
     * Creates a NativeQuery from a named native query.
237
     *
238
     * @param string $name
239
     *
240
     * @return NativeQuery
241
     */
242
    public function createNamedNativeQuery($name)
243
    {
244
        return $this->em->createNamedNativeQuery($name);
245
    }
246
247
    /**
248
     * Create a QueryBuilder instance
249
     *
250
     * @return QueryBuilder
251
     */
252
    public function createQueryBuilder()
253
    {
254
        return $this->em->createQueryBuilder();
255
    }
256
257
    /**
258
     * Gets a reference to the entity identified by the given type and identifier
259
     * without actually loading it, if the entity is not yet loaded.
260
     *
261
     * @param string $entityName The name of the entity type.
262
     * @param mixed $id          The entity identifier.
263
     *
264
     * @return object The entity reference.
265
     *
266
     * @throws ORMException
267
     */
268
    public function getReference($entityName, $id)
269
    {
270
        return $this->em->getReference($entityName, $id);
271
    }
272
273
    /**
274
     * Gets a partial reference to the entity identified by the given type and identifier
275
     * without actually loading it, if the entity is not yet loaded.
276
     *
277
     * The returned reference may be a partial object if the entity is not yet loaded/managed.
278
     * If it is a partial object it will not initialize the rest of the entity state on access.
279
     * Thus you can only ever safely access the identifier of an entity obtained through
280
     * this method.
281
     *
282
     * The use-cases for partial references involve maintaining bidirectional associations
283
     * without loading one side of the association or to update an entity without loading it.
284
     * Note, however, that in the latter case the original (persistent) entity data will
285
     * never be visible to the application (especially not event listeners) as it will
286
     * never be loaded in the first place.
287
     *
288
     * @param string $entityName The name of the entity type.
289
     * @param mixed $identifier  The entity identifier.
290
     *
291
     * @return object The (partial) entity reference.
292
     */
293
    public function getPartialReference($entityName, $identifier)
294
    {
295
        return $this->em->getPartialReference($entityName, $identifier);
296
    }
297
298
    /**
299
     * Closes the EntityManager. All entities that are currently managed
300
     * by this EntityManager become detached. The EntityManager may no longer
301
     * be used after it is closed.
302
     *
303
     * @return void
304
     */
305
    public function close()
306
    {
307
        $this->em->close();
308
    }
309
310
    /**
311
     * Creates a copy of the given entity. Can create a shallow or a deep copy.
312
     *
313
     * @param object $entity The entity to copy.
314
     * @param boolean $deep  FALSE for a shallow copy, TRUE for a deep copy.
315
     *
316
     * @return object The new entity.
317
     *
318
     * @throws \BadMethodCallException
319
     */
320
    public function copy($entity, $deep = false)
321
    {
322
        return $this->em->copy($entity, $deep);
323
    }
324
325
    /**
326
     * Acquire a lock on the given entity.
327
     *
328
     * @param object $entity
329
     * @param int $lockMode
330
     * @param int|null $lockVersion
331
     *
332
     * @return void
333
     *
334
     * @throws OptimisticLockException
335
     * @throws PessimisticLockException
336
     */
337
    public function lock($entity, $lockMode, $lockVersion = null)
338
    {
339
        $this->em->lock($entity, $lockMode, $lockVersion);
340
    }
341
342
    /**
343
     * Gets the EventManager used by the EntityManager.
344
     *
345
     * @return \Doctrine\Common\EventManager
346
     */
347
    public function getEventManager()
348
    {
349
        return $this->em->getEventManager();
350
    }
351
352
    /**
353
     * Gets the Configuration used by the EntityManager.
354
     *
355
     * @return Configuration
356
     */
357
    public function getConfiguration()
358
    {
359
        return $this->em->getConfiguration();
360
    }
361
362
    /**
363
     * Check if the Entity manager is open or closed.
364
     *
365
     * @return bool
366
     */
367
    public function isOpen()
368
    {
369
        return $this->em->isOpen();
370
    }
371
372
    /**
373
     * Gets a hydrator for the given hydration mode.
374
     *
375
     * This method caches the hydrator instances which is used for all queries that don't
376
     * selectively iterate over the result.
377
     *
378
     * @deprecated
379
     *
380
     * @param int $hydrationMode
381
     *
382
     * @return \Doctrine\ORM\Internal\Hydration\AbstractHydrator
383
     */
384
    public function getHydrator($hydrationMode)
385
    {
386
        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...
387
    }
388
389
    /**
390
     * Create a new instance for the given hydration mode.
391
     *
392
     * @param int $hydrationMode
393
     *
394
     * @return \Doctrine\ORM\Internal\Hydration\AbstractHydrator
395
     *
396
     * @throws ORMException
397
     */
398
    public function newHydrator($hydrationMode)
399
    {
400
        return $this->em->newHydrator($hydrationMode);
401
    }
402
403
    /**
404
     * Gets the enabled filters.
405
     *
406
     * @return \Doctrine\ORM\Query\FilterCollection The active filter collection.
407
     */
408
    public function getFilters()
409
    {
410
        return $this->em->getFilters();
411
    }
412
413
    /**
414
     * Checks whether the state of the filter collection is clean.
415
     *
416
     * @return boolean True, if the filter collection is clean.
417
     */
418
    public function isFiltersStateClean()
419
    {
420
        return $this->em->isFiltersStateClean();
421
    }
422
423
    /**
424
     * Checks whether the Entity Manager has filters.
425
     *
426
     * @return boolean True, if the EM has a filter collection.
427
     */
428
    public function hasFilters()
429
    {
430
        return $this->em->hasFilters();
431
    }
432
433
    /**
434
     * Finds an object by its identifier.
435
     *
436
     * This is just a convenient shortcut for getRepository($className)->find($id).
437
     *
438
     * @param string $className The class name of the object to find.
439
     * @param mixed $id         The identity of the object to find.
440
     *
441
     * @return object The found object.
442
     */
443
    public function find($className, $id)
444
    {
445
        return $this->em->find($className, $id);
446
    }
447
448
    /**
449
     * Tells the ObjectManager to make an instance managed and persistent.
450
     *
451
     * The object will be entered into the database as a result of the flush operation.
452
     *
453
     * NOTE: The persist operation always considers objects that are not yet known to
454
     * this ObjectManager as NEW. Do not pass detached objects to the persist operation.
455
     *
456
     * @param object $object The instance to make managed and persistent.
457
     *
458
     * @return void
459
     */
460
    public function persist($object)
461
    {
462
        $this->em->persist($object);
463
    }
464
465
    /**
466
     * Removes an object instance.
467
     *
468
     * A removed object will be removed from the database as a result of the flush operation.
469
     *
470
     * @param object $object The object instance to remove.
471
     *
472
     * @return void
473
     */
474
    public function remove($object)
475
    {
476
        $this->em->remove($object);
477
    }
478
479
    /**
480
     * Merges the state of a detached object into the persistence context
481
     * of this ObjectManager and returns the managed copy of the object.
482
     * The object passed to merge will not become associated/managed with this ObjectManager.
483
     *
484
     * @param object $object
485
     *
486
     * @return object
487
     */
488
    public function merge($object)
489
    {
490
        return $this->em->merge($object);
491
    }
492
493
    /**
494
     * Clears the ObjectManager. All objects that are currently managed
495
     * by this ObjectManager become detached.
496
     *
497
     * @param string|null $objectName if given, only objects of this type will get detached.
498
     *
499
     * @return void
500
     */
501
    public function clear($objectName = null)
502
    {
503
        $this->em->clear($objectName);
504
    }
505
506
    /**
507
     * Detaches an object from the ObjectManager, causing a managed object to
508
     * become detached. Unflushed changes made to the object if any
509
     * (including removal of the object), will not be synchronized to the database.
510
     * Objects which previously referenced the detached object will continue to
511
     * reference it.
512
     *
513
     * @param object $object The object to detach.
514
     *
515
     * @return void
516
     */
517
    public function detach($object)
518
    {
519
        $this->em->detach($object);
520
    }
521
522
    /**
523
     * Refreshes the persistent state of an object from the database,
524
     * overriding any local changes that have not yet been persisted.
525
     *
526
     * @param object $object The object to refresh.
527
     *
528
     * @return void
529
     */
530
    public function refresh($object)
531
    {
532
        $this->em->refresh($object);
533
    }
534
535
    /**
536
     * Flushes all changes to objects that have been queued up to now to the database.
537
     * This effectively synchronizes the in-memory state of managed objects with the
538
     * database.
539
     *
540
     * @return void
541
     */
542
    public function flush()
543
    {
544
        $this->em->flush();
545
    }
546
547
    /**
548
     * Gets the repository for a class.
549
     *
550
     * @param string $className
551
     *
552
     * @return \Doctrine\Common\Persistence\ObjectRepository
553
     */
554
    public function getRepository($className)
555
    {
556
        return $this->em->getRepository($className);
557
    }
558
559
    /**
560
     * Gets the metadata factory used to gather the metadata of classes.
561
     *
562
     * @return \Doctrine\Common\Persistence\Mapping\ClassMetadataFactory
563
     */
564
    public function getMetadataFactory()
565
    {
566
        return $this->em->getMetadataFactory();
567
    }
568
569
    /**
570
     * Helper method to initialize a lazy loading proxy or persistent collection.
571
     *
572
     * This method is a no-op for other objects.
573
     *
574
     * @param object $obj
575
     *
576
     * @return void
577
     */
578
    public function initializeObject($obj)
579
    {
580
        $this->em->initializeObject($obj);
581
    }
582
583
    /**
584
     * Checks if the object is part of the current UnitOfWork and therefore managed.
585
     *
586
     * @param object $object
587
     *
588
     * @return bool
589
     */
590
    public function contains($object)
591
    {
592
        return $this->em->contains($object);
593
    }
594
595
    /**
596
     * @param string $className
597
     * @return Mapping\ClassMetadata
598
     */
599
    public function getClassMetadata($className) : Mapping\ClassMetadata
600
    {
601
        return $this->em->getClassMetadata($className);
602
    }
603
}
604