object = new CompiledTestConverterFactory(); } /** * @param mixed $outputTestFormat * @dataProvider dataProviderTestCreateConverterFailsIfParameterNotString */ public function testCreateConverterFailsIfParameterNotString($outputTestFormat) { $this->expectException(InvalidArgumentException::class); $this->object->createConverter($outputTestFormat); } public function testCreateConverterFailsIfFormatNotSupported() { $this->expectException(UnsupportedCompiledTestFormatException::class); $unsupportedFormat = 'CUSTOM_FORMAT'; $this->object->createConverter($unsupportedFormat); } /** * @param string $outputTestFormat * @param string $expectedCompilationImplementation * * @dataProvider dataProviderTestCreateConverterReturnsCompiledTestConverter */ public function testCreateConverterReturnsCompiledTestConverter($outputTestFormat, $expectedCompilationImplementation) { $slMock = $this->getServiceLocatorMock([ CompilationDataService::SERVICE_ID => $this->createMock(CompilationDataService::class), LoggerService::SERVICE_ID => $this->createMock(LoggerService::class), ]); $this->object->setServiceLocator($slMock); $result = $this->object->createConverter($outputTestFormat); $this->assertInstanceOf(CompiledTestConverterService::class, $result, 'Factory must create a test converter instance of required type.'); // Assert that correct implementation of CompilationDataService was initialized by factory based on input parameter. $reflectionProperty = new \ReflectionProperty(get_class($result), 'compilationDataWriter'); $reflectionProperty->setAccessible(true); $outputCompilationService = $reflectionProperty->getValue($result); $this->assertInstanceOf($expectedCompilationImplementation, $outputCompilationService, 'Factory must create a compiled test converter with correct "compilationDataWriter" implementation.'); } /** * @param string $format * @param string|CompilationDataService $compilationDataServiceClassName * @throws UnsupportedCompiledTestFormatException * * @dataProvider dataProviderTestCreateConverterReturnsCompiledTestConverter */ public function testCreateConverterReturnsNullWhenNoConversionIsNeeded( string $format, string $compilationDataServiceClassName ): void { $this->object->setServiceLocator( $this->getServiceLocatorMock( [ CompilationDataService::SERVICE_ID => new $compilationDataServiceClassName(), LoggerService::SERVICE_ID => $this->createMock(LoggerService::class), ] ) ); $this->assertNull($this->object->createConverter($format)); } /** * @return array */ public function dataProviderTestCreateConverterFailsIfParameterNotString() { return [ 'Output test format is a NULL' => [ 'outputTestFormat' => null, ], 'Output test format is a boolean' => [ 'outputTestFormat' => true, ], 'Output test format is an integer' => [ 'outputTestFormat' => 111, ], 'Output test format is an array' => [ 'outputTestFormat' => ['test_format'], ], 'Output test format is an object' => [ 'outputTestFormat' => new \stdClass(), ], ]; } /** * @return array */ public function dataProviderTestCreateConverterReturnsCompiledTestConverter() { return [ 'PHP format' => [ 'outputTestFormat' => 'php', 'expectedCompilationImplementation' => PhpCodeCompilationDataService::class, ], 'PHP format capitalized' => [ 'outputTestFormat' => 'PHP', 'expectedCompilationImplementation' => PhpCodeCompilationDataService::class, ], 'PHP format with whitespaces' => [ 'outputTestFormat' => ' php ', 'expectedCompilationImplementation' => PhpCodeCompilationDataService::class, ], 'PHP serialized format' => [ 'outputTestFormat' => 'php_serialized', 'expectedCompilationImplementation' => PhpSerializationCompilationDataService::class, ], 'XML format' => [ 'outputTestFormat' => 'xml', 'expectedCompilationImplementation' => XmlCompilationDataService::class, ], ]; } }