Beispiel #1
0
 /**
  * @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;
 }
Beispiel #2
0
 /**
  * @test
  */
 public function The_static_is_method_creates_and_returns_a_new_instance()
 {
     // given
     $function = function () {
     };
     // when
     $ReturnValue = FactoryFunction::is($function);
     // then
     $this->assertInstanceOf('tueena\\core\\Services\\ServiceDefinitionParameters\\FactoryFunction', $ReturnValue);
     $this->assertSame($function, $ReturnValue->getFactoryFunction());
 }
 /**
  * @test
  */
 public function Circular_references_are_invalid()
 {
     // given
     $ServiceDefinitions = new ServiceDefinitions();
     $ServiceDefinitions->add(IdentifyingType::is(Type::fromName('tueena\\spec\\core\\stubs\\A')), FactoryFunction::is(function (B $B) {
     }))->add(IdentifyingType::is(Type::fromName('tueena\\spec\\core\\stubs\\B')), FactoryFunction::is(function (C $C) {
     }))->add(IdentifyingType::is(Type::fromName('tueena\\spec\\core\\stubs\\C')), FactoryFunction::is(function (D $D) {
     }))->add(IdentifyingType::is(Type::fromName('tueena\\spec\\core\\stubs\\D')), ImplementingType::isTheSame(), InitFunction::is(function (B $B) {
     }));
     $line3 = __LINE__ - 1;
     $line2 = $line3 - 5;
     $line1 = $line2 - 4;
     $file = __FILE__;
     $Target = new ServiceDefinitionsValidator();
     // when, then
     try {
         $Target->validate($ServiceDefinitions);
     } catch (InvalidServiceDefinition $Exception) {
         $expectedExceptionMessage = "Invalid definition of the service tueena\\spec\\core\\stubs\\B in {$file} on line {$line1}: ";
         $expectedExceptionMessage .= "Circular reference detected: tueena\\spec\\core\\stubs\\B requires tueena\\spec\\core\\stubs\\C in it's factory function (defined in {$file} on line {$line1}), tueena\\spec\\core\\stubs\\C requires tueena\\spec\\core\\stubs\\D in it's factory function (defined in {$file} on line {$line2}), tueena\\spec\\core\\stubs\\D requires tueena\\spec\\core\\stubs\\B in it's init function (defined in {$file} on line {$line3}).";
         $this->assertEquals($expectedExceptionMessage, $Exception->getMessage());
         return;
     }
     $this->fail('Exception expected.');
 }
Beispiel #4
0
 /**
  * @test
  * @dataProvider getInvalidFactoryFunctionResults
  */
 public function An_exception_is_thrown_if_the_factory_function_returns_not_an_instance_of_the_defined_implementing_type($factoryFunctionReturnValue, $expectedErrorMessage)
 {
     // given
     $serviceDefinitions = new ServiceDefinitions();
     $serviceDefinitions->add(IdentifyingType::is(Type::fromName('tueena\\spec\\core\\stubs\\IMyService')), FactoryFunction::is(function () use($factoryFunctionReturnValue) {
         return $factoryFunctionReturnValue;
     }));
     $line = __LINE__ - 1;
     $Target = new ServiceFactory($serviceDefinitions);
     $Injector = new Injector($Target);
     // when, then
     try {
         $Target->getService(Type::fromName('tueena\\spec\\core\\stubs\\IMyService'), $Injector);
     } catch (InvalidServiceDefinition $Exception) {
         $expectedExceptionMessage = 'Invalid definition of the service tueena\\spec\\core\\stubs\\IMyService in ';
         $expectedExceptionMessage .= __FILE__ . ' on line ' . $line . ': ';
         $expectedExceptionMessage .= $expectedErrorMessage . ' ';
         $expectedExceptionMessage .= 'The factory function of a service must return an object that implements the identifying type.';
         $this->assertEquals($expectedExceptionMessage, $Exception->getMessage());
         return;
     }
     $this->fail('Exception expected.');
 }
Beispiel #5
0
 /**
  * @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());
 }
Beispiel #6
0
 /**
  * @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());
 }