object = new AssemblyFilesReader(); } public function testGetFilesEmptyDirectoryReturnsEmptyGenerator() { $expectedFilesCount = 0; $iterator = $this->getFilesIterator([]); $directoryMock = $this->createMock(tao_models_classes_service_StorageDirectory::class); $directoryMock->method('getFlyIterator') ->willReturn($iterator); $result = $this->object->getFiles($directoryMock); $this->assertInstanceOf(Generator::class, $result); $this->assertEquals($expectedFilesCount, count(iterator_to_array($result)), 'Number of returned files for empty directory must be 0.'); } public function testGetFilesReturnsGeneratorWithFilesStreams() { $expectedFilePath1 = 'file/prefix1'; $expectedFilePath2 = 'file/prefix2'; $iterator = $this->getFilesIterator([$expectedFilePath1, $expectedFilePath2]); $directoryMock = $this->createMock(tao_models_classes_service_StorageDirectory::class); $directoryMock->method('getFlyIterator') ->willReturn($iterator); $result = $this->object->getFiles($directoryMock); $this->assertInstanceOf(Generator::class, $result, 'Files reader must return an instance type of Generator.'); $this->assertEquals($expectedFilePath1, $result->key(), 'Returned file path must be as expected.'); $this->assertInstanceOf(StreamInterface::class, $result->current(), 'Returned iterator value must be an instance of StreamInterface.'); $result->next(); $this->assertEquals($expectedFilePath2, $result->key(), 'Returned file path must be as expected.'); $this->assertInstanceOf(StreamInterface::class, $result->current(), 'Returned iterator value must be an instance of StreamInterface.'); } public function testGetFilesConvertsCompiledTestFile() { $expectedFilePath = 'compact-test.EXT2'; $originalTestFileMock = $this->createMock(File::class); $originalTestFileMock->method('getBasename') ->willReturn('compact-test.EXT1'); $convertedTestFileMock = $this->createMock(File::class); $convertedTestFileMock->method('getPrefix') ->willReturn($expectedFilePath); $convertedTestFileMock->method('readPsrStream') ->willReturn($this->createMock(StreamInterface::class)); $testConverterMock = $this->createMock(CompiledTestConverterService::class); $testConverterMock->method('convert') ->willReturn($convertedTestFileMock); $this->object->setCompiledTestConverter($testConverterMock); $directoryMock = $this->createMock(tao_models_classes_service_StorageDirectory::class); $directoryMock->method('getFlyIterator') ->willReturn(new ArrayIterator([$originalTestFileMock])); $result = $this->object->getFiles($directoryMock); $this->assertInstanceOf(Generator::class, $result, 'Files reader must return an instance type of Generator.'); $this->assertEquals($expectedFilePath, $result->key(), 'Returned converted file path must be as expected.'); $this->assertInstanceOf(StreamInterface::class, $result->current(), 'Returned iterator value must be an instance of StreamInterface.'); } /** * @param array $filesPaths * @return ArrayIterator */ private function getFilesIterator(array $filesPaths) { $files = []; foreach ($filesPaths as $filePath) { $fileMock = $this->createMock(File::class); $fileMock->method('getPrefix') ->willReturn($filePath); $fileMock->method('readPsrStream') ->willReturn($this->createMock(StreamInterface::class)); $files[] = $fileMock; } return new ArrayIterator($files); } }