1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\Tests; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Doctrine\Bundle\DoctrineBundle\Registry; |
7
|
|
|
use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\TestKernel; |
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
9
|
|
|
use Fixtures\Bundles\RepositoryServiceBundle\Entity\TestCustomClassRepoEntity; |
10
|
|
|
use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomClassRepoRepository; |
11
|
|
|
use ProxyManager\Proxy\LazyLoadingInterface; |
12
|
|
|
use ProxyManager\Proxy\ProxyInterface; |
13
|
|
|
use stdClass; |
14
|
|
|
|
15
|
|
|
class RegistryTest extends TestCase |
16
|
|
|
{ |
17
|
|
View Code Duplication |
public function testGetDefaultConnectionName() : void |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
20
|
|
|
$registry = new Registry($container, [], [], 'default', 'default'); |
|
|
|
|
21
|
|
|
|
22
|
|
|
$this->assertEquals('default', $registry->getDefaultConnectionName()); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
View Code Duplication |
public function testGetDefaultEntityManagerName() : void |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
28
|
|
|
$registry = new Registry($container, [], [], 'default', 'default'); |
|
|
|
|
29
|
|
|
|
30
|
|
|
$this->assertEquals('default', $registry->getDefaultManagerName()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
View Code Duplication |
public function testGetDefaultConnection() : void |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$conn = $this->getMockBuilder('Doctrine\DBAL\Connection')->disableOriginalConstructor()->getMock(); |
36
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
37
|
|
|
$container->expects($this->once()) |
38
|
|
|
->method('get') |
39
|
|
|
->with($this->equalTo('doctrine.dbal.default_connection')) |
40
|
|
|
->will($this->returnValue($conn)); |
41
|
|
|
|
42
|
|
|
$registry = new Registry($container, ['default' => 'doctrine.dbal.default_connection'], [], 'default', 'default'); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$this->assertSame($conn, $registry->getConnection()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
public function testGetConnection() : void |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$conn = $this->getMockBuilder('Doctrine\DBAL\Connection')->disableOriginalConstructor()->getMock(); |
50
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
51
|
|
|
$container->expects($this->once()) |
52
|
|
|
->method('get') |
53
|
|
|
->with($this->equalTo('doctrine.dbal.default_connection')) |
54
|
|
|
->will($this->returnValue($conn)); |
55
|
|
|
|
56
|
|
|
$registry = new Registry($container, ['default' => 'doctrine.dbal.default_connection'], [], 'default', 'default'); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$this->assertSame($conn, $registry->getConnection('default')); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @expectedException \InvalidArgumentException |
63
|
|
|
* @expectedExceptionMessage Doctrine ORM Connection named "default" does not exist. |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function testGetUnknownConnection() : void |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
68
|
|
|
$registry = new Registry($container, [], [], 'default', 'default'); |
|
|
|
|
69
|
|
|
|
70
|
|
|
$registry->getConnection('default'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testGetConnectionNames() : void |
74
|
|
|
{ |
75
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
76
|
|
|
$registry = new Registry($container, ['default' => 'doctrine.dbal.default_connection'], [], 'default', 'default'); |
|
|
|
|
77
|
|
|
|
78
|
|
|
$this->assertEquals(['default' => 'doctrine.dbal.default_connection'], $registry->getConnectionNames()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
View Code Duplication |
public function testGetDefaultEntityManager() : void |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$em = new stdClass(); |
84
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
85
|
|
|
$container->expects($this->once()) |
86
|
|
|
->method('get') |
87
|
|
|
->with($this->equalTo('doctrine.orm.default_entity_manager')) |
88
|
|
|
->will($this->returnValue($em)); |
89
|
|
|
|
90
|
|
|
$registry = new Registry($container, [], ['default' => 'doctrine.orm.default_entity_manager'], 'default', 'default'); |
|
|
|
|
91
|
|
|
|
92
|
|
|
$this->assertSame($em, $registry->getManager()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
public function testGetEntityManager() : void |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$em = new stdClass(); |
98
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
99
|
|
|
$container->expects($this->once()) |
100
|
|
|
->method('get') |
101
|
|
|
->with($this->equalTo('doctrine.orm.default_entity_manager')) |
102
|
|
|
->will($this->returnValue($em)); |
103
|
|
|
|
104
|
|
|
$registry = new Registry($container, [], ['default' => 'doctrine.orm.default_entity_manager'], 'default', 'default'); |
|
|
|
|
105
|
|
|
|
106
|
|
|
$this->assertSame($em, $registry->getManager('default')); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @expectedException \InvalidArgumentException |
111
|
|
|
* @expectedExceptionMessage Doctrine ORM Manager named "default" does not exist. |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
public function testGetUnknownEntityManager() : void |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
116
|
|
|
$registry = new Registry($container, [], [], 'default', 'default'); |
|
|
|
|
117
|
|
|
|
118
|
|
|
$registry->getManager('default'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @expectedException \InvalidArgumentException |
123
|
|
|
* @expectedExceptionMessage Doctrine ORM Manager named "default" does not exist. |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function testResetUnknownEntityManager() : void |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
128
|
|
|
$registry = new Registry($container, [], [], 'default', 'default'); |
|
|
|
|
129
|
|
|
|
130
|
|
|
$registry->resetManager('default'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testReset() : void |
134
|
|
|
{ |
135
|
|
|
$noProxyManager = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); |
136
|
|
|
$noProxyManager->expects($this->once()) |
137
|
|
|
->method('clear'); |
138
|
|
|
|
139
|
|
|
$proxyManager = $this->getMockBuilder([LazyLoadingInterface::class, EntityManagerInterface::class])->getMock(); |
140
|
|
|
$proxyManager->expects($this->once()) |
141
|
|
|
->method('setProxyInitializer') |
142
|
|
|
->with($this->isInstanceOf(Closure::class)); |
143
|
|
|
|
144
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
145
|
|
|
$container->expects($this->any()) |
146
|
|
|
->method('initialized') |
147
|
|
|
->withConsecutive(['doctrine.orm.uninitialized_entity_manager'], ['doctrine.orm.noproxy_entity_manager'], ['doctrine.orm.proxy_entity_manager']) |
148
|
|
|
->willReturnOnConsecutiveCalls(false, true, true, true); |
149
|
|
|
|
150
|
|
|
$container->expects($this->any()) |
151
|
|
|
->method('get') |
152
|
|
|
->withConsecutive(['doctrine.orm.noproxy_entity_manager'], ['doctrine.orm.proxy_entity_manager'], ['doctrine.orm.proxy_entity_manager']) |
153
|
|
|
->willReturnOnConsecutiveCalls($noProxyManager, $proxyManager, $proxyManager); |
154
|
|
|
|
155
|
|
|
$entityManagers = [ |
156
|
|
|
'uninitialized' => 'doctrine.orm.uninitialized_entity_manager', |
157
|
|
|
'noproxy' => 'doctrine.orm.noproxy_entity_manager', |
158
|
|
|
'proxy' => 'doctrine.orm.proxy_entity_manager', |
159
|
|
|
]; |
160
|
|
|
|
161
|
|
|
$registry = new Registry($container, [], $entityManagers, 'default', 'default'); |
|
|
|
|
162
|
|
|
$registry->reset(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function testIdentityMapsStayConsistentAfterReset() |
166
|
|
|
{ |
167
|
|
|
$kernel = new TestKernel(); |
168
|
|
|
$kernel->boot(); |
169
|
|
|
|
170
|
|
|
$container = $kernel->getContainer(); |
171
|
|
|
$registry = $container->get('doctrine'); |
172
|
|
|
$entityManager = $container->get('doctrine.orm.default_entity_manager'); |
173
|
|
|
$repository = $entityManager->getRepository(TestCustomClassRepoEntity::class); |
174
|
|
|
|
175
|
|
|
$this->assertInstanceOf(ProxyInterface::class, $entityManager); |
176
|
|
|
assert($entityManager instanceof EntityManagerInterface); |
177
|
|
|
assert($registry instanceof Registry); |
178
|
|
|
assert($repository instanceof TestCustomClassRepoRepository); |
179
|
|
|
|
180
|
|
|
$entity = new TestCustomClassRepoEntity(); |
181
|
|
|
$repository->getEntityManager()->persist($entity); |
182
|
|
|
|
183
|
|
|
$this->assertTrue($entityManager->getUnitOfWork()->isEntityScheduled($entity)); |
184
|
|
|
$this->assertTrue($repository->getEntityManager()->getUnitOfWork()->isEntityScheduled($entity)); |
185
|
|
|
|
186
|
|
|
$registry->reset(); |
187
|
|
|
|
188
|
|
|
$this->assertFalse($entityManager->getUnitOfWork()->isEntityScheduled($entity)); |
189
|
|
|
$this->assertFalse($repository->getEntityManager()->getUnitOfWork()->isEntityScheduled($entity)); |
190
|
|
|
|
191
|
|
|
$entityManager->flush(); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.