Exemplo n.º 1
0
 public function testProvidersLazySingleton()
 {
     // Arrange
     $injector = new Injector(null);
     $p = ClassFactoryProvider::init(HeavyTire::class, function (Injector $injector) {
         //no HeavyTire
         return new HeavyTire();
     })->singleton();
     $injector->providers([Tire::class => $p->lazy()]);
     /** @var Tire $entity */
     $entity = $injector->{Tire::class};
     $this->assertInstanceOf(Tire::class, $entity);
     $this->assertInstanceOf(HeavyTire::class, $entity);
     //not
     $this->assertEquals($entity->type, 'HeavyTire');
     $entity2 = $injector->{Tire::class};
     $this->assertEquals($entity2->type, 'HeavyTire');
     $this->assertSame($entity->getInstance(), $entity2->getInstance());
 }
Exemplo n.º 2
0
 public function testNonInjectableDependency()
 {
     // Arrange
     $injector = new Injector(null);
     $injector->providers([NonInjectableWrapper::class => ClassProvider::init(NonInjectableWrapper::class), NonInjectable::class => ClassFactoryProvider::init(NonInjectable::class, function () {
         return new NonInjectable(5);
     })]);
     /** @var NonInjectableWrapper $wrapper */
     $wrapper = $injector->get(NonInjectableWrapper::class);
     $this->assertEquals($wrapper->nonInjectable->val, 5);
 }