aboutsummaryrefslogtreecommitdiff
path: root/tests/AddFileTest.php
blob: d4b5d5a14beed9788efabdc588f8be4940fa7fc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
declare(strict_types = 1);

namespace Pablotron\ZipStream\Tests;

use \PHPUnit\Framework\TestCase;
use \Pablotron\ZipStream\ZipStream;

final class AddFileTest extends BaseTestCase {
  public function testAddFile() : void {
    $this->with_temp_zip(function(ZipStream &$zip) {
      $zip->add_file('hello.txt', 'hello!');
    }, function(string $zip_path) {
      $zip = $this->open_archive($zip_path);

      $this->assertEquals(
        'hello!',
        $zip->getFromName('hello.txt')
      );
    });
  }

  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 $zip_path) use ($comment) {
      $zip = $this->open_archive($zip_path);

      $this->assertEquals(
        $comment,
        $zip->getCommentName('hello.txt')
      );
    });
  }

  public function testAddFileWithUnknownMethod() : void {
    $this->expectException(\Pablotron\ZipStream\UnknownMethodError::class);

    $this->with_temp_zip(function(ZipStream &$zip) {
      $zip->add_file('hello.txt', 'hello!', [
        'method'  => -20,
      ]);
    });
  }

  public function testAddFileTimestamp() : void {
    # get timezone offset
    # $ofs = \DateTimeZone::getOffset(\DateTime::getTimezone());
    $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 ($expected_time) {
      $zip = $this->open_archive($zip_path);
      $st = $zip->statName('hello.txt');

      $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'))
      );
    });
  }
};