summaryrefslogtreecommitdiff
path: root/tests/AddFileTest.php
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2018-09-03 06:50:31 -0400
committerPaul Duncan <pabs@pablotron.org>2018-09-03 09:54:52 -0400
commit5dda6b1011472e558e3fd9c744c2fd537528b115 (patch)
treec8dd03943dbcda0a790196414e5f1c9d3cec5111 /tests/AddFileTest.php
parenta9a5749e6ecbd25ce220daa5a02aec4ba222ccd5 (diff)
downloadzipstream-php-5dda6b1011472e558e3fd9c744c2fd537528b115.tar.bz2
zipstream-php-5dda6b1011472e558e3fd9c744c2fd537528b115.zip
add CommentError, Methods::check(), and several additional tests
Diffstat (limited to 'tests/AddFileTest.php')
-rw-r--r--tests/AddFileTest.php127
1 files changed, 116 insertions, 11 deletions
diff --git a/tests/AddFileTest.php b/tests/AddFileTest.php
index 3ea65e0..d4b5d5a 100644
--- a/tests/AddFileTest.php
+++ b/tests/AddFileTest.php
@@ -7,11 +7,11 @@ use \PHPUnit\Framework\TestCase;
use \Pablotron\ZipStream\ZipStream;
final class AddFileTest extends BaseTestCase {
- public function testCreateFile() : void {
+ public function testAddFile() : void {
$this->with_temp_zip(function(ZipStream &$zip) {
$zip->add_file('hello.txt', 'hello!');
- }, function(string $path) {
- $zip = $this->open_archive($path);
+ }, function(string $zip_path) {
+ $zip = $this->open_archive($zip_path);
$this->assertEquals(
'hello!',
@@ -20,14 +20,57 @@ final class AddFileTest extends BaseTestCase {
});
}
- public function testCreateFileWithComment() : void {
+ public function testAddFileFromPath() : void {
+ $this->with_temp_zip(function(ZipStream &$zip) {
+ $zip->add_file_from_path('test.php', __FILE__);
+ }, function(string $zip_path) {
+ $zip = $this->open_archive($zip_path);
+
+ $this->assertEquals(
+ sha1(file_get_contents(__FILE__)),
+ sha1($zip->getFromName('test.php'))
+ );
+ });
+ }
+
+ public function testAddStream() : void {
+ $this->with_temp_zip(function(ZipStream &$zip) {
+ $fh = fopen(__FILE__, 'rb');
+ $zip->add_stream('test.php', $fh);
+ fclose($fh);
+ }, function(string $zip_path) {
+ $zip = $this->open_archive($zip_path);
+
+ $this->assertEquals(
+ sha1(file_get_contents(__FILE__)),
+ sha1($zip->getFromName('test.php'))
+ );
+ });
+ }
+
+ public function testAdd() : void {
+ $this->with_temp_zip(function(ZipStream &$zip) {
+ $zip->add('test.php', function(&$e) {
+ $e->write(file_get_contents(__FILE__));
+ });
+ }, function(string $zip_path) {
+ $zip = $this->open_archive($zip_path);
+
+ $this->assertEquals(
+ sha1(file_get_contents(__FILE__)),
+ sha1($zip->getFromName('test.php'))
+ );
+ });
+ }
+
+ public function testAddFileWithComment() : 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);
+ }, function(string $zip_path) use ($comment) {
+ $zip = $this->open_archive($zip_path);
$this->assertEquals(
$comment,
@@ -36,7 +79,7 @@ final class AddFileTest extends BaseTestCase {
});
}
- public function testCreateFileWithUnknownMethod() : void {
+ public function testAddFileWithUnknownMethod() : void {
$this->expectException(\Pablotron\ZipStream\UnknownMethodError::class);
$this->with_temp_zip(function(ZipStream &$zip) {
@@ -46,22 +89,84 @@ final class AddFileTest extends BaseTestCase {
});
}
- public function testCreateFileTimestamp() : void {
+ public function testAddFileTimestamp() : void {
# get timezone offset
# $ofs = \DateTimeZone::getOffset(\DateTime::getTimezone());
- $ofs = 4 * 3600; # hard-coded to EDT for now
+ $ofs = 4 * 3600; # FIXME: hard-coded to EDT for now
# get time from 2 hours ago (round to even number of seconds)
$time = ((time() - (2 * 3600)) >> 1) << 1;
+ # get test time
+ $expected_time = $time + $ofs;
+
$this->with_temp_zip(function(ZipStream &$zip) use ($time) {
$zip->add_file('hello.txt', 'hello!', [
'time' => $time,
]);
- }, function($zip_path) use ($time, $ofs) {
+ }, function($zip_path) use ($expected_time) {
$zip = $this->open_archive($zip_path);
$st = $zip->statName('hello.txt');
- $this->assertEquals($time, $st['mtime'] - $ofs);
+
+ $this->assertEquals($expected_time, $st['mtime']);
+ });
+ }
+
+ public function testAddFileCRC() : void {
+ $data = 'hello!';
+
+ # calculate crc32b of file data
+ $hash = hash('crc32b', $data, true);
+
+ # pack expected crc as integer
+ $expected_crc = (
+ (ord($hash[0]) << 24) |
+ (ord($hash[1]) << 16) |
+ (ord($hash[2]) << 8) |
+ (ord($hash[3]))
+ );
+
+ $this->with_temp_zip(function(ZipStream &$zip) use ($data) {
+ $zip->add_file('hello.txt', $data);
+ }, function($zip_path) use ($expected_crc) {
+ $zip = $this->open_archive($zip_path);
+ $st = $zip->statName('hello.txt');
+
+ $this->assertEquals($expected_crc, $st['crc']);
+ });
+ }
+
+ public function testAddFileWithMethodStore() : void {
+ $data = file_get_contents(__FILE__);
+
+ $this->with_temp_zip(function(ZipStream &$zip) use ($data) {
+ $zip->add_file('test.php', $data, [
+ 'method' => \Pablotron\ZipStream\Methods::STORE,
+ ]);
+ }, function($zip_path) use ($data) {
+ $zip = $this->open_archive($zip_path);
+
+ $this->assertEquals(
+ sha1($data),
+ sha1($zip->getFromName('test.php'))
+ );
+ });
+ }
+
+ public function testAddFileWithMethodDeflate() : void {
+ $data = file_get_contents(__FILE__);
+
+ $this->with_temp_zip(function(ZipStream &$zip) use ($data) {
+ $zip->add_file('test.php', $data, [
+ 'method' => \Pablotron\ZipStream\Methods::DEFLATE,
+ ]);
+ }, function($zip_path) use ($data) {
+ $zip = $this->open_archive($zip_path);
+
+ $this->assertEquals(
+ sha1($data),
+ sha1($zip->getFromName('test.php'))
+ );
});
}
};