Exemplo n.º 1
0
 /**
  * @covers Symfony\Component\DependencyInjection\Container::get
  * @covers Symfony\Component\DependencyInjection\Container::offsetGet
  */
 public function testGet()
 {
     $sc = new ProjectServiceContainer();
     $sc->set('foo', $foo = new \stdClass());
     $this->assertEquals($foo, $sc->get('foo'), '->get() returns the service for the given id');
     $this->assertEquals($sc->__bar, $sc->get('bar'), '->get() returns the service for the given id');
     $this->assertEquals($sc->__foo_bar, $sc->get('foo_bar'), '->get() returns the service if a get*Method() is defined');
     $this->assertEquals($sc->__foo_baz, $sc->get('foo.baz'), '->get() returns the service if a get*Method() is defined');
     $sc->set('bar', $bar = new \stdClass());
     $this->assertEquals(spl_object_hash($sc->get('bar')), spl_object_hash($bar), '->getServiceIds() prefers to return a service defined with a getXXXService() method than one defined with set()');
     try {
         $sc->get(new \stdClass());
         $this->fail('->get() throws a \\InvalidArgumentException exception if the service id is not a string');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\InvalidArgumentException', $e, '->get() throws a \\InvalidArgumentException exception if the service id is not a string');
     }
     try {
         $sc->get('');
         $this->fail('->get() throws a \\InvalidArgumentException exception if the service is empty');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\InvalidArgumentException', $e, '->get() throws a \\InvalidArgumentException exception if the service is empty');
         $this->assertEquals('The service "" does not exist.', $e->getMessage(), '->get() throws a \\InvalidArgumentException exception if the service is empty');
     }
     $this->assertNull($sc->get('', ContainerInterface::NULL_ON_INVALID_REFERENCE));
     try {
         $sc[''];
         $this->fail('->get() throws a \\InvalidArgumentException exception if the service is empty');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\InvalidArgumentException', $e, '->get() throws a \\InvalidArgumentException exception if the service is empty');
         $this->assertEquals('The service "" does not exist.', $e->getMessage(), '->get() throws a \\InvalidArgumentException exception if the service is empty');
     }
 }
Exemplo n.º 2
0
    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());
        }
    }
Exemplo n.º 3
0
 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);
 }