Failed Conditions
Pull Request — develop (#6596)
by
unknown
64:04
created

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