try { $sc->getService('baba'); $t->fail('->getService() thrown an \\InvalidArgumentException if the service does not exist'); } catch (\InvalidArgumentException $e) { $t->pass('->getService() thrown an \\InvalidArgumentException if the service does not exist'); } try { $sc->baba; $t->fail('->__get() thrown an \\InvalidArgumentException if the service does not exist'); } catch (\InvalidArgumentException $e) { $t->pass('->__get() thrown an \\InvalidArgumentException if the service does not exist'); } try { unset($sc->baba); $t->fail('->__unset() thrown an LogicException if you try to remove a service'); } catch (LogicException $e) { $t->pass('->__unset() thrown an LogicException if you try to remove a service'); } $t->is(spl_object_hash($sc->getService('foo_bar')), spl_object_hash($sc->__foo_bar), '->getService() camelizes the service id when looking for a method'); $t->is(spl_object_hash($sc->getService('foo.baz')), spl_object_hash($sc->__foo_baz), '->getService() camelizes the service id when looking for a method'); // __call() $t->diag('__call()'); $sc = new Container(); $sc->setService('foo_bar.foo', $foo = new stdClass()); $t->is($sc->getFooBar_FooService(), $foo, '__call() finds services is the method is getXXXService()'); try { $sc->getFooBar_Foo(); $t->fail('__call() throws a \\RuntimeException exception if the method is not a service method'); } catch (\RuntimeException $e) { $t->pass('__call() throws a \\RuntimeException exception if the method is not a service method'); }
public function testMagicCall() { $sc = new Container(); $sc->setService('foo_bar.foo', $foo = new \stdClass()); $this->assertEquals($sc->getFooBar_FooService(), $foo, '__call() finds services is the method is getXXXService()'); try { $sc->getFooBar_Foo(); $this->fail('__call() throws a \\RuntimeException exception if the method is not a service method'); } catch (\RuntimeException $e) { } }
public function testMagicCall() { $sc = new Container(); $sc->setService('foo_bar.foo', $foo = new \stdClass()); $this->assertEquals($foo, $sc->getFooBar_FooService(), '__call() finds services is the method is getXXXService()'); try { $sc->getFooBar_Foo(); $this->fail('__call() throws a \\BadMethodCallException exception if the method is not a service method'); } catch (\Exception $e) { $this->assertInstanceOf('\\BadMethodCallException', $e, '__call() throws a \\BadMethodCallException exception if the method is not a service method'); $this->assertEquals('Call to undefined method Symfony\\Components\\DependencyInjection\\Container::getFooBar_Foo.', $e->getMessage(), '__call() throws a \\BadMethodCallException exception if the method is not a service method'); } }
// ->setService() ->hasService() ->getService() $t->diag('->setService() ->hasService() ->getService()'); $sc = new Container(); $sc->setService('foo', $obj = new stdClass()); $t->is(spl_object_hash($sc->getService('foo')), spl_object_hash($obj), '->setService() registers a service under a key name'); $sc->foo1 = $obj1 = new stdClass(); $t->is(spl_object_hash($sc->foo1), spl_object_hash($obj1), '->__set() sets a service'); $t->is(spl_object_hash($sc->foo), spl_object_hash($obj), '->__get() gets a service by name'); $t->ok($sc->hasService('foo'), '->hasService() returns true if the service is defined'); $t->ok(isset($sc->foo), '->__isset() returns true if the service is defined'); $t->ok(!$sc->hasService('bar'), '->hasService() returns false if the service is not defined'); $t->ok(!isset($sc->bar), '->__isset() returns false if the service is not defined'); // ->getServiceIds() $t->diag('->getServiceIds()'); $sc = new Container(); $sc->setService('foo', $obj = new stdClass()); $sc->setService('bar', $obj = new stdClass()); $t->is($sc->getServiceIds(), array('service_container', 'foo', 'bar'), '->getServiceIds() returns all defined service ids'); class ProjectServiceContainer extends Container { public $__bar, $__foo_bar, $__foo_baz; public function __construct() { parent::__construct(); $this->__bar = new stdClass(); $this->__foo_bar = new stdClass(); $this->__foo_baz = new stdClass(); } protected function getBarService() { return $this->__bar;