1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\Tests; |
4
|
|
|
|
5
|
|
|
use Doctrine\Bundle\DoctrineBundle\Registry; |
6
|
|
|
use stdClass; |
7
|
|
|
|
8
|
|
|
class RegistryTest extends TestCase |
9
|
|
|
{ |
10
|
|
View Code Duplication |
public function testGetDefaultConnectionName() |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
13
|
|
|
$registry = new Registry($container, [], [], 'default', 'default'); |
|
|
|
|
14
|
|
|
|
15
|
|
|
$this->assertEquals('default', $registry->getDefaultConnectionName()); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
View Code Duplication |
public function testGetDefaultEntityManagerName() |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
21
|
|
|
$registry = new Registry($container, [], [], 'default', 'default'); |
|
|
|
|
22
|
|
|
|
23
|
|
|
$this->assertEquals('default', $registry->getDefaultManagerName()); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testGetDefaultConnection() |
27
|
|
|
{ |
28
|
|
|
$conn = $this->getMockBuilder('Doctrine\DBAL\Connection')->disableOriginalConstructor()->getMock(); |
29
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
30
|
|
|
$container->expects($this->once()) |
31
|
|
|
->method('get') |
32
|
|
|
->with($this->equalTo('doctrine.dbal.default_connection')) |
33
|
|
|
->will($this->returnValue($conn)); |
34
|
|
|
|
35
|
|
|
$registry = new Registry($container, ['default' => 'doctrine.dbal.default_connection'], [], 'default', 'default'); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$this->assertSame($conn, $registry->getConnection()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
View Code Duplication |
public function testGetConnection() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$conn = $this->getMockBuilder('Doctrine\DBAL\Connection')->disableOriginalConstructor()->getMock(); |
43
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
44
|
|
|
$container->expects($this->once()) |
45
|
|
|
->method('get') |
46
|
|
|
->with($this->equalTo('doctrine.dbal.default_connection')) |
47
|
|
|
->will($this->returnValue($conn)); |
48
|
|
|
|
49
|
|
|
$registry = new Registry($container, ['default' => 'doctrine.dbal.default_connection'], [], 'default', 'default'); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$this->assertSame($conn, $registry->getConnection('default')); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @expectedException \InvalidArgumentException |
56
|
|
|
* @expectedExceptionMessage Doctrine ORM Connection named "default" does not exist. |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function testGetUnknownConnection() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
61
|
|
|
$registry = new Registry($container, [], [], 'default', 'default'); |
|
|
|
|
62
|
|
|
|
63
|
|
|
$registry->getConnection('default'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testGetConnectionNames() |
67
|
|
|
{ |
68
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
69
|
|
|
$registry = new Registry($container, ['default' => 'doctrine.dbal.default_connection'], [], 'default', 'default'); |
|
|
|
|
70
|
|
|
|
71
|
|
|
$this->assertEquals(['default' => 'doctrine.dbal.default_connection'], $registry->getConnectionNames()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
public function testGetDefaultEntityManager() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$em = new stdClass(); |
77
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
78
|
|
|
$container->expects($this->once()) |
79
|
|
|
->method('get') |
80
|
|
|
->with($this->equalTo('doctrine.orm.default_entity_manager')) |
81
|
|
|
->will($this->returnValue($em)); |
82
|
|
|
|
83
|
|
|
$registry = new Registry($container, [], ['default' => 'doctrine.orm.default_entity_manager'], 'default', 'default'); |
|
|
|
|
84
|
|
|
|
85
|
|
|
$this->assertSame($em, $registry->getManager()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
public function testGetEntityManager() |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$em = new stdClass(); |
91
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
92
|
|
|
$container->expects($this->once()) |
93
|
|
|
->method('get') |
94
|
|
|
->with($this->equalTo('doctrine.orm.default_entity_manager')) |
95
|
|
|
->will($this->returnValue($em)); |
96
|
|
|
|
97
|
|
|
$registry = new Registry($container, [], ['default' => 'doctrine.orm.default_entity_manager'], 'default', 'default'); |
|
|
|
|
98
|
|
|
|
99
|
|
|
$this->assertSame($em, $registry->getManager('default')); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @expectedException \InvalidArgumentException |
104
|
|
|
* @expectedExceptionMessage Doctrine ORM Manager named "default" does not exist. |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
public function testGetUnknownEntityManager() |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
109
|
|
|
$registry = new Registry($container, [], [], 'default', 'default'); |
|
|
|
|
110
|
|
|
|
111
|
|
|
$registry->getManager('default'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @expectedException \InvalidArgumentException |
116
|
|
|
* @expectedExceptionMessage Doctrine ORM Manager named "default" does not exist. |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public function testResetUnknownEntityManager() |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
121
|
|
|
$registry = new Registry($container, [], [], 'default', 'default'); |
|
|
|
|
122
|
|
|
|
123
|
|
|
$registry->resetManager('default'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
View Code Duplication |
public function testReset() |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$conn = $this->getMockBuilder('Doctrine\DBAL\Connection')->disableOriginalConstructor()->getMock(); |
129
|
|
|
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager')->disableOriginalConstructor()->getMock(); |
130
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
131
|
|
|
|
132
|
|
|
$container->expects($this->once()) |
133
|
|
|
->method('get') |
134
|
|
|
->with($this->equalTo('doctrine.orm.default_entity_manager')) |
135
|
|
|
->will($this->returnValue($em)); |
136
|
|
|
|
137
|
|
|
$registry = new Registry( |
138
|
|
|
$container, |
|
|
|
|
139
|
|
|
[ |
|
|
|
|
140
|
|
|
'default' => $conn, |
141
|
|
|
], |
142
|
|
|
[ |
143
|
|
|
'default' => 'doctrine.orm.default_entity_manager', |
144
|
|
|
], |
145
|
|
|
'default', |
146
|
|
|
'default' |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
$registry->reset(); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
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.