5
5
namespace Doctrine \Bundle \MigrationsBundle \Tests \DependencyInjection ;
6
6
7
7
use Doctrine \Bundle \MigrationsBundle \DependencyInjection \DoctrineMigrationsExtension ;
8
+ use Doctrine \Bundle \MigrationsBundle \DoctrineMigrationsBundle ;
8
9
use Doctrine \Bundle \MigrationsBundle \Tests \Fixtures \CustomEntityManager ;
9
10
use Doctrine \DBAL \Connection ;
10
11
use Doctrine \Migrations \Configuration \Configuration ;
25
26
use Symfony \Component \DependencyInjection \ParameterBag \ParameterBag ;
26
27
use function assert ;
27
28
use function method_exists ;
28
- use function print_r ;
29
29
use function sys_get_temp_dir ;
30
30
31
31
class DoctrineMigrationsExtensionTest extends TestCase
32
32
{
33
33
public function testXmlConfigs () : void
34
34
{
35
- $ container = $ this ->getContainer ();
35
+ $ container = $ this ->getContainerBuilder ();
36
36
37
37
$ conn = $ this ->createMock (Connection::class);
38
38
$ container ->set ('doctrine.dbal.default_connection ' , $ conn );
39
39
40
40
$ container ->registerExtension (new DoctrineMigrationsExtension ());
41
+
41
42
$ container ->setAlias ('doctrine.migrations.configuration.test ' , new Alias ('doctrine.migrations.configuration ' , true ));
42
43
43
44
$ loader = new XmlFileLoader ($ container , new FileLocator (__DIR__ . '/../Fixtures ' ));
@@ -51,9 +52,6 @@ public function testXmlConfigs() : void
51
52
52
53
public function testFullConfig () : void
53
54
{
54
- $ container = $ this ->getContainer ();
55
- $ extension = new DoctrineMigrationsExtension ();
56
-
57
55
$ config = [
58
56
'name ' => 'Doctrine Sandbox Migrations ' ,
59
57
'storage ' => [
@@ -78,13 +76,11 @@ public function testFullConfig() : void
78
76
'all_or_nothing ' => true ,
79
77
'check_database_platform ' => true ,
80
78
];
79
+ $ container = $ this ->getContainer ($ config );
81
80
82
81
$ conn = $ this ->createMock (Connection::class);
83
82
$ container ->set ('doctrine.dbal.default_connection ' , $ conn );
84
83
85
- $ extension ->load (['doctrine_migrations ' => $ config ], $ container );
86
-
87
- $ container ->getDefinition ('doctrine.migrations.configuration ' )->setPublic (true );
88
84
$ container ->compile ();
89
85
90
86
$ config = $ container ->get ('doctrine.migrations.configuration ' );
@@ -97,20 +93,13 @@ public function testNoConfig() : void
97
93
$ this ->expectException (InvalidConfigurationException::class);
98
94
$ this ->expectExceptionMessage ('The child node "migrations_paths" at path "doctrine_migrations" must be configured. ' );
99
95
100
- $ container = $ this ->getContainer ();
101
- $ extension = new DoctrineMigrationsExtension ();
96
+ $ container = $ this ->getContainer ([]);
102
97
103
98
$ conn = $ this ->createMock (Connection::class);
104
99
$ container ->set ('doctrine.dbal.default_connection ' , $ conn );
105
-
106
- $ extension ->load ([], $ container );
107
-
108
- $ container ->getDefinition ('doctrine.migrations.configuration ' )->setPublic (true );
109
100
$ container ->compile ();
110
101
111
- $ config = $ container ->get ('doctrine.migrations.configuration ' );
112
-
113
- print_r ($ config );
102
+ $ container ->get ('doctrine.migrations.configuration ' );
114
103
}
115
104
116
105
@@ -141,17 +130,11 @@ private function assertConfigs(?object $config) : void
141
130
142
131
public function testCustomSorter () : void
143
132
{
144
- $ container = $ this ->getContainer ();
145
- $ extension = new DoctrineMigrationsExtension ();
146
-
147
- $ config = [
133
+ $ config = [
148
134
'migrations_paths ' => ['DoctrineMigrationsTest ' => 'a ' ],
149
135
'services ' => [Comparator::class => 'my_sorter ' ],
150
136
];
151
-
152
- $ extension ->load (['doctrine_migrations ' => $ config ], $ container );
153
-
154
- $ container ->getDefinition ('doctrine.migrations.dependency_factory ' )->setPublic (true );
137
+ $ container = $ this ->getContainer ($ config );
155
138
156
139
$ conn = $ this ->createMock (Connection::class);
157
140
$ container ->set ('doctrine.dbal.default_connection ' , $ conn );
@@ -172,17 +155,11 @@ public function compare(Version $a, Version $b) : int
172
155
173
156
public function testCustomConnection () : void
174
157
{
175
- $ container = $ this ->getContainer ();
176
- $ extension = new DoctrineMigrationsExtension ();
177
-
178
- $ config = [
158
+ $ config = [
179
159
'migrations_paths ' => ['DoctrineMigrationsTest ' => 'a ' ],
180
160
'connection ' => 'custom ' ,
181
161
];
182
-
183
- $ extension ->load (['doctrine_migrations ' => $ config ], $ container );
184
-
185
- $ container ->getDefinition ('doctrine.migrations.dependency_factory ' )->setPublic (true );
162
+ $ container = $ this ->getContainer ($ config );
186
163
187
164
$ conn = $ this ->createMock (Connection::class);
188
165
$ container ->set ('doctrine.dbal.custom_connection ' , $ conn );
@@ -197,20 +174,14 @@ public function testCustomConnection() : void
197
174
198
175
public function testPrefersEntityManagerOverConnection () : void
199
176
{
200
- $ container = $ this ->getContainer ();
201
- $ extension = new DoctrineMigrationsExtension ();
177
+ $ config = [
178
+ 'migrations_paths ' => ['DoctrineMigrationsTest ' => 'a ' ],
179
+ ];
180
+ $ container = $ this ->getContainer ($ config );
202
181
203
182
$ em = $ this ->createMock (EntityManager::class);
204
183
$ container ->set ('doctrine.orm.default_entity_manager ' , $ em );
205
184
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
-
214
185
$ container ->compile ();
215
186
216
187
$ di = $ container ->get ('doctrine.migrations.dependency_factory ' );
@@ -221,21 +192,15 @@ public function testPrefersEntityManagerOverConnection() : void
221
192
222
193
public function testCustomEntityManager () : void
223
194
{
224
- $ container = $ this ->getContainer ();
225
- $ extension = new DoctrineMigrationsExtension ();
226
-
227
- $ config = [
195
+ $ config = [
228
196
'em ' => 'custom ' ,
229
197
'migrations_paths ' => ['DoctrineMigrationsTest ' => 'a ' ],
230
198
];
199
+ $ container = $ this ->getContainer ($ config );
231
200
232
201
$ em = new Definition (CustomEntityManager::class);
233
202
$ container ->setDefinition ('doctrine.orm.custom_entity_manager ' , $ em );
234
203
235
- $ extension ->load (['doctrine_migrations ' => $ config ], $ container );
236
-
237
- $ container ->getDefinition ('doctrine.migrations.dependency_factory ' )->setPublic (true );
238
-
239
204
$ container ->compile ();
240
205
241
206
$ di = $ container ->get ('doctrine.migrations.dependency_factory ' );
@@ -251,24 +216,19 @@ public function testCustomEntityManager() : void
251
216
252
217
public function testCustomMetadataStorage () : void
253
218
{
254
- $ container = $ this ->getContainer ();
255
- $ extension = new DoctrineMigrationsExtension ();
256
-
257
219
$ config = [
258
220
'migrations_paths ' => ['DoctrineMigrationsTest ' => 'a ' ],
259
221
'services ' => [MetadataStorage::class => 'mock_storage_service ' ],
260
222
];
261
223
224
+ $ container = $ this ->getContainer ($ config );
225
+
262
226
$ mockStorage = $ this ->createMock (MetadataStorage::class);
263
227
$ container ->set ('mock_storage_service ' , $ mockStorage );
264
228
265
229
$ conn = $ this ->createMock (Connection::class);
266
230
$ container ->set ('doctrine.dbal.default_connection ' , $ conn );
267
231
268
- $ extension ->load (['doctrine_migrations ' => $ config ], $ container );
269
-
270
- $ container ->getDefinition ('doctrine.migrations.dependency_factory ' )->setPublic (true );
271
-
272
232
$ container ->compile ();
273
233
274
234
$ di = $ container ->get ('doctrine.migrations.dependency_factory ' );
@@ -280,43 +240,56 @@ public function testInvalidService() : void
280
240
{
281
241
$ this ->expectException (InvalidConfigurationException::class);
282
242
$ 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 ();
285
243
286
- $ config = [
244
+ $ config = [
287
245
'migrations_paths ' => ['DoctrineMigrationsTest ' => 'a ' ],
288
246
'services ' => ['foo ' => 'mock_storage_service ' ],
289
247
];
248
+ $ container = $ this ->getContainer ($ config );
290
249
291
250
$ conn = $ this ->createMock (Connection::class);
292
251
$ container ->set ('doctrine.dbal.default_connection ' , $ conn );
293
252
294
- $ extension ->load (['doctrine_migrations ' => $ config ], $ container );
295
-
296
253
$ container ->compile ();
297
254
}
298
255
299
256
public function testCanNotSpecifyBothEmAndConnection () : void
300
257
{
301
258
$ this ->expectExceptionMessage ('You cannot specify both "connection" and "em" in the DoctrineMigrationsBundle configurations ' );
302
259
$ this ->expectException (InvalidArgumentException::class);
303
- $ container = $ this ->getContainer ();
304
- $ extension = new DoctrineMigrationsExtension ();
305
260
306
261
$ config = [
307
262
'migrations_paths ' => ['DoctrineMigrationsTest ' => 'a ' ],
308
263
'em ' => 'custom ' ,
309
264
'connection ' => 'custom ' ,
310
265
];
311
266
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
+
312
284
$ extension ->load (['doctrine_migrations ' => $ config ], $ container );
313
285
314
286
$ container ->getDefinition ('doctrine.migrations.dependency_factory ' )->setPublic (true );
287
+ $ container ->getDefinition ('doctrine.migrations.configuration ' )->setPublic (true );
315
288
316
- $ container-> compile () ;
289
+ return $ container ;
317
290
}
318
291
319
- private function getContainer () : ContainerBuilder
292
+ private function getContainerBuilder () : ContainerBuilder
320
293
{
321
294
return new ContainerBuilder (new ParameterBag ([
322
295
'kernel.debug ' => false ,
0 commit comments