/**
  * @requires extension zip
  */
 public function testZipArchiveCreation()
 {
     // Zip extension does not support stream wrappers. Use real filesystem objects.
     $tmpFile = tmpfile();
     $archiveFile = stream_get_meta_data($tmpFile)['uri'];
     $manager = new ArchiveManager();
     $archive = $manager->createArchive(ArchiveManager::ZIP, $archiveFile);
     $manager->addFile($archive, __FILE__, 'äöü.txt');
     $manager->closeArchive($archive);
     $this->assertFileExists($archiveFile);
     $this->assertTrue($manager->isArchive(ArchiveManager::ZIP, $archiveFile));
     $testArchive = new \ZipArchive();
     $this->assertTrue($testArchive->open($archiveFile));
     $this->assertEquals(1, $testArchive->numFiles);
     $content = $testArchive->getFromName('äöü.txt');
     $this->assertNotFalse($content);
     // Message is easier readable in case of error
     $this->assertEquals(file_get_contents(__FILE__), $content);
 }
Exemplo n.º 2
0
 /**
  * @requires extension zip
  */
 public function testZipArchiveCreation()
 {
     // The Zip extension does not support stream wrappers. Use real
     // filesystem objects instead. Since the target file must not exist,
     // tmpfile() is not suitable. Instead, use tempnam() with a dedicated
     // directory to get a safe filename and delete the created file. This
     // is mostly safe because the only source for filename clashes would be
     // another test running on the same tree in parallel, and the randomized
     // filename part reduces the risk even further.
     $tmpDir = \Library\Module::getPath('data/Test/ArchiveManager');
     $archiveFile = tempnam($tmpDir, 'zip');
     try {
         if (dirname($archiveFile) != $tmpDir) {
             throw new \UnexpectedValueException('Could not generate temporary file in safe location');
         }
         unlink($archiveFile);
         $manager = new ArchiveManager();
         $archive = $manager->createArchive(ArchiveManager::ZIP, $archiveFile);
         $manager->addFile($archive, __FILE__, 'äöü.txt');
         $manager->closeArchive($archive);
         $this->assertFileExists($archiveFile);
         $this->assertTrue($manager->isArchive(ArchiveManager::ZIP, $archiveFile));
         $testArchive = new \ZipArchive();
         $this->assertTrue($testArchive->open($archiveFile));
         $this->assertEquals(1, $testArchive->numFiles);
         $content = $testArchive->getFromName('äöü.txt');
         $testArchive->close();
         $this->assertNotFalse($content);
         // Message is easier readable in case of error
         $this->assertEquals(file_get_contents(__FILE__), $content);
         unlink($archiveFile);
     } catch (\Exception $e) {
         if ($archiveFile) {
             @unlink($archiveFile);
             throw $e;
         }
     }
 }