コード例 #1
0
 /**
  * @test
  */
 public function namedConstructorInjectionWithMultipleParamAndNamedParamGroup()
 {
     $binder = new Binder();
     $binder->bind(Employee::class)->named('schst')->to(Boss::class);
     $binder->bind(Employee::class)->to(TeamMember::class);
     $injector = $binder->getInjector();
     $group = $injector->getInstance(DevelopersMultipleConstructorParamsGroupedName::class);
     assert($group, equals(new DevelopersMultipleConstructorParamsGroupedName(new Boss(), new Boss())));
 }
コード例 #2
0
 /**
  * @test
  */
 public function injectWithProviderClass()
 {
     $binder = new Binder();
     $binder->bind(Answer::class)->toProviderClass(new \ReflectionClass(MyProviderClass::class));
     $question = $binder->getInjector()->getInstance(AnotherQuestion::class);
     assert($question->getAnswer(), isInstanceOf(Answer::class));
 }
コード例 #3
0
 /**
  * @test
  */
 public function classAnnotatedWithSingletonWillOnlyBeCreatedOnce()
 {
     $binder = new Binder();
     $binder->bind(Number::class)->to(RandomSingleton::class);
     $slot = $binder->getInjector()->getInstance(SlotMachine::class);
     assert($slot->number1, isInstanceOf(RandomSingleton::class)->and(isSameAs($slot->number2)));
 }
コード例 #4
0
 /**
  * @test
  */
 public function otherScopeIsUsedToCreateInstance()
 {
     $binder = new Binder();
     $instance = new \stdClass();
     $binder->bind(\stdClass::class)->to(\stdClass::class)->in(NewInstance::of(BindingScope::class)->mapCalls(['getInstance' => $instance]));
     assert($binder->getInjector()->getInstance(\stdClass::class), isSameAs($instance));
 }
コード例 #5
0
 /**
  * @test
  */
 public function injectWithClosure()
 {
     $binder = new Binder();
     $answer = new Answer();
     $binder->bind(Answer::class)->toClosure(function () use($answer) {
         return $answer;
     });
     $question = $binder->getInjector()->getInstance(AnotherQuestion::class);
     assert($question, isInstanceOf(AnotherQuestion::class));
     assert($question->getAnswer(), isSameAs($answer));
 }
コード例 #6
0
 /**
  * @test
  */
 public function explicitBindingOverwritesProvidedByAnnotation()
 {
     $binder = new Binder();
     $binder->bind(Person2::class)->to(Mikey::class);
     assert($binder->getInjector()->getInstance(Person2::class), isInstanceOf(Mikey::class));
 }
コード例 #7
0
 /**
  * configure the binder
  *
  * @param  \stubbles\ioc\Binder  $binder
  * @param  string                $projectPath  optional  project base path
  */
 public function configure(Binder $binder, string $projectPath = null)
 {
     $binder->bind('bar')->to('stdClass');
 }
コード例 #8
0
 /**
  * @test
  */
 public function mixedAnnotations()
 {
     $plugin = NewInstance::of(Plugin::class);
     $binder = new Binder();
     $binder->bindList('listConfig');
     $binder->bindMap('mapConfig');
     $binder->bind(Plugin::class)->named('foo')->toInstance($plugin);
     $binder->bindConstant('foo')->to(42);
     $binder->bindList('aList')->withValue(313);
     $binder->bindMap('aMap')->withEntry('tb', 303);
     assert($this->createPluginHandler($binder)->getArgs(), equals(['std' => $plugin, 'answer' => 42, 'list' => [313], 'map' => ['tb' => 303]]));
 }
コード例 #9
0
 /**
  * set up test environment
  */
 public function setUp()
 {
     $binder = new Binder();
     $binder->bind(Person2::class)->to(Mikey::class)->inSession();
     $this->injector = $binder->getInjector();
 }