21
21
use Symfony \Component \DependencyInjection \ContainerInterface ;
22
22
use Symfony \Component \DependencyInjection \Definition ;
23
23
use Symfony \Component \DependencyInjection \Exception \RuntimeException ;
24
+ use Symfony \Component \DependencyInjection \Exception \InactiveScopeException ;
25
+ use Symfony \Component \DependencyInjection \Exception \ServiceCircularReferenceException ;
24
26
use Symfony \Component \DependencyInjection \Exception \ServiceNotFoundException ;
25
27
use Symfony \Component \DependencyInjection \Loader \ClosureLoader ;
26
28
use Symfony \Component \DependencyInjection \Reference ;
@@ -69,7 +71,6 @@ public function testCreateDeprecatedService()
69
71
70
72
$ builder = new ContainerBuilder ();
71
73
$ builder ->setDefinition ('deprecated_foo ' , $ definition );
72
- $ builder ->compile ();
73
74
$ builder ->get ('deprecated_foo ' );
74
75
}
75
76
@@ -91,80 +92,41 @@ public function testHas()
91
92
$ this ->assertTrue ($ builder ->has ('bar ' ), '->has() returns true if a service exists ' );
92
93
}
93
94
94
- /**
95
- * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
96
- * @expectedExceptionMessage You have requested a non-existent service "foo".
97
- */
98
- public function testGetThrowsExceptionIfServiceDoesNotExist ()
95
+ public function testGet ()
99
96
{
100
97
$ builder = new ContainerBuilder ();
101
- $ builder ->compile ();
102
- $ builder ->get ('foo ' );
103
- }
104
-
105
- public function testGetReturnsNullIfServiceDoesNotExistAndInvalidReferenceIsUsed ()
106
- {
107
- $ builder = new ContainerBuilder ();
108
- $ builder ->compile ();
98
+ try {
99
+ $ builder ->get ('foo ' );
100
+ $ this ->fail ('->get() throws a ServiceNotFoundException if the service does not exist ' );
F438
101
+ } catch (ServiceNotFoundException $ e ) {
102
+ $ this ->assertEquals ('You have requested a non-existent service "foo". ' , $ e ->getMessage (), '->get() throws a ServiceNotFoundException if the service does not exist ' );
103
+ }
109
104
110
105
$ this ->assertNull ($ builder ->get ('foo ' , ContainerInterface::NULL_ON_INVALID_REFERENCE ), '->get() returns null if the service does not exist and NULL_ON_INVALID_REFERENCE is passed as a second argument ' );
111
- }
112
-
113
- /**
114
- * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
115
- */
116
- public function testGetThrowsCircularReferenceExceptionIfServiceHasReferenceToItself ()
117
- {
118
- $ builder = new ContainerBuilder ();
119
- $ builder ->register ('baz ' , 'stdClass ' )->setArguments (array (new Reference ('baz ' )));
120
- $ builder ->compile ();
121
- $ builder ->get ('baz ' );
122
- }
123
-
124
- public function testGetReturnsSameInstanceWhenServiceIsShared ()
125
- {
126
- $ builder = new ContainerBuilder ();
127
- $ builder ->register ('bar ' , 'stdClass ' );
128
- $ builder ->compile ();
129
-
130
- $ this ->assertTrue ($ builder ->get ('bar ' ) === $ builder ->get ('bar ' ), '->get() always returns the same instance if the service is shared ' );
131
- }
132
106
133
- public function testGetCreatesServiceBasedOnDefinition ()
134
- {
135
- $ builder = new ContainerBuilder ();
136
107
$ builder ->register ('foo ' , 'stdClass ' );
137
- $ builder ->compile ();
138
-
139
108
$ this ->assertInternalType ('object ' , $ builder ->get ('foo ' ), '->get() returns the service definition associated with the id ' );
140
- }
141
-
142
- public function testGetReturnsRegisteredService ()
143
- {
144
- $ builder = new ContainerBuilder ();
145
- $ builder ->set ('bar ' , $ bar = new \stdClass ());
146
- $ builder ->compile ();
147
-
148
- $ this ->assertSame ($ bar , $ builder ->get ('bar ' ), '->get() returns the service associated with the id ' );
149
- }
150
-
151
- public function testRegisterDoesNotOverrideExistingService ()
152
- {
153
- $ builder = new ContainerBuilder ();
154
109
$ builder ->set ('bar ' , $ bar = new \stdClass ());
110
+ $ this ->assertEquals ($ bar , $ builder ->get ('bar ' ), '->get() returns the service associated with the id ' );
155
111
$ builder ->register ('bar ' , 'stdClass ' );
156
- $ builder ->compile ();
112
+ $ this ->assertEquals ($ bar , $ builder ->get ('bar ' ), '->get() returns the service associated with the id even if a definition has been defined ' );
113
+
114
+ $ builder ->register ('baz ' , 'stdClass ' )->setArguments (array (new Reference ('baz ' )));
115
+ try {
116
+ @$ builder ->get ('baz ' );
117
+ $ this ->fail ('->get() throws a ServiceCircularReferenceException if the service has a circular reference to itself ' );
118
+ } catch (\Symfony \Component \DependencyInjection \Exception \ServiceCircularReferenceException $ e ) {
119
+ $ this ->assertEquals ('Circular reference detected for service "baz", path: "baz". ' , $ e ->getMessage (), '->get() throws a LogicException if the service has a circular reference to itself ' );
120
+ }
157
121
158
- $ this ->assertSame ( $ bar, $ builder ->get ('bar ' ), '->get() returns the service associated with the id even if a definition has been defined ' );
122
+ $ this ->assertTrue ( $ builder -> get ( ' bar ' ) === $ builder ->get ('bar ' ), '->get() always returns the same instance if the service is shared ' );
159
123
}
160
124
161
125
public function testNonSharedServicesReturnsDifferentInstances ()
162
126
{
163
127
$ builder = new ContainerBuilder ();
164
128
$ builder ->register ('bar ' , 'stdClass ' )->setShared (false );
165
129
166
- $ builder ->compile ();
167
-
168
130
$ this ->assertNotSame ($ builder ->get ('bar ' ), $ builder ->get ('bar ' ));
169
131
}
170
132
@@ -177,8 +139,6 @@ public function testGetUnsetLoadingServiceWhenCreateServiceThrowsAnException()
177
139
$ builder = new ContainerBuilder ();
178
140
$ builder ->register ('foo ' , 'stdClass ' )->setSynthetic (true );
179
141
180
- $ builder ->compile ();
181
-
182
142
// we expect a RuntimeException here as foo is synthetic
183
143
try {
184
144
$ builder ->get ('foo ' );
@@ -207,9 +167,6 @@ public function testAliases()
207
167
$ this ->assertFalse ($ builder ->hasAlias ('foobar ' ), '->hasAlias() returns false if the alias does not exist ' );
208
168
$ this ->assertEquals ('foo ' , (string ) $ builder ->getAlias ('bar ' ), '->getAlias() returns the aliased service ' );
209
169
$ this ->assertTrue ($ builder ->has ('bar ' ), '->setAlias() defines a new service ' );
210
-
211
- $ builder ->compile ();
212
-
213
170
$ this ->assertTrue ($ builder ->get ('bar ' ) === $ builder ->get ('foo ' ), '->setAlias() creates a service that is an alias to another one ' );
214
171
215
172
try {
@@ -277,9 +234,6 @@ public function testSetReplacesAlias()
277
234
$ builder ->set ('aliased ' , new \stdClass ());
278
235
279
236
$ builder ->set ('alias ' , $ foo = new \stdClass ());
280
-
281
- $ builder ->compile ();
282
-
283
237
$ this ->assertSame ($ foo , $ builder ->get ('alias ' ), '->set() replaces an existing alias ' );
284
238
}
285
239
@@ -301,12 +255,9 @@ public function testCreateService()
301
255
{
302
256
$ builder = new ContainerBuilder ();
303
257
$ builder ->register ('foo1 ' , 'Bar\FooClass ' )->setFile (__DIR__ .'/Fixtures/includes/foo.php ' );
258
+ $ this ->assertInstanceOf ('\Bar\FooClass ' , $ builder ->get ('foo1 ' ), '->createService() requires the file defined by the service definition ' );
304
259
$ builder ->register ('foo2 ' , 'Bar\FooClass ' )->setFile (__DIR__ .'/Fixtures/includes/%file%.php ' );
305
260
$ builder ->setParameter ('file ' , 'foo ' );
306
-
307
- $ builder ->compile ();
308
-
309
- $ this ->assertInstanceOf ('\Bar\FooClass ' , $ builder ->get ('foo1 ' ), '->createService() requires the file defined by the service definition ' );
310
261
$ this ->assertInstanceOf ('\Bar\FooClass ' , $ builder ->get ('foo2 ' ), '->createService() replaces parameters in the file provided by the service definition ' );
311
262
}
312
263
@@ -317,8 +268,6 @@ public function testCreateProxyWithRealServiceInstantiator()
317
268
$ builder ->register ('foo1 ' , 'Bar\FooClass ' )->setFile (__DIR__ .'/Fixtures/includes/foo.php ' );
318
269
$ builder ->getDefinition ('foo1 ' )->setLazy (true );
319
270
320
- $ builder ->compile ();
321
-
322
271
$ foo1 = $ builder ->get ('foo1 ' );
323
272
324
273
$ this ->assertSame ($ foo1 , $ builder ->get ('foo1 ' ), 'The same proxy is retrieved on multiple subsequent calls ' );
@@ -330,9 +279,6 @@ public function testCreateServiceClass()
330
279
$ builder = new ContainerBuilder ();
331
280
$ builder ->register ('foo1 ' , '%class% ' );
332
281
$ builder ->setParameter ('class ' , 'stdClass ' );
333
-
334
- $ builder ->compile ();
335
-
336
282
$ this ->assertInstanceOf ('\stdClass ' , $ builder ->get ('foo1 ' ), '->createService() replaces parameters in the class provided by the service definition ' );
337
283
}
338
284
@@ -342,9 +288,6 @@ public function testCreateServiceArguments()
342
288
$ builder ->register ('bar ' , 'stdClass ' );
343
289
$ builder ->register ('foo1 ' , 'Bar\FooClass ' )->addArgument (array ('foo ' => '%value% ' , '%value% ' => 'foo ' , new Reference ('bar ' ), '%%unescape_it%% ' ));
344
290
$ builder ->setParameter ('value ' , 'bar ' );
345
-
346
- $ builder ->compile ();
347
-
348
291
$ this ->assertEquals (array ('foo ' => 'bar ' , 'bar ' => 'foo ' , $ builder ->get ('bar ' ), '%unescape_it% ' ), $ builder ->get ('foo1 ' )->arguments , '->createService() replaces parameters and service references in the arguments provided by the service definition ' );
349
292
}
350
293
@@ -356,8 +299,6 @@ public function testCreateServiceFactory()
356
299
$ builder ->register ('bar ' , 'Bar\FooClass ' )->setFactory (array (new Definition ('Bar\FooClass ' ), 'getInstance ' ));
357
300
$ builder ->register ('baz ' , 'Bar\FooClass ' )->setFactory (array (new Reference ('bar ' ), 'getInstance ' ));
358
301
359
- $ builder ->compile ();
360
-
361
302
$ this ->assertTrue ($ builder ->get ('foo ' )->called , '->createService() calls the factory method to create the service instance ' );
362
303
$ this ->assertTrue ($ builder ->get ('qux ' )->called , '->createService() calls the factory method to create the service instance ' );
363
304
$ this ->assertTrue ($ builder ->get ('bar ' )->called , '->createService() uses anonymous service as factory ' );
@@ -370,9 +311,6 @@ public function testCreateServiceMethodCalls()
370
311
$ builder ->register ('bar ' , 'stdClass ' );
371
312
$ builder ->register ('foo1 ' , 'Bar\FooClass ' )->addMethodCall ('setBar ' , array (array ('%value% ' , new Reference ('bar ' ))));
372
313
$ builder ->setParameter ('value ' , 'bar ' );
373
-
374
- $ builder ->compile ();
375
-
376
314
$ this ->assertEquals (array ('bar ' , $ builder ->get ('bar ' )), $ builder ->get ('foo1 ' )->bar , '->createService() replaces the values in the method calls arguments ' );
377
315
}
378
316
@@ -382,9 +320,6 @@ public function testCreateServiceMethodCallsWithEscapedParam()
382
320
$ builder ->register ('bar ' , 'stdClass ' );
383
321
$ builder ->register ('foo1 ' , 'Bar\FooClass ' )->addMethodCall ('setBar ' , array (array ('%%unescape_it%% ' )));
384
322
$ builder ->setParameter ('value ' , 'bar ' );
385
-
386
- $ builder ->compile ();
387
-
388
323
$ this ->assertEquals (array ('%unescape_it% ' ), $ builder ->get ('foo1 ' )->bar , '->createService() replaces the values in the method calls arguments ' );
389
324
}
390
325
@@ -394,30 +329,27 @@ public function testCreateServiceProperties()
394
329
$ builder ->register ('bar ' , 'stdClass ' );
395
330
$ builder ->register ('foo1 ' , 'Bar\FooClass ' )->setProperty ('bar ' , array ('%value% ' , new Reference ('bar ' ), '%%unescape_it%% ' ));
396
331
$ builder ->setParameter ('value ' , 'bar ' );
397
-
398
- $ builder ->compile ();
399
-
400
332
$ this ->assertEquals (array ('bar ' , $ builder ->get ('bar ' ), '%unescape_it% ' ), $ builder ->get ('foo1 ' )->bar , '->createService() replaces the values in the properties ' );
401
333
}
402
334
403
335
public function testCreateServiceConfigurator ()
404
336
{
405
337
$ builder = new ContainerBuilder ();
406
338
$ builder ->register ('foo1 ' , 'Bar\FooClass ' )->setConfigurator ('sc_configure ' );
339
+ $ this ->assertTrue ($ builder ->get ('foo1 ' )->configured , '->createService() calls the configurator ' );
340
+
407
341
$ builder ->register ('foo2 ' , 'Bar\FooClass ' )->setConfigurator (array ('%class% ' , 'configureStatic ' ));
408
342
$ builder ->setParameter ('class ' , 'BazClass ' );
343
+ $ this ->assertTrue ($ builder ->get ('foo2 ' )->configured , '->createService() calls the configurator ' );
344
+
409
345
$ builder ->register ('baz ' , 'BazClass ' );
410
346
$ builder ->register ('foo3 ' , 'Bar\FooClass ' )->setConfigurator (array (new Reference ('baz ' ), 'configure ' ));
411
- $ builder ->register ('foo4 ' , 'Bar\FooClass ' )->setConfigurator (array ($ builder ->getDefinition ('baz ' ), 'configure ' ));
412
- $ builder ->register ('foo5 ' , 'Bar\FooClass ' )->setConfigurator ('foo ' );
413
-
414
- $ builder ->compile ();
415
-
416
- $ this ->assertTrue ($ builder ->get ('foo1 ' )->configured , '->createService() calls the configurator ' );
417
- $ this ->assertTrue ($ builder ->get ('foo2 ' )->configured , '->createService() calls the configurator ' );
418
347
$ this ->assertTrue ($ builder ->get ('foo3 ' )->configured , '->createService() calls the configurator ' );
348
+
349
+ $ builder ->register ('foo4 ' , 'Bar\FooClass ' )->setConfigurator (array ($ builder ->getDefinition ('baz ' ), 'configure ' ));
419
350
$ this ->assertTrue ($ builder ->get ('foo4 ' )->configured , '->createService() calls the configurator ' );
420
351
352
+ $ builder ->register ('foo5 ' , 'Bar\FooClass ' )->setConfigurator ('foo ' );
421
353
try {
422
354
$ builder ->get ('foo5 ' );
423
355
$ this ->fail ('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable ' );
@@ -433,9 +365,6 @@ public function testCreateSyntheticService()
433
365
{
434
366
$ builder = new ContainerBuilder ();
435
367
$ builder ->register ('foo ' , 'Bar\FooClass ' )->setSynthetic (true );
436
-
437
- $ builder ->compile ();
438
-
439
368
$ builder ->get ('foo ' );
440
369
}
441
370
@@ -445,18 +374,13 @@ public function testCreateServiceWithExpression()
445
374
$ builder ->setParameter ('bar ' , 'bar ' );
446
375
$ builder ->register ('bar ' , 'BarClass ' );
447
376
$ builder ->register ('foo ' , 'Bar\FooClass ' )->addArgument (array ('foo ' => new Expression ('service("bar").foo ~ parameter("bar") ' )));
448
-
449
- $ builder ->compile ();
450
-
451
377
$ this ->assertEquals ('foobar ' , $ builder ->get ('foo ' )->arguments ['foo ' ]);
452
378
}
453
379
454
380
public function testResolveServices ()
455
381
{
456
382
$ builder = new ContainerBuilder ();
457
383
$ builder ->register ('foo ' , 'Bar\FooClass ' );
458
- $ builder ->compile ();
459
-
460
384
$ this ->assertEquals ($ builder ->get ('foo ' ), $ builder ->resolveServices (new Reference ('foo ' )), '->resolveServices() resolves service references to service instances ' );
461
385
$ this ->assertEquals (array ('foo ' => array ('foo ' , $ builder ->get ('foo ' ))), $ builder ->resolveServices (array ('foo ' => array ('foo ' , new Reference ('foo ' )))), '->resolveServices() resolves service references to service instances in nested arrays ' );
462
386
$ this ->assertEquals ($ builder ->get ('foo ' ), $ builder ->resolveServices (new Expression ('service("foo") ' )), '->resolveServices() resolves expressions ' );
0 commit comments