You are here

public function FixtureTrait::fixtureCreateArchive in Mini site 8

Create archive from an array of specified files.

Parameters

array $files: Array of files as described in fixtureCreateFiles().

string $type: (optional) The type of the archive. Defaults to 'zip'.

string $filename: (optional) The resulting file name of the archive. If not provided, a random file name is generated.

Return value

string Absolute path to created archive file.

2 calls to FixtureTrait::fixtureCreateArchive()
FixtureTest::testFixtureCreateArchive in tests/src/Unit/FixtureTest.php
Test fixtureCreateArchive() method.
MinisiteTestBase::getTestArchiveValid in tests/src/Functional/MinisiteTestBase.php
Shorthand to get a valid archive file.

File

tests/src/Traits/FixtureTrait.php, line 115

Class

FixtureTrait
Trait FixtureTrait.

Namespace

Drupal\Tests\minisite\Traits

Code

public function fixtureCreateArchive(array $files, $type = 'zip', $filename = NULL) {
  $filename = empty($filename) ? uniqid() : $filename;
  $filename = basename($filename, '.' . $type) . '.' . $type;
  $file_path = $this->fixtureDir . \DIRECTORY_SEPARATOR . uniqid() . \DIRECTORY_SEPARATOR . $filename;
  $fs = new Filesystem();
  $fs
    ->mkdir(dirname($file_path));
  switch ($type) {
    case 'zip':
      $archive = new \ZipArchive();
      if ($archive
        ->open($file_path, \ZipArchive::CREATE) !== TRUE) {
        throw new \RuntimeException(sprintf('Cannot open file "%s"', $file_path));
      }
      break;
    default:
      throw new \RuntimeException(sprintf('Unsupported archive type "%s" provided.', $type));
  }
  $files = $this
    ->fixtureCreateFiles($files);
  foreach ($files as $absolute_path => $path) {
    if (is_dir($absolute_path)) {
      $archive
        ->addEmptyDir($path);
    }
    else {
      $archive
        ->addFile($absolute_path, $path);
    }
  }
  $archive
    ->close();
  return $file_path;
}