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/AddFileTest.php | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tests/AddFileTest.php (limited to 'tests/AddFileTest.php') 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 @@ +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); + }); + } +}; -- cgit v1.2.3