/**
  * @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)));
 }
 /**
  * @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));
 }
 /**
  * @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));
 }
 /**
  * @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())));
 }
 /**
  * @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));
 }
Example #6
0
 /**
  * @test
  */
 public function instanceCreationThrowsBindingExceptionWhenNoPropertiesBound()
 {
     $injector = Binder::createInjector();
     expect(function () use($injector) {
         $injector->getInstance(PropertyReceiver::class);
     })->throws(BindingException::class)->message(startsWith('Can not inject into ' . PropertyReceiver::class . '::__construct($foo).' . ' No binding for type __PROPERTY__ (named "example.foo") specified.'));
 }
 /**
  * @since  2.1.0
  * @test
  * @group  issue_31
  */
 public function injectConstantViaClosure()
 {
     $binder = new Binder();
     $binder->bindConstant('answer')->toClosure(function () {
         return 42;
     });
     $this->assertConstantInjection($binder->getInjector());
 }
 /**
  * @test
  */
 public function explicitBindingOverwritesProvidedByAnnotation()
 {
     $binder = new Binder();
     $binder->bind(Person2::class)->to(Mikey::class);
     assert($binder->getInjector()->getInstance(Person2::class), isInstanceOf(Mikey::class));
 }
 /**
  * @test
  * @since  4.1.3
  */
 public function propertyBindingUsedWhenParamHasTypeHintButIsAnnotated()
 {
     try {
         $binder = new Binder();
         $properties = new Properties(['config' => ['example.password' => 'somePassword']]);
         $binder->bindProperties($properties, 'PROD');
         $example = $binder->getInjector()->getInstance(Example::class);
         assert($example->password, isInstanceOf(Secret::class));
     } finally {
         // ensure all references are removed to clean up environment
         unset($properties);
         $example->password = null;
         unset($example);
         unset($binder);
         gc_collect_cycles();
     }
 }
 /**
  * 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');
 }
 /**
  * creates plugin handler instance
  *
  * @param   Binder  $binder
  * @return  PluginHandler
  */
 private function createPluginHandler(Binder $binder) : PluginHandler
 {
     return $binder->getInjector()->getInstance(PluginHandler::class);
 }
 /**
  * @test
  * @since  6.0.0
  */
 public function throwsBindingExceptionWhenNoFallbackSpecifiedAndNoModeSet()
 {
     $injector = Binder::createInjector();
     expect(function () use($injector) {
         $injector->getInstance(Person4::class);
     })->throws(BindingException::class);
 }
 /**
  * set up test environment
  */
 public function setUp()
 {
     $binder = new Binder();
     $binder->bind(Person2::class)->to(Mikey::class)->inSession();
     $this->injector = $binder->getInjector();
 }
 /**
  * @test
  * @since  5.1.0
  */
 public function constructorInjectionWithOptionalSecondParam()
 {
     $injector = Binder::createInjector(function (Binder $binder) {
         $binder->bind(Tire::class)->to(Goodyear::class);
     });
     $bike = $injector->getInstance(BikeWithOptionalOtherParam::class);
     assert($bike->other, equals('foo'));
 }
Example #15
0
 /**
  * configure the binder
  *
  * @param  \stubbles\ioc\Binder  $binder
  * @param  string                $projectPath  optional  project base path
  */
 public function configure(Binder $binder, string $projectPath = null)
 {
     $this->environment->registerErrorHandler($projectPath);
     $this->environment->registerExceptionHandler($projectPath);
     $binder->setEnvironment($this->environment->name())->bind(Environment::class)->toInstance($this->environment);
     if (file_exists($this->propertiesFile($projectPath))) {
         $binder->bindPropertiesFromFile($this->propertiesFile($projectPath), $this->environment->name());
     }
     $binder->bindConstant('stubbles.project.path')->to($projectPath);
     foreach ($this->buildPathes($projectPath) as $name => $value) {
         $binder->bindConstant($name)->to($value);
     }
 }