public function testContainerToCallback() { $c = new Container(); $c->share('service', function (Container $container) { return $container; }); $this->assertInstanceOf(Container::class, $c->get('service')); }
public function testShare() { $this->object['obj'] = $this->object->share(function () { return new \stdClass(); }); $this->assertInstanceOf('\\stdClass', $this->object['obj']); $obj1 = $this->object['obj']; $obj2 = $this->object['obj']; $this->assertTrue($obj2 === $obj1); }
public function testComplex() { $c = new Container(); $c->share('A', function (Container $c) { return new A($c->get('B'), $c->get('C')); }); $c->share('B', function (Container $c) { return new B($c->get('D')); }); $c->share('C', function () { return new C(); }); $c->share('D', function () { return new D(); }); /** @var A $a */ $a = $c->get('A'); $this->assertEquals('ok', $a->x()); }
/** * @expectedException UnexpectedValueException * @expectedExceptionMessage Item "sharedService" is not a closure or invokable object and cannot be extended */ public function testExtendSharedServiceAfterConstruct() { $container = new Container(); $container->share('sharedService', function ($container) { return new \stdClass(); }); $service = $container->get('sharedService'); $container->extend('sharedService', function ($container) { $container->get('sharedService')->member = 'value'; }); }
public function testGetShared() { $container = new Container(); $container->share('service', function () { return new \stdClass(); }); $service1 = $container->service; $service2 = $container->service; $this->assertInstanceOf('stdClass', $service1); $this->assertInstanceOf('stdClass', $service2); $this->assertSame($service2, $service1); }