コード例 #1
0
 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);
 }
コード例 #2
0
 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']);
 }
コード例 #3
0
 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()));
 }
コード例 #4
0
 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()));
 }
コード例 #6
0
 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)));
 }
コード例 #7
0
 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);
 }
コード例 #8
0
 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);
 }
コード例 #9
0
 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);
 }
コード例 #10
0
 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);
 }
コード例 #11
0
 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);
 }
コード例 #12
0
 function testCanBeConfiguredToPreferSpecificImplementation()
 {
     $injector = new Phemto();
     $injector->willUse('phemto\\acceptance\\SecondImplementation');
     $this->assertInstanceOf('phemto\\acceptance\\SecondImplementation', $injector->create('phemto\\acceptance\\InterfaceWithManyImplementations'));
 }
コード例 #13
0
 function testCanUseShorterSyntacticForm()
 {
     $injector = new Phemto();
     $this->assertEquals($injector->create('phemto\\acceptance\\ClassWithAnyOldParameters', 3, 5), new ClassWithAnyOldParameters(3, 5));
 }
コード例 #14
0
 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);
 }
コード例 #15
0
ファイル: SimpleInjection.php プロジェクト: anter-x/phemto
<?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'));
コード例 #16
0
 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');
 }
コード例 #17
0
 function testSameInstanceCanBeReusedWithinFactory()
 {
     $injector = new Phemto();
     $injector->willUse(new Reused('phemto\\acceptance\\CreateMeOnce'));
     $this->assertSame($injector->create('phemto\\acceptance\\CreateMeOnce'), $injector->create('phemto\\acceptance\\CreateMeOnce'));
 }
コード例 #18
0
 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()));
 }
コード例 #19
0
ファイル: Context.php プロジェクト: anter-x/phemto
 /**
  * @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()));
 }
コード例 #21
0
 function testCanBeConfiguredToPreferSpecificSubclass()
 {
     $injector = new Phemto();
     $injector->willUse('phemto\\acceptance\\SecondSubclass');
     $this->assertInstanceOf('phemto\\acceptance\\SecondSubclass', $injector->create('phemto\\acceptance\\ClassWithManySubclasses'));
 }
コード例 #22
0
 function testInjectingString()
 {
     $injector = new Phemto();
     $injector->forVariable('thing')->useString('100');
     $this->assertEquals($injector->create('phemto\\acceptance\\WrapAnything'), new WrapAnything('100'));
 }
コード例 #23
0
 function testCanFillMissingParametersWithExplicitValues()
 {
     $injector = new Phemto();
     $this->assertEquals($injector->fill('a', 'b')->with(3, 5)->create('phemto\\acceptance\\ClassWithParameters'), new ClassWithParameters(3, 5));
 }