public function testProviders() { // Arrange $injector = new Injector(null); $injector->providers([Controller::class => ClassProvider::init(ControllerA::class), '%BASE_URL%' => ValueFactoryProvider::init(function () { return 'http://sysvyz.org/'; })]); /** @var ControllerA $ctrl */ $ctrl = $injector->get(Controller::class); $this->assertInstanceOf(ControllerA::class, $ctrl); $this->assertSame($ctrl->requestService, $ctrl->serviceZ->requestService); $this->assertNotSame($ctrl->requestService, $ctrl->serviceZ->serviceY->requestService); $this->assertNotEquals($ctrl->requestService->url, $ctrl->serviceZ->serviceY->requestService->url); }
public function testProvidersSingletonLazy() { $injector = new Injector(null); $injector->providers(['tire' => ValueFactoryProvider::init(function () { return new Tire(); })->lazy()->singleton()]); $this->assertEquals($injector->{'tire'}->type, 'Tire'); $injector->{'tire'}->a = "a"; $this->assertEquals($injector->{'tire'}->a, 'a'); $this->assertNotSame($injector->{'tire'}, $injector->{'tire'}); $this->assertNotInstanceOf(Tire::class, $injector->{'tire'}); $this->assertInstanceOf(LazyProxyObject::class, $injector->{'tire'}); $this->assertInstanceOf(Tire::class, $injector->{'tire'}->getInstance()); $this->assertSame($injector->{'tire'}->getInstance(), $injector->{'tire'}->getInstance()); }
public function testProxy() { $injector = new Injector(null); $provider = ValueFactoryProvider::init(function () { return new MethodReflectionTestObject(); })->lazy(); /** @var MethodReflectionTestObject $proxy */ $proxy = $provider->get($injector); $testFunction = function (LazyProxyObject $a) { $this->assertInstanceOf(LazyProxyObject::class, $a); return true; }; $this->assertEquals($proxy->getPri(), 409); $this->assertEquals($proxy->privateMethod(), "__call:privateMethod"); //private cant be called, calls -> __call instead $this->assertEquals($proxy->publicMethod(), 'publicMethod'); $this->assertEquals($proxy->publicMethodWithoutModifier(), 'publicMethodWithoutModifier'); $this->assertEquals($proxy . "", '_TO_STRING_'); $this->assertEquals($proxy->getProvider(), 'PROVIDER'); $this->assertTrue($testFunction($proxy)); }
public static function _DI_PROVIDERS() { return [Tire::class => ClassProvider::init(SmallTire::class, false), '%CAR_NAME%' => ValueFactoryProvider::init(function () { return 'CAR'; })]; }