public function testBinding() { $injector = new Injector(null); $injector->bind([Binding::init(HeavyEngine::class)->lazy(), bind("AAA")->toAlias(HeavyEngine::class)]); $engine = $injector->get('AAA'); $this->assertInstanceOf(Engine::class, $engine); }
/** * Injector constructor. * @param $injector */ public function __construct(Injector $injector = null) { $this->injector = $injector; if (!$injector) { $this->bind(Binding::init(LazyProxyBuilder::class)->toValue(LazyProxyBuilder::init()), bind(ProxyCache::class)->toValue(ProxyCache::init())); } }
public function testBindingFuunfctionProvider() { $injector = new Injector(null); $injector(bind(HeavyTire::class)); $entity = $injector->{HeavyTire::class}; $this->assertInstanceOf(Tire::class, $entity); $this->assertInstanceOf(HeavyTire::class, $entity); }
public function testBindings() { // Arrange $injector = new Injector(null); $injector->bind(bind(Car::class)->toClass(FastCar::class), bind(Engine::class)->toClass(Engine::class)->singleton()); /** @var Car $car */ $car = $injector->get(Car::class); /** @var Engine $engine */ $engine = $injector->get(Engine::class); $this->assertSame($car->engine, $engine); $this->assertNotSame($car->tire0, $car->tire1); $this->assertNotSame($car->tire0, $car->tire2); $this->assertNotSame($car->tire0, $car->tire3); $this->assertNotSame($car->tire1, $car->tire2); $this->assertNotSame($car->tire1, $car->tire3); $this->assertNotSame($car->tire2, $car->tire3); $this->assertInstanceOf(SmallTire::class, $car->tire0); $this->assertInstanceOf(SmallTire::class, $car->tire1); $this->assertInstanceOf(HeavyTire::class, $car->tire2); $this->assertInstanceOf(HeavyTire::class, $car->tire3); }