1 | <?php |
||
21 | */ |
||
22 | class ManagerBuilderTest extends \PHPUnit_Framework_TestCase |
||
23 | { |
||
24 | public function testSingleManagers() |
||
25 | { |
||
26 | $settings = [ |
||
27 | 'entity_manager' => [ |
||
28 | 'connection' => [ |
||
29 | 'driver' => 'pdo_sqlite', |
||
30 | 'memory' => true, |
||
31 | ], |
||
32 | 'metadata_mapping' => [ |
||
33 | [ |
||
34 | 'type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION, |
||
35 | 'path' => __DIR__, |
||
36 | ], |
||
37 | ], |
||
38 | ], |
||
39 | 'mongodb_document_manager' => [ |
||
40 | 'connection' => [ |
||
41 | 'server' => 'mongodb://localhost:27017', |
||
42 | 'options' => ['connect' => false], |
||
43 | ], |
||
44 | 'metadata_mapping' => [ |
||
45 | [ |
||
46 | 'type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION, |
||
47 | 'path' => __DIR__, |
||
48 | ], |
||
49 | ], |
||
50 | ], |
||
51 | 'couchdb_document_manager' => [ |
||
52 | 'connection' => [ |
||
53 | 'host' => 'localhost', |
||
54 | 'dbname' => 'doctrine', |
||
55 | ], |
||
56 | 'metadata_mapping' => [ |
||
57 | [ |
||
58 | 'type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION, |
||
59 | 'path' => __DIR__, |
||
60 | ], |
||
61 | ], |
||
62 | ], |
||
63 | ]; |
||
64 | |||
65 | $managerBuilder = (new ManagerBuilder())->loadSettings($settings); |
||
66 | |||
67 | $managers = $managerBuilder->getManagers(); |
||
68 | |||
69 | self::assertCount(3, $managers); |
||
70 | |||
71 | self::assertInstanceOf(EntityManager::class, $managerBuilder->getManager('entityManager')); |
||
72 | self::assertEquals($managers['entityManager'], $managerBuilder->getManager('entityManager')); |
||
73 | |||
74 | self::assertInstanceOf(MongoDBDocumentManager::class, $managerBuilder->getManager('mongoDocumentManager')); |
||
75 | self::assertEquals($managers['mongoDocumentManager'], $managerBuilder->getManager('mongoDocumentManager')); |
||
76 | |||
77 | self::assertInstanceOf(CouchDBDocumentManager::class, $managerBuilder->getManager('couchDocumentManager')); |
||
78 | self::assertEquals($managers['couchDocumentManager'], $managerBuilder->getManager('couchDocumentManager')); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @expectedException \RuntimeException |
||
83 | * @expectedExceptionMessage "managerOne" manager builder is already registered |
||
84 | */ |
||
85 | public function testDuplicatedNamedManager() |
||
86 | { |
||
87 | $settings = [ |
||
88 | 'entity_manager' => [ |
||
89 | 'managerOne' => [ |
||
90 | 'connection' => [ |
||
91 | 'driver' => 'pdo_sqlite', |
||
92 | 'memory' => true, |
||
93 | ], |
||
94 | 'metadata_mapping' => [ |
||
95 | [ |
||
96 | 'type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION, |
||
97 | 'path' => __DIR__, |
||
98 | ], |
||
99 | ], |
||
100 | ], |
||
101 | ], |
||
102 | 'mongodb_document_manager' => [ |
||
103 | 'managerOne' => [ |
||
104 | 'connection' => [ |
||
105 | 'server' => 'mongodb://localhost:27017', |
||
106 | 'options' => ['connect' => false], |
||
107 | ], |
||
108 | 'metadata_mapping' => [ |
||
109 | [ |
||
110 | 'type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION, |
||
111 | 'path' => __DIR__, |
||
112 | ], |
||
113 | ], |
||
114 | ], |
||
115 | ], |
||
116 | ]; |
||
117 | |||
118 | (new ManagerBuilder())->loadSettings($settings); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @expectedException \RuntimeException |
||
123 | * @expectedExceptionMessage "noOnesManager" is not a registered manager |
||
124 | */ |
||
125 | public function testNoRegisteredManager() |
||
126 | { |
||
127 | self::assertCount(0, (new ManagerBuilder())->getManagers()); |
||
128 | |||
129 | $settings = [ |
||
130 | 'entity_manager' => [ |
||
131 | 'connection' => [ |
||
132 | 'driver' => 'pdo_sqlite', |
||
133 | 'memory' => true, |
||
134 | ], |
||
135 | 'metadata_mapping' => [ |
||
136 | [ |
||
137 | 'type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION, |
||
138 | 'path' => __DIR__, |
||
139 | ], |
||
140 | ], |
||
141 | ], |
||
142 | ]; |
||
143 | |||
144 | $managerBuilder = (new ManagerBuilder())->loadSettings($settings); |
||
145 | |||
146 | $managerBuilder->getManager('noOnesManager'); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @expectedException \RuntimeException |
||
151 | * @expectedExceptionMessage Only named manager builders allowed |
||
152 | */ |
||
153 | public function testUnnamedBuilder() |
||
154 | { |
||
155 | $builder = $this->getMockBuilder(AbstractManagerBuilder::class) |
||
156 | ->disableOriginalConstructor() |
||
157 | ->getMockForAbstractClass(); |
||
158 | /* @var AbstractManagerBuilder $builder */ |
||
159 | |||
160 | (new ManagerBuilder())->addBuilder($builder); |
||
161 | } |
||
162 | |||
163 | public function testCLIApplication() |
||
164 | { |
||
165 | $settings = [ |
||
166 | 'entity_manager' => [ |
||
167 | 'managerOne' => [ |
||
168 | 'connection' => [ |
||
169 | 'driver' => 'pdo_sqlite', |
||
170 | 'memory' => true, |
||
171 | ], |
||
172 | 'metadata_mapping' => [ |
||
173 | [ |
||
174 | 'type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION, |
||
175 | 'path' => __DIR__, |
||
176 | ], |
||
177 | ], |
||
178 | ], |
||
179 | ], |
||
180 | 'mongodb_document_manager' => [ |
||
181 | 'managerTwo' => [ |
||
182 | 'connection' => [ |
||
183 | 'server' => 'mongodb://localhost:27017', |
||
184 | 'options' => ['connect' => false], |
||
185 | ], |
||
186 | 'metadata_mapping' => [ |
||
187 | [ |
||
188 | 'type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION, |
||
189 | 'path' => __DIR__, |
||
190 | ], |
||
191 | ], |
||
192 | ], |
||
193 | ], |
||
194 | ]; |
||
195 | |||
196 | $managerBuilder = (new ManagerBuilder())->loadSettings($settings); |
||
197 | |||
198 | $application = $managerBuilder->getCLIApplication(); |
||
199 | |||
200 | self::assertTrue($application->has('managerOne:dbal:run-sql')); |
||
201 | self::assertTrue($application->has('managerTwo:odm:query')); |
||
202 | } |
||
203 | } |
||
204 |