/** * @test */ public function The_static_is_method_creates_and_returns_a_new_instance() { // given $Type = Type::fromName('tueena\\spec\\core\\stubs\\MyService'); // when $ReturnValue = ImplementingType::is($Type); // then $this->assertInstanceOf('tueena\\core\\Services\\ServiceDefinitionParameters\\ImplementingType', $ReturnValue); $this->assertSame($Type, $ReturnValue->getType()); $this->assertFalse($ReturnValue->isSameAsIdentifyingType()); }
/** * @test */ public function A_definition_is_invalid_if_the_implementing_type_is_not_an_instance_of_the_identifying_type() { // given $ServiceDefinitions = new ServiceDefinitions(); $ServiceDefinitions->add(IdentifyingType::is(Type::fromName('tueena\\spec\\core\\stubs\\A')), ImplementingType::is(Type::fromName(__CLASS__))); $line = __LINE__ - 1; $file = __FILE__; $Target = new ServiceDefinitionsValidator(); // when try { $Target->validate($ServiceDefinitions); } catch (InvalidServiceDefinition $Exception) { $expectedExceptionMessage = "Invalid definition of the service tueena\\spec\\core\\stubs\\A in {$file} on line {$line}: "; $expectedExceptionMessage .= __CLASS__ . ' does not implement tueena\\spec\\core\\stubs\\A. '; $expectedExceptionMessage .= 'The implementing type of a service must be an instance of the identifying type.'; $this->assertEquals($expectedExceptionMessage, $Exception->getMessage()); return; } $this->fail('Exception expected.'); }
/** * @test */ public function The_getService_method_returns_always_the_same_instance_of_a_service() { // given $serviceDefinitions = new ServiceDefinitions(); $serviceDefinitions->add(IdentifyingType::is(Type::fromName('tueena\\spec\\core\\stubs\\IMyService')), ImplementingType::is(Type::fromName('tueena\\spec\\core\\stubs\\MyService'))); $Target = new ServiceFactory($serviceDefinitions); $Injector = new Injector($Target); // when $Service1 = $Target->getService(Type::fromName('tueena\\spec\\core\\stubs\\IMyService'), $Injector); $Service2 = $Target->getService(Type::fromName('tueena\\spec\\core\\stubs\\IMyService'), $Injector); // then $this->AssertSame($Service1, $Service2); }
/** * @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 The_identifying_type_is_unique() { // given $Target = new ServiceDefinitions(); $Target->add(IdentifyingType::is(Type::fromName('tueena\\spec\\core\\stubs\\IMyService')), ImplementingType::is(Type::fromName('tueena\\spec\\core\\stubs\\MyService'))); $line1 = __LINE__ - 1; // when, then try { $line2 = __LINE__ + 4; $Target->add(IdentifyingType::is(Type::fromName('tueena\\spec\\core\\stubs\\IMyService')), ImplementingType::is(Type::fromName('tueena\\spec\\core\\stubs\\MyService'))); } catch (InvalidServiceDefinition $Exception) { $expectedExceptionMessage = 'Invalid definition of the service tueena\\spec\\core\\stubs\\IMyService in ' . __FILE__ . ' on line ' . $line2 . ': '; $expectedExceptionMessage .= 'A service of type tueena\\spec\\core\\stubs\\IMyService has already been defined in ' . __FILE__ . ' on line ' . $line1 . '. '; $expectedExceptionMessage .= 'There cannot be defined two services with the same identifying type.'; $this->assertEquals($expectedExceptionMessage, $Exception->getMessage()); return; } $this->fail('Exception expected.'); }