From 012c2817434e6b03b29076c644fb7b0f5fc83ff2 Mon Sep 17 00:00:00 2001 From: Fyorl Date: Mon, 6 Aug 2012 20:43:54 +0100 Subject: [PATCH] [ticket/11045] Added unit tests for the compress class PHPBB3-11045 --- tests/compress/archive/.gitkeep | 0 tests/compress/compress_test.php | 157 +++++++++++++++++++++++ tests/compress/extract/.gitkeep | 0 tests/compress/fixtures/1.txt | 1 + tests/compress/fixtures/archive.tar | Bin 0 -> 10240 bytes tests/compress/fixtures/archive.tar.bz2 | Bin 0 -> 224 bytes tests/compress/fixtures/archive.tar.gz | Bin 0 -> 239 bytes tests/compress/fixtures/archive.zip | Bin 0 -> 412 bytes tests/compress/fixtures/dir/2.txt | 1 + tests/compress/fixtures/dir/3.txt | 1 + tests/compress/fixtures/dir/subdir/4.txt | 1 + 11 files changed, 161 insertions(+) create mode 100644 tests/compress/archive/.gitkeep create mode 100644 tests/compress/compress_test.php create mode 100644 tests/compress/extract/.gitkeep create mode 100644 tests/compress/fixtures/1.txt create mode 100644 tests/compress/fixtures/archive.tar create mode 100644 tests/compress/fixtures/archive.tar.bz2 create mode 100644 tests/compress/fixtures/archive.tar.gz create mode 100644 tests/compress/fixtures/archive.zip create mode 100644 tests/compress/fixtures/dir/2.txt create mode 100644 tests/compress/fixtures/dir/3.txt create mode 100644 tests/compress/fixtures/dir/subdir/4.txt diff --git a/tests/compress/archive/.gitkeep b/tests/compress/archive/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/compress/compress_test.php b/tests/compress/compress_test.php new file mode 100644 index 0000000000..a88e5e5b32 --- /dev/null +++ b/tests/compress/compress_test.php @@ -0,0 +1,157 @@ +path = __DIR__ . '/fixtures/'; + $compress = new compress(); + + if (sizeof($compress->methods()) < 4) + { + $this->markTestSkipped('PHP needs to be compiled with --with-zlib and --with-bz2 in order to run these tests'); + } + } + + protected function tearDown() + { + foreach (array(__DIR__ . self::EXTRACT_DIR, __DIR__ . self::ARCHIVE_DIR) as $dir) { + $this->clear_dir($dir); + } + } + + protected function clear_dir($dir) + { + $iterator = new DirectoryIterator($dir); + foreach ($iterator as $fileinfo) + { + $name = $fileinfo->getFilename(); + $path = $fileinfo->getPathname(); + + if ($name[0] !== '.') + { + if ($fileinfo->isDir()) + { + $this->clear_dir($path); + rmdir($path); + } + else + { + unlink($path); + } + } + } + } + + protected function archive_files($compress) + { + $compress->add_file($this->path . '1.txt', $this->path); + $compress->add_file( + 'tests/compress/fixtures/dir/', + 'tests/compress/fixtures/', + '', + // The comma here is not an error, this is a comma-separated list + 'subdir/4.txt,3.txt' + ); + $compress->add_custom_file($this->path . 'dir/3.txt', 'dir/3.txt'); + $compress->add_data(file_get_contents($this->path . 'dir/subdir/4.txt'), 'dir/subdir/4.txt'); + } + + protected function valid_extraction() + { + foreach ($this->filelist as $filename) + { + $path = __DIR__ . self::EXTRACT_DIR . $filename; + $this->assertTrue(file_exists($path)); + + // Check the file's contents is correct + $this->assertEquals(basename($filename, '.txt') . "\n", file_get_contents($path)); + } + } + + public function tar_archive_list() + { + return array( + array('archive.tar', '.tar'), + array('archive.tar.gz', '.tar.gz'), + array('archive.tar.bz2', '.tar.bz2'), + ); + } + + /** + * @dataProvider tar_archive_list + */ + public function test_extract_tar($filename, $type) + { + $compress = new compress_tar('r', $this->path . $filename); + $compress->extract('tests/compress/' . self::EXTRACT_DIR); + $this->valid_extraction(); + } + + public function test_extract_zip() + { + $compress = new compress_zip('r', $this->path . 'archive.zip'); + $compress->extract('tests/compress/' . self::EXTRACT_DIR); + $this->valid_extraction(); + } + + /** + * @depends test_extract_tar + * @dataProvider tar_archive_list + */ + public function test_compress_tar($filename, $type) + { + $tar = __DIR__ . self::ARCHIVE_DIR . $filename; + $compress = new compress_tar('w', $tar); + $this->archive_files($compress); + $compress->close(); + $this->assertTrue(file_exists($tar)); + + $compress->mode = 'r'; + $compress->open(); + $compress->extract('tests/compress/' . self::EXTRACT_DIR); + $this->valid_extraction(); + } + + /** + * @depends test_extract_zip + */ + public function test_compress_zip() + { + $zip = __DIR__ . self::ARCHIVE_DIR . 'archive.zip'; + $compress = new compress_zip('w', $zip); + $this->archive_files($compress); + $compress->close(); + $this->assertTrue(file_exists($zip)); + + $compress = new compress_zip('r', $zip); + $compress->extract('tests/compress/' . self::EXTRACT_DIR); + $this->valid_extraction(); + } +} diff --git a/tests/compress/extract/.gitkeep b/tests/compress/extract/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/compress/fixtures/1.txt b/tests/compress/fixtures/1.txt new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/tests/compress/fixtures/1.txt @@ -0,0 +1 @@ +1 diff --git a/tests/compress/fixtures/archive.tar b/tests/compress/fixtures/archive.tar new file mode 100644 index 0000000000000000000000000000000000000000..54ed56084ed5035d52a6758895e953771a6f0ac5 GIT binary patch literal 10240 zcmeI0&kDmJ48}e0Df$GPQKQeZ(MuV-^bhRumlO)JL)RS?p0kH%WFtrDzs92R z(0>ig&Bn;if1y+v`0M+>p8p)lZvyQ9E3BFT{a2uj5Fh{oAOHd&00JNY0w4eaAOHd& Q00JNY0w4eaAW)yc2QS-gZ2$lO literal 0 HcmV?d00001 diff --git a/tests/compress/fixtures/archive.tar.bz2 b/tests/compress/fixtures/archive.tar.bz2 new file mode 100644 index 0000000000000000000000000000000000000000..04c0eccd743c82a71968aba3d44d4300f317ced8 GIT binary patch literal 224 zcmV<603ZKCT4**gL0KkKS-?;N8UO(*`+~ucKp;Q?|A0b_bSfS|KmY&;FaYyv!b)V- z8VwjDK+&|+5lKzyOhKXQ000Rj(lSgYfrMzp&)x^#h+|IJV&Rlb0vzZIY^{B*Kxi1S zLt`k2xNrw>z=1)o6MU^l(9ml$SydrY6u|~r80Vd$3PDQ2I99|#L}nc_O;M3CN^rol z2=4%3?*)?#ULo9)Ft~x-a;F6ZPPXY71j_-P1c5Z1Q5k_^Eswsu4RcTysaP!qKvWcx a;x5HPqSz!mfEikzU$-z4X0aU-Q0 zf1t~5>3>H5Yl!IosV)Bh plGZV>tN+vgU+_GQ{#TIDe*gdg00000004l$^Z|MERe%O3Z_s1YGQF|63Ac^ka`oKdPXLDX50=1 zY61d