diff options
Diffstat (limited to 'tests/AddFileTest.php')
-rw-r--r-- | tests/AddFileTest.php | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/AddFileTest.php b/tests/AddFileTest.php new file mode 100644 index 0000000..3ea65e0 --- /dev/null +++ b/tests/AddFileTest.php @@ -0,0 +1,67 @@ +<?php +declare(strict_types = 1); + +namespace Pablotron\ZipStream\Tests; + +use \PHPUnit\Framework\TestCase; +use \Pablotron\ZipStream\ZipStream; + +final class AddFileTest extends BaseTestCase { + public function testCreateFile() : void { + $this->with_temp_zip(function(ZipStream &$zip) { + $zip->add_file('hello.txt', 'hello!'); + }, function(string $path) { + $zip = $this->open_archive($path); + + $this->assertEquals( + 'hello!', + $zip->getFromName('hello.txt') + ); + }); + } + + public function testCreateFileWithComment() : void { + $comment = 'test comment'; + $this->with_temp_zip(function(ZipStream &$zip) use ($comment) { + $zip->add_file('hello.txt', 'hello!', [ + 'comment' => $comment, + ]); + }, function(string $path) use ($comment) { + $zip = $this->open_archive($path); + + $this->assertEquals( + $comment, + $zip->getCommentName('hello.txt') + ); + }); + } + + public function testCreateFileWithUnknownMethod() : void { + $this->expectException(\Pablotron\ZipStream\UnknownMethodError::class); + + $this->with_temp_zip(function(ZipStream &$zip) { + $zip->add_file('hello.txt', 'hello!', [ + 'method' => -20, + ]); + }); + } + + public function testCreateFileTimestamp() : void { + # get timezone offset + # $ofs = \DateTimeZone::getOffset(\DateTime::getTimezone()); + $ofs = 4 * 3600; # hard-coded to EDT for now + + # get time from 2 hours ago (round to even number of seconds) + $time = ((time() - (2 * 3600)) >> 1) << 1; + + $this->with_temp_zip(function(ZipStream &$zip) use ($time) { + $zip->add_file('hello.txt', 'hello!', [ + 'time' => $time, + ]); + }, function($zip_path) use ($time, $ofs) { + $zip = $this->open_archive($zip_path); + $st = $zip->statName('hello.txt'); + $this->assertEquals($time, $st['mtime'] - $ofs); + }); + } +}; |