1
0
mirror of https://github.com/splitbrain/php-archive.git synced 2025-01-17 13:38:26 +01:00

tests enhancement

This commit is contained in:
peter279k 2018-02-04 19:01:02 +08:00
parent 2a63b8cf0b
commit ef1985eead
8 changed files with 433 additions and 127 deletions

View File

@ -1,20 +1,18 @@
dist: trusty
language: php language: php
sudo: false
php: php:
- 5.4 - 5.4
- 5.5 - 5.5
- 5.6 - 5.6
- 7.0 - 7.0
- 7.1 - 7.1
- nightly - 7.2
- hhvm - nightly
- hhvm
before_script: before_script:
- composer self-update - composer install
- composer install --prefer-source --no-interaction --dev
script: ./vendor/bin/phpunit script: ./vendor/bin/phpunit
after_success: after_success:
- sh generate-api.sh - sh generate-api.sh
env: env:
global: global:
secure: ctCQVPQgQziwIZf5QGHcnhHlXsyauG0W3AWF/6R8cTP+in2S/RygunPp7CkXiqA1YMluGr2Lo9h4DTVg7oqeXl79FXFXedijQmQEu3g3f4iDWtxbQhGf4bJQk57jXFldge4rQedlOJDzwGzJ1abrimJQlu090BZNeonzWL5cRK4= secure: ctCQVPQgQziwIZf5QGHcnhHlXsyauG0W3AWF/6R8cTP+in2S/RygunPp7CkXiqA1YMluGr2Lo9h4DTVg7oqeXl79FXFXedijQmQEu3g3f4iDWtxbQhGf4bJQk57jXFldge4rQedlOJDzwGzJ1abrimJQlu090BZNeonzWL5cRK4=

View File

@ -20,12 +20,21 @@
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "4.5.*" "phpunit/phpunit": "^4.8",
"mikey179/vfsStream": "^1.6",
"ext-zip": "*",
"ext-bz2": "*"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"splitbrain\\PHPArchive\\": "src" "splitbrain\\PHPArchive\\": "src"
} }
},
"autoload-dev": {
"psr-4": {
"splitbrain\\PHPArchive\\": "tests"
}
} }
} }

View File

@ -14,4 +14,9 @@
<directory suffix=".php">./tests/</directory> <directory suffix=".php">./tests/</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>
</phpunit> <filter>
<whitelist processUncoveredFilesFromWhitelist="false">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>

View File

@ -164,7 +164,7 @@ class Tar extends Archive
// extract data // extract data
if (!$fileinfo->getIsdir()) { if (!$fileinfo->getIsdir()) {
$fp = fopen($output, "wb"); $fp = @fopen($output, "wb");
if (!$fp) { if (!$fp) {
throw new ArchiveIOException('Could not open file for writing: '.$output); throw new ArchiveIOException('Could not open file for writing: '.$output);
} }
@ -245,7 +245,7 @@ class Tar extends Archive
throw new ArchiveIOException('Archive has been closed, files can no longer be added'); throw new ArchiveIOException('Archive has been closed, files can no longer be added');
} }
$fp = fopen($file, 'rb'); $fp = @fopen($file, 'rb');
if (!$fp) { if (!$fp) {
throw new ArchiveIOException('Could not open file for reading: '.$file); throw new ArchiveIOException('Could not open file for reading: '.$file);
} }
@ -379,7 +379,7 @@ class Tar extends Archive
$this->setCompression($this->complevel, $this->filetype($file)); $this->setCompression($this->complevel, $this->filetype($file));
} }
if (!file_put_contents($file, $this->getArchive())) { if (!@file_put_contents($file, $this->getArchive())) {
throw new ArchiveIOException('Could not write to file: '.$file); throw new ArchiveIOException('Could not write to file: '.$file);
} }
} }
@ -433,7 +433,7 @@ class Tar extends Archive
* *
* @param int $bytes seek to this position * @param int $bytes seek to this position
*/ */
function skipbytes($bytes) protected function skipbytes($bytes)
{ {
if ($this->comptype === Archive::COMPRESS_GZIP) { if ($this->comptype === Archive::COMPRESS_GZIP) {
@gzseek($this->fh, $bytes, SEEK_CUR); @gzseek($this->fh, $bytes, SEEK_CUR);
@ -645,7 +645,7 @@ class Tar extends Archive
{ {
// for existing files, try to read the magic bytes // for existing files, try to read the magic bytes
if(file_exists($file) && is_readable($file) && filesize($file) > 5) { if(file_exists($file) && is_readable($file) && filesize($file) > 5) {
$fh = fopen($file, 'rb'); $fh = @fopen($file, 'rb');
if(!$fh) return false; if(!$fh) return false;
$magic = fread($fh, 5); $magic = fread($fh, 5);
fclose($fh); fclose($fh);

View File

@ -111,7 +111,7 @@ class Zip extends Archive
* @throws ArchiveIOException * @throws ArchiveIOException
* @return FileInfo[] * @return FileInfo[]
*/ */
function extract($outdir, $strip = '', $exclude = '', $include = '') public function extract($outdir, $strip = '', $exclude = '', $include = '')
{ {
if ($this->closed || !$this->file) { if ($this->closed || !$this->file) {
throw new ArchiveIOException('Can not read from a closed archive'); throw new ArchiveIOException('Can not read from a closed archive');
@ -163,7 +163,7 @@ class Zip extends Archive
} }
// open file for writing // open file for writing
$fp = fopen($extractto, "wb"); $fp = @fopen($extractto, "wb");
if (!$fp) { if (!$fp) {
throw new ArchiveIOException('Could not open file for writing: '.$extractto); throw new ArchiveIOException('Could not open file for writing: '.$extractto);
} }
@ -419,7 +419,7 @@ class Zip extends Archive
*/ */
public function save($file) public function save($file)
{ {
if (!file_put_contents($file, $this->getArchive())) { if (!@file_put_contents($file, $this->getArchive())) {
throw new ArchiveIOException('Could not write to file: '.$file); throw new ArchiveIOException('Could not write to file: '.$file);
} }
} }

View File

@ -1,8 +1,11 @@
<?php <?php
use splitbrain\PHPArchive\FileInfo; namespace splitbrain\PHPArchive;
class FileInfoTest extends PHPUnit_Framework_TestCase use splitbrain\PHPArchive\FileInfo;
use PHPUnit\Framework\TestCase;
class FileInfoTest extends TestCase
{ {
public function testDefaults() public function testDefaults()
@ -96,4 +99,12 @@ class FileInfoTest extends PHPUnit_Framework_TestCase
$this->assertTrue($fileinfo->getIsdir()); $this->assertTrue($fileinfo->getIsdir());
$this->assertSame(0, $fileinfo->getSize()); $this->assertSame(0, $fileinfo->getSize());
} }
}
/**
* @expectedException splitbrain\PHPArchive\FileInfoException
*/
public function testFromPathWithFileNotExisted()
{
$fileinfo = FileInfo::fromPath('invalid_file_path');
}
}

View File

@ -1,15 +1,19 @@
<?php <?php
use splitbrain\PHPArchive\Tar; namespace splitbrain\PHPArchive;
class Tar_TestCase extends PHPUnit_Framework_TestCase use splitbrain\PHPArchive\Tar;
use PHPUnit\Framework\TestCase;
use org\bovigo\vfs\vfsStream;
class TarTestCase extends TestCase
{ {
/** /**
* file extensions that several tests use * file extensions that several tests use
*/ */
protected $extensions = array('tar'); protected $extensions = array('tar');
public function setUp() protected function setUp()
{ {
parent::setUp(); parent::setUp();
if (extension_loaded('zlib')) { if (extension_loaded('zlib')) {
@ -20,35 +24,38 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
$this->extensions[] = 'tbz'; $this->extensions[] = 'tbz';
$this->extensions[] = 'tar.bz2'; $this->extensions[] = 'tar.bz2';
} }
vfsStream::setup('home_root_path');
}
protected function tearDown()
{
parent::tearDown();
$this->extensions[] = null;
} }
/* /*
* dependency for tests needing zlib extension to pass * dependency for tests needing zlib extension to pass
*/ */
public function test_ext_zlib() public function testExtZlibIsInstalled()
{ {
if (!extension_loaded('zlib')) { $this->assertTrue(function_exists('gzopen'));
$this->markTestSkipped('skipping all zlib tests. Need zlib extension');
}
} }
/* /*
* dependency for tests needing zlib extension to pass * dependency for tests needing bz2 extension to pass
*/ */
public function test_ext_bz2() public function testExtBz2IsInstalled()
{ {
if (!extension_loaded('bz2')) { $this->assertTrue(function_exists('bzopen'));
$this->markTestSkipped('skipping all bzip2 tests. Need bz2 extension');
}
} }
/** /**
* @expectedException splitbrain\PHPArchive\ArchiveIOException * @expectedException splitbrain\PHPArchive\ArchiveIOException
*/ */
public function test_missing() public function testTarFileIsNotExisted()
{ {
$tar = new Tar(); $tar = new Tar();
$tar->open('nope.tar'); $tar->open('non_existed_file.tar');
} }
/** /**
@ -57,7 +64,7 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
* *
* No check for format correctness * No check for format correctness
*/ */
public function test_createdynamic() public function testCreateDynamic()
{ {
$tar = new Tar(); $tar = new Tar();
@ -65,8 +72,8 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
$tdir = ltrim($dir, '/'); $tdir = ltrim($dir, '/');
$tar->create(); $tar->create();
$tar->AddFile("$dir/testdata1.txt"); $tar->addFile("$dir/testdata1.txt");
$tar->AddFile("$dir/foobar/testdata2.txt", 'noway/testdata2.txt'); $tar->addFile("$dir/foobar/testdata2.txt", 'noway/testdata2.txt');
$tar->addData('another/testdata3.txt', 'testcontent3'); $tar->addData('another/testdata3.txt', 'testcontent3');
$data = $tar->getArchive(); $data = $tar->getArchive();
@ -95,17 +102,17 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
* *
* No check for format correctness * No check for format correctness
*/ */
public function test_createfile() public function testCreateFile()
{ {
$tar = new Tar(); $tar = new Tar();
$dir = dirname(__FILE__) . '/tar'; $dir = dirname(__FILE__) . '/tar';
$tdir = ltrim($dir, '/'); $tdir = ltrim($dir, '/');
$tmp = tempnam(sys_get_temp_dir(), 'dwtartest'); $tmp = vfsStream::url('home_root_path/test.tar');
$tar->create($tmp); $tar->create($tmp);
$tar->AddFile("$dir/testdata1.txt"); $tar->addFile("$dir/testdata1.txt");
$tar->AddFile("$dir/foobar/testdata2.txt", 'noway/testdata2.txt'); $tar->addFile("$dir/foobar/testdata2.txt", 'noway/testdata2.txt');
$tar->addData('another/testdata3.txt', 'testcontent3'); $tar->addData('another/testdata3.txt', 'testcontent3');
$tar->close(); $tar->close();
@ -128,14 +135,12 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
$this->assertTrue(strpos($data, "foobar.txt") === false, 'File not in TAR'); $this->assertTrue(strpos($data, "foobar.txt") === false, 'File not in TAR');
$this->assertTrue(strpos($data, "foobar") === false, 'Path not in TAR'); $this->assertTrue(strpos($data, "foobar") === false, 'Path not in TAR');
@unlink($tmp);
} }
/** /**
* List the contents of the prebuilt TAR files * List the contents of the prebuilt TAR files
*/ */
public function test_tarcontent() public function testTarcontent()
{ {
$dir = dirname(__FILE__) . '/tar'; $dir = dirname(__FILE__) . '/tar';
@ -158,7 +163,7 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
/** /**
* Create an archive and unpack it again * Create an archive and unpack it again
*/ */
public function test_dogfood() public function testDogfood()
{ {
foreach ($this->extensions as $ext) { foreach ($this->extensions as $ext) {
$input = glob(dirname(__FILE__) . '/../src/*'); $input = glob(dirname(__FILE__) . '/../src/*');
@ -184,7 +189,7 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
$this->nativeCheck($archive, $ext); $this->nativeCheck($archive, $ext);
self::rdelete($extract); self::RDelete($extract);
unlink($archive); unlink($archive);
} }
} }
@ -224,7 +229,7 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
/** /**
* Extract the prebuilt tar files * Extract the prebuilt tar files
*/ */
public function test_tarextract() public function testTarExtract()
{ {
$dir = dirname(__FILE__) . '/tar'; $dir = dirname(__FILE__) . '/tar';
$out = sys_get_temp_dir() . '/dwtartest' . md5(time()); $out = sys_get_temp_dir() . '/dwtartest' . md5(time());
@ -244,14 +249,14 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
$this->assertFileExists($out . '/tar/foobar/testdata2.txt', "Extracted $file"); $this->assertFileExists($out . '/tar/foobar/testdata2.txt', "Extracted $file");
$this->assertEquals(13, filesize($out . '/tar/foobar/testdata2.txt'), "Extracted $file"); $this->assertEquals(13, filesize($out . '/tar/foobar/testdata2.txt'), "Extracted $file");
self::rdelete($out); self::RDelete($out);
} }
} }
/** /**
* Extract the prebuilt tar files with component stripping * Extract the prebuilt tar files with component stripping
*/ */
public function test_compstripextract() public function testCompStripExtract()
{ {
$dir = dirname(__FILE__) . '/tar'; $dir = dirname(__FILE__) . '/tar';
$out = sys_get_temp_dir() . '/dwtartest' . md5(time()); $out = sys_get_temp_dir() . '/dwtartest' . md5(time());
@ -271,14 +276,14 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
$this->assertFileExists($out . '/foobar/testdata2.txt', "Extracted $file"); $this->assertFileExists($out . '/foobar/testdata2.txt', "Extracted $file");
$this->assertEquals(13, filesize($out . '/foobar/testdata2.txt'), "Extracted $file"); $this->assertEquals(13, filesize($out . '/foobar/testdata2.txt'), "Extracted $file");
self::rdelete($out); self::RDelete($out);
} }
} }
/** /**
* Extract the prebuilt tar files with prefix stripping * Extract the prebuilt tar files with prefix stripping
*/ */
public function test_prefixstripextract() public function testPrefixStripExtract()
{ {
$dir = dirname(__FILE__) . '/tar'; $dir = dirname(__FILE__) . '/tar';
$out = sys_get_temp_dir() . '/dwtartest' . md5(time()); $out = sys_get_temp_dir() . '/dwtartest' . md5(time());
@ -298,14 +303,14 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
$this->assertFileExists($out . '/testdata2.txt', "Extracted $file"); $this->assertFileExists($out . '/testdata2.txt', "Extracted $file");
$this->assertEquals(13, filesize($out . '/testdata2.txt'), "Extracted $file"); $this->assertEquals(13, filesize($out . '/testdata2.txt'), "Extracted $file");
self::rdelete($out); self::RDelete($out);
} }
} }
/** /**
* Extract the prebuilt tar files with include regex * Extract the prebuilt tar files with include regex
*/ */
public function test_includeextract() public function testIncludeExtract()
{ {
$dir = dirname(__FILE__) . '/tar'; $dir = dirname(__FILE__) . '/tar';
$out = sys_get_temp_dir() . '/dwtartest' . md5(time()); $out = sys_get_temp_dir() . '/dwtartest' . md5(time());
@ -324,14 +329,14 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
$this->assertFileExists($out . '/tar/foobar/testdata2.txt', "Extracted $file"); $this->assertFileExists($out . '/tar/foobar/testdata2.txt', "Extracted $file");
$this->assertEquals(13, filesize($out . '/tar/foobar/testdata2.txt'), "Extracted $file"); $this->assertEquals(13, filesize($out . '/tar/foobar/testdata2.txt'), "Extracted $file");
self::rdelete($out); self::RDelete($out);
} }
} }
/** /**
* Extract the prebuilt tar files with exclude regex * Extract the prebuilt tar files with exclude regex
*/ */
public function test_excludeextract() public function testExcludeExtract()
{ {
$dir = dirname(__FILE__) . '/tar'; $dir = dirname(__FILE__) . '/tar';
$out = sys_get_temp_dir() . '/dwtartest' . md5(time()); $out = sys_get_temp_dir() . '/dwtartest' . md5(time());
@ -350,14 +355,14 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
$this->assertFileNotExists($out . '/tar/foobar/testdata2.txt', "Extracted $file"); $this->assertFileNotExists($out . '/tar/foobar/testdata2.txt', "Extracted $file");
self::rdelete($out); self::RDelete($out);
} }
} }
/** /**
* Check the extension to compression guesser * Check the extension to compression guesser
*/ */
public function test_filetype() public function testFileType()
{ {
$tar = new Tar(); $tar = new Tar();
$this->assertEquals(Tar::COMPRESS_NONE, $tar->filetype('foo')); $this->assertEquals(Tar::COMPRESS_NONE, $tar->filetype('foo'));
@ -380,12 +385,12 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
} }
/** /**
* @depends test_ext_zlib * @depends testExtZlibIsInstalled
*/ */
public function test_longpathextract() public function testLongPathExtract()
{ {
$dir = dirname(__FILE__) . '/tar'; $dir = dirname(__FILE__) . '/tar';
$out = sys_get_temp_dir() . '/dwtartest' . md5(time()); $out = vfsStream::url('home_root_path/dwtartest' . md5(time()));
foreach (array('ustar', 'gnu') as $format) { foreach (array('ustar', 'gnu') as $format) {
$tar = new Tar(); $tar = new Tar();
@ -395,17 +400,15 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
$this->assertFileExists( $this->assertFileExists(
$out . '/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/test.txt' $out . '/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/test.txt'
); );
self::rdelete($out);
} }
} }
// FS#1442 // FS#1442
public function test_createlongfile() public function testCreateLongFile()
{ {
$tar = new Tar(); $tar = new Tar();
$tar->setCompression(0); $tar->setCompression(0);
$tmp = tempnam(sys_get_temp_dir(), 'dwtartest'); $tmp = vfsStream::url('home_root_path/dwtartest');
$path = '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.txt'; $path = '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.txt';
@ -420,15 +423,13 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
$this->assertTrue(strpos($data, 'testcontent1') !== false, 'content in TAR'); $this->assertTrue(strpos($data, 'testcontent1') !== false, 'content in TAR');
$this->assertTrue(strpos($data, $path) !== false, 'path in TAR'); $this->assertTrue(strpos($data, $path) !== false, 'path in TAR');
$this->assertTrue(strpos($data, '@LongLink') !== false, '@LongLink in TAR'); $this->assertTrue(strpos($data, '@LongLink') !== false, '@LongLink in TAR');
@unlink($tmp);
} }
public function test_createlongpathustar() public function testCreateLongPathTar()
{ {
$tar = new Tar(); $tar = new Tar();
$tar->setCompression(0); $tar->setCompression(0);
$tmp = tempnam(sys_get_temp_dir(), 'dwtartest'); $tmp = vfsStream::url('home_root_path/dwtartest');
$path = ''; $path = '';
for ($i = 0; $i < 11; $i++) { for ($i = 0; $i < 11; $i++) {
@ -449,15 +450,13 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
$this->assertTrue(strpos($data, $path) !== false, 'path in TAR'); $this->assertTrue(strpos($data, $path) !== false, 'path in TAR');
$this->assertFalse(strpos($data, "$path/test.txt") !== false, 'full filename in TAR'); $this->assertFalse(strpos($data, "$path/test.txt") !== false, 'full filename in TAR');
$this->assertFalse(strpos($data, '@LongLink') !== false, '@LongLink in TAR'); $this->assertFalse(strpos($data, '@LongLink') !== false, '@LongLink in TAR');
@unlink($tmp);
} }
public function test_createlongpathgnu() public function testCreateLongPathGnu()
{ {
$tar = new Tar(); $tar = new Tar();
$tar->setCompression(0); $tar->setCompression(0);
$tmp = tempnam(sys_get_temp_dir(), 'dwtartest'); $tmp = vfsStream::url('home_root_path/dwtartest');
$path = ''; $path = '';
for ($i = 0; $i < 20; $i++) { for ($i = 0; $i < 20; $i++) {
@ -478,18 +477,16 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
$this->assertTrue(strpos($data, $path) !== false, 'path in TAR'); $this->assertTrue(strpos($data, $path) !== false, 'path in TAR');
$this->assertTrue(strpos($data, "$path/test.txt") !== false, 'full filename in TAR'); $this->assertTrue(strpos($data, "$path/test.txt") !== false, 'full filename in TAR');
$this->assertTrue(strpos($data, '@LongLink') !== false, '@LongLink in TAR'); $this->assertTrue(strpos($data, '@LongLink') !== false, '@LongLink in TAR');
@unlink($tmp);
} }
/** /**
* Extract a tarbomomb * Extract a tarbomomb
* @depends test_ext_zlib * @depends testExtZlibIsInstalled
*/ */
public function test_tarbomb() public function testTarBomb()
{ {
$dir = dirname(__FILE__) . '/tar'; $dir = dirname(__FILE__) . '/tar';
$out = sys_get_temp_dir() . '/dwtartest' . md5(time()); $out = vfsStream::url('home_root_path/dwtartest' . md5(time()));
$tar = new Tar(); $tar = new Tar();
@ -501,14 +498,12 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
$this->assertFileExists( $this->assertFileExists(
$out . '/AAAAAAAAAAAAAAAAA/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB.txt' $out . '/AAAAAAAAAAAAAAAAA/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB.txt'
); );
self::rdelete($out);
} }
/** /**
* A single zero file should be just a header block + the footer * A single zero file should be just a header block + the footer
*/ */
public function test_zerofile() public function testZeroFile()
{ {
$dir = dirname(__FILE__) . '/tar'; $dir = dirname(__FILE__) . '/tar';
$tar = new Tar(); $tar = new Tar();
@ -520,7 +515,7 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
$this->assertEquals(512 * 3, strlen($file)); // 1 header block + 2 footer blocks $this->assertEquals(512 * 3, strlen($file)); // 1 header block + 2 footer blocks
} }
public function test_zerodata() public function testZeroData()
{ {
$tar = new Tar(); $tar = new Tar();
$tar->setCompression(0); $tar->setCompression(0);
@ -534,7 +529,7 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
/** /**
* A file of exactly one block should be just a header block + data block + the footer * A file of exactly one block should be just a header block + data block + the footer
*/ */
public function test_blockfile() public function testBlockFile()
{ {
$dir = dirname(__FILE__) . '/tar'; $dir = dirname(__FILE__) . '/tar';
$tar = new Tar(); $tar = new Tar();
@ -546,7 +541,7 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
$this->assertEquals(512 * 4, strlen($file)); // 1 header block + data block + 2 footer blocks $this->assertEquals(512 * 4, strlen($file)); // 1 header block + data block + 2 footer blocks
} }
public function test_blockdata() public function testBlockData()
{ {
$tar = new Tar(); $tar = new Tar();
$tar->setCompression(0); $tar->setCompression(0);
@ -556,11 +551,11 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
$this->assertEquals(512 * 4, strlen($file)); // 1 header block + data block + 2 footer blocks $this->assertEquals(512 * 4, strlen($file)); // 1 header block + data block + 2 footer blocks
} }
/** /**
* @depends test_ext_zlib * @depends testExtZlibIsInstalled
*/ */
public function test_gzipisvalid() public function testGzipIsValid()
{ {
foreach (['tgz', 'tar.gz'] as $ext) { foreach (['tgz', 'tar.gz'] as $ext) {
$input = glob(dirname(__FILE__) . '/../src/*'); $input = glob(dirname(__FILE__) . '/../src/*');
@ -576,9 +571,9 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
} }
$tar->save($archive); $tar->save($archive);
$this->assertFileExists($archive); $this->assertFileExists($archive);
try { try {
$phar = new PharData($archive); $phar = new \PharData($archive);
$phar->extractTo($extract); $phar->extractTo($extract);
} catch (\Exception $e) {}; } catch (\Exception $e) {};
@ -587,18 +582,157 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
$this->nativeCheck($archive, $ext); $this->nativeCheck($archive, $ext);
self::rdelete($extract); self::RDelete($extract);
unlink($archive); unlink($archive);
} }
} }
/**
* @expectedException splitbrain\PHPArchive\ArchiveIOException
*/
public function testContentsWithInvalidArchiveStream()
{
$tar = new Tar();
$tar->contents();
}
/**
* @expectedException splitbrain\PHPArchive\ArchiveIOException
*/
public function testExtractWithInvalidOutDir()
{
$dir = dirname(__FILE__) . '/tar';
$out = '/root/invalid_out_dir';
$tar = new Tar();
$tar->open("$dir/tarbomb.tgz");
$tar->extract($out);
}
/**
* @expectedException splitbrain\PHPArchive\ArchiveIOException
*/
public function testExtractWithArchiveStreamIsClosed()
{
$dir = dirname(__FILE__) . '/tar';
$out = '/root/invalid_out_dir';
$tar = new Tar();
$tar->open("$dir/tarbomb.tgz");
$tar->close();
$tar->extract($out);
}
/**
* @expectedException splitbrain\PHPArchive\ArchiveIOException
*/
public function testCreateWithInvalidFile()
{
$dir = dirname(__FILE__) . '/tar';
$tar = new Tar();
$tar->open("$dir/tarbomb.tgz");
$tar->create('/root/invalid_file');
}
/**
* @expectedException splitbrain\PHPArchive\ArchiveIOException
*/
public function testAddFileWithArchiveStreamIsClosed()
{
$archive = sys_get_temp_dir() . '/dwtartest' . md5(time()) . '.tar';
$tar = new Tar();
$tar->create($archive);
$tar->close();
$tar->addFile('archive_file', false);
}
/**
* @expectedException splitbrain\PHPArchive\ArchiveIOException
*/
public function testAddFileWithInvalidFile()
{
$archive = sys_get_temp_dir() . '/dwtartest' . md5(time()) . '.tar';
$tar = new Tar();
$tar->create($archive);
$tar->addFile('archive_file', false);
}
/**
* @expectedException splitbrain\PHPArchive\ArchiveIOException
*/
public function testAddDataWithArchiveStreamIsClosed()
{
$archive = sys_get_temp_dir() . '/dwtartest' . md5(time()) . '.tar';
$tar = new Tar();
$tar->create($archive);
$tar->close();
$tar->addData(false, '');
}
public function testCloseHasBeenClosed()
{
$archive = sys_get_temp_dir() . '/dwtartest' . md5(time()) . '.tar';
$tar = new Tar();
$tar->create($archive);
$tar->close();
$this->assertNull($tar->close());
}
/**
* @depends testExtBz2IsInstalled
*/
public function testGetArchiveWithBzipCompress()
{
$dir = dirname(__FILE__) . '/tar';
$tar = new Tar();
$tar->setCompression(9, Tar::COMPRESS_BZIP);
$tar->create();
$tar->addFile("$dir/zero.txt", 'zero.txt');
$file = $tar->getArchive();
$this->assertEquals(104, strlen($file)); // 1 header block + 2 footer blocks
}
public function testSaveWithCompressionAuto()
{
$dir = dirname(__FILE__) . '/tar';
$tar = new Tar();
$tar->setCompression(-1);
$tar->create();
$tar->addFile("$dir/zero.txt", 'zero.txt');
$this->assertNull($tar->save(vfsStream::url('home_root_path/archive_file')));
}
/**
* @expectedException splitbrain\PHPArchive\ArchiveIOException
*/
public function testSaveWithInvalidDestinationFile()
{
$dir = dirname(__FILE__) . '/tar';
$tar = new Tar();
$tar->setCompression();
$tar->create();
$tar->addFile("$dir/zero.txt", 'zero.txt');
$this->assertNull($tar->save(vfsStream::url('archive_file')));
}
/** /**
* recursive rmdir()/unlink() * recursive rmdir()/unlink()
* *
* @static * @static
* @param $target string * @param $target string
*/ */
public static function rdelete($target) public static function RDelete($target)
{ {
if (!is_dir($target)) { if (!is_dir($target)) {
unlink($target); unlink($target);
@ -608,7 +742,7 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase
if ($entry == '.' || $entry == '..') { if ($entry == '.' || $entry == '..') {
continue; continue;
} }
self::rdelete("$target/$entry"); self::RDelete("$target/$entry");
} }
$dh->close(); $dh->close();
rmdir($target); rmdir($target);

View File

@ -1,14 +1,30 @@
<?php <?php
use splitbrain\PHPArchive\Zip; namespace splitbrain\PHPArchive;
class Zip_TestCase extends PHPUnit_Framework_TestCase use splitbrain\PHPArchive\Zip;
use PHPUnit\Framework\TestCase;
use org\bovigo\vfs\vfsStream;
class ZipTestCase extends TestCase
{ {
protected function setUp()
{
vfsStream::setup('home_root_path');
}
/*
* dependency for tests needing zip extension to pass
*/
public function testExtZipIsInstalled()
{
$this->assertTrue(function_exists('zip_open'));
}
/** /**
* @expectedException splitbrain\PHPArchive\ArchiveIOException * @expectedException splitbrain\PHPArchive\ArchiveIOException
*/ */
public function test_missing() public function testMissing()
{ {
$tar = new Zip(); $tar = new Zip();
$tar->open('nope.zip'); $tar->open('nope.zip');
@ -19,8 +35,9 @@ class Zip_TestCase extends PHPUnit_Framework_TestCase
* the uncompressed zip stream * the uncompressed zip stream
* *
* No check for format correctness * No check for format correctness
* @depends testExtZipIsInstalled
*/ */
public function test_createdynamic() public function testCreateDynamic()
{ {
$zip = new Zip(); $zip = new Zip();
@ -58,14 +75,15 @@ class Zip_TestCase extends PHPUnit_Framework_TestCase
* uncompressed zip file * uncompressed zip file
* *
* No check for format correctness * No check for format correctness
* @depends testExtZipIsInstalled
*/ */
public function test_createfile() public function testCreateFile()
{ {
$zip = new Zip(); $zip = new Zip();
$dir = dirname(__FILE__).'/zip'; $dir = dirname(__FILE__).'/zip';
$tdir = ltrim($dir, '/'); $tdir = ltrim($dir, '/');
$tmp = tempnam(sys_get_temp_dir(), 'dwziptest'); $tmp = vfsStream::url('home_root_path/test.zip');
$zip->create($tmp); $zip->create($tmp);
$zip->setCompression(0); $zip->setCompression(0);
@ -93,17 +111,56 @@ class Zip_TestCase extends PHPUnit_Framework_TestCase
$this->assertTrue(strpos($data, "foobar.txt") === false, 'File not in ZIP'); $this->assertTrue(strpos($data, "foobar.txt") === false, 'File not in ZIP');
$this->assertTrue(strpos($data, "foobar") === false, 'Path not in ZIP'); $this->assertTrue(strpos($data, "foobar") === false, 'Path not in ZIP');
}
$this->nativeCheck($tmp); /**
$this->native7ZipCheck($tmp); * @expectedException splitbrain\PHPArchive\ArchiveIOException
*/
public function testCreateWithInvalidFilePath()
{
$zip = new Zip();
@unlink($tmp); $dir = dirname(__FILE__).'/zip';
$tmp = vfsStream::url('invalid_root_path/test.zip');
$zip->create($tmp);
}
/**
* @expectedException splitbrain\PHPArchive\ArchiveIOException
*/
public function testAddFileWithArchiveStreamIsClosed()
{
$zip = new Zip();
$dir = dirname(__FILE__).'/zip';
$tmp = vfsStream::url('home_root_path/test.zip');
$zip->setCompression(0);
$zip->close();
$zip->addFile("$dir/testdata1.txt", "$dir/testdata1.txt");
}
/**
* @expectedException splitbrain\PHPArchive\ArchiveIOException
*/
public function testAddFileWithInvalidFile()
{
$zip = new Zip();
$tmp = vfsStream::url('home_root_path/test.zip');
$zip->create($tmp);
$zip->setCompression(0);
$zip->addFile('invalid_file', false);
$zip->close();
} }
/** /**
* List the contents of the prebuilt ZIP file * List the contents of the prebuilt ZIP file
* @depends testExtZipIsInstalled
*/ */
public function test_zipcontent() public function testZipContent()
{ {
$dir = dirname(__FILE__).'/zip'; $dir = dirname(__FILE__).'/zip';
@ -122,11 +179,26 @@ class Zip_TestCase extends PHPUnit_Framework_TestCase
} }
/** /**
* Create an archive and unpack it again * @expectedException splitbrain\PHPArchive\ArchiveIOException
*/ */
public function test_dogfood() public function testZipContentWithArchiveStreamIsClosed()
{ {
$dir = dirname(__FILE__).'/zip';
$zip = new Zip();
$file = "$dir/test.zip";
$zip->open($file);
$zip->close();
$content = $zip->contents();
}
/**
* Create an archive and unpack it again
* @depends testExtZipIsInstalled
*/
public function testDogFood()
{
$input = glob(dirname(__FILE__) . '/../src/*'); $input = glob(dirname(__FILE__) . '/../src/*');
$archive = sys_get_temp_dir() . '/dwziptest' . md5(time()) . '.zip'; $archive = sys_get_temp_dir() . '/dwziptest' . md5(time()) . '.zip';
$extract = sys_get_temp_dir() . '/dwziptest' . md5(time() + 1); $extract = sys_get_temp_dir() . '/dwziptest' . md5(time() + 1);
@ -151,11 +223,15 @@ class Zip_TestCase extends PHPUnit_Framework_TestCase
$this->nativeCheck($archive); $this->nativeCheck($archive);
$this->native7ZipCheck($archive); $this->native7ZipCheck($archive);
self::rdelete($extract); self::RDelete($extract);
unlink($archive); unlink($archive);
} }
public function test_utf8() { /**
* @depends testExtZipIsInstalled
*/
public function testUtf8()
{
$archive = sys_get_temp_dir() . '/dwziptest' . md5(time()) . '.zip'; $archive = sys_get_temp_dir() . '/dwziptest' . md5(time()) . '.zip';
$extract = sys_get_temp_dir() . '/dwziptest' . md5(time() + 1); $extract = sys_get_temp_dir() . '/dwziptest' . md5(time() + 1);
@ -176,10 +252,56 @@ class Zip_TestCase extends PHPUnit_Framework_TestCase
$this->nativeCheck($archive); $this->nativeCheck($archive);
$this->native7ZipCheck($archive); $this->native7ZipCheck($archive);
self::rdelete($extract); self::RDelete($extract);
unlink($archive); unlink($archive);
} }
/**
* @expectedException splitbrain\PHPArchive\ArchiveIOException
*/
public function testAddDataWithArchiveStreamIsClosed()
{
$archive = sys_get_temp_dir() . '/dwziptest' . md5(time()) . '.zip';
$zip = new Zip();
$zip->create($archive);
$zip->close();
$zip->addData('tüst.txt', 'test');
}
public function testCloseWithArchiveStreamIsClosed()
{
$archive = sys_get_temp_dir() . '/dwziptest' . md5(time()) . '.zip';
$zip = new Zip();
$zip->create($archive);
$zip->close();
$this->assertNull($zip->close());
}
public function testSaveArchiveFile()
{
$dir = dirname(__FILE__) . '/tar';
$zip = new zip();
$zip->setCompression(-1);
$zip->create();
$zip->addFile("$dir/zero.txt", 'zero.txt');
$this->assertNull($zip->save(vfsStream::url('home_root_path/archive_file')));
}
/**
* @expectedException splitbrain\PHPArchive\ArchiveIOException
*/
public function testSaveWithInvalidFilePath()
{
$archive = sys_get_temp_dir() . '/dwziptest' . md5(time()) . '.zip';
$zip = new Zip();
$zip->create($archive);
$zip->save(vfsStream::url('invalid_root_path/save.zip'));
}
/** /**
* Test the given archive with a native zip installation (if available) * Test the given archive with a native zip installation (if available)
@ -227,8 +349,9 @@ class Zip_TestCase extends PHPUnit_Framework_TestCase
/** /**
* Extract the prebuilt zip files * Extract the prebuilt zip files
* @depends testExtZipIsInstalled
*/ */
public function test_zipextract() public function testZipExtract()
{ {
$dir = dirname(__FILE__).'/zip'; $dir = dirname(__FILE__).'/zip';
$out = sys_get_temp_dir().'/dwziptest'.md5(time()); $out = sys_get_temp_dir().'/dwziptest'.md5(time());
@ -251,13 +374,30 @@ class Zip_TestCase extends PHPUnit_Framework_TestCase
$this->assertEquals(1836, filesize($out.'/zip/compressable.txt'), "Extracted $file"); $this->assertEquals(1836, filesize($out.'/zip/compressable.txt'), "Extracted $file");
$this->assertFileNotExists($out.'/zip/compressable.txt.gz', "Extracted $file"); $this->assertFileNotExists($out.'/zip/compressable.txt.gz', "Extracted $file");
self::rdelete($out); self::RDelete($out);
}
/**
* @expectedException splitbrain\PHPArchive\ArchiveIOException
*/
public function testZipExtractWithArchiveStreamIsClosed()
{
$dir = dirname(__FILE__).'/zip';
$out = sys_get_temp_dir().'/dwziptest'.md5(time());
$zip = new Zip();
$file = "$dir/test.zip";
$zip->open($file);
$zip->close();
$zip->extract($out);
} }
/** /**
* Extract the prebuilt zip files with component stripping * Extract the prebuilt zip files with component stripping
* @depends testExtZipIsInstalled
*/ */
public function test_compstripextract() public function testCompStripExtract()
{ {
$dir = dirname(__FILE__).'/zip'; $dir = dirname(__FILE__).'/zip';
$out = sys_get_temp_dir().'/dwziptest'.md5(time()); $out = sys_get_temp_dir().'/dwziptest'.md5(time());
@ -276,13 +416,14 @@ class Zip_TestCase extends PHPUnit_Framework_TestCase
$this->assertFileExists($out.'/foobar/testdata2.txt', "Extracted $file"); $this->assertFileExists($out.'/foobar/testdata2.txt', "Extracted $file");
$this->assertEquals(13, filesize($out.'/foobar/testdata2.txt'), "Extracted $file"); $this->assertEquals(13, filesize($out.'/foobar/testdata2.txt'), "Extracted $file");
self::rdelete($out); self::RDelete($out);
} }
/** /**
* Extract the prebuilt zip files with prefix stripping * Extract the prebuilt zip files with prefix stripping
* @depends testExtZipIsInstalled
*/ */
public function test_prefixstripextract() public function testPrefixStripExtract()
{ {
$dir = dirname(__FILE__).'/zip'; $dir = dirname(__FILE__).'/zip';
$out = sys_get_temp_dir().'/dwziptest'.md5(time()); $out = sys_get_temp_dir().'/dwziptest'.md5(time());
@ -301,13 +442,14 @@ class Zip_TestCase extends PHPUnit_Framework_TestCase
$this->assertFileExists($out.'/testdata2.txt', "Extracted $file"); $this->assertFileExists($out.'/testdata2.txt', "Extracted $file");
$this->assertEquals(13, filesize($out.'/testdata2.txt'), "Extracted $file"); $this->assertEquals(13, filesize($out.'/testdata2.txt'), "Extracted $file");
self::rdelete($out); self::RDelete($out);
} }
/** /**
* Extract the prebuilt zip files with include regex * Extract the prebuilt zip files with include regex
* @depends testExtZipIsInstalled
*/ */
public function test_includeextract() public function testIncludeExtract()
{ {
$dir = dirname(__FILE__).'/zip'; $dir = dirname(__FILE__).'/zip';
$out = sys_get_temp_dir().'/dwziptest'.md5(time()); $out = sys_get_temp_dir().'/dwziptest'.md5(time());
@ -325,13 +467,14 @@ class Zip_TestCase extends PHPUnit_Framework_TestCase
$this->assertFileExists($out.'/zip/foobar/testdata2.txt', "Extracted $file"); $this->assertFileExists($out.'/zip/foobar/testdata2.txt', "Extracted $file");
$this->assertEquals(13, filesize($out.'/zip/foobar/testdata2.txt'), "Extracted $file"); $this->assertEquals(13, filesize($out.'/zip/foobar/testdata2.txt'), "Extracted $file");
self::rdelete($out); self::RDelete($out);
} }
/** /**
* Extract the prebuilt zip files with exclude regex * Extract the prebuilt zip files with exclude regex
* @depends testExtZipIsInstalled
*/ */
public function test_excludeextract() public function testExcludeExtract()
{ {
$dir = dirname(__FILE__).'/zip'; $dir = dirname(__FILE__).'/zip';
$out = sys_get_temp_dir().'/dwziptest'.md5(time()); $out = sys_get_temp_dir().'/dwziptest'.md5(time());
@ -349,12 +492,15 @@ class Zip_TestCase extends PHPUnit_Framework_TestCase
$this->assertFileNotExists($out.'/zip/foobar/testdata2.txt', "Extracted $file"); $this->assertFileNotExists($out.'/zip/foobar/testdata2.txt', "Extracted $file");
self::rdelete($out); self::RDelete($out);
} }
public function test_umlautWinrar() /**
* @depends testExtZipIsInstalled
*/
public function testUmlautWinrar()
{ {
$out = sys_get_temp_dir().'/dwziptest'.md5(time()); $out = vfsStream::url('home_root_path/dwtartest' . md5(time()));
$zip = new Zip(); $zip = new Zip();
$zip->open(__DIR__ . '/zip/issue14-winrar.zip'); $zip->open(__DIR__ . '/zip/issue14-winrar.zip');
@ -362,9 +508,12 @@ class Zip_TestCase extends PHPUnit_Framework_TestCase
$this->assertFileExists("$out/tüst.txt"); $this->assertFileExists("$out/tüst.txt");
} }
public function test_umlautWindows() /**
* @depends testExtZipIsInstalled
*/
public function testUmlautWindows()
{ {
$out = sys_get_temp_dir().'/dwziptest'.md5(time()); $out = vfsStream::url('home_root_path/dwtartest' . md5(time()));
$zip = new Zip(); $zip = new Zip();
$zip->open(__DIR__ . '/zip/issue14-windows.zip'); $zip->open(__DIR__ . '/zip/issue14-windows.zip');
@ -379,7 +528,7 @@ class Zip_TestCase extends PHPUnit_Framework_TestCase
* @static * @static
* @param $target string * @param $target string
*/ */
public static function rdelete($target) public static function RDelete($target)
{ {
if (!is_dir($target)) { if (!is_dir($target)) {
unlink($target); unlink($target);
@ -389,7 +538,7 @@ class Zip_TestCase extends PHPUnit_Framework_TestCase
if ($entry == '.' || $entry == '..') { if ($entry == '.' || $entry == '..') {
continue; continue;
} }
self::rdelete("$target/$entry"); self::RDelete("$target/$entry");
} }
$dh->close(); $dh->close();
rmdir($target); rmdir($target);