public function testTraversePass() { $ioc = new IocImpl(); $ioc->register(A2::class, ["traverse" => true]); $a_1 = $ioc->get(IA::class); $a_11 = $ioc->get(IA2::class); $this->assertSame($a_1, $a_11); }
public function testResolveMultipleDependencyPass() { $ioc = new IocImpl(); $ioc->register(DependentAB::class); $ioc->register(A::class); $ioc->register(B::class); $a = $ioc->get(IA::class); $b = $ioc->get(IB::class); $dependentAB = $ioc->get(IDependentAB::class); $this->assertNotNull($dependentAB); $this->assertNotNull($a); $this->assertNotNull($b); $this->assertSame($a, $dependentAB->getA()); $this->assertSame($b, $dependentAB->getB()); }
public function testCreateClassWithDependenciesAndParameter_Pass() { $ioc = new IocImpl(); $testValue = "Hello"; $ioc->register(ClassA::class); $ioc->register(ClassB::class, ["factory" => function (InterfaceA $a, $value) { return new ClassB($a, $value); }, "parameters" => ["value" => $testValue]]); $a = $ioc->get(InterfaceA::class); $this->assertNotNull($a); $b = $ioc->get(InterfaceB::class); $this->assertNotNull($b); $this->assertEquals($testValue, $b->getValue()); $this->assertInstanceOf(InterfaceA::class, $a); $this->assertInstanceOf(InterfaceB::class, $b); $this->assertSame($a, $b->getA()); }
public function testInvokeClosureWithMappings_Pass() { $ioc = new IocImpl(); $ioc->register(ClassA::class, ["id" => "1"]); $ioc->register(ClassA::class, ["id" => "2"]); $a1 = $ioc->get(InterfaceA::class, "1"); $a2 = $ioc->get(InterfaceA::class, "2"); $result1 = $ioc->invoke(function (InterfaceA $a) { return $a; }, ["mappings" => ["a" => "1"]]); $this->assertNotNull($result1); $this->assertSame($a1, $result1); $result2 = $ioc->invoke(function (InterfaceA $a) { return $a; }, ["mappings" => ["a" => "2"]]); $this->assertNotNull($result2); $this->assertSame($a2, $result2); }
public function testWithParameterPass() { $ioc = new IocImpl(); $param = "123"; $ioc->register(UnknownVarNoDefault::class, ["parameters" => ["var" => $param]]); $unknown = $ioc->get(IUnknownVar::class); $this->assertNotNull($unknown); $this->assertSame($param, $unknown->getVar()); }
public function testRegisterUnknownCallsResolverWithId() { $idHistory = []; $resolver = function (IocNonResolvable $nonResolvable) use(&$idHistory) { $config = $nonResolvable->config(); $this->assertNotNull($config); $idHistory[] = $config["id"]; return $config["id"]; }; $ioc = new IocImpl($resolver); $ioc->register(UnknownVarNoDefault::class, ["id" => "i1"]); $ioc->register(UnknownVarNoDefault::class, ["id" => "i2"]); $i1 = $ioc->get(IUnknownVar::class, "i1"); $i2 = $ioc->get(IUnknownVar::class, "i2"); $this->assertEquals(2, count($idHistory)); $this->assertEquals("i1", $idHistory[0]); $this->assertEquals("i2", $idHistory[1]); }
public function testWithTypeSuccessA() { $ioc = new IocImpl(); $ioc->register(B::class); $ioc->register(A::class); $ioc->register(A2::class); $ioc->register(DependentArrayA::class, ["types" => ["a" => IA::class]]); $dependentA = $ioc->get(IDependentArrayA::class); $this->assertEquals(2, count($dependentA->getAs())); $this->assertInstanceOf(IA::class, $dependentA->getAs()[0]); $this->assertInstanceOf(IA::class, $dependentA->getAs()[1]); }
public function testUnknownFailsWithClosureToDeliveryArray() { $array = [1]; $closure = function (IocNonResolvable $nonResolvable) use($array) { return $array; }; $ioc = new IocImpl($closure); $ioc->register(UnknownArrayNoDefault::class); $unknown = $ioc->get(IUnknownArray::class); $this->assertNotNull($unknown); $this->assertSame($array, $unknown->getArray()); }
public function testResolveClassByClassName() { $ioc = new IocImpl(); $a = new A(); $ioc->register($a, ["registerClass" => true]); $this->assertSame($a, $ioc->get(A::class)); }