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'); }
/** * @covers Symfony\Components\DependencyInjection\Container::has * @covers Symfony\Components\DependencyInjection\Container::offsetExists */ public function testHas() { $sc = new ProjectServiceContainer(); $sc->set('foo', new \stdClass()); $this->assertFalse($sc->has('foo1'), '->has() returns false if the service does not exist'); $this->assertTrue($sc->has('foo'), '->has() returns true if the service exists'); $this->assertTrue($sc->has('bar'), '->has() returns true if a get*Method() is defined'); $this->assertTrue($sc->has('foo_bar'), '->has() returns true if a get*Method() is defined'); $this->assertTrue($sc->has('foo.baz'), '->has() returns true if a get*Method() is defined'); $this->assertFalse(isset($sc['foo1']), '->offsetExists() returns false if the service does not exist'); $this->assertTrue(isset($sc['foo']), '->offsetExists() returns true if the service exists'); $this->assertTrue(isset($sc['bar']), '->offsetExists() returns true if a get*Method() is defined'); $this->assertTrue(isset($sc['foo_bar']), '->offsetExists() returns true if a get*Method() is defined'); $this->assertTrue(isset($sc['foo.baz']), '->offsetExists() returns true if a get*Method() is defined'); }