/** * @covers Symfony\Component\DependencyInjection\Container::has * @covers Symfony\Component\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'); }
public function testGetThrowsException() { $c = new ProjectServiceContainer(); try { $c->get('throw_exception'); $this->fail(); } catch (\Exception $e) { $this->assertEquals('Something went terribly wrong!', $e->getMessage()); } try { $c->get('throw_exception'); $this->fail(); } catch (\Exception $e) { $this->assertEquals('Something went terribly wrong!', $e->getMessage()); } }
public function testEnterLeaveCurrentScope() { $container = new ProjectServiceContainer(); $container->addScope(new Scope('foo')); $container->enterScope('foo'); $scoped1 = $container->get('scoped'); $scopedFoo1 = $container->get('scoped_foo'); $container->enterScope('foo'); $scoped2 = $container->get('scoped'); $scoped3 = $container->get('scoped'); $scopedFoo2 = $container->get('scoped_foo'); $container->leaveScope('foo'); $scoped4 = $container->get('scoped'); $scopedFoo3 = $container->get('scoped_foo'); $this->assertNotSame($scoped1, $scoped2); $this->assertSame($scoped2, $scoped3); $this->assertSame($scoped1, $scoped4); $this->assertNotSame($scopedFoo1, $scopedFoo2); $this->assertSame($scopedFoo1, $scopedFoo3); }