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