1
0
mirror of https://github.com/Ne-Lexa/php-zip.git synced 2025-01-17 15:28:14 +01:00
php-zip/tests/ZipFileAddDirTest.php

414 lines
12 KiB
PHP
Raw Normal View History

2017-03-13 08:33:45 +03:00
<?php
2017-11-13 15:33:37 +03:00
namespace PhpZip\Tests;
2017-03-13 08:33:45 +03:00
use PhpZip\Exception\ZipException;
2017-03-13 08:33:45 +03:00
use PhpZip\Util\Iterator\IgnoreFilesFilterIterator;
use PhpZip\Util\Iterator\IgnoreFilesRecursiveFilterIterator;
use PhpZip\ZipFile;
2017-03-13 08:33:45 +03:00
/**
* Test add directory to zip archive.
2019-12-05 19:36:11 +03:00
*
* @internal
*
* @small
2017-03-13 08:33:45 +03:00
*/
class ZipFileAddDirTest extends ZipFileSetTestCase
2017-03-13 08:33:45 +03:00
{
/**
* @throws ZipException
*/
2017-03-13 08:33:45 +03:00
public function testAddDirWithLocalPath()
{
$localPath = 'to/path';
$zipFile = new ZipFile();
$zipFile->addDir($this->outputDirname, $localPath);
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
2019-12-05 19:36:11 +03:00
static::assertCorrectZipArchive($this->outputFilename);
2017-03-13 08:33:45 +03:00
$zipFile->openFile($this->outputFilename);
2019-12-05 19:36:11 +03:00
static::assertFilesResult(
$zipFile,
[
'.hidden',
'text file.txt',
'Текстовый документ.txt',
'empty dir/',
'LoremIpsum.txt',
2019-12-05 19:36:11 +03:00
],
$localPath
);
2017-03-13 08:33:45 +03:00
$zipFile->close();
}
/**
* @throws ZipException
*/
2017-03-13 08:33:45 +03:00
public function testAddDirWithoutLocalPath()
{
$zipFile = new ZipFile();
$zipFile->addDir($this->outputDirname);
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
2019-12-05 19:36:11 +03:00
static::assertCorrectZipArchive($this->outputFilename);
2017-03-13 08:33:45 +03:00
$zipFile->openFile($this->outputFilename);
2019-12-05 19:36:11 +03:00
static::assertFilesResult(
$zipFile,
[
'.hidden',
'text file.txt',
'Текстовый документ.txt',
'empty dir/',
'LoremIpsum.txt',
2019-12-05 19:36:11 +03:00
]
);
2017-03-13 08:33:45 +03:00
$zipFile->close();
}
/**
* @throws ZipException
*/
2017-03-13 08:33:45 +03:00
public function testAddFilesFromIterator()
{
$localPath = 'to/project';
$directoryIterator = new \DirectoryIterator($this->outputDirname);
$zipFile = new ZipFile();
$zipFile->addFilesFromIterator($directoryIterator, $localPath);
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
2019-12-05 19:36:11 +03:00
static::assertCorrectZipArchive($this->outputFilename);
2017-03-13 08:33:45 +03:00
$zipFile->openFile($this->outputFilename);
2019-12-05 19:36:11 +03:00
static::assertFilesResult(
$zipFile,
[
'.hidden',
'text file.txt',
'Текстовый документ.txt',
'empty dir/',
'LoremIpsum.txt',
2019-12-05 19:36:11 +03:00
],
$localPath
);
2017-03-13 08:33:45 +03:00
$zipFile->close();
}
/**
* @throws ZipException
*/
2017-11-13 15:33:37 +03:00
public function testAddFilesFromIteratorEmptyLocalPath()
{
$localPath = '';
$directoryIterator = new \DirectoryIterator($this->outputDirname);
$zipFile = new ZipFile();
$zipFile->addFilesFromIterator($directoryIterator, $localPath);
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
2019-12-05 19:36:11 +03:00
static::assertCorrectZipArchive($this->outputFilename);
2017-11-13 15:33:37 +03:00
$zipFile->openFile($this->outputFilename);
2019-12-05 19:36:11 +03:00
static::assertFilesResult(
$zipFile,
[
'.hidden',
'text file.txt',
'Текстовый документ.txt',
'empty dir/',
'LoremIpsum.txt',
2019-12-05 19:36:11 +03:00
]
);
2017-11-13 15:33:37 +03:00
$zipFile->close();
}
/**
* @throws ZipException
*/
2017-03-13 08:33:45 +03:00
public function testAddFilesFromRecursiveIterator()
{
$localPath = 'to/project';
$directoryIterator = new \RecursiveDirectoryIterator($this->outputDirname);
$zipFile = new ZipFile();
$zipFile->addFilesFromIterator($directoryIterator, $localPath);
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
2019-12-05 19:36:11 +03:00
static::assertCorrectZipArchive($this->outputFilename);
2017-03-13 08:33:45 +03:00
$zipFile->openFile($this->outputFilename);
2019-12-05 19:36:11 +03:00
static::assertFilesResult($zipFile, array_keys(self::$files), $localPath);
2017-03-13 08:33:45 +03:00
$zipFile->close();
}
/**
* @throws ZipException
*/
2017-03-13 08:33:45 +03:00
public function testAddRecursiveDirWithLocalPath()
{
$localPath = 'to/path';
$zipFile = new ZipFile();
$zipFile->addDirRecursive($this->outputDirname, $localPath);
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
2019-12-05 19:36:11 +03:00
static::assertCorrectZipArchive($this->outputFilename);
2017-03-13 08:33:45 +03:00
$zipFile->openFile($this->outputFilename);
2019-12-05 19:36:11 +03:00
static::assertFilesResult($zipFile, array_keys(self::$files), $localPath);
2017-03-13 08:33:45 +03:00
$zipFile->close();
}
/**
* @throws ZipException
*/
2017-03-13 08:33:45 +03:00
public function testAddRecursiveDirWithoutLocalPath()
{
$zipFile = new ZipFile();
$zipFile->addDirRecursive($this->outputDirname);
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
2019-12-05 19:36:11 +03:00
static::assertCorrectZipArchive($this->outputFilename);
2017-03-13 08:33:45 +03:00
$zipFile->openFile($this->outputFilename);
2019-12-05 19:36:11 +03:00
static::assertFilesResult($zipFile, array_keys(self::$files));
2017-03-13 08:33:45 +03:00
$zipFile->close();
}
/**
* @throws ZipException
*/
2017-11-13 15:33:37 +03:00
public function testAddFilesFromIteratorWithIgnoreFiles()
{
2017-03-13 08:33:45 +03:00
$localPath = 'to/project';
$ignoreFiles = [
'Текстовый документ.txt',
2019-12-05 19:36:11 +03:00
'empty dir/',
'LoremIpsum.txt',
2017-03-13 08:33:45 +03:00
];
$directoryIterator = new \DirectoryIterator($this->outputDirname);
$ignoreIterator = new IgnoreFilesFilterIterator($directoryIterator, $ignoreFiles);
$zipFile = new ZipFile();
$zipFile->addFilesFromIterator($ignoreIterator, $localPath);
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
2019-12-05 19:36:11 +03:00
static::assertCorrectZipArchive($this->outputFilename);
2017-03-13 08:33:45 +03:00
$zipFile->openFile($this->outputFilename);
2019-12-05 19:36:11 +03:00
static::assertFilesResult(
$zipFile,
[
'.hidden',
'text file.txt',
],
$localPath
);
2017-03-13 08:33:45 +03:00
$zipFile->close();
}
/**
* @throws ZipException
*/
2017-11-13 15:33:37 +03:00
public function testAddFilesFromRecursiveIteratorWithIgnoreFiles()
{
2017-03-13 08:33:45 +03:00
$localPath = 'to/project';
$ignoreFiles = [
'.hidden',
'empty dir2/ещё пустой каталог/',
'list.txt',
'category/Pictures/240x320',
'LoremIpsum.txt',
2017-03-13 08:33:45 +03:00
];
$directoryIterator = new \RecursiveDirectoryIterator($this->outputDirname);
$ignoreIterator = new IgnoreFilesRecursiveFilterIterator($directoryIterator, $ignoreFiles);
$zipFile = new ZipFile();
$zipFile->addFilesFromIterator($ignoreIterator, $localPath);
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
2019-12-05 19:36:11 +03:00
static::assertCorrectZipArchive($this->outputFilename);
2017-03-13 08:33:45 +03:00
$zipFile->openFile($this->outputFilename);
2019-12-05 19:36:11 +03:00
static::assertFilesResult(
$zipFile,
[
'text file.txt',
'Текстовый документ.txt',
'empty dir/',
'catalog/New File',
'catalog/New File 2',
'catalog/Empty Dir/',
'category/Pictures/128x160/Car/01.jpg',
'category/Pictures/128x160/Car/02.jpg',
],
$localPath
);
2017-03-13 08:33:45 +03:00
$zipFile->close();
}
/**
2019-12-05 19:36:11 +03:00
* Create archive and add files from glob pattern.
*
* @throws ZipException
2017-03-13 08:33:45 +03:00
*/
public function testAddFilesFromGlob()
{
$localPath = '/';
$zipFile = new ZipFile();
$zipFile->addFilesFromGlob($this->outputDirname, '**.{txt,jpg}', $localPath);
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
2019-12-05 19:36:11 +03:00
static::assertCorrectZipArchive($this->outputFilename);
2017-03-13 08:33:45 +03:00
$zipFile->openFile($this->outputFilename);
2019-12-05 19:36:11 +03:00
static::assertFilesResult(
$zipFile,
[
'text file.txt',
'Текстовый документ.txt',
'LoremIpsum.txt',
2019-12-05 19:36:11 +03:00
],
$localPath
);
2017-03-13 08:33:45 +03:00
$zipFile->close();
}
/**
2019-12-05 19:36:11 +03:00
* Create archive and add recursively files from glob pattern.
*
* @throws ZipException
2017-03-13 08:33:45 +03:00
*/
public function testAddFilesFromGlobRecursive()
{
$localPath = '/';
$zipFile = new ZipFile();
$zipFile->addFilesFromGlobRecursive($this->outputDirname, '**.{txt,jpg}', $localPath);
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
2019-12-05 19:36:11 +03:00
static::assertCorrectZipArchive($this->outputFilename);
2017-03-13 08:33:45 +03:00
$zipFile->openFile($this->outputFilename);
2019-12-05 19:36:11 +03:00
static::assertFilesResult(
$zipFile,
[
'text file.txt',
'Текстовый документ.txt',
'category/list.txt',
'category/Pictures/128x160/Car/01.jpg',
'category/Pictures/128x160/Car/02.jpg',
'category/Pictures/240x320/Car/01.jpg',
'category/Pictures/240x320/Car/02.jpg',
'LoremIpsum.txt',
2019-12-05 19:36:11 +03:00
],
$localPath
);
2017-03-13 08:33:45 +03:00
$zipFile->close();
}
/**
2019-12-05 19:36:11 +03:00
* Create archive and add files from regex pattern.
*
* @throws ZipException
2017-03-13 08:33:45 +03:00
*/
public function testAddFilesFromRegex()
{
$localPath = 'path';
$zipFile = new ZipFile();
$zipFile->addFilesFromRegex($this->outputDirname, '~\.(txt|jpe?g)$~i', $localPath);
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
2019-12-05 19:36:11 +03:00
static::assertCorrectZipArchive($this->outputFilename);
2017-03-13 08:33:45 +03:00
$zipFile->openFile($this->outputFilename);
2019-12-05 19:36:11 +03:00
static::assertFilesResult(
$zipFile,
[
'text file.txt',
'Текстовый документ.txt',
'LoremIpsum.txt',
2019-12-05 19:36:11 +03:00
],
$localPath
);
2017-03-13 08:33:45 +03:00
$zipFile->close();
}
/**
2019-12-05 19:36:11 +03:00
* Create archive and add files recursively from regex pattern.
*
* @throws ZipException
2017-03-13 08:33:45 +03:00
*/
public function testAddFilesFromRegexRecursive()
{
$localPath = '/';
$zipFile = new ZipFile();
$zipFile->addFilesFromRegexRecursive($this->outputDirname, '~\.(txt|jpe?g)$~i', $localPath);
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
2019-12-05 19:36:11 +03:00
static::assertCorrectZipArchive($this->outputFilename);
2017-03-13 08:33:45 +03:00
$zipFile->openFile($this->outputFilename);
2019-12-05 19:36:11 +03:00
static::assertFilesResult(
$zipFile,
[
'text file.txt',
'Текстовый документ.txt',
'category/list.txt',
'LoremIpsum.txt',
2019-12-05 19:36:11 +03:00
'category/Pictures/128x160/Car/01.jpg',
'category/Pictures/128x160/Car/02.jpg',
'category/Pictures/240x320/Car/01.jpg',
'category/Pictures/240x320/Car/02.jpg',
],
$localPath
);
2017-03-13 08:33:45 +03:00
$zipFile->close();
}
/**
* @throws ZipException
*/
2017-03-13 08:33:45 +03:00
public function testArrayAccessAddDir()
{
$localPath = 'path/to';
$iterator = new \RecursiveDirectoryIterator($this->outputDirname);
$zipFile = new ZipFile();
$zipFile[$localPath] = $iterator;
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
2019-12-05 19:36:11 +03:00
static::assertCorrectZipArchive($this->outputFilename);
2017-03-13 08:33:45 +03:00
$zipFile->openFile($this->outputFilename);
2019-12-05 19:36:11 +03:00
static::assertFilesResult($zipFile, array_keys(self::$files), $localPath);
2017-03-13 08:33:45 +03:00
$zipFile->close();
}
2017-11-13 15:33:37 +03:00
}