exporterMock = $this->createMock(taoQtiTest_models_classes_export_TestExport22::class); $this->fileSystemServiceMock = $this->createMock(FileSystemService::class); $this->fileHelperServiceMock = $this->createMock(FileHelperService::class); $this->fileHelperServiceMock->method('createTempDir')->willReturn('FAKE_TMP_DIR'); $this->subject = new QtiPackageExporter( $this->exporterMock, $this->fileSystemServiceMock, $this->fileHelperServiceMock ); } public function testExportDeliveryQtiPackage_ThrowsExceptionWhenExportFails(): void { $testUri = 'FAKE_TEST_URI'; $expectedReport = common_report_Report::createFailure('FAKE ERROR MESSAGE'); $this->exporterMock->method('export') ->willReturn($expectedReport); $this->expectException(common_Exception::class); $this->subject->exportDeliveryQtiPackage($testUri); } /** * @param array $reportData * * @dataProvider dataProviderReportDataWithoutValidPath */ public function testExportQtiTestPackageToFile_ThrowsExceptionWhenReportDoesNotHaveValidPath(array $reportData): void { $testUri = 'FAKE_TEST_URI'; $fileSystemId = 'FILE_SYSTEM_ID'; $filePath = 'FILE_PATH'; $expectedReport = common_report_Report::createSuccess('FAKE ERROR MESSAGE'); $expectedReport->setData($reportData); $this->exporterMock->method('export') ->willReturn($expectedReport); $this->expectException(common_Exception::class); $this->subject->exportQtiTestPackageToFile($testUri, $fileSystemId, $filePath); } public function testExportQtiTestPackageToFile_ReturnsValidFileAfterSuccessfulExport(): void { $testUri = 'FAKE_TEST_URI'; $fileSystemId = 'FILE_SYSTEM_ID'; $filePath = 'FILE_PATH'; $expectedFileContent = 'EXPORTED_FILE_CONTENT'; $expectedReport = common_report_Report::createSuccess('FAKE ERROR MESSAGE'); $expectedReport->setData(['path' => 'FAKE_QTI_PACKAGE_PATH']); $this->exporterMock->method('export') ->willReturn($expectedReport); $this->fileHelperServiceMock->method('readFile') ->willReturn($expectedFileContent); $fileMock = $this->createMock(File::class); $fileMock->expects(self::once()) ->method('put') ->with($expectedFileContent); $directoryMock = $this->createMock(Directory::class); $directoryMock->expects(self::once()) ->method('getFile') ->with($filePath) ->willReturn($fileMock); $this->fileSystemServiceMock ->expects(self::once()) ->method('getDirectory') ->with($fileSystemId) ->willReturn($directoryMock); $this->subject->exportQtiTestPackageToFile($testUri, $fileSystemId, $filePath); } /** * @return array */ public function dataProviderReportDataWithoutValidPath(): array { return [ 'No path key' => [ 'reportData' => [], ], 'Path is not a string' => [ 'reportData' => [ 'path' => ['some array value'], ], ], ]; } }