public function testDetermineContextFromParent()
 {
     $injector = new Phemto();
     $injector->whenCreating(FromContextTwo::class)->forVariable('property')->useString('property_value');
     $injector->whenCreating(FromContextOne::class)->forVariable('param')->useString('param_value');
     $object = $injector->create(FromContextOne::class);
     $this->assertEquals('property_value', $object->dependency->property);
 }
 public function testInstantiateListOfDependenciesDeclareInSubcontext()
 {
     $injector = new Phemto();
     $injector->whenCreating(RequiresDependencies::class)->forVariable('dependencies')->willUse(new ListOf(new Graph(InstantiatedByGrathDependency::class, 'graph1'), AutomaticallyInstantiatedDependency::class, new DirectlyInstantiatedDependency()));
     $injector->whenCreating(InstantiatedByGrathDependency::class)->forVariable('property')->useString('property for graph1');
     $object = $injector->create(RequiresDependencies::class);
     $this->assertCount(3, $object->dependencies);
     $this->assertInstanceOf(InstantiatedByGrathDependency::class, $object->dependencies[0]);
     $this->assertInstanceOf(AutomaticallyInstantiatedDependency::class, $object->dependencies[1]);
     $this->assertInstanceOf(DirectlyInstantiatedDependency::class, $object->dependencies[2]);
     $this->assertEquals('property for graph1', $object->dependencies[0]->property);
 }
 function TODO_testSessionableWorksWithinContext()
 {
     $injector = new Phemto();
     $injector->whenCreating('phemto\\acceptance\\HoldsSessionable')->willUse(new Sessionable('phemto\\acceptance\\SerialiseMe'));
     $holder = $injector->create('phemto\\acceptance\\HoldsSessionable');
     $this->assertSame($holder->dependency, $_SESSION['SerialiseMe']);
 }
 public function testDetermineImplementationFromTopContext()
 {
     $injector = new Phemto();
     $injector->willUse(new Reused(Preference::class));
     $injector->whenCreating(HasDependency::class)->forVariable('dependency')->willUse(Preference::class);
     $object = $injector->create(HasDependency::class);
     $this->assertSame(spl_object_hash($object->dependency), spl_object_hash($injector->create(Preference::class)));
 }
 function testCanOverridePreferenceWhenInstantiatingFromAnInterface()
 {
     $injector = new Phemto();
     $injector->whenCreating('phemto\\acceptance\\SpecialInterface')->willUse('phemto\\acceptance\\SpecialImplementation');
     $injector->willUse('phemto\\acceptance\\UsualImplementation');
     $special = $injector->create('phemto\\acceptance\\SpecialInterface');
     $this->assertEquals($special, new ClassWithSpecial(new SpecialImplementation()));
 }
 public function testCanCallSettersForDependecyWhenListOfUsed()
 {
     $injector = new Phemto();
     $injector->whenCreating(SomeClassNeedArray::class)->forVariable('dependencies')->willUse(new ListOf(new Reused(DependencyWithSetter::class)));
     $injector->forType(DependencyWithSetter::class)->call('setProperty');
     $object = $injector->create(SomeClassNeedArray::class);
     $this->assertInstanceOf(NotWithoutMe::class, $object->dependencies[0]->property);
 }
 function testCanWrapWithDecorator()
 {
     $injector = new Phemto();
     $injector->whenCreating('phemto\\acceptance\\Bare')->wrapWith('phemto\\acceptance\\WrapperForBare');
     $this->assertEquals($injector->create('phemto\\acceptance\\Bare'), new WrapperForBare(new BareImplementation()));
 }
 public function testInstantiateBySettersWhenGraphIsUsed()
 {
     $injector = new Phemto();
     $injector->whenCreating(ThirdClass::class)->forVariable('dependency')->willUse(new Graph(WithSetterByVariable::class, 'graph1'));
     $injector->forType(WithSetterByVariable::class)->call('setProperty');
     $injector->whenCreating(WithSetterByVariable::class, 'graph1')->forVariable('property')->useString('value for graph1');
     $object = $injector->create(ThirdClass::class);
     $this->assertEquals('value for graph1', $object->dependency->property);
 }