From e4850cba6fd67a6d4d9d8c969281e57447c17fd9 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sun, 2 Sep 2018 18:58:32 -0400 Subject: add tests/ and composer test script --- tests/ArchiveTest.php | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tests/ArchiveTest.php (limited to 'tests/ArchiveTest.php') diff --git a/tests/ArchiveTest.php b/tests/ArchiveTest.php new file mode 100644 index 0000000..cbac0f1 --- /dev/null +++ b/tests/ArchiveTest.php @@ -0,0 +1,89 @@ +assertInstanceOf( + ZipStream::class, + $zip + ); + } + + public function testFileWriter() { + $this->with_temp_file(function(string $dst_path) { + ZipStream::send($dst_path, function(ZipStream &$zip) { + $zip->add_file('hello.txt', 'hello!'); + }, [ + 'output' => new FileWriter(), + ]); + + # open archive + $zip = $this->open_archive($dst_path); + + # read hello.txt, check text + $this->assertEquals( + 'hello!', + $zip->getFromName('hello.txt') + ); + }); + } + + public function testStreamWriter() { + $this->with_temp_file(function(string $dst_path) { + $fh = fopen($dst_path, 'wb'); + if ($fh === false) { + throw new Exception("fopen() failed"); + } + + ZipStream::send($dst_path, function(ZipStream &$zip) { + $zip->add_file('hello.txt', 'hello!'); + }, [ + 'output' => new StreamWriter($fh), + ]); + + # close stream + fclose($fh); + + # open archive + $zip = $this->open_archive($dst_path); + + # read hello.txt, check text + $this->assertEquals( + 'hello!', + $zip->getFromName('hello.txt') + ); + }); + } + + public function testArchiveComment() : void { + $this->with_temp_file(function($dst_path) { + $comment = 'test archive comment'; + + # write archive + ZipStream::send($dst_path, function(ZipStream &$zip) { + $zip->add_file('hello.txt', 'hello!'); + }, [ + 'comment' => $comment, + 'output' => new FileWriter(), + ]); + + # open archive + $zip = $this->open_archive($dst_path); + + # read hello.txt, check text + $this->assertEquals( + $comment, + $zip->getArchiveComment() + ); + }); + } +}; -- cgit v1.2.3