aboutsummaryrefslogtreecommitdiff
path: root/tests/AddFileTest.php
blob: 3ea65e0f3e1cc234f540ad5fcc4ea0cf16778012 (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
<?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);
    });
  }
};