public function testCanCallMethodWithParam() { $di = new Phemto(); $instance = $di->create('phemto\\acceptance\\ClassWithMethods'); $this->assertEquals('ok', $di->call($instance, 'aMethod')); $this->assertEquals(new RandomDependency(), $instance->dependency); }
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']); }
function testExplicitlyNamedVariables() { $injector = new Phemto(); $injector->forVariable('first')->willUse('phemto\\acceptance\\NeededForFirst'); $injector->forVariable('second')->willUse('phemto\\acceptance\\NeededForSecond'); $this->assertEquals($injector->create('phemto\\acceptance\\VariablesInConstructor'), new VariablesInConstructor(new NeededForFirst(), new NeededForSecond())); }
public function testInstantiateListOfDependenciesDeclareInTopContext() { $injector = new Phemto(); $injector->forVariable('dependencies')->willUse(new ListOf(AutomaticallyInstantiatedDependency::class)); $object = $injector->create(RequiresDependencies::class); $this->assertInstanceOf(AutomaticallyInstantiatedDependency::class, $object->dependencies[0]); }
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 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))); }
public function testUsingApplicationConfigValue() { $injector = new Phemto(); $injector->forVariable(ConfigValue::CONFIG_NAME)->willUse(SomeApplicationConfig::class); $injector->forVariable('property')->useConfig('config_param1'); $object = $injector->create(SomeClassNeedConfigValue::class); $this->assertEquals((new SomeApplicationConfig())->data['config_param1'], $object->property); }
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 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 testSetterInjectionStopsAtFirstMissingArgument() { $injector = new Phemto(); $injector->forVariable('maybe')->willUse('phemto\\acceptance\\MaybeThis'); $injector->forVariable('unlikely')->willUse('phemto\\acceptance\\MaybeThis'); $injector->forType('phemto\\acceptance\\OptionalArgumentsInSetter')->call('readyToGo'); $expected = new OptionalArgumentsInSetter(); $expected->readyToGo(new MaybeThis(), null, new MaybeThis()); $this->assertEquals($injector->create('phemto\\acceptance\\OptionalArgumentsInSetter'), $expected); }
public function testSameInstanceCanBeReusedWithinFactoryDependingOnObjectParam() { $injector = new Phemto(); $injector->willUse(new ReusedByParam(CreateMeOnceByObjectParam::class, 'dependency')); $dependency = new SomeDependency(); $object1 = $injector->create(CreateMeOnceByObjectParam::class, $dependency); $object2 = $injector->create(CreateMeOnceByObjectParam::class, $dependency); $object3 = $injector->create(CreateMeOnceByObjectParam::class, new SomeDependency()); $this->assertSame($object1, $object2); $this->assertNotSame($object1, $object3); }
function testCanBeConfiguredToPreferSpecificImplementation() { $injector = new Phemto(); $injector->willUse('phemto\\acceptance\\SecondImplementation'); $this->assertInstanceOf('phemto\\acceptance\\SecondImplementation', $injector->create('phemto\\acceptance\\InterfaceWithManyImplementations')); }
function testCanUseShorterSyntacticForm() { $injector = new Phemto(); $this->assertEquals($injector->create('phemto\\acceptance\\ClassWithAnyOldParameters', 3, 5), new ClassWithAnyOldParameters(3, 5)); }
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); }
<?php use phemto\Phemto; require_once __DIR__ . '/../vendor/autoload.php'; $di = new Phemto(); class Config { public $bar = 'asdf'; } // Would be done during startup once we resolve which config to load. $di->willUse('Config'); class Database { public function __construct(Config $config) { $this->config = $config; } } $di->willUse('Database'); var_dump($di->create('Database'));
public function test_exception_message_for_double_nested_missing_dependencies() { $di = new Phemto(); $this->setExpectedException('phemto\\exception\\MissingDependency', 'While creating phemto\\acceptance\\DoubleNestedMissingDependency: While creating phemto\\acceptance\\ClassWithMissingDependency: Missing dependency \'d\''); $di->create('phemto\\acceptance\\DoubleNestedMissingDependency'); }
function testSameInstanceCanBeReusedWithinFactory() { $injector = new Phemto(); $injector->willUse(new Reused('phemto\\acceptance\\CreateMeOnce')); $this->assertSame($injector->create('phemto\\acceptance\\CreateMeOnce'), $injector->create('phemto\\acceptance\\CreateMeOnce')); }
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())); }
/** * @return ClassRepository */ function repository() { return $this->parent->repository(); }
function testRepeatedHintJustGetsTwoSeparateInstances() { $injector = new Phemto(); $this->assertEquals($injector->create('phemto\\acceptance\\RepeatedHintConstructor'), new RepeatedHintConstructor(new NeededForConstructor(), new NeededForConstructor())); }
function testCanBeConfiguredToPreferSpecificSubclass() { $injector = new Phemto(); $injector->willUse('phemto\\acceptance\\SecondSubclass'); $this->assertInstanceOf('phemto\\acceptance\\SecondSubclass', $injector->create('phemto\\acceptance\\ClassWithManySubclasses')); }
function testInjectingString() { $injector = new Phemto(); $injector->forVariable('thing')->useString('100'); $this->assertEquals($injector->create('phemto\\acceptance\\WrapAnything'), new WrapAnything('100')); }
function testCanFillMissingParametersWithExplicitValues() { $injector = new Phemto(); $this->assertEquals($injector->fill('a', 'b')->with(3, 5)->create('phemto\\acceptance\\ClassWithParameters'), new ClassWithParameters(3, 5)); }