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