summaryrefslogtreecommitdiff
path: root/tests/AddFileTest.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/AddFileTest.php
parentd70e1b519d27e5b0090412c566bee79d57f7838e (diff)
downloadzipstream-php-e4850cba6fd67a6d4d9d8c969281e57447c17fd9.tar.bz2
zipstream-php-e4850cba6fd67a6d4d9d8c969281e57447c17fd9.zip
add tests/ and composer test script
Diffstat (limited to 'tests/AddFileTest.php')
-rw-r--r--tests/AddFileTest.php67
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);
+ });
+ }
+};