1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\Persistence; |
6
|
|
|
|
7
|
|
|
use Doctrine\Persistence\AbstractManagerRegistry; |
8
|
|
|
use Doctrine\Persistence\Mapping\ClassMetadata; |
9
|
|
|
use Doctrine\Persistence\Mapping\Driver\MappingDriver; |
10
|
|
|
use Doctrine\Persistence\ObjectManager; |
11
|
|
|
use Doctrine\Persistence\ObjectManagerAware; |
12
|
|
|
use Doctrine\Persistence\ObjectRepository; |
13
|
|
|
use Doctrine\Persistence\Proxy; |
14
|
|
|
use Doctrine\Tests\DoctrineTestCase; |
15
|
|
|
use Doctrine\Tests\Persistence\Mapping\TestClassMetadataFactory; |
16
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
17
|
|
|
use ReflectionException; |
18
|
|
|
use function call_user_func; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @uses Doctrine\Tests\Persistence\TestObject |
22
|
|
|
* |
23
|
|
|
* @groups DCOM-270 |
24
|
|
|
*/ |
25
|
|
|
class ManagerRegistryTest extends DoctrineTestCase |
26
|
|
|
{ |
27
|
|
|
/** @var TestManagerRegistry */ |
28
|
|
|
private $mr; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
protected function setUp() : void |
34
|
|
|
{ |
35
|
|
|
$this->mr = new TestManagerRegistry( |
36
|
|
|
'ORM', |
37
|
|
|
['default' => 'default_connection'], |
38
|
|
|
['default' => 'default_manager'], |
39
|
|
|
'default', |
40
|
|
|
'default', |
41
|
|
|
Proxy::class, |
42
|
|
|
$this->getManagerFactory() |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testGetManagerForClass() : void |
47
|
|
|
{ |
48
|
|
|
self::assertInstanceOf( |
49
|
|
|
ObjectManager::class, |
50
|
|
|
$this->mr->getManagerForClass(TestObject::class) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testGetManagerForProxyInterface() : void |
55
|
|
|
{ |
56
|
|
|
self::assertNull($this->mr->getManagerForClass(ObjectManagerAware::class)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testGetManagerForInvalidClass() : void |
60
|
|
|
{ |
61
|
|
|
$this->expectException(ReflectionException::class); |
62
|
|
|
$this->expectExceptionMessage('Class Doctrine\Tests\Persistence\TestObjectInexistent does not exist'); |
63
|
|
|
|
64
|
|
|
$this->mr->getManagerForClass('prefix:TestObjectInexistent'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testGetManagerForAliasedClass() : void |
68
|
|
|
{ |
69
|
|
|
self::assertInstanceOf( |
70
|
|
|
ObjectManager::class, |
71
|
|
|
$this->mr->getManagerForClass('prefix:TestObject') |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testGetManagerForInvalidAliasedClass() : void |
76
|
|
|
{ |
77
|
|
|
$this->expectException(ReflectionException::class); |
78
|
|
|
$this->expectExceptionMessage('Class Doctrine\Tests\Persistence\TestObject:Foo does not exist'); |
79
|
|
|
|
80
|
|
|
$this->mr->getManagerForClass('prefix:TestObject:Foo'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testResetManager() : void |
84
|
|
|
{ |
85
|
|
|
$manager = $this->mr->getManager(); |
86
|
|
|
$newManager = $this->mr->resetManager(); |
87
|
|
|
|
88
|
|
|
self::assertNotSame($manager, $newManager); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testGetRepository() : void |
92
|
|
|
{ |
93
|
|
|
$repository = $this->createMock(ObjectRepository::class); |
94
|
|
|
|
95
|
|
|
/** @var MockObject $defaultManager */ |
96
|
|
|
$defaultManager = $this->mr->getManager(); |
97
|
|
|
$defaultManager |
98
|
|
|
->expects(self::once()) |
99
|
|
|
->method('getRepository') |
100
|
|
|
->with(self::equalTo(TestObject::class)) |
101
|
|
|
->will(self::returnValue($repository)); |
102
|
|
|
|
103
|
|
|
self::assertSame($repository, $this->mr->getRepository(TestObject::class)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testGetRepositoryWithSpecificManagerName() : void |
107
|
|
|
{ |
108
|
|
|
$this->mr = new TestManagerRegistry( |
109
|
|
|
'ORM', |
110
|
|
|
['default' => 'default_connection'], |
111
|
|
|
['default' => 'default_manager', 'other' => 'other_manager'], |
112
|
|
|
'default', |
113
|
|
|
'default', |
114
|
|
|
ObjectManagerAware::class, |
115
|
|
|
$this->getManagerFactory() |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
$repository = $this->createMock(ObjectRepository::class); |
119
|
|
|
|
120
|
|
|
/** @var MockObject $defaultManager */ |
121
|
|
|
$defaultManager = $this->mr->getManager(); |
122
|
|
|
$defaultManager |
123
|
|
|
->expects(self::never()) |
124
|
|
|
->method('getRepository'); |
125
|
|
|
|
126
|
|
|
/** @var MockObject $otherManager */ |
127
|
|
|
$otherManager = $this->mr->getManager('other'); |
128
|
|
|
$otherManager |
129
|
|
|
->expects(self::once()) |
130
|
|
|
->method('getRepository') |
131
|
|
|
->with(self::equalTo(TestObject::class)) |
132
|
|
|
->will(self::returnValue($repository)); |
133
|
|
|
|
134
|
|
|
self::assertSame($repository, $this->mr->getRepository(TestObject::class, 'other')); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testGetRepositoryWithManagerDetection() : void |
138
|
|
|
{ |
139
|
|
|
$this->mr = new TestManagerRegistry( |
140
|
|
|
'ORM', |
141
|
|
|
['default' => 'default_connection'], |
142
|
|
|
['default' => 'default_manager', 'other' => 'other_manager'], |
143
|
|
|
'default', |
144
|
|
|
'default', |
145
|
|
|
Proxy::class, |
146
|
|
|
$this->getManagerFactory() |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
$repository = $this->createMock(ObjectRepository::class); |
150
|
|
|
|
151
|
|
|
/** @var MockObject $defaultManager */ |
152
|
|
|
$defaultManager = $this->mr->getManager(); |
153
|
|
|
$defaultManager |
154
|
|
|
->expects(self::never()) |
155
|
|
|
->method('getRepository'); |
156
|
|
|
|
157
|
|
|
/** @var MockObject $otherManager */ |
158
|
|
|
$otherManager = $this->mr->getManager('other'); |
159
|
|
|
$otherManager |
160
|
|
|
->expects(self::once()) |
161
|
|
|
->method('getRepository') |
162
|
|
|
->with(self::equalTo(OtherTestObject::class)) |
163
|
|
|
->will(self::returnValue($repository)); |
164
|
|
|
|
165
|
|
|
self::assertSame($repository, $this->mr->getRepository(OtherTestObject::class)); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
private function getManagerFactory() : callable |
169
|
|
|
{ |
170
|
|
|
return function (string $name) { |
171
|
|
|
$mock = $this->createMock(ObjectManager::class); |
172
|
|
|
|
173
|
|
|
$driver = $this->createMock(MappingDriver::class); |
174
|
|
|
$metadata = $this->createMock(ClassMetadata::class); |
175
|
|
|
|
176
|
|
|
$metadata |
177
|
|
|
->expects(self::any()) |
178
|
|
|
->method('getName') |
179
|
|
|
->willReturn($name === 'other_manager' ? OtherTestObject::class : TestObject::class); |
180
|
|
|
|
181
|
|
|
$mock->method('getMetadataFactory')->willReturn(new TestClassMetadataFactory($driver, $metadata)); |
182
|
|
|
|
183
|
|
|
return $mock; |
184
|
|
|
}; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
class TestManagerRegistry extends AbstractManagerRegistry |
189
|
|
|
{ |
190
|
|
|
/** @var object[] */ |
191
|
|
|
private $services; |
192
|
|
|
|
193
|
|
|
/** @var callable */ |
194
|
|
|
private $managerFactory; |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param string[] $connections |
198
|
|
|
* @param string[] $managers |
199
|
|
|
*/ |
200
|
|
|
public function __construct( |
201
|
|
|
string $name, |
202
|
|
|
array $connections, |
203
|
|
|
array $managers, |
204
|
|
|
string $defaultConnection, |
205
|
|
|
string $defaultManager, |
206
|
|
|
string $proxyInterfaceName, |
207
|
|
|
callable $managerFactory |
208
|
|
|
) { |
209
|
|
|
$this->managerFactory = $managerFactory; |
210
|
|
|
|
211
|
|
|
parent::__construct( |
212
|
|
|
$name, |
213
|
|
|
$connections, |
214
|
|
|
$managers, |
215
|
|
|
$defaultConnection, |
216
|
|
|
$defaultManager, |
217
|
|
|
$proxyInterfaceName |
218
|
|
|
); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
protected function getService(string $name) : object |
222
|
|
|
{ |
223
|
|
|
if (! isset($this->services[$name])) { |
224
|
|
|
$this->services[$name] = call_user_func($this->managerFactory, $name); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $this->services[$name]; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
protected function resetService(string $name) : void |
231
|
|
|
{ |
232
|
|
|
unset($this->services[$name]); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function getAliasNamespace(string $alias) : string |
236
|
|
|
{ |
237
|
|
|
return __NAMESPACE__; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|