/** * @covers Symfony\Components\DependencyInjection\Container::getServiceIds */ public function testGetServiceIds() { $sc = new Container(); $sc->set('foo', $obj = new \stdClass()); $sc->set('bar', $obj = new \stdClass()); $this->assertEquals(array('service_container', 'foo', 'bar'), $sc->getServiceIds(), '->getServiceIds() returns all defined service ids'); $sc = new ProjectServiceContainer(); $this->assertEquals(array('bar', 'foo_bar', 'foo.baz', 'service_container'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods'); }
$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; } protected function getFooBarService()
public function testGetServiceIds() { $sc = new Container(); $sc->setService('foo', $obj = new \stdClass()); $sc->setService('bar', $obj = new \stdClass()); $this->assertEquals(array('service_container', 'foo', 'bar'), $sc->getServiceIds(), '->getServiceIds() returns all defined service ids'); $sc = new ProjectServiceContainer(); $this->assertEquals(spl_object_hash($sc->__bar), spl_object_hash($sc->getService('bar')), '->getService() looks for a getXXXService() method'); $this->assertTrue($sc->hasService('bar'), '->hasService() returns true if the service has been defined as a getXXXService() method'); $sc->setService('bar', $bar = new \stdClass()); $this->assertEquals(spl_object_hash($sc->getService('bar')), spl_object_hash($bar), '->getService() prefers to return a service defined with a getXXXService() method than one defined with setService()'); try { $sc->getService('baba'); $this->fail('->getService() thrown an \\InvalidArgumentException if the service does not exist'); } catch (\Exception $e) { $this->assertInstanceOf('\\InvalidArgumentException', $e, '->getService() thrown an \\InvalidArgumentException if the service does not exist'); $this->assertEquals('The service "baba" does not exist.', $e->getMessage(), '->getService() thrown an \\InvalidArgumentException if the service does not exist'); } try { $sc->baba; $this->fail('->__get() thrown an \\InvalidArgumentException if the service does not exist'); } catch (\Exception $e) { $this->assertInstanceOf('\\InvalidArgumentException', $e, '->__get() thrown an \\InvalidArgumentException if the service does not exist'); $this->assertEquals('The service "baba" does not exist.', $e->getMessage(), '->__get() thrown an \\InvalidArgumentException if the service does not exist'); } try { unset($sc->baba); $this->fail('->__unset() thrown an LogicException if you try to remove a service'); } catch (\Exception $e) { $this->assertInstanceOf('\\LogicException', $e, '->__unset() thrown an LogicException if you try to remove a service'); $this->assertEquals('You can\'t unset a service.', $e->getMessage(), '->__unset() thrown an LogicException if you try to remove a service'); } $this->assertEquals(spl_object_hash($sc->__foo_bar), spl_object_hash($sc->getService('foo_bar')), '->getService() camelizes the service id when looking for a method'); $this->assertEquals(spl_object_hash($sc->__foo_baz), spl_object_hash($sc->getService('foo.baz')), '->getService() camelizes the service id when looking for a method'); }
/** * Gets all service ids. * * @return array An array of all defined service ids */ public function getServiceIds() { return array_unique(array_merge(array_keys($this->getDefinitions()), array_keys($this->aliases), parent::getServiceIds())); }