55namespace Doctrine \Bundle \MigrationsBundle \Tests \DependencyInjection ;
66
77use Doctrine \Bundle \MigrationsBundle \DependencyInjection \DoctrineMigrationsExtension ;
8+ use Doctrine \Bundle \MigrationsBundle \DoctrineMigrationsBundle ;
89use Doctrine \Bundle \MigrationsBundle \Tests \Fixtures \CustomEntityManager ;
910use Doctrine \DBAL \Connection ;
1011use Doctrine \Migrations \Configuration \Configuration ;
2526use Symfony \Component \DependencyInjection \ParameterBag \ParameterBag ;
2627use function assert ;
2728use function method_exists ;
28- use function print_r ;
2929use function sys_get_temp_dir ;
3030
3131class DoctrineMigrationsExtensionTest extends TestCase
3232{
3333 public function testXmlConfigs () : void
3434 {
35- $ container = $ this ->getContainer ();
35+ $ container = $ this ->getContainerBuilder ();
3636
3737 $ conn = $ this ->createMock (Connection::class);
3838 $ container ->set ('doctrine.dbal.default_connection ' , $ conn );
3939
4040 $ container ->registerExtension (new DoctrineMigrationsExtension ());
41+
4142 $ container ->setAlias ('doctrine.migrations.configuration.test ' , new Alias ('doctrine.migrations.configuration ' , true ));
4243
4344 $ loader = new XmlFileLoader ($ container , new FileLocator (__DIR__ . '/../Fixtures ' ));
@@ -51,9 +52,6 @@ public function testXmlConfigs() : void
5152
5253 public function testFullConfig () : void
5354 {
54- $ container = $ this ->getContainer ();
55- $ extension = new DoctrineMigrationsExtension ();
56-
5755 $ config = [
5856 'name ' => 'Doctrine Sandbox Migrations ' ,
5957 'storage ' => [
@@ -78,13 +76,11 @@ public function testFullConfig() : void
7876 'all_or_nothing ' => true ,
7977 'check_database_platform ' => true ,
8078 ];
79+ $ container = $ this ->getContainer ($ config );
8180
8281 $ conn = $ this ->createMock (Connection::class);
8382 $ container ->set ('doctrine.dbal.default_connection ' , $ conn );
8483
85- $ extension ->load (['doctrine_migrations ' => $ config ], $ container );
86-
87- $ container ->getDefinition ('doctrine.migrations.configuration ' )->setPublic (true );
8884 $ container ->compile ();
8985
9086 $ config = $ container ->get ('doctrine.migrations.configuration ' );
@@ -97,20 +93,13 @@ public function testNoConfig() : void
9793 $ this ->expectException (InvalidConfigurationException::class);
9894 $ this ->expectExceptionMessage ('The child node "migrations_paths" at path "doctrine_migrations" must be configured. ' );
9995
100- $ container = $ this ->getContainer ();
101- $ extension = new DoctrineMigrationsExtension ();
96+ $ container = $ this ->getContainer ([]);
10297
10398 $ conn = $ this ->createMock (Connection::class);
10499 $ container ->set ('doctrine.dbal.default_connection ' , $ conn );
105-
106- $ extension ->load ([], $ container );
107-
108- $ container ->getDefinition ('doctrine.migrations.configuration ' )->setPublic (true );
109100 $ container ->compile ();
110101
111- $ config = $ container ->get ('doctrine.migrations.configuration ' );
112-
113- print_r ($ config );
102+ $ container ->get ('doctrine.migrations.configuration ' );
114103 }
115104
116105
@@ -141,17 +130,11 @@ private function assertConfigs(?object $config) : void
141130
142131 public function testCustomSorter () : void
143132 {
144- $ container = $ this ->getContainer ();
145- $ extension = new DoctrineMigrationsExtension ();
146-
147- $ config = [
133+ $ config = [
148134 'migrations_paths ' => ['DoctrineMigrationsTest ' => 'a ' ],
149135 'services ' => [Comparator::class => 'my_sorter ' ],
150136 ];
151-
152- $ extension ->load (['doctrine_migrations ' => $ config ], $ container );
153-
154- $ container ->getDefinition ('doctrine.migrations.dependency_factory ' )->setPublic (true );
137+ $ container = $ this ->getContainer ($ config );
155138
156139 $ conn = $ this ->createMock (Connection::class);
157140 $ container ->set ('doctrine.dbal.default_connection ' , $ conn );
@@ -172,17 +155,11 @@ public function compare(Version $a, Version $b) : int
172155
173156 public function testCustomConnection () : void
174157 {
175- $ container = $ this ->getContainer ();
176- $ extension = new DoctrineMigrationsExtension ();
177-
178- $ config = [
158+ $ config = [
179159 'migrations_paths ' => ['DoctrineMigrationsTest ' => 'a ' ],
180160 'connection ' => 'custom ' ,
181161 ];
182-
183- $ extension ->load (['doctrine_migrations ' => $ config ], $ container );
184-
185- $ container ->getDefinition ('doctrine.migrations.dependency_factory ' )->setPublic (true );
162+ $ container = $ this ->getContainer ($ config );
186163
187164 $ conn = $ this ->createMock (Connection::class);
188165 $ container ->set ('doctrine.dbal.custom_connection ' , $ conn );
@@ -197,20 +174,14 @@ public function testCustomConnection() : void
197174
198175 public function testPrefersEntityManagerOverConnection () : void
199176 {
200- $ container = $ this ->getContainer ();
201- $ extension = new DoctrineMigrationsExtension ();
177+ $ config = [
178+ 'migrations_paths ' => ['DoctrineMigrationsTest ' => 'a ' ],
179+ ];
180+ $ container = $ this ->getContainer ($ config );
202181
203182 $ em = $ this ->createMock (EntityManager::class);
204183 $ container ->set ('doctrine.orm.default_entity_manager ' , $ em );
205184
206- $ extension ->load ([
207- 'doctrine_migrations ' => [
208- 'migrations_paths ' => ['DoctrineMigrationsTest ' => 'a ' ],
209- ],
210- ], $ container );
211-
212- $ container ->getDefinition ('doctrine.migrations.dependency_factory ' )->setPublic (true );
213-
214185 $ container ->compile ();
215186
216187 $ di = $ container ->get ('doctrine.migrations.dependency_factory ' );
@@ -221,21 +192,15 @@ public function testPrefersEntityManagerOverConnection() : void
221192
222193 public function testCustomEntityManager () : void
223194 {
224- $ container = $ this ->getContainer ();
225- $ extension = new DoctrineMigrationsExtension ();
226-
227- $ config = [
195+ $ config = [
228196 'em ' => 'custom ' ,
229197 'migrations_paths ' => ['DoctrineMigrationsTest ' => 'a ' ],
230198 ];
199+ $ container = $ this ->getContainer ($ config );
231200
232201 $ em = new Definition (CustomEntityManager::class);
233202 $ container ->setDefinition ('doctrine.orm.custom_entity_manager ' , $ em );
234203
235- $ extension ->load (['doctrine_migrations ' => $ config ], $ container );
236-
237- $ container ->getDefinition ('doctrine.migrations.dependency_factory ' )->setPublic (true );
238-
239204 $ container ->compile ();
240205
241206 $ di = $ container ->get ('doctrine.migrations.dependency_factory ' );
@@ -251,24 +216,19 @@ public function testCustomEntityManager() : void
251216
252217 public function testCustomMetadataStorage () : void
253218 {
254- $ container = $ this ->getContainer ();
255- $ extension = new DoctrineMigrationsExtension ();
256-
257219 $ config = [
258220 'migrations_paths ' => ['DoctrineMigrationsTest ' => 'a ' ],
259221 'services ' => [MetadataStorage::class => 'mock_storage_service ' ],
260222 ];
261223
224+ $ container = $ this ->getContainer ($ config );
225+
262226 $ mockStorage = $ this ->createMock (MetadataStorage::class);
263227 $ container ->set ('mock_storage_service ' , $ mockStorage );
264228
265229 $ conn = $ this ->createMock (Connection::class);
266230 $ container ->set ('doctrine.dbal.default_connection ' , $ conn );
267231
268- $ extension ->load (['doctrine_migrations ' => $ config ], $ container );
269-
270- $ container ->getDefinition ('doctrine.migrations.dependency_factory ' )->setPublic (true );
271-
272232 $ container ->compile ();
273233
274234 $ di = $ container ->get ('doctrine.migrations.dependency_factory ' );
@@ -280,43 +240,56 @@ public function testInvalidService() : void
280240 {
281241 $ this ->expectException (InvalidConfigurationException::class);
282242 $ this ->expectExceptionMessage ('Invalid configuration for path "doctrine_migrations.services": Valid services for the DoctrineMigrationsBundle must be in the "Doctrine\Migrations" namespace. ' );
283- $ container = $ this ->getContainer ();
284- $ extension = new DoctrineMigrationsExtension ();
285243
286- $ config = [
244+ $ config = [
287245 'migrations_paths ' => ['DoctrineMigrationsTest ' => 'a ' ],
288246 'services ' => ['foo ' => 'mock_storage_service ' ],
289247 ];
248+ $ container = $ this ->getContainer ($ config );
290249
291250 $ conn = $ this ->createMock (Connection::class);
292251 $ container ->set ('doctrine.dbal.default_connection ' , $ conn );
293252
294- $ extension ->load (['doctrine_migrations ' => $ config ], $ container );
295-
296253 $ container ->compile ();
297254 }
298255
299256 public function testCanNotSpecifyBothEmAndConnection () : void
300257 {
301258 $ this ->expectExceptionMessage ('You cannot specify both "connection" and "em" in the DoctrineMigrationsBundle configurations ' );
302259 $ this ->expectException (InvalidArgumentException::class);
303- $ container = $ this ->getContainer ();
304- $ extension = new DoctrineMigrationsExtension ();
305260
306261 $ config = [
307262 'migrations_paths ' => ['DoctrineMigrationsTest ' => 'a ' ],
308263 'em ' => 'custom ' ,
309264 'connection ' => 'custom ' ,
310265 ];
311266
267+ $ container = $ this ->getContainer ($ config );
268+
269+ $ container ->compile ();
270+ }
271+
272+ /**
273+ * @param mixed[] $config
274+ */
275+ private function getContainer (array $ config ) : ContainerBuilder
276+ {
277+ $ container = $ this ->getContainerBuilder ();
278+
279+ $ bundle = new DoctrineMigrationsBundle ();
280+ $ bundle ->build ($ container );
281+
282+ $ extension = new DoctrineMigrationsExtension ();
283+
312284 $ extension ->load (['doctrine_migrations ' => $ config ], $ container );
313285
314286 $ container ->getDefinition ('doctrine.migrations.dependency_factory ' )->setPublic (true );
287+ $ container ->getDefinition ('doctrine.migrations.configuration ' )->setPublic (true );
315288
316- $ container-> compile () ;
289+ return $ container ;
317290 }
318291
319- private function getContainer () : ContainerBuilder
292+ private function getContainerBuilder () : ContainerBuilder
320293 {
321294 return new ContainerBuilder (new ParameterBag ([
322295 'kernel.debug ' => false ,
0 commit comments