aboutsummaryrefslogtreecommitdiff
path: root/tests/ArchiveTest.php
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2018-09-02 18:58:32 -0400
committerPaul Duncan <pabs@pablotron.org>2018-09-03 09:54:47 -0400
commite4850cba6fd67a6d4d9d8c969281e57447c17fd9 (patch)
tree29fb04954bdabde80a2208d6fa88c575cc7ed802 /tests/ArchiveTest.php
parentd70e1b519d27e5b0090412c566bee79d57f7838e (diff)
downloadzipstream-php-e4850cba6fd67a6d4d9d8c969281e57447c17fd9.tar.bz2
zipstream-php-e4850cba6fd67a6d4d9d8c969281e57447c17fd9.zip
add tests/ and composer test script
Diffstat (limited to 'tests/ArchiveTest.php')
-rw-r--r--tests/ArchiveTest.php89
1 files changed, 89 insertions, 0 deletions
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 @@
+<?php
+declare(strict_types = 1);
+
+namespace Pablotron\ZipStream\Tests;
+
+use PHPUnit\Framework\TestCase;
+use Pablotron\ZipStream\ZipStream;
+use Pablotron\ZipStream\FileWriter;
+use Pablotron\ZipStream\StreamWriter;
+
+final class ArchiveTest extends BaseTestCase {
+ public function testCreate() : void {
+ $zip = new ZipStream('test.zip');
+
+ $this->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()
+ );
+ });
+ }
+};