From ef1985eeadd29ad0bcf3b3211947274538723c72 Mon Sep 17 00:00:00 2001 From: peter279k Date: Sun, 4 Feb 2018 19:01:02 +0800 Subject: [PATCH 01/13] tests enhancement --- .travis.yml | 22 +- composer.json | 11 +- phpunit.xml | 7 +- src/Tar.php | 10 +- src/Zip.php | 6 +- tests/{FileInfo.test.php => FileInfoTest.php} | 15 +- tests/{tar.test.php => TarTestCase.php} | 274 +++++++++++++----- tests/{zip.test.php => ZipTestCase.php} | 209 +++++++++++-- 8 files changed, 430 insertions(+), 124 deletions(-) rename tests/{FileInfo.test.php => FileInfoTest.php} (91%) rename tests/{tar.test.php => TarTestCase.php} (74%) rename tests/{zip.test.php => ZipTestCase.php} (70%) diff --git a/.travis.yml b/.travis.yml index 2d129f6..21c485a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,20 +1,18 @@ -dist: trusty language: php -sudo: false php: -- 5.4 -- 5.5 -- 5.6 -- 7.0 -- 7.1 -- nightly -- hhvm + - 5.4 + - 5.5 + - 5.6 + - 7.0 + - 7.1 + - 7.2 + - nightly + - hhvm before_script: -- composer self-update -- composer install --prefer-source --no-interaction --dev + - composer install script: ./vendor/bin/phpunit after_success: -- sh generate-api.sh + - sh generate-api.sh env: global: secure: ctCQVPQgQziwIZf5QGHcnhHlXsyauG0W3AWF/6R8cTP+in2S/RygunPp7CkXiqA1YMluGr2Lo9h4DTVg7oqeXl79FXFXedijQmQEu3g3f4iDWtxbQhGf4bJQk57jXFldge4rQedlOJDzwGzJ1abrimJQlu090BZNeonzWL5cRK4= diff --git a/composer.json b/composer.json index 36c6497..2427fea 100644 --- a/composer.json +++ b/composer.json @@ -20,12 +20,21 @@ }, "require-dev": { - "phpunit/phpunit": "4.5.*" + "phpunit/phpunit": "^4.8", + "mikey179/vfsStream": "^1.6", + "ext-zip": "*", + "ext-bz2": "*" }, "autoload": { "psr-4": { "splitbrain\\PHPArchive\\": "src" } + }, + + "autoload-dev": { + "psr-4": { + "splitbrain\\PHPArchive\\": "tests" + } } } diff --git a/phpunit.xml b/phpunit.xml index d7e1f24..c5e1ad3 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -14,4 +14,9 @@ ./tests/ - \ No newline at end of file + + + src + + + diff --git a/src/Tar.php b/src/Tar.php index b255758..5f01f39 100644 --- a/src/Tar.php +++ b/src/Tar.php @@ -164,7 +164,7 @@ public function extract($outdir, $strip = '', $exclude = '', $include = '') // extract data if (!$fileinfo->getIsdir()) { - $fp = fopen($output, "wb"); + $fp = @fopen($output, "wb"); if (!$fp) { throw new ArchiveIOException('Could not open file for writing: '.$output); } @@ -245,7 +245,7 @@ public function addFile($file, $fileinfo = '') throw new ArchiveIOException('Archive has been closed, files can no longer be added'); } - $fp = fopen($file, 'rb'); + $fp = @fopen($file, 'rb'); if (!$fp) { throw new ArchiveIOException('Could not open file for reading: '.$file); } @@ -379,7 +379,7 @@ public function save($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); } } @@ -433,7 +433,7 @@ protected function writebytes($data) * * @param int $bytes seek to this position */ - function skipbytes($bytes) + protected function skipbytes($bytes) { if ($this->comptype === Archive::COMPRESS_GZIP) { @gzseek($this->fh, $bytes, SEEK_CUR); @@ -645,7 +645,7 @@ public function filetype($file) { // for existing files, try to read the magic bytes if(file_exists($file) && is_readable($file) && filesize($file) > 5) { - $fh = fopen($file, 'rb'); + $fh = @fopen($file, 'rb'); if(!$fh) return false; $magic = fread($fh, 5); fclose($fh); diff --git a/src/Zip.php b/src/Zip.php index 272a902..649f954 100644 --- a/src/Zip.php +++ b/src/Zip.php @@ -111,7 +111,7 @@ public function contents() * @throws ArchiveIOException * @return FileInfo[] */ - function extract($outdir, $strip = '', $exclude = '', $include = '') + public function extract($outdir, $strip = '', $exclude = '', $include = '') { if ($this->closed || !$this->file) { throw new ArchiveIOException('Can not read from a closed archive'); @@ -163,7 +163,7 @@ function extract($outdir, $strip = '', $exclude = '', $include = '') } // open file for writing - $fp = fopen($extractto, "wb"); + $fp = @fopen($extractto, "wb"); if (!$fp) { throw new ArchiveIOException('Could not open file for writing: '.$extractto); } @@ -419,7 +419,7 @@ public function getArchive() */ 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); } } diff --git a/tests/FileInfo.test.php b/tests/FileInfoTest.php similarity index 91% rename from tests/FileInfo.test.php rename to tests/FileInfoTest.php index 234f51e..515e644 100644 --- a/tests/FileInfo.test.php +++ b/tests/FileInfoTest.php @@ -1,8 +1,11 @@ assertTrue($fileinfo->getIsdir()); $this->assertSame(0, $fileinfo->getSize()); } -} \ No newline at end of file + + /** + * @expectedException splitbrain\PHPArchive\FileInfoException + */ + public function testFromPathWithFileNotExisted() + { + $fileinfo = FileInfo::fromPath('invalid_file_path'); + } +} diff --git a/tests/tar.test.php b/tests/TarTestCase.php similarity index 74% rename from tests/tar.test.php rename to tests/TarTestCase.php index eb34093..40c0d34 100644 --- a/tests/tar.test.php +++ b/tests/TarTestCase.php @@ -1,15 +1,19 @@ extensions[] = 'tbz'; $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 */ - public function test_ext_zlib() + public function testExtZlibIsInstalled() { - if (!extension_loaded('zlib')) { - $this->markTestSkipped('skipping all zlib tests. Need zlib extension'); - } + $this->assertTrue(function_exists('gzopen')); } /* - * 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->markTestSkipped('skipping all bzip2 tests. Need bz2 extension'); - } + $this->assertTrue(function_exists('bzopen')); } /** * @expectedException splitbrain\PHPArchive\ArchiveIOException */ - public function test_missing() + public function testTarFileIsNotExisted() { $tar = new Tar(); - $tar->open('nope.tar'); + $tar->open('non_existed_file.tar'); } /** @@ -57,7 +64,7 @@ public function test_missing() * * No check for format correctness */ - public function test_createdynamic() + public function testCreateDynamic() { $tar = new Tar(); @@ -65,8 +72,8 @@ public function test_createdynamic() $tdir = ltrim($dir, '/'); $tar->create(); - $tar->AddFile("$dir/testdata1.txt"); - $tar->AddFile("$dir/foobar/testdata2.txt", 'noway/testdata2.txt'); + $tar->addFile("$dir/testdata1.txt"); + $tar->addFile("$dir/foobar/testdata2.txt", 'noway/testdata2.txt'); $tar->addData('another/testdata3.txt', 'testcontent3'); $data = $tar->getArchive(); @@ -95,17 +102,17 @@ public function test_createdynamic() * * No check for format correctness */ - public function test_createfile() + public function testCreateFile() { $tar = new Tar(); $dir = dirname(__FILE__) . '/tar'; $tdir = ltrim($dir, '/'); - $tmp = tempnam(sys_get_temp_dir(), 'dwtartest'); + $tmp = vfsStream::url('home_root_path/test.tar'); $tar->create($tmp); - $tar->AddFile("$dir/testdata1.txt"); - $tar->AddFile("$dir/foobar/testdata2.txt", 'noway/testdata2.txt'); + $tar->addFile("$dir/testdata1.txt"); + $tar->addFile("$dir/foobar/testdata2.txt", 'noway/testdata2.txt'); $tar->addData('another/testdata3.txt', 'testcontent3'); $tar->close(); @@ -128,14 +135,12 @@ public function test_createfile() $this->assertTrue(strpos($data, "foobar.txt") === false, 'File not in TAR'); $this->assertTrue(strpos($data, "foobar") === false, 'Path not in TAR'); - - @unlink($tmp); } /** * List the contents of the prebuilt TAR files */ - public function test_tarcontent() + public function testTarcontent() { $dir = dirname(__FILE__) . '/tar'; @@ -158,7 +163,7 @@ public function test_tarcontent() /** * Create an archive and unpack it again */ - public function test_dogfood() + public function testDogfood() { foreach ($this->extensions as $ext) { $input = glob(dirname(__FILE__) . '/../src/*'); @@ -184,7 +189,7 @@ public function test_dogfood() $this->nativeCheck($archive, $ext); - self::rdelete($extract); + self::RDelete($extract); unlink($archive); } } @@ -224,7 +229,7 @@ protected function nativeCheck($archive, $ext) /** * Extract the prebuilt tar files */ - public function test_tarextract() + public function testTarExtract() { $dir = dirname(__FILE__) . '/tar'; $out = sys_get_temp_dir() . '/dwtartest' . md5(time()); @@ -244,14 +249,14 @@ public function test_tarextract() $this->assertFileExists($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 */ - public function test_compstripextract() + public function testCompStripExtract() { $dir = dirname(__FILE__) . '/tar'; $out = sys_get_temp_dir() . '/dwtartest' . md5(time()); @@ -271,14 +276,14 @@ public function test_compstripextract() $this->assertFileExists($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 */ - public function test_prefixstripextract() + public function testPrefixStripExtract() { $dir = dirname(__FILE__) . '/tar'; $out = sys_get_temp_dir() . '/dwtartest' . md5(time()); @@ -298,14 +303,14 @@ public function test_prefixstripextract() $this->assertFileExists($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 */ - public function test_includeextract() + public function testIncludeExtract() { $dir = dirname(__FILE__) . '/tar'; $out = sys_get_temp_dir() . '/dwtartest' . md5(time()); @@ -324,14 +329,14 @@ public function test_includeextract() $this->assertFileExists($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 */ - public function test_excludeextract() + public function testExcludeExtract() { $dir = dirname(__FILE__) . '/tar'; $out = sys_get_temp_dir() . '/dwtartest' . md5(time()); @@ -350,14 +355,14 @@ public function test_excludeextract() $this->assertFileNotExists($out . '/tar/foobar/testdata2.txt', "Extracted $file"); - self::rdelete($out); + self::RDelete($out); } } /** * Check the extension to compression guesser */ - public function test_filetype() + public function testFileType() { $tar = new Tar(); $this->assertEquals(Tar::COMPRESS_NONE, $tar->filetype('foo')); @@ -380,12 +385,12 @@ public function test_filetype() } /** - * @depends test_ext_zlib + * @depends testExtZlibIsInstalled */ - public function test_longpathextract() + public function testLongPathExtract() { $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) { $tar = new Tar(); @@ -395,17 +400,15 @@ public function test_longpathextract() $this->assertFileExists( $out . '/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/test.txt' ); - - self::rdelete($out); } } // FS#1442 - public function test_createlongfile() + public function testCreateLongFile() { $tar = new Tar(); $tar->setCompression(0); - $tmp = tempnam(sys_get_temp_dir(), 'dwtartest'); + $tmp = vfsStream::url('home_root_path/dwtartest'); $path = '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.txt'; @@ -420,15 +423,13 @@ public function test_createlongfile() $this->assertTrue(strpos($data, 'testcontent1') !== false, 'content in TAR'); $this->assertTrue(strpos($data, $path) !== false, 'path in TAR'); $this->assertTrue(strpos($data, '@LongLink') !== false, '@LongLink in TAR'); - - @unlink($tmp); } - public function test_createlongpathustar() + public function testCreateLongPathTar() { $tar = new Tar(); $tar->setCompression(0); - $tmp = tempnam(sys_get_temp_dir(), 'dwtartest'); + $tmp = vfsStream::url('home_root_path/dwtartest'); $path = ''; for ($i = 0; $i < 11; $i++) { @@ -449,15 +450,13 @@ public function test_createlongpathustar() $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, '@LongLink') !== false, '@LongLink in TAR'); - - @unlink($tmp); } - public function test_createlongpathgnu() + public function testCreateLongPathGnu() { $tar = new Tar(); $tar->setCompression(0); - $tmp = tempnam(sys_get_temp_dir(), 'dwtartest'); + $tmp = vfsStream::url('home_root_path/dwtartest'); $path = ''; for ($i = 0; $i < 20; $i++) { @@ -478,18 +477,16 @@ public function test_createlongpathgnu() $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, '@LongLink') !== false, '@LongLink in TAR'); - - @unlink($tmp); } /** * Extract a tarbomomb - * @depends test_ext_zlib + * @depends testExtZlibIsInstalled */ - public function test_tarbomb() + public function testTarBomb() { $dir = dirname(__FILE__) . '/tar'; - $out = sys_get_temp_dir() . '/dwtartest' . md5(time()); + $out = vfsStream::url('home_root_path/dwtartest' . md5(time())); $tar = new Tar(); @@ -501,14 +498,12 @@ public function test_tarbomb() $this->assertFileExists( $out . '/AAAAAAAAAAAAAAAAA/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB.txt' ); - - self::rdelete($out); } /** * A single zero file should be just a header block + the footer */ - public function test_zerofile() + public function testZeroFile() { $dir = dirname(__FILE__) . '/tar'; $tar = new Tar(); @@ -520,7 +515,7 @@ public function test_zerofile() $this->assertEquals(512 * 3, strlen($file)); // 1 header block + 2 footer blocks } - public function test_zerodata() + public function testZeroData() { $tar = new Tar(); $tar->setCompression(0); @@ -534,7 +529,7 @@ public function test_zerodata() /** * 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'; $tar = new Tar(); @@ -546,7 +541,7 @@ public function test_blockfile() $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->setCompression(0); @@ -556,11 +551,11 @@ public function test_blockdata() $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) { $input = glob(dirname(__FILE__) . '/../src/*'); @@ -576,9 +571,9 @@ public function test_gzipisvalid() } $tar->save($archive); $this->assertFileExists($archive); - + try { - $phar = new PharData($archive); + $phar = new \PharData($archive); $phar->extractTo($extract); } catch (\Exception $e) {}; @@ -587,18 +582,157 @@ public function test_gzipisvalid() $this->nativeCheck($archive, $ext); - self::rdelete($extract); + self::RDelete($extract); 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() * * @static * @param $target string */ - public static function rdelete($target) + public static function RDelete($target) { if (!is_dir($target)) { unlink($target); @@ -608,7 +742,7 @@ public static function rdelete($target) if ($entry == '.' || $entry == '..') { continue; } - self::rdelete("$target/$entry"); + self::RDelete("$target/$entry"); } $dh->close(); rmdir($target); diff --git a/tests/zip.test.php b/tests/ZipTestCase.php similarity index 70% rename from tests/zip.test.php rename to tests/ZipTestCase.php index 301d530..76de3df 100644 --- a/tests/zip.test.php +++ b/tests/ZipTestCase.php @@ -1,14 +1,30 @@ assertTrue(function_exists('zip_open')); + } /** * @expectedException splitbrain\PHPArchive\ArchiveIOException */ - public function test_missing() + public function testMissing() { $tar = new Zip(); $tar->open('nope.zip'); @@ -19,8 +35,9 @@ public function test_missing() * the uncompressed zip stream * * No check for format correctness + * @depends testExtZipIsInstalled */ - public function test_createdynamic() + public function testCreateDynamic() { $zip = new Zip(); @@ -58,14 +75,15 @@ public function test_createdynamic() * uncompressed zip file * * No check for format correctness + * @depends testExtZipIsInstalled */ - public function test_createfile() + public function testCreateFile() { $zip = new Zip(); $dir = dirname(__FILE__).'/zip'; $tdir = ltrim($dir, '/'); - $tmp = tempnam(sys_get_temp_dir(), 'dwziptest'); + $tmp = vfsStream::url('home_root_path/test.zip'); $zip->create($tmp); $zip->setCompression(0); @@ -93,17 +111,56 @@ public function test_createfile() $this->assertTrue(strpos($data, "foobar.txt") === false, 'File not in ZIP'); $this->assertTrue(strpos($data, "foobar") === false, 'Path not in ZIP'); + } + + /** + * @expectedException splitbrain\PHPArchive\ArchiveIOException + */ + public function testCreateWithInvalidFilePath() + { + $zip = new Zip(); + + $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"); + } - $this->nativeCheck($tmp); - $this->native7ZipCheck($tmp); + /** + * @expectedException splitbrain\PHPArchive\ArchiveIOException + */ + public function testAddFileWithInvalidFile() + { + $zip = new Zip(); - @unlink($tmp); + $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 + * @depends testExtZipIsInstalled */ - public function test_zipcontent() + public function testZipContent() { $dir = dirname(__FILE__).'/zip'; @@ -122,11 +179,26 @@ public function test_zipcontent() } /** - * 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/*'); $archive = sys_get_temp_dir() . '/dwziptest' . md5(time()) . '.zip'; $extract = sys_get_temp_dir() . '/dwziptest' . md5(time() + 1); @@ -151,11 +223,15 @@ public function test_dogfood() $this->nativeCheck($archive); $this->native7ZipCheck($archive); - self::rdelete($extract); + self::RDelete($extract); unlink($archive); } - public function test_utf8() { + /** + * @depends testExtZipIsInstalled + */ + public function testUtf8() + { $archive = sys_get_temp_dir() . '/dwziptest' . md5(time()) . '.zip'; $extract = sys_get_temp_dir() . '/dwziptest' . md5(time() + 1); @@ -176,10 +252,56 @@ public function test_utf8() { $this->nativeCheck($archive); $this->native7ZipCheck($archive); - self::rdelete($extract); + self::RDelete($extract); 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) @@ -227,8 +349,9 @@ protected function native7ZipCheck($archive) /** * Extract the prebuilt zip files + * @depends testExtZipIsInstalled */ - public function test_zipextract() + public function testZipExtract() { $dir = dirname(__FILE__).'/zip'; $out = sys_get_temp_dir().'/dwziptest'.md5(time()); @@ -251,13 +374,30 @@ public function test_zipextract() $this->assertEquals(1836, filesize($out.'/zip/compressable.txt'), "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 + * @depends testExtZipIsInstalled */ - public function test_compstripextract() + public function testCompStripExtract() { $dir = dirname(__FILE__).'/zip'; $out = sys_get_temp_dir().'/dwziptest'.md5(time()); @@ -276,13 +416,14 @@ public function test_compstripextract() $this->assertFileExists($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 + * @depends testExtZipIsInstalled */ - public function test_prefixstripextract() + public function testPrefixStripExtract() { $dir = dirname(__FILE__).'/zip'; $out = sys_get_temp_dir().'/dwziptest'.md5(time()); @@ -301,13 +442,14 @@ public function test_prefixstripextract() $this->assertFileExists($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 + * @depends testExtZipIsInstalled */ - public function test_includeextract() + public function testIncludeExtract() { $dir = dirname(__FILE__).'/zip'; $out = sys_get_temp_dir().'/dwziptest'.md5(time()); @@ -325,13 +467,14 @@ public function test_includeextract() $this->assertFileExists($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 + * @depends testExtZipIsInstalled */ - public function test_excludeextract() + public function testExcludeExtract() { $dir = dirname(__FILE__).'/zip'; $out = sys_get_temp_dir().'/dwziptest'.md5(time()); @@ -349,12 +492,15 @@ public function test_excludeextract() $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->open(__DIR__ . '/zip/issue14-winrar.zip'); @@ -362,9 +508,12 @@ public function test_umlautWinrar() $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->open(__DIR__ . '/zip/issue14-windows.zip'); @@ -379,7 +528,7 @@ public function test_umlautWindows() * @static * @param $target string */ - public static function rdelete($target) + public static function RDelete($target) { if (!is_dir($target)) { unlink($target); @@ -389,7 +538,7 @@ public static function rdelete($target) if ($entry == '.' || $entry == '..') { continue; } - self::rdelete("$target/$entry"); + self::RDelete("$target/$entry"); } $dh->close(); rmdir($target); From da604987bec6051790e698a7a787de23677904fe Mon Sep 17 00:00:00 2001 From: peter279k Date: Sun, 4 Feb 2018 19:06:54 +0800 Subject: [PATCH 02/13] change 104 to 102 --- .travis.yml | 29 ++++++++++++++++++++--------- tests/TarTestCase.php | 2 +- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index 21c485a..6658b3b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,24 @@ language: php -php: - - 5.4 - - 5.5 - - 5.6 - - 7.0 - - 7.1 - - 7.2 - - nightly - - hhvm +matrix: + include: + - php: 5.3 + dist: precise + - php: 5.4 + dist: trusty + - php: 5.5 + dist: trusty + - php: 5.6 + dist: trusty + - php: 7.0 + dist: trusty + - php: 7.1 + dist: trusty + - php: 7.2 + dist: trusty + - php: nightly + dist: trusty + - php: hhvm + dist: trusty before_script: - composer install script: ./vendor/bin/phpunit diff --git a/tests/TarTestCase.php b/tests/TarTestCase.php index 40c0d34..a8abb4d 100644 --- a/tests/TarTestCase.php +++ b/tests/TarTestCase.php @@ -698,7 +698,7 @@ public function testGetArchiveWithBzipCompress() $tar->addFile("$dir/zero.txt", 'zero.txt'); $file = $tar->getArchive(); - $this->assertEquals(104, strlen($file)); // 1 header block + 2 footer blocks + $this->assertEquals(102, strlen($file)); // 1 header block + 2 footer blocks } public function testSaveWithCompressionAuto() From 12f443977f4ff7b40c5ae36121a6b1e3cccb7eef Mon Sep 17 00:00:00 2001 From: peter279k Date: Sun, 4 Feb 2018 19:18:34 +0800 Subject: [PATCH 03/13] set the assertGreaterThanOrEqual --- tests/TarTestCase.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/TarTestCase.php b/tests/TarTestCase.php index a8abb4d..daba53c 100644 --- a/tests/TarTestCase.php +++ b/tests/TarTestCase.php @@ -686,9 +686,7 @@ public function testCloseHasBeenClosed() $this->assertNull($tar->close()); } - /** - * @depends testExtBz2IsInstalled - */ + public function testGetArchiveWithBzipCompress() { $dir = dirname(__FILE__) . '/tar'; @@ -698,7 +696,7 @@ public function testGetArchiveWithBzipCompress() $tar->addFile("$dir/zero.txt", 'zero.txt'); $file = $tar->getArchive(); - $this->assertEquals(102, strlen($file)); // 1 header block + 2 footer blocks + $this->assertGreaterThanOrEqual(102, strlen($file)); // 1 header block + 2 footer blocks } public function testSaveWithCompressionAuto() From 2b5fb5f5c972b28efc58cb79ef0c6a8443d9b4be Mon Sep 17 00:00:00 2001 From: peter279k Date: Sun, 4 Feb 2018 19:23:47 +0800 Subject: [PATCH 04/13] change array syntax for php 5.3 --- tests/TarTestCase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TarTestCase.php b/tests/TarTestCase.php index daba53c..30e4e18 100644 --- a/tests/TarTestCase.php +++ b/tests/TarTestCase.php @@ -557,7 +557,7 @@ public function testBlockData() */ public function testGzipIsValid() { - foreach (['tgz', 'tar.gz'] as $ext) { + foreach (array('tgz', 'tar.gz') as $ext) { $input = glob(dirname(__FILE__) . '/../src/*'); $archive = sys_get_temp_dir() . '/dwtartest' . md5(time()) . '.' . $ext; $extract = sys_get_temp_dir() . '/dwtartest' . md5(time() + 1); From 9098d2509b1c3ced4a14aae446f29ad76c1f5288 Mon Sep 17 00:00:00 2001 From: peter279k Date: Sun, 4 Feb 2018 19:34:41 +0800 Subject: [PATCH 05/13] drop the PHP 5.3 from test set the required 5.4+ --- .travis.yml | 29 +++++++++-------------------- composer.json | 2 +- tests/TarTestCase.php | 2 +- 3 files changed, 11 insertions(+), 22 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6658b3b..21c485a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,24 +1,13 @@ language: php -matrix: - include: - - php: 5.3 - dist: precise - - php: 5.4 - dist: trusty - - php: 5.5 - dist: trusty - - php: 5.6 - dist: trusty - - php: 7.0 - dist: trusty - - php: 7.1 - dist: trusty - - php: 7.2 - dist: trusty - - php: nightly - dist: trusty - - php: hhvm - dist: trusty +php: + - 5.4 + - 5.5 + - 5.6 + - 7.0 + - 7.1 + - 7.2 + - nightly + - hhvm before_script: - composer install script: ./vendor/bin/phpunit diff --git a/composer.json b/composer.json index 2427fea..bdc2e02 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "license": "MIT", "require": { - "php": ">=5.3.0" + "php": ">=5.4" }, "suggest": { diff --git a/tests/TarTestCase.php b/tests/TarTestCase.php index 30e4e18..daba53c 100644 --- a/tests/TarTestCase.php +++ b/tests/TarTestCase.php @@ -557,7 +557,7 @@ public function testBlockData() */ public function testGzipIsValid() { - foreach (array('tgz', 'tar.gz') as $ext) { + foreach (['tgz', 'tar.gz'] as $ext) { $input = glob(dirname(__FILE__) . '/../src/*'); $archive = sys_get_temp_dir() . '/dwtartest' . md5(time()) . '.' . $ext; $extract = sys_get_temp_dir() . '/dwtartest' . md5(time() + 1); From b5105a0c6cafe199e888dcb39f40107187247fa0 Mon Sep 17 00:00:00 2001 From: peter279k Date: Sun, 4 Feb 2018 19:46:35 +0800 Subject: [PATCH 06/13] using bin2hex before getting file content string --- tests/TarTestCase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TarTestCase.php b/tests/TarTestCase.php index daba53c..813be69 100644 --- a/tests/TarTestCase.php +++ b/tests/TarTestCase.php @@ -696,7 +696,7 @@ public function testGetArchiveWithBzipCompress() $tar->addFile("$dir/zero.txt", 'zero.txt'); $file = $tar->getArchive(); - $this->assertGreaterThanOrEqual(102, strlen($file)); // 1 header block + 2 footer blocks + $this->assertEquals(104, strlen(bin2hex($file))/2); // 1 header block + 2 footer blocks } public function testSaveWithCompressionAuto() From e62ad36138ccf476414d2f682b42868328495ce3 Mon Sep 17 00:00:00 2001 From: peter279k Date: Sun, 4 Feb 2018 19:34:41 +0800 Subject: [PATCH 07/13] drop the PHP 5.3 from test set the required 5.4+ --- .travis.yml | 29 +++++++++-------------------- composer.json | 2 +- tests/TarTestCase.php | 4 ++-- 3 files changed, 12 insertions(+), 23 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6658b3b..21c485a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,24 +1,13 @@ language: php -matrix: - include: - - php: 5.3 - dist: precise - - php: 5.4 - dist: trusty - - php: 5.5 - dist: trusty - - php: 5.6 - dist: trusty - - php: 7.0 - dist: trusty - - php: 7.1 - dist: trusty - - php: 7.2 - dist: trusty - - php: nightly - dist: trusty - - php: hhvm - dist: trusty +php: + - 5.4 + - 5.5 + - 5.6 + - 7.0 + - 7.1 + - 7.2 + - nightly + - hhvm before_script: - composer install script: ./vendor/bin/phpunit diff --git a/composer.json b/composer.json index 2427fea..bdc2e02 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "license": "MIT", "require": { - "php": ">=5.3.0" + "php": ">=5.4" }, "suggest": { diff --git a/tests/TarTestCase.php b/tests/TarTestCase.php index 30e4e18..813be69 100644 --- a/tests/TarTestCase.php +++ b/tests/TarTestCase.php @@ -557,7 +557,7 @@ public function testBlockData() */ public function testGzipIsValid() { - foreach (array('tgz', 'tar.gz') as $ext) { + foreach (['tgz', 'tar.gz'] as $ext) { $input = glob(dirname(__FILE__) . '/../src/*'); $archive = sys_get_temp_dir() . '/dwtartest' . md5(time()) . '.' . $ext; $extract = sys_get_temp_dir() . '/dwtartest' . md5(time() + 1); @@ -696,7 +696,7 @@ public function testGetArchiveWithBzipCompress() $tar->addFile("$dir/zero.txt", 'zero.txt'); $file = $tar->getArchive(); - $this->assertGreaterThanOrEqual(102, strlen($file)); // 1 header block + 2 footer blocks + $this->assertEquals(104, strlen(bin2hex($file))/2); // 1 header block + 2 footer blocks } public function testSaveWithCompressionAuto() From 84db2380b49b395937344d257789ee290eaeee44 Mon Sep 17 00:00:00 2001 From: peter279k Date: Sun, 4 Feb 2018 19:51:37 +0800 Subject: [PATCH 08/13] chnage assertion method --- tests/TarTestCase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TarTestCase.php b/tests/TarTestCase.php index 813be69..63a4a1f 100644 --- a/tests/TarTestCase.php +++ b/tests/TarTestCase.php @@ -696,7 +696,7 @@ public function testGetArchiveWithBzipCompress() $tar->addFile("$dir/zero.txt", 'zero.txt'); $file = $tar->getArchive(); - $this->assertEquals(104, strlen(bin2hex($file))/2); // 1 header block + 2 footer blocks + $this->assertInternalType('string', $file); // 1 header block + 2 footer blocks } public function testSaveWithCompressionAuto() From 7aaacf33a831cd9821562a00e810cc24c2027328 Mon Sep 17 00:00:00 2001 From: peter279k Date: Sun, 4 Feb 2018 20:01:13 +0800 Subject: [PATCH 09/13] using filesize to check file size --- tests/TarTestCase.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/TarTestCase.php b/tests/TarTestCase.php index 63a4a1f..8e3e241 100644 --- a/tests/TarTestCase.php +++ b/tests/TarTestCase.php @@ -695,8 +695,10 @@ public function testGetArchiveWithBzipCompress() $tar->create(); $tar->addFile("$dir/zero.txt", 'zero.txt'); $file = $tar->getArchive(); + $output = vfsStream::url('home_root_path/saved.tar.bz2'); + file_put_contents($output, $file); - $this->assertInternalType('string', $file); // 1 header block + 2 footer blocks + $this->assertEquals(104, filesize($output)); // 1 header block + 2 footer blocks } public function testSaveWithCompressionAuto() From 0f4a5d61de19a74390cbd8a1a4596bf03a6f4af6 Mon Sep 17 00:00:00 2001 From: peter279k Date: Sun, 4 Feb 2018 21:16:23 +0800 Subject: [PATCH 10/13] add clear cache --- tests/TarTestCase.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/TarTestCase.php b/tests/TarTestCase.php index 8e3e241..8e22dc8 100644 --- a/tests/TarTestCase.php +++ b/tests/TarTestCase.php @@ -697,6 +697,7 @@ public function testGetArchiveWithBzipCompress() $file = $tar->getArchive(); $output = vfsStream::url('home_root_path/saved.tar.bz2'); file_put_contents($output, $file); + clearstatcache(); $this->assertEquals(104, filesize($output)); // 1 header block + 2 footer blocks } From f114f26ce5e543917645fcfcb81a1226a806bd36 Mon Sep 17 00:00:00 2001 From: peter279k Date: Sun, 4 Feb 2018 22:01:54 +0800 Subject: [PATCH 11/13] usin assertInternalType method --- tests/TarTestCase.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/TarTestCase.php b/tests/TarTestCase.php index 8e22dc8..97f295b 100644 --- a/tests/TarTestCase.php +++ b/tests/TarTestCase.php @@ -686,7 +686,9 @@ public function testCloseHasBeenClosed() $this->assertNull($tar->close()); } - + /** + * @depends testExtBz2IsInstalled + */ public function testGetArchiveWithBzipCompress() { $dir = dirname(__FILE__) . '/tar'; @@ -695,11 +697,8 @@ public function testGetArchiveWithBzipCompress() $tar->create(); $tar->addFile("$dir/zero.txt", 'zero.txt'); $file = $tar->getArchive(); - $output = vfsStream::url('home_root_path/saved.tar.bz2'); - file_put_contents($output, $file); - clearstatcache(); - $this->assertEquals(104, filesize($output)); // 1 header block + 2 footer blocks + $this->assertInternalType('string', $file); // 1 header block + 2 footer blocks } public function testSaveWithCompressionAuto() From 9711a651a461d168b597a9d94e4ba192b1c95e28 Mon Sep 17 00:00:00 2001 From: peter279k Date: Sun, 4 Feb 2018 19:34:41 +0800 Subject: [PATCH 12/13] drop the PHP 5.3 from test set the required 5.4+ --- .travis.yml | 29 +++++++++-------------------- composer.json | 2 +- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6658b3b..21c485a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,24 +1,13 @@ language: php -matrix: - include: - - php: 5.3 - dist: precise - - php: 5.4 - dist: trusty - - php: 5.5 - dist: trusty - - php: 5.6 - dist: trusty - - php: 7.0 - dist: trusty - - php: 7.1 - dist: trusty - - php: 7.2 - dist: trusty - - php: nightly - dist: trusty - - php: hhvm - dist: trusty +php: + - 5.4 + - 5.5 + - 5.6 + - 7.0 + - 7.1 + - 7.2 + - nightly + - hhvm before_script: - composer install script: ./vendor/bin/phpunit diff --git a/composer.json b/composer.json index 2427fea..bdc2e02 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "license": "MIT", "require": { - "php": ">=5.3.0" + "php": ">=5.4" }, "suggest": { From e6e5a8edf6b6cfe8dbfb80e7a35bfb83dbbf2396 Mon Sep 17 00:00:00 2001 From: peter279k Date: Sun, 4 Feb 2018 22:17:09 +0800 Subject: [PATCH 13/13] using assertInternalType method --- tests/TarTestCase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TarTestCase.php b/tests/TarTestCase.php index a8abb4d..97f295b 100644 --- a/tests/TarTestCase.php +++ b/tests/TarTestCase.php @@ -698,7 +698,7 @@ public function testGetArchiveWithBzipCompress() $tar->addFile("$dir/zero.txt", 'zero.txt'); $file = $tar->getArchive(); - $this->assertEquals(102, strlen($file)); // 1 header block + 2 footer blocks + $this->assertInternalType('string', $file); // 1 header block + 2 footer blocks } public function testSaveWithCompressionAuto()