20
20
use Symfony \Component \DependencyInjection \ContainerInterface ;
21
21
use Symfony \Component \DependencyInjection \Definition ;
22
22
use Symfony \Component \DependencyInjection \Exception \RuntimeException ;
23
- use Symfony \Component \DependencyInjection \Exception \InactiveScopeException ;
24
- use Symfony \Component \DependencyInjection \Exception \ServiceCircularReferenceException ;
25
23
use Symfony \Component \DependencyInjection \Exception \ServiceNotFoundException ;
26
24
use Symfony \Component \DependencyInjection \Loader \ClosureLoader ;
27
25
use Symfony \Component \DependencyInjection \Reference ;
@@ -76,6 +74,9 @@ public function testCreateDeprecatedService()
76
74
77
75
$ builder = new ContainerBuilder ();
78
76
$ builder ->setDefinition ('deprecated_foo ' , $ definition );
77
+
78
+ $ builder ->compile ();
79
+
79
80
$ builder ->get ('deprecated_foo ' );
80
81
81
82
restore_error_handler ();
@@ -102,41 +103,80 @@ public function testHas()
102
103
$ this ->assertTrue ($ builder ->has ('bar ' ), '->has() returns true if a service exists ' );
103
104
}
104
105
105
- public function testGet ()
106
+ /**
107
+ * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
108
+ * @expectedExceptionMessage You have requested a non-existent service "foo".
109
+ */
110
+ public function testGetThrowsExceptionIfServiceDoesNotExist ()
106
111
{
107
112
$ builder = new ContainerBuilder ();
108
- try {
109
- $ builder ->get ('foo ' );
110
- $ this ->fail ('->get() throws a ServiceNotFoundException if the service does not exist ' );
111
- } catch (ServiceNotFoundException $ e ) {
112
- $ this ->assertEquals ('You have requested a non-existent service "foo". ' , $ e ->getMessage (), '->get() throws a ServiceNotFoundException if the service does not exist ' );
113
- }
113
+ $ builder ->compile ();
114
+ $ builder ->get ('foo ' );
115
+ }
116
+
117
+ public function testGetReturnsNullIfServiceDoesNotExistAndInvalidReferenceIsUsed ()
118
+ {
119
+ $ builder = new ContainerBuilder ();
120
+ $ builder ->compile ();
114
121
115
122
$ 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 ' );
123
+ }
116
124
125
+ /**
126
+ * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
127
+ */
128
+ public function testGetThrowsCircularReferenceExceptionIfServiceHasReferenceToItself ()
129
+ {
130
+ $ builder = new ContainerBuilder ();
131
+ $ builder ->register ('baz ' , 'stdClass ' )->setArguments (array (new Reference ('baz ' )));
132
+ $ builder ->compile ();
133
+ $ builder ->get ('baz ' );
134
+ }
135
+
136
+ public function testGetReturnsSameInstanceWhenServiceIsShared ()
137
+ {
138
+ $ builder = new ContainerBuilder ();
139
+ $ builder ->register ('bar ' , 'stdClass ' );
140
+ $ builder ->compile ();
141
+
142
+ $ this ->assertTrue ($ builder ->get ('bar ' ) === $ builder ->get ('bar ' ), '->get() always returns the same instance if the service is shared ' );
143
+ }
144
+
145
+ public function testGetCreatesServiceBasedOnDefinition ()
146
+ {
147
+ $ builder = new ContainerBuilder ();
117
148
$ builder ->register ('foo ' , 'stdClass ' );
149
+ $ builder ->compile ();
150
+
118
151
$ this ->assertInternalType ('object ' , $ builder ->get ('foo ' ), '->get() returns the service definition associated with the id ' );
152
+ }
153
+
154
+ public function testGetReturnsRegisteredService ()
155
+ {
156
+ $ builder = new ContainerBuilder ();
119
157
$ builder ->set ('bar ' , $ bar = new \stdClass ());
120
- $ this ->assertEquals ($ bar , $ builder ->get ('bar ' ), '->get() returns the service associated with the id ' );
121
- $ builder ->register ('bar ' , 'stdClass ' );
122
- $ this ->assertEquals ($ bar , $ builder ->get ('bar ' ), '->get() returns the service associated with the id even if a definition has been defined ' );
158
+ $ builder ->compile ();
123
159
124
- $ builder ->register ('baz ' , 'stdClass ' )->setArguments (array (new Reference ('baz ' )));
125
- try {
126
- @$ builder ->get ('baz ' );
127
- $ this ->fail ('->get() throws a ServiceCircularReferenceException if the service has a circular reference to itself ' );
128
- } catch (\Symfony \Component \DependencyInjection \Exception \ServiceCircularReferenceException $ e ) {
129
- $ 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 ' );
130
- }
160
+ $ this ->assertSame ($ bar , $ builder ->get ('bar ' ), '->get() returns the service associated with the id ' );
161
+ }
131
162
132
- $ this ->assertTrue ($ builder ->get ('bar ' ) === $ builder ->get ('bar ' ), '->get() always returns the same instance if the service is shared ' );
163
+ public function testRegisterDoesNotOverrideExistingService ()
164
+ {
165
+ $ builder = new ContainerBuilder ();
166
+ $ builder ->set ('bar ' , $ bar = new \stdClass ());
167
+ $ builder ->register ('bar ' , 'stdClass ' );
168
+ $ builder ->compile ();
169
+
170
+ $ this ->assertSame ($ bar , $ builder ->get ('bar ' ), '->get() returns the service associated with the id even if a definition has been defined ' );
133
171
}
134
172
135
173
public function testNonSharedServicesReturnsDifferentInstances ()
136
174
{
137
175
$ builder = new ContainerBuilder ();
138
176
$ builder ->register ('bar ' , 'stdClass ' )->setShared (false );
139
177
178
+ $ builder ->compile ();
179
+
140
180
$ this ->assertNotSame ($ builder ->get ('bar ' ), $ builder ->get ('bar ' ));
141
181
}
142
182
@@ -149,6 +189,8 @@ public function testGetUnsetLoadingServiceWhenCreateServiceThrowsAnException()
149
189
$ builder = new ContainerBuilder ();
150
190
$ builder ->register ('foo ' , 'stdClass ' )->setSynthetic (true );
151
191
192
+ $ builder ->compile ();
193
+
152
194
// we expect a RuntimeException here as foo is synthetic
153
195
try {
154
196
$ builder ->get ('foo ' );
@@ -177,6 +219,9 @@ public function testAliases()
177
219
$ this ->assertFalse ($ builder ->hasAlias ('foobar ' ), '->hasAlias() returns false if the alias does not exist ' );
178
220
$ this ->assertEquals ('foo ' , (string ) $ builder ->getAlias ('bar ' ), '->getAlias() returns the aliased service ' );
179
221
$ this ->assertTrue ($ builder ->has ('bar ' ), '->setAlias() defines a new service ' );
222
+
223
+ $ builder ->compile ();
224
+
180
225
$ this ->assertTrue ($ builder ->get ('bar ' ) === $ builder ->get ('foo ' ), '->setAlias() creates a service that is an alias to another one ' );
181
226
182
227
try {
@@ -244,6 +289,9 @@ public function testSetReplacesAlias()
244
289
$ builder ->set ('aliased ' , new \stdClass ());
245
290
246
291
$ builder ->set ('alias ' , $ foo = new \stdClass ());
292
+
293
+ $ builder ->compile ();
294
+
247
295
$ this ->assertSame ($ foo , $ builder ->get ('alias ' ), '->set() replaces an existing alias ' );
248
296
}
249
297
@@ -261,9 +309,12 @@ public function testCreateService()
261
309
{
262
310
$ builder = new ContainerBuilder ();
263
311
$ builder ->register ('foo1 ' , 'Bar\FooClass ' )->setFile (__DIR__ .'/Fixtures/includes/foo.php ' );
264
- $ this ->assertInstanceOf ('\Bar\FooClass ' , $ builder ->get ('foo1 ' ), '->createService() requires the file defined by the service definition ' );
265
312
$ builder ->register ('foo2 ' , 'Bar\FooClass ' )->setFile (__DIR__ .'/Fixtures/includes/%file%.php ' );
266
313
$ builder ->setParameter ('file ' , 'foo ' );
314
+
315
+ $ builder ->compile ();
316
+
317
+ $ this ->assertInstanceOf ('\Bar\FooClass ' , $ builder ->get ('foo1 ' ), '->createService() requires the file defined by the service definition ' );
267
318
$ this ->assertInstanceOf ('\Bar\FooClass ' , $ builder ->get ('foo2 ' ), '->createService() replaces parameters in the file provided by the service definition ' );
268
319
}
269
320
@@ -274,6 +325,8 @@ public function testCreateProxyWithRealServiceInstantiator()
274
325
$ builder ->register ('foo1 ' , 'Bar\FooClass ' )->setFile (__DIR__ .'/Fixtures/includes/foo.php ' );
275
326
$ builder ->getDefinition ('foo1 ' )->setLazy (true );
276
327
328
+ $ builder ->compile ();
329
+
277
330
$ foo1 = $ builder ->get ('foo1 ' );
278
331
279
332
$ this ->assertSame ($ foo1 , $ builder ->get ('foo1 ' ), 'The same proxy is retrieved on multiple subsequent calls ' );
@@ -285,6 +338,9 @@ public function testCreateServiceClass()
285
338
$ builder = new ContainerBuilder ();
286
339
$ builder ->register ('foo1 ' , '%class% ' );
287
340
$ builder ->setParameter ('class ' , 'stdClass ' );
341
+
342
+ $ builder ->compile ();
343
+
288
344
$ this ->assertInstanceOf ('\stdClass ' , $ builder ->get ('foo1 ' ), '->createService() replaces parameters in the class provided by the service definition ' );
289
345
}
290
346
@@ -294,6 +350,9 @@ public function testCreateServiceArguments()
294
350
$ builder ->register ('bar ' , 'stdClass ' );
295
351
$ builder ->register ('foo1 ' , 'Bar\FooClass ' )->addArgument (array ('foo ' => '%value% ' , '%value% ' => 'foo ' , new Reference ('bar ' ), '%%unescape_it%% ' ));
296
352
$ builder ->setParameter ('value ' , 'bar ' );
353
+
354
+ $ builder ->compile ();
355
+
297
356
$ 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 ' );
298
357
}
299
358
@@ -305,6 +364,8 @@ public function testCreateServiceFactory()
305
364
$ builder ->register ('bar ' , 'Bar\FooClass ' )->setFactory (array (new Definition ('Bar\FooClass ' ), 'getInstance ' ));
306
365
$ builder ->register ('baz ' , 'Bar\FooClass ' )->setFactory (array (new Reference ('bar ' ), 'getInstance ' ));
307
366
367
+ $ builder ->compile ();
368
+
308
369
$ this ->assertTrue ($ builder ->get ('foo ' )->called , '->createService() calls the factory method to create the service instance ' );
309
370
$ this ->assertTrue ($ builder ->get ('qux ' )->called , '->createService() calls the factory method to create the service instance ' );
310
371
$ this ->assertTrue ($ builder ->get ('bar ' )->called , '->createService() uses anonymous service as factory ' );
@@ -317,6 +378,9 @@ public function testCreateServiceMethodCalls()
317
378
$ builder ->register ('bar ' , 'stdClass ' );
318
379
$ builder ->register ('foo1 ' , 'Bar\FooClass ' )->addMethodCall ('setBar ' , array (array ('%value% ' , new Reference ('bar ' ))));
319
380
$ builder ->setParameter ('value ' , 'bar ' );
381
+
382
+ $ builder ->compile ();
383
+
320
384
$ this ->assertEquals (array ('bar ' , $ builder ->get ('bar ' )), $ builder ->get ('foo1 ' )->bar , '->createService() replaces the values in the method calls arguments ' );
321
385
}
322
386
@@ -326,6 +390,9 @@ public function testCreateServiceMethodCallsWithEscapedParam()
326
390
$ builder ->register ('bar ' , 'stdClass ' );
327
391
$ builder ->register ('foo1 ' , 'Bar\FooClass ' )->addMethodCall ('setBar ' , array (array ('%%unescape_it%% ' )));
328
392
$ builder ->setParameter ('value ' , 'bar ' );
393
+
394
+ $ builder ->compile ();
395
+
329
396
$ this ->assertEquals (array ('%unescape_it% ' ), $ builder ->get ('foo1 ' )->bar , '->createService() replaces the values in the method calls arguments ' );
330
397
}
331
398
@@ -335,27 +402,30 @@ public function testCreateServiceProperties()
335
402
$ builder ->register ('bar ' , 'stdClass ' );
336
403
$ builder ->register ('foo1 ' , 'Bar\FooClass ' )->setProperty ('bar ' , array ('%value% ' , new Reference ('bar ' ), '%%unescape_it%% ' ));
337
404
$ builder ->setParameter ('value ' , 'bar ' );
405
+
406
+ $ builder ->compile ();
407
+
338
408
$ this ->assertEquals (array ('bar ' , $ builder ->get ('bar ' ), '%unescape_it% ' ), $ builder ->get ('foo1 ' )->bar , '->createService() replaces the values in the properties ' );
339
409
}
340
410
341
411
public function testCreateServiceConfigurator ()
342
412
{
343
413
$ builder = new ContainerBuilder ();
344
414
$ builder ->register ('foo1 ' , 'Bar\FooClass ' )->setConfigurator ('sc_configure ' );
345
- $ this ->assertTrue ($ builder ->get ('foo1 ' )->configured , '->createService() calls the configurator ' );
346
-
347
415
$ builder ->register ('foo2 ' , 'Bar\FooClass ' )->setConfigurator (array ('%class% ' , 'configureStatic ' ));
348
416
$ builder ->setParameter ('class ' , 'BazClass ' );
349
- $ this ->assertTrue ($ builder ->get ('foo2 ' )->configured , '->createService() calls the configurator ' );
350
-
351
417
$ builder ->register ('baz ' , 'BazClass ' );
352
418
$ builder ->register ('foo3 ' , 'Bar\FooClass ' )->setConfigurator (array (new Reference ('baz ' ), 'configure ' ));
353
- $ this ->assertTrue ($ builder ->get ('foo3 ' )->configured , '->createService() calls the configurator ' );
354
-
355
419
$ builder ->register ('foo4 ' , 'Bar\FooClass ' )->setConfigurator (array ($ builder ->getDefinition ('baz ' ), 'configure ' ));
420
+ $ builder ->register ('foo5 ' , 'Bar\FooClass ' )->setConfigurator ('foo ' );
421
+
422
+ $ builder ->compile ();
423
+
424
+ $ this ->assertTrue ($ builder ->get ('foo1 ' )->configured , '->createService() calls the configurator ' );
425
+ $ this ->assertTrue ($ builder ->get ('foo2 ' )->configured , '->createService() calls the configurator ' );
426
+ $ this ->assertTrue ($ builder ->get ('foo3 ' )->configured , '->createService() calls the configurator ' );
356
427
$ this ->assertTrue ($ builder ->get ('foo4 ' )->configured , '->createService() calls the configurator ' );
357
428
358
- $ builder ->register ('foo5 ' , 'Bar\FooClass ' )->setConfigurator ('foo ' );
359
429
try {
360
430
$ builder ->get ('foo5 ' );
361
431
$ this ->fail ('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable ' );
@@ -371,6 +441,9 @@ public function testCreateSyntheticService()
371
441
{
372
442
$ builder = new ContainerBuilder ();
373
443
$ builder ->register ('foo ' , 'Bar\FooClass ' )->setSynthetic (true );
444
+
445
+ $ builder ->compile ();
446
+
374
447
$ builder ->get ('foo ' );
375
448
}
376
449
@@ -380,13 +453,18 @@ public function testCreateServiceWithExpression()
380
453
$ builder ->setParameter ('bar ' , 'bar ' );
381
454
$ builder ->register ('bar ' , 'BarClass ' );
382
455
$ builder ->register ('foo ' , 'Bar\FooClass ' )->addArgument (array ('foo ' => new Expression ('service("bar").foo ~ parameter("bar") ' )));
456
+
457
+ $ builder ->compile ();
458
+
383
459
$ this ->assertEquals ('foobar ' , $ builder ->get ('foo ' )->arguments ['foo ' ]);
384
460
}
385
461
386
462
public function testResolveServices ()
387
463
{
388
464
$ builder = new ContainerBuilder ();
389
465
$ builder ->register ('foo ' , 'Bar\FooClass ' );
466
+ $ builder ->compile ();
467
+
390
468
$ this ->assertEquals ($ builder ->get ('foo ' ), $ builder ->resolveServices (new Reference ('foo ' )), '->resolveServices() resolves service references to service instances ' );
391
469
$ 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 ' );
392
470
$ this ->assertEquals ($ builder ->get ('foo ' ), $ builder ->resolveServices (new Expression ('service("foo") ' )), '->resolveServices() resolves expressions ' );
0 commit comments