fileMigrationHelper = new MigrationHelper(); $this->resourceFileSerializer = new ResourceFileSerializer(); $this->urlFileSerializer = new UrlFileSerializer(); $serviceLocator = $this->getServiceLocatorMock([FileSystemService::SERVICE_ID => $this->getMockFileSystem()]); $this->fileMigrationHelper->setServiceLocator($serviceLocator); $this->resourceFileSerializer->setServiceLocator($serviceLocator); $this->urlFileSerializer->setServiceLocator($serviceLocator); $this->ontologyMock = $this->getOntologyMock(); } /** * Test the migration of a file resource */ public function testResourceMigration() { try { $fileResource = $this->getFileResource(); $this->fileMigrationHelper->migrateResource( $fileResource, $this->ontologyMock->getProperty(self::PARENT_RESOURCE_URI), $this->ontologyMock->getResource(self::PROPERTY_URI) ); } catch (\Exception $e) { if ($this->testFile !== null) { $this->testFile->delete(); } throw new \Exception($e->getMessage()); } self::assertSame($this->fileMigrationHelper->migrationInformation['migrated_count'], 1); $this->testFile->delete(); } /** * Generate a file resource used for testing */ private function getFileResource() { $dir = $this->getTempDirectory(); $fileClass = $this->ontologyMock->getClass(GenerisRdf::CLASS_GENERIS_FILE); $this->testFile = $dir->getFile(self::SAMPLE_FILE); $this->testFile->write(self::SAMPLE_FILE, 'PHP Unit test file'); if ($this->testFile instanceof File) { $filename = $this->testFile->getBasename(); $filePath = dirname($this->testFile->getPrefix()); } elseif ($this->testFile instanceof Directory) { $filename = ''; $filePath = $this->testFile->getPrefix(); } else { return false; } $resource = $fileClass->createInstanceWithProperties( [ GenerisRdf::PROPERTY_FILE_FILENAME => $filename, GenerisRdf::PROPERTY_FILE_FILEPATH => $filePath, GenerisRdf::PROPERTY_FILE_FILESYSTEM => $this->ontologyMock->getResource($this->testFile->getFileSystemId()), ] ); self::assertInstanceOf(FileSystemHandler::class, $this->resourceFileSerializer->unserialize($resource)); $unitTestResource = $this->ontologyMock->getResource(self::PARENT_RESOURCE_URI); $testFileProperty = $this->ontologyMock->getProperty(self::PROPERTY_URI); $unitTestResource->setPropertyValue($testFileProperty, $unitTestResource); return $resource; } /** * @return Directory */ private function getTempDirectory() { if (!$this->tempDirectory) { $this->tempDirectory = $this->getFileSystemMock(['unit-test'])->getDirectory('unit-test'); } return $this->tempDirectory; } protected function tearDown(): void { $dir = $this->getTempDirectory(); $this->testFile = $dir->getFile(self::SAMPLE_FILE); $this->testFile->delete(); } /** * @return FileSystemService */ private function getMockFileSystem() { if ($this->fileSystemService === null) { $this->fileSystemService = $this->getServiceLocatorMock([FileSystemService::SERVICE_ID => new FileSystemService()])->get(FileSystemService::SERVICE_ID); } return $this->fileSystemService; } }