1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\Common\Persistence; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\AbstractManagerRegistry; |
6
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
7
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; |
8
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
9
|
|
|
use Doctrine\Common\Persistence\ObjectManagerAware; |
10
|
|
|
use Doctrine\Tests\Common\Persistence\Mapping\TestClassMetadataFactory; |
11
|
|
|
use Doctrine\Tests\DoctrineTestCase; |
12
|
|
|
use ReflectionException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @groups DCOM-270 |
16
|
|
|
* @uses Doctrine\Tests\Common\Persistence\TestObject |
17
|
|
|
*/ |
18
|
|
|
class ManagerRegistryTest extends DoctrineTestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var TestManagerRegistry |
22
|
|
|
*/ |
23
|
|
|
private $mr; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function setUp() |
29
|
|
|
{ |
30
|
|
|
$this->mr = new TestManagerRegistry( |
31
|
|
|
'ORM', |
32
|
|
|
['default' => 'default_connection'], |
33
|
|
|
['default' => 'default_manager'], |
34
|
|
|
'default', |
35
|
|
|
'default', |
36
|
|
|
ObjectManagerAware::class, |
37
|
|
|
$this->getManagerFactory() |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testGetManagerForClass() |
42
|
|
|
{ |
43
|
|
|
self::assertNull($this->mr->getManagerForClass(TestObject::class)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testGetManagerForProxyInterface() |
47
|
|
|
{ |
48
|
|
|
self::assertNull($this->mr->getManagerForClass(ObjectManagerAware::class)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testGetManagerForInvalidClass() |
52
|
|
|
{ |
53
|
|
|
$this->expectException(ReflectionException::class); |
54
|
|
|
$this->expectExceptionMessage('Class Doctrine\Tests\Common\Persistence\TestObjectInexistent does not exist'); |
55
|
|
|
|
56
|
|
|
$this->mr->getManagerForClass('prefix:TestObjectInexistent'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testGetManagerForAliasedClass() |
60
|
|
|
{ |
61
|
|
|
self::assertNull($this->mr->getManagerForClass('prefix:TestObject')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testGetManagerForInvalidAliasedClass() |
65
|
|
|
{ |
66
|
|
|
$this->expectException(ReflectionException::class); |
67
|
|
|
$this->expectExceptionMessage('Class Doctrine\Tests\Common\Persistence\TestObject:Foo does not exist'); |
68
|
|
|
|
69
|
|
|
$this->mr->getManagerForClass('prefix:TestObject:Foo'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testResetManager() |
73
|
|
|
{ |
74
|
|
|
$manager = $this->mr->getManager(); |
75
|
|
|
$newManager = $this->mr->resetManager(); |
76
|
|
|
|
77
|
|
|
self::assertInstanceOf(ObjectManager::class, $newManager); |
78
|
|
|
self::assertNotSame($manager, $newManager); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function getManagerFactory() |
82
|
|
|
{ |
83
|
|
|
return function () { |
84
|
|
|
$mock = $this->createMock(ObjectManager::class); |
85
|
|
|
$driver = $this->createMock(MappingDriver::class); |
86
|
|
|
$metadata = $this->createMock(ClassMetadata::class); |
87
|
|
|
$mock->method('getMetadataFactory')->willReturn(new TestClassMetadataFactory($driver, $metadata)); |
88
|
|
|
|
89
|
|
|
return $mock; |
90
|
|
|
}; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
class TestManagerRegistry extends AbstractManagerRegistry |
95
|
|
|
{ |
96
|
|
|
private $services; |
97
|
|
|
|
98
|
|
|
private $managerFactory; |
99
|
|
|
|
100
|
|
|
public function __construct($name, array $connections, array $managers, $defaultConnection, $defaultManager, $proxyInterfaceName, callable $managerFactory) |
101
|
|
|
{ |
102
|
|
|
$this->managerFactory = $managerFactory; |
103
|
|
|
|
104
|
|
|
parent::__construct($name, $connections, $managers, $defaultConnection, $defaultManager, $proxyInterfaceName); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function getService($name) |
108
|
|
|
{ |
109
|
|
|
if ( ! isset($this->services[$name])) { |
110
|
|
|
$this->services[$name] = call_user_func($this->managerFactory); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->services[$name]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function resetService($name) |
117
|
|
|
{ |
118
|
|
|
unset($this->services[$name]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function getAliasNamespace($alias) |
122
|
|
|
{ |
123
|
|
|
return __NAMESPACE__; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|