summaryrefslogtreecommitdiff
path: root/tests/ArchiveTest.php
blob: 8345ee4c89e420406f911f927cdd6f81d05ba7b5 (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
<?php
declare(strict_types = 1);

namespace Pablotron\ZipStream\Tests;

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

final class ArchiveTest extends BaseTestCase {
  public function testCreate() : void {
    $zip = new ZipStream('test.zip');

    $this->assertInstanceOf(
      ZipStream::class,
      $zip
    );
  }

  public function testFileWriter() {
    $this->with_temp_file(function(string $zip_path) {
      ZipStream::send($zip_path, function(ZipStream &$zip) {
        $zip->add_file('hello.txt', 'hello!');
      }, [
        'output' => new FileWriter(),
      ]);

      # open archive
      $zip = $this->open_archive($zip_path);

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

  public function testStreamWriter() {
    $this->with_temp_file(function(string $zip_path) {
      $fh = fopen($zip_path, 'wb');
      if ($fh === false) {
        throw new Exception("fopen() failed");
      }

      ZipStream::send($zip_path, function(ZipStream &$zip) {
        $zip->add_file('hello.txt', 'hello!');
      }, [
        'output' => new StreamWriter($fh),
      ]);

      # close stream
      fclose($fh);

      # open archive
      $zip = $this->open_archive($zip_path);

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

  public function testArchiveComment() : void {
    $this->with_temp_file(function($zip_path) {
      $comment = 'test archive comment';

      # write archive
      ZipStream::send($zip_path, function(ZipStream &$zip) {
        $zip->add_file('hello.txt', 'hello!');
      }, [
        'comment' => $comment,
        'output'  => new FileWriter(),
      ]);

      # open archive
      $zip = $this->open_archive($zip_path);

      # read hello.txt, check text
      $this->assertEquals(
        $comment,
        $zip->getArchiveComment()
      );
    });
  }

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

    $this->with_temp_file(function($zip_path) {
      $comment = str_repeat('x', 0xFFFF);

      # write archive
      ZipStream::send($zip_path, function(ZipStream &$zip) {
        $zip->add_file('hello.txt', 'hello!');
      }, [
        'comment' => $comment,
        'output'  => new FileWriter(),
      ]);
    });
  }

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

    $this->with_temp_file(function($zip_path) {
      # write archive with invalid writer
      ZipStream::send($zip_path, function(ZipStream &$zip) {
        $zip->add_file('hello.txt', 'hello!');
      }, [
        'output' => 'bad writer',
      ]);
    });
  }

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

    $this->with_temp_file(function($zip_path) {
      # write archive with invalid compression method
      ZipStream::send($zip_path, function(ZipStream &$zip) {
        $zip->add_file('hello.txt', 'hello!');
      }, [
        'method'  => 100,
        'output'  => new FileWriter(),
      ]);
    });
  }
};