interface1 = $interface1; } } class DIIntergrationTests extends TestCase { /** @var DIContainer */ private $container; /** @var ServerContainer */ private $server; protected function setUp(): void { parent::setUp(); $this->server = new ServerContainer(); $this->container = new DIContainer('App1', [], $this->server); } public function testInjectFromServer(): void { $this->server->registerService(Interface1::class, function () { return new ClassA1(); }); $this->server->registerService(ClassB::class, function (SimpleContainer $c) { return new ClassB( $c->query(Interface1::class) ); }); /** @var ClassB $res */ $res = $this->container->query(ClassB::class); $this->assertSame(ClassA1::class, get_class($res->interface1)); } public function testInjectDepFromServer(): void { $this->server->registerService(Interface1::class, function () { return new ClassA1(); }); $this->container->registerService(ClassB::class, function (SimpleContainer $c) { return new ClassB( $c->query(Interface1::class) ); }); /** @var ClassB $res */ $res = $this->container->query(ClassB::class); $this->assertSame(ClassA1::class, get_class($res->interface1)); } public function testOverwriteDepFromServer(): void { $this->server->registerService(Interface1::class, function () { return new ClassA1(); }); $this->container->registerService(Interface1::class, function () { return new ClassA2(); }); $this->container->registerService(ClassB::class, function (SimpleContainer $c) { return new ClassB( $c->query(Interface1::class) ); }); /** @var ClassB $res */ $res = $this->container->query(ClassB::class); $this->assertSame(ClassA2::class, get_class($res->interface1)); } public function testIgnoreOverwriteInServerClass(): void { $this->server->registerService(Interface1::class, function () { return new ClassA1(); }); $this->container->registerService(Interface1::class, function () { return new ClassA2(); }); $this->server->registerService(ClassB::class, function (SimpleContainer $c) { return new ClassB( $c->query(Interface1::class) ); }); /** @var ClassB $res */ $res = $this->container->query(ClassB::class); $this->assertSame(ClassA1::class, get_class($res->interface1)); } }