/** * @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_is_method_creates_and_returns_a_new_instance() { // given $function = function () { }; // when $ReturnValue = InitFunction::is($function); // then $this->assertInstanceOf('tueena\\core\\Services\\ServiceDefinitionParameters\\InitFunction', $ReturnValue); $this->assertSame($function, $ReturnValue->getInitFunction()); }
/** * @test */ public function hasFactoryFunction_returns_false_if_no_factory_function_passed_in() { // given $IdentifyingType = IdentifyingType::is(Type::fromName(__CLASS__)); $ImplementingType = ImplementingType::is(Type::fromName(__CLASS__)); $InitFunction = InitFunction::is(function () { }); // when $Target = new ServiceDefinition('', 0, $IdentifyingType, $ImplementingType, null, $InitFunction); // then $this->AssertFalse($Target->hasFactoryFunction()); $this->AssertTrue($Target->hasInitFunction()); }
/** * @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 A_service_definition_can_be_added_with_a_factory_and_an_init_function() { // given $FactoryFunction = function () { }; $InitFunction = function () { }; $Target = new ServiceDefinitions(); // when $Target->add(IdentifyingType::is(Type::fromName('tueena\\spec\\core\\stubs\\IMyService')), FactoryFunction::is($FactoryFunction), InitFunction::is($InitFunction)); // then $ServiceDefinition = $Target->get(Type::fromName('tueena\\spec\\core\\stubs\\IMyService')); $this->assertSame($FactoryFunction, $ServiceDefinition->getFactoryFunction()); $this->assertSame($InitFunction, $ServiceDefinition->getInitFunction()); }