/**
  * @covers DependencyInjection::getInstance
  * @todo   Implement testGetInstance().
  */
 public function testGetInstance()
 {
     $this->assertTrue($this->object->getInstance(A::class) instanceof A);
     $this->object->setModule(I::class, B::class);
     $this->assertTrue($this->object->getInstance(I::class) instanceof B);
     $this->object->setModule(B::class, C::class);
     $this->assertTrue($this->object->getInstance(B::class) instanceof C);
     try {
         $this->object->getInstance('InexistantClassName');
         $this->fail('Expected DependencyInjection_Exception');
     } catch (DependencyInjection_Exception $ex) {
     }
     $p = $this->object->getInstance(P::class);
     $this->assertSame($p, $this->object->getInstance(P::class));
     try {
         $this->object->getInstance(NI::class);
         $this->fail('Expected DependencyInjection_Exception');
     } catch (DependencyInjection_Exception $ex) {
     }
     $ni = new NI();
     $this->object->setInstance($ni);
     $this->assertSame($ni, $this->object->getInstance(NI::class));
     $inject = $this->object->getInstance(Inject::class);
     $this->assertTrue($inject->i instanceof I);
     $this->assertTrue($inject->a instanceof A);
     $withoutFactory = $this->object->getInstance(WithoutCustomFactory::class);
     $this->assertSame(0, $withoutFactory->a);
     $this->assertSame(1, $withoutFactory->b);
     $withFactory = $this->object->getInstance(WithCustomFactory::class);
     $this->assertSame(5, $withFactory->a);
     $this->assertSame(3, $withFactory->b);
     $this->object->setModule(MyAbstractFactory::class, InvalidInterfaceFactory::class);
     try {
         $this->object->getInstance(WithAbstractFactory::class);
         $this->fail('Expected DependencyInjection_Exception');
     } catch (DependencyInjection_Exception $ex) {
     }
     $this->object->setModule(MyAbstractFactory::class, InvalidImplFactory::class);
     try {
         $this->object->getInstance(WithAbstractFactory::class);
         $this->fail('Expected DependencyInjection_Exception');
     } catch (DependencyInjection_Exception $ex) {
     }
     $this->object->setModule(MyAbstractFactory::class, ValidFactory::class);
     $withAbstract = $this->object->getInstance(WithAbstractFactory::class);
     $this->assertTrue($withAbstract->a instanceof A);
     $this->assertSame(9, $withAbstract->value);
     $this->object->setModule(MyAbstractFactory::class, ValidFactory2::class);
     $withAbstract = $this->object->getInstance(WithAbstractFactory::class);
     $this->assertTrue($withAbstract->a instanceof A);
     $this->assertSame(9 * 2, $withAbstract->value);
 }
示例#2
0
 public function __construct()
 {
     $this->injector = new DependencyInjection();
     $this->injector->setInstance($this);
 }