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\Utility\IdentifierFlattener; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Special EntityManager mock used for testing purposes. |
17
|
|
|
*/ |
18
|
|
|
class EntityManagerMock extends EntityManagerDecorator |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var \Doctrine\ORM\UnitOfWork|null |
22
|
|
|
*/ |
23
|
|
|
private $uowMock; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return EntityManagerInterface |
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 \Doctrine\ORM\UnitOfWork $uow |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
public function setUnitOfWork($uow) |
49
|
|
|
{ |
50
|
|
|
$this->uowMock = $uow; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return \Doctrine\ORM\Proxy\Factory\ProxyFactory |
55
|
|
|
*/ |
56
|
|
|
public function getProxyFactory() |
57
|
|
|
{ |
58
|
|
|
return $this->proxyFactoryMock ?? $this->wrapped->getProxyFactory(); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Mock factory method to create an EntityManager. |
63
|
|
|
* |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public static function create($conn, Configuration $config = null, EventManager $eventManager = null) |
67
|
|
|
{ |
68
|
|
|
if (null === $config) { |
69
|
|
|
$config = new Configuration(); |
70
|
|
|
|
71
|
|
|
$config->setProxyNamespace('Doctrine\Tests\Proxies'); |
72
|
|
|
$config->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_EVAL); |
73
|
|
|
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (null === $eventManager) { |
77
|
|
|
$eventManager = $conn->getEventManager(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$em = EntityManager::create($conn, $config, $eventManager); |
81
|
|
|
$emMock = new EntityManagerMock($em); |
82
|
|
|
$uowMock = new UnitOfWorkMock($emMock); |
83
|
|
|
|
84
|
|
|
// Ugly hacks due to cyclic dependencies |
85
|
|
|
// in the tests, we cannot afford having two different UnitOfWork instances |
86
|
|
|
$uowReflection = new \ReflectionProperty($em, 'unitOfWork'); |
87
|
|
|
$identifierFlattenerReflection = new \ReflectionProperty($em, 'identifierFlattener'); |
88
|
|
|
|
89
|
|
|
$uowReflection->setAccessible(true); |
90
|
|
|
$identifierFlattenerReflection->setAccessible(true); |
91
|
|
|
|
92
|
|
|
$uowReflection->setValue($em, $uowMock); |
93
|
|
|
$identifierFlattenerReflection->setValue($em, new IdentifierFlattener($uowMock, $em->getMetadataFactory())); |
94
|
|
|
|
95
|
|
|
return $emMock; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: