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\Decorator\EntityManagerDecorator; |
10
|
|
|
use Doctrine\ORM\EntityManager; |
11
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Special EntityManager mock used for testing purposes. |
15
|
|
|
*/ |
16
|
|
|
class EntityManagerMock extends EntityManagerDecorator |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \Doctrine\ORM\UnitOfWork|null |
20
|
|
|
*/ |
21
|
|
|
private $uowMock; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \Doctrine\ORM\Proxy\Factory\ProxyFactory|null |
25
|
|
|
*/ |
26
|
|
|
private $proxyFactoryMock; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return EntityManagerInterface |
30
|
|
|
*/ |
31
|
|
|
public function getWrappedEntityManager() : EntityManagerInterface |
32
|
|
|
{ |
33
|
|
|
return $this->wrapped; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function getUnitOfWork() |
40
|
|
|
{ |
41
|
|
|
return $this->uowMock ?? $this->wrapped->getUnitOfWork(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Sets a (mock) UnitOfWork that will be returned when getUnitOfWork() is called. |
46
|
|
|
* |
47
|
|
|
* @param \Doctrine\ORM\UnitOfWork $uow |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function setUnitOfWork($uow) |
52
|
|
|
{ |
53
|
|
|
$this->uowMock = $uow; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param \Doctrine\ORM\Proxy\Factory\ProxyFactory $proxyFactory |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public function setProxyFactory($proxyFactory) |
62
|
|
|
{ |
63
|
|
|
$this->proxyFactoryMock = $proxyFactory; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return \Doctrine\ORM\Proxy\Factory\ProxyFactory |
68
|
|
|
*/ |
69
|
|
|
public function getProxyFactory() |
70
|
|
|
{ |
71
|
|
|
return $this->proxyFactoryMock ?? $this->wrapped->getProxyFactory(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Mock factory method to create an EntityManager. |
76
|
|
|
* |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public static function create($conn, Configuration $config = null, EventManager $eventManager = null) |
80
|
|
|
{ |
81
|
|
|
if (null === $config) { |
82
|
|
|
$config = new Configuration(); |
83
|
|
|
|
84
|
|
|
$config->setProxyDir(__DIR__ . '/../Proxies'); |
85
|
|
|
$config->setProxyNamespace('Doctrine\Tests\Proxies'); |
86
|
|
|
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (null === $eventManager) { |
90
|
|
|
$eventManager = $conn->getEventManager(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$em = EntityManager::create($conn, $config, $eventManager); |
94
|
|
|
|
95
|
|
|
return new EntityManagerMock($em); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|