2121use Symfony \Component \DependencyInjection \ContainerInterface ;
2222use Symfony \Component \DependencyInjection \Definition ;
2323use Symfony \Component \DependencyInjection \Exception \RuntimeException ;
24+ use Symfony \Component \DependencyInjection \Exception \InactiveScopeException ;
25+ use Symfony \Component \DependencyInjection \Exception \ServiceCircularReferenceException ;
2426use Symfony \Component \DependencyInjection \Exception \ServiceNotFoundException ;
2527use Symfony \Component \DependencyInjection \Loader \ClosureLoader ;
2628use Symfony \Component \DependencyInjection \Reference ;
@@ -69,7 +71,6 @@ public function testCreateDeprecatedService()
6971
7072$ builder =new ContainerBuilder ();
7173$ builder ->setDefinition ('deprecated_foo ' ,$ definition );
72- $ builder ->compile ();
7374$ builder ->get ('deprecated_foo ' );
7475 }
7576
@@ -91,80 +92,41 @@ public function testHas()
9192$ this ->assertTrue ($ builder ->has ('bar ' ),'->has() returns true if a service exists ' );
9293 }
9394
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 ()
9996 {
10097$ 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 ' );
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+ }
109104
110105$ 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- }
132106
133- public function testGetCreatesServiceBasedOnDefinition ()
134- {
135- $ builder =new ContainerBuilder ();
136107$ builder ->register ('foo ' ,'stdClass ' );
137- $ builder ->compile ();
138-
139108$ 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 ();
154109$ builder ->set ('bar ' ,$ bar =new \stdClass ());
110+ $ this ->assertEquals ($ bar ,$ builder ->get ('bar ' ),'->get() returns the service associated with the id ' );
155111$ 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+ }
157121
158- $ this ->assertSame ( $ bar, $ builder ->get ('bar ' ),'->get() returns theservice associated with theid even if a definition has been defined ' );
122+ $ this ->assertTrue ( $ builder -> get ( ' bar ' ) === $ builder ->get ('bar ' ),'->get()always returns thesame instance if theservice is shared ' );
159123 }
160124
161125public function testNonSharedServicesReturnsDifferentInstances ()
162126 {
163127$ builder =new ContainerBuilder ();
164128$ builder ->register ('bar ' ,'stdClass ' )->setShared (false );
165129
166- $ builder ->compile ();
167-
168130$ this ->assertNotSame ($ builder ->get ('bar ' ),$ builder ->get ('bar ' ));
169131 }
170132
@@ -177,8 +139,6 @@ public function testGetUnsetLoadingServiceWhenCreateServiceThrowsAnException()
177139$ builder =new ContainerBuilder ();
178140$ builder ->register ('foo ' ,'stdClass ' )->setSynthetic (true );
179141
180- $ builder ->compile ();
181-
182142// we expect a RuntimeException here as foo is synthetic
183143try {
184144$ builder ->get ('foo ' );
@@ -207,9 +167,6 @@ public function testAliases()
207167$ this ->assertFalse ($ builder ->hasAlias ('foobar ' ),'->hasAlias() returns false if the alias does not exist ' );
208168$ this ->assertEquals ('foo ' , (string )$ builder ->getAlias ('bar ' ),'->getAlias() returns the aliased service ' );
209169$ this ->assertTrue ($ builder ->has ('bar ' ),'->setAlias() defines a new service ' );
210-
211- $ builder ->compile ();
212-
213170$ this ->assertTrue ($ builder ->get ('bar ' ) ===$ builder ->get ('foo ' ),'->setAlias() creates a service that is an alias to another one ' );
214171
215172try {
@@ -277,9 +234,6 @@ public function testSetReplacesAlias()
277234$ builder ->set ('aliased ' ,new \stdClass ());
278235
279236$ builder ->set ('alias ' ,$ foo =new \stdClass ());
280-
281- $ builder ->compile ();
282-
283237$ this ->assertSame ($ foo ,$ builder ->get ('alias ' ),'->set() replaces an existing alias ' );
284238 }
285239
@@ -301,12 +255,9 @@ public function testCreateService()
301255 {
302256$ builder =new ContainerBuilder ();
303257$ 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 ' );
304259$ builder ->register ('foo2 ' ,'Bar\FooClass ' )->setFile (__DIR__ .'/Fixtures/includes/%file%.php ' );
305260$ 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 ' );
310261$ this ->assertInstanceOf ('\Bar\FooClass ' ,$ builder ->get ('foo2 ' ),'->createService() replaces parameters in the file provided by the service definition ' );
311262 }
312263
@@ -317,8 +268,6 @@ public function testCreateProxyWithRealServiceInstantiator()
317268$ builder ->register ('foo1 ' ,'Bar\FooClass ' )->setFile (__DIR__ .'/Fixtures/includes/foo.php ' );
318269$ builder ->getDefinition ('foo1 ' )->setLazy (true );
319270
320- $ builder ->compile ();
321-
322271$ foo1 =$ builder ->get ('foo1 ' );
323272
324273$ this ->assertSame ($ foo1 ,$ builder ->get ('foo1 ' ),'The same proxy is retrieved on multiple subsequent calls ' );
@@ -330,9 +279,6 @@ public function testCreateServiceClass()
330279$ builder =new ContainerBuilder ();
331280$ builder ->register ('foo1 ' ,'%class% ' );
332281$ builder ->setParameter ('class ' ,'stdClass ' );
333-
334- $ builder ->compile ();
335-
336282$ this ->assertInstanceOf ('\stdClass ' ,$ builder ->get ('foo1 ' ),'->createService() replaces parameters in the class provided by the service definition ' );
337283 }
338284
@@ -342,9 +288,6 @@ public function testCreateServiceArguments()
342288$ builder ->register ('bar ' ,'stdClass ' );
343289$ builder ->register ('foo1 ' ,'Bar\FooClass ' )->addArgument (array ('foo ' =>'%value% ' ,'%value% ' =>'foo ' ,new Reference ('bar ' ),'%%unescape_it%% ' ));
344290$ builder ->setParameter ('value ' ,'bar ' );
345-
346- $ builder ->compile ();
347-
348291$ 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 ' );
349292 }
350293
@@ -356,8 +299,6 @@ public function testCreateServiceFactory()
356299$ builder ->register ('bar ' ,'Bar\FooClass ' )->setFactory (array (new Definition ('Bar\FooClass ' ),'getInstance ' ));
357300$ builder ->register ('baz ' ,'Bar\FooClass ' )->setFactory (array (new Reference ('bar ' ),'getInstance ' ));
358301
359- $ builder ->compile ();
360-
361302$ this ->assertTrue ($ builder ->get ('foo ' )->called ,'->createService() calls the factory method to create the service instance ' );
362303$ this ->assertTrue ($ builder ->get ('qux ' )->called ,'->createService() calls the factory method to create the service instance ' );
363304$ this ->assertTrue ($ builder ->get ('bar ' )->called ,'->createService() uses anonymous service as factory ' );
@@ -370,9 +311,6 @@ public function testCreateServiceMethodCalls()
370311$ builder ->register ('bar ' ,'stdClass ' );
371312$ builder ->register ('foo1 ' ,'Bar\FooClass ' )->addMethodCall ('setBar ' ,array (array ('%value% ' ,new Reference ('bar ' ))));
372313$ builder ->setParameter ('value ' ,'bar ' );
373-
374- $ builder ->compile ();
375-
376314$ this ->assertEquals (array ('bar ' ,$ builder ->get ('bar ' )),$ builder ->get ('foo1 ' )->bar ,'->createService() replaces the values in the method calls arguments ' );
377315 }
378316
@@ -382,9 +320,6 @@ public function testCreateServiceMethodCallsWithEscapedParam()
382320$ builder ->register ('bar ' ,'stdClass ' );
383321$ builder ->register ('foo1 ' ,'Bar\FooClass ' )->addMethodCall ('setBar ' ,array (array ('%%unescape_it%% ' )));
384322$ builder ->setParameter ('value ' ,'bar ' );
385-
386- $ builder ->compile ();
387-
388323$ this ->assertEquals (array ('%unescape_it% ' ),$ builder ->get ('foo1 ' )->bar ,'->createService() replaces the values in the method calls arguments ' );
389324 }
390325
@@ -394,30 +329,27 @@ public function testCreateServiceProperties()
394329$ builder ->register ('bar ' ,'stdClass ' );
395330$ builder ->register ('foo1 ' ,'Bar\FooClass ' )->setProperty ('bar ' ,array ('%value% ' ,new Reference ('bar ' ),'%%unescape_it%% ' ));
396331$ builder ->setParameter ('value ' ,'bar ' );
397-
398- $ builder ->compile ();
399-
400332$ this ->assertEquals (array ('bar ' ,$ builder ->get ('bar ' ),'%unescape_it% ' ),$ builder ->get ('foo1 ' )->bar ,'->createService() replaces the values in the properties ' );
401333 }
402334
403335public function testCreateServiceConfigurator ()
404336 {
405337$ builder =new ContainerBuilder ();
406338$ builder ->register ('foo1 ' ,'Bar\FooClass ' )->setConfigurator ('sc_configure ' );
339+ $ this ->assertTrue ($ builder ->get ('foo1 ' )->configured ,'->createService() calls the configurator ' );
340+
407341$ builder ->register ('foo2 ' ,'Bar\FooClass ' )->setConfigurator (array ('%class% ' ,'configureStatic ' ));
408342$ builder ->setParameter ('class ' ,'BazClass ' );
343+ $ this ->assertTrue ($ builder ->get ('foo2 ' )->configured ,'->createService() calls the configurator ' );
344+
409345$ builder ->register ('baz ' ,'BazClass ' );
410346$ 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 ' );
418347$ this ->assertTrue ($ builder ->get ('foo3 ' )->configured ,'->createService() calls the configurator ' );
348+
349+ $ builder ->register ('foo4 ' ,'Bar\FooClass ' )->setConfigurator (array ($ builder ->getDefinition ('baz ' ),'configure ' ));
419350$ this ->assertTrue ($ builder ->get ('foo4 ' )->configured ,'->createService() calls the configurator ' );
420351
352+ $ builder ->register ('foo5 ' ,'Bar\FooClass ' )->setConfigurator ('foo ' );
421353try {
422354$ builder ->get ('foo5 ' );
423355$ this ->fail ('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable ' );
@@ -433,9 +365,6 @@ public function testCreateSyntheticService()
433365 {
434366$ builder =new ContainerBuilder ();
435367$ builder ->register ('foo ' ,'Bar\FooClass ' )->setSynthetic (true );
436-
437- $ builder ->compile ();
438-
439368$ builder ->get ('foo ' );
440369 }
441370
@@ -445,18 +374,13 @@ public function testCreateServiceWithExpression()
445374$ builder ->setParameter ('bar ' ,'bar ' );
446375$ builder ->register ('bar ' ,'BarClass ' );
447376$ builder ->register ('foo ' ,'Bar\FooClass ' )->addArgument (array ('foo ' =>new Expression ('service("bar").foo ~ parameter("bar") ' )));
448-
449- $ builder ->compile ();
450-
451377$ this ->assertEquals ('foobar ' ,$ builder ->get ('foo ' )->arguments ['foo ' ]);
452378 }
453379
454380public function testResolveServices ()
455381 {
456382$ builder =new ContainerBuilder ();
457383$ builder ->register ('foo ' ,'Bar\FooClass ' );
458- $ builder ->compile ();
459-
460384$ this ->assertEquals ($ builder ->get ('foo ' ),$ builder ->resolveServices (new Reference ('foo ' )),'->resolveServices() resolves service references to service instances ' );
461385$ 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 ' );
462386$ this ->assertEquals ($ builder ->get ('foo ' ),$ builder ->resolveServices (new Expression ('service("foo") ' )),'->resolveServices() resolves expressions ' );