/** * @param string $definingFilePath * @param int $definingLineNumber * @param IdentifyingType $IdentifyingType * @param ImplementingType $ImplementingType * @param FactoryFunction $FactoryFunction * @param InitFunction $InitFunction */ public function __construct($definingFilePath, $definingLineNumber, IdentifyingType $IdentifyingType, ImplementingType $ImplementingType = null, FactoryFunction $FactoryFunction = null, InitFunction $InitFunction = null) { $this->definingFilePath = $definingFilePath; $this->definingLineNumber = $definingLineNumber; $this->IdentifyingType = $IdentifyingType->getType(); if ($ImplementingType === null) { $this->ImplementingType = null; } else { if ($ImplementingType->isSameAsIdentifyingType()) { $this->ImplementingType = $this->IdentifyingType; } else { $this->ImplementingType = $ImplementingType->getType(); } } $this->FactoryFunction = $FactoryFunction !== null ? $FactoryFunction->getFactoryFunction() : null; $this->InitFunction = $InitFunction !== null ? $InitFunction->getInitFunction() : null; }
/** * @test */ public function The_static_isTheSame_method_creates_and_returns_a_new_instance() { // when $ReturnValue = ImplementingType::isTheSame(); // then $this->assertInstanceOf('tueena\\core\\Services\\ServiceDefinitionParameters\\ImplementingType', $ReturnValue); $this->assertNull($ReturnValue->getType()); $this->assertTrue($ReturnValue->isSameAsIdentifyingType()); }
/** * @test */ public function If_a_service_requires_itself_in_its_init_function_this_is_not_considered_as_circular_reference() { // given $ServiceDefinitions = new ServiceDefinitions(); $ServiceDefinitions->add(IdentifyingType::is(Type::fromName('tueena\\spec\\core\\stubs\\A')), ImplementingType::isTheSame(), InitFunction::is(function (A $A) { })); $Target = new ServiceDefinitionsValidator(); // when, then $Target->validate($ServiceDefinitions); }
/** * @test */ public function The_init_function_can_be_injected() { // given $counter = 0; $ServiceDefinitions = new ServiceDefinitions(); $ServiceDefinitions->add(IdentifyingType::is(Type::fromName('tueena\\spec\\core\\stubs\\A')), ImplementingType::isTheSame())->add(IdentifyingType::is(Type::fromName('tueena\\spec\\core\\stubs\\B')), ImplementingType::isTheSame(), InitFunction::is(function (A $A, B $B) use(&$counter) { $counter++; })); $Target = new ServiceFactory($ServiceDefinitions); $Injector = new Injector($Target); // when $Target->getService(Type::fromName('tueena\\spec\\core\\stubs\\B'), $Injector); // then // We just have to ensure, the init function has been called. A and B must // have been injected. $this->AssertEquals(1, $counter); }
/** * @test */ public function hasInitFunction_returns_false_if_no_init_function_passed_in() { // given $IdentifyingType = IdentifyingType::is(Type::fromName(__CLASS__)); $ImplementingType = ImplementingType::is(Type::fromName(__CLASS__)); $FactoryFunction = FactoryFunction::is(function () { }); // when $Target = new ServiceDefinition('', 0, $IdentifyingType, $ImplementingType, $FactoryFunction, null); // then $this->AssertTrue($Target->hasFactoryFunction()); $this->AssertFalse($Target->hasInitFunction()); }
/** * @test */ public function The_main_method_is_resolved_by_the_Injector() { // given $loaderConfigurator = function () { }; $serviceDefiner = function (ServiceDefinitions $ServiceDefinitions) { $ServiceDefinitions->add(IdentifyingType::is(Type::fromName('tueena\\spec\\core\\stubs\\IMyService')), ImplementingType::is(Type::fromName('tueena\\spec\\core\\stubs\\MyService'))); }; $passedInServiceInstance = null; $mainFunction = function (IMyService $MyService) use(&$passedInServiceInstance) { $passedInServiceInstance = $MyService; }; $Target = new Application($loaderConfigurator, $serviceDefiner, $mainFunction); // when $Target->run(); // then $this->assertInstanceOf('\\tueena\\spec\\core\\stubs\\MyService', $passedInServiceInstance); }
/** * @test */ public function If_a_service_is_not_defined_with_a_factory_function_the_constructor_of_that_service_gets_injected_with_the_required_services() { // given $serviceDefinitions = new ServiceDefinitions(); $serviceDefinitions->add(IdentifyingType::is(Type::fromName('tueena\\spec\\core\\stubs\\D')), ImplementingType::isTheSame())->add(IdentifyingType::is(Type::fromName('tueena\\spec\\core\\stubs\\E')), ImplementingType::isTheSame()); $ServiceFactory = new ServiceFactory($serviceDefinitions); $Target = new Injector($ServiceFactory); $PropertyDOfE = null; $Closure = function (\tueena\spec\core\stubs\E $E) use(&$PropertyDOfE) { $PropertyDOfE = $E->D; }; // when $Target->resolve(new InjectionTargetClosure($Closure)); // then $this->assertInstanceOf('tueena\\spec\\core\\stubs\\D', $PropertyDOfE); }
/** * @test */ public function The_getAll_method_returns_all_service_definitions() { // given $Target = new ServiceDefinitions(); $Target->add(IdentifyingType::is(Type::fromName('tueena\\spec\\core\\stubs\\IMyService')), ImplementingType::isTheSame())->add(IdentifyingType::is(Type::fromName('tueena\\spec\\core\\stubs\\MyService')), ImplementingType::isTheSame()); // when $result = $Target->getAll(); // then $this->assertEquals(['tueena\\spec\\core\\stubs\\IMyService' => $Target->get(Type::fromName('tueena\\spec\\core\\stubs\\IMyService')), 'tueena\\spec\\core\\stubs\\MyService' => $Target->get(Type::fromName('tueena\\spec\\core\\stubs\\MyService'))], $result); }