summaryrefslogtreecommitdiff
path: root/vendor/league/flysystem-ziparchive/tests/ZipArchiveTests.php
blob: 634871c7d8d21b8ef9b58bf70fa8f2476d492a0f (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php

use League\Flysystem\Config;
use League\Flysystem\ZipArchive\ZipArchiveAdapter as Zip;

class ZipArchiveTests extends PHPUnit_Framework_TestCase
{
    /**
     * @return Zip[]
     */
    public function zipProvider()
    {
        if (! is_dir($folder = __DIR__.'/files')) {
            mkdir($folder, 0777);
        }

        if (is_file($location = __DIR__.'/files/tester.zip')) {
            unlink($location);
        }

        return [
            [new Zip($location, new ZipArchive())],
        ];
    }

    public function testInstance()
    {
        $adapter = new Zip(__DIR__.'/files/tester.zip', new ZipArchive());
        $this->assertInstanceOf('League\Flysystem\AdapterInterface', $adapter);
    }

    /**
     * @dataProvider zipProvider
     */
    public function testGetArchive(Zip $zip)
    {
        $this->assertInstanceOf('ZipArchive', $zip->getArchive());
    }

    /**
     * @dataProvider zipProvider
     */
    public function testZip(Zip $zip)
    {
        $this->assertCount(0, $zip->listContents());
        $this->assertInternalType('array', $zip->write('file.txt', 'contents', new Config()));
        $this->assertCount(1, $zip->listContents());
        $this->assertInternalType('array', $zip->write('nested/file.txt', 'contents', new Config()));
        $this->assertCount(3, $zip->listContents());
        $zip->setPathPrefix('nested/');
        $this->assertCount(1, $zip->listContents());
        $zip->setPathPrefix('');
        $result = $zip->read('nested/file.txt');
        $this->assertEquals('contents', $result['contents']);
        $zip->update('nested/file.txt', 'new contents', new Config());
        $result = $zip->read('nested/file.txt');
        $this->assertEquals('new contents', $result['contents']);
        $result = $zip->readStream('nested/file.txt');
        $this->assertArrayHasKey('stream', $result);
        $result = $zip->getSize('nested/file.txt');
        $this->assertEquals(12, $result['size']);
        $result = $zip->getTimestamp('nested/file.txt');
        $this->assertInternalType('integer', $result['timestamp']);
        $result = $zip->getMimetype('nested/file.txt');
        $this->assertEquals('text/plain', $result['mimetype']);
        $zip->deleteDir('nested');
        $this->assertCount(1, $zip->listContents());
        $zip->rename('file.txt', 'renamed.txt');
        $this->assertFalse($zip->has('file.txt'));

        $zip->createDir('empty_dir', new Config());
        $this->assertInternalType('array', $zip->getMetadata('empty_dir'));

        $stream = tmpfile();
        fwrite($stream, 'something');
        rewind($stream);
        $zip->writeStream('streamed.txt', $stream, new Config());
        fclose($stream);
        $this->assertInternalType('array', $zip->has('streamed.txt'));

        $stream = tmpfile();
        fwrite($stream, 'something');
        rewind($stream);
        $zip->updateStream('streamed-other.txt', $stream, new Config());
        fclose($stream);
        $this->assertInternalType('array', $zip->has('streamed-other.txt'));
    }

    /**
     * @dataProvider zipProvider
     * @expectedException  LogicException
     */
    public function testWriteStreamFail(Zip $zip)
    {
        $zip->writeStream('file.txt', tmpfile(), new Config(['visibility' => 'private']));
    }

    /**
     * @expectedException LogicException
     * @dataProvider zipProvider
     */
    public function testGetVisibility($zip)
    {
        $zip->getVisibility('path');
    }

    /**
     * @expectedException LogicException
     * @dataProvider zipProvider
     */
    public function testSetVisibility($zip)
    {
        $zip->setVisibility('path', 'public');
    }

    /**
     * @expectedException LogicException
     * @dataProvider zipProvider
     */
    public function testSetVisibilityWrite($zip)
    {
        $zip->write('path', 'contents', new Config(['visibility' => 'private']));
    }

    /**
     * @expectedException  LogicException
     */
    public function testZipOpenFails()
    {
        if (defined('HHVM_VERSION')) {
            $this->markTestSkipped('This test results in a fatal error on HHVM');
        }

        $mock = Mockery::mock('ZipArchive');
        $mock->shouldReceive('open')->andReturn(false);
        $zip = new Zip('location', $mock);
    }

    public function testZipReadWriteFails()
    {
        if (defined('HHVM_VERSION')) {
            $this->markTestSkipped('HHVM does not support mocking of ZipArchive');
        }

        $mock = Mockery::mock('ZipArchive');
        $mock->shouldReceive('open')->andReturn(true);
        $mock->shouldReceive('close')->andReturn(true);
        $mock->shouldReceive('addFromString')->andReturn(false);
        $mock->shouldReceive('getFromName')->andReturn(false);
        $mock->shouldReceive('getStream')->andReturn(false);
        $zip = new Zip('location', $mock);

        $this->assertFalse($zip->write('file', 'contents', new Config()));
        $this->assertFalse($zip->read('file'));
        $this->assertFalse($zip->getMimetype('file'));
        $this->assertFalse($zip->readStream('file'));
    }

    public function testCopy()
    {
        if (defined('HHVM_VERSION')) {
            $this->markTestSkipped('This test results in a fatal error on HHVM');
        }

        $resource = fopen(__DIR__.'/../readme.md', 'r+');
        $mock = Mockery::mock('ZipArchive');
        $mock->shouldReceive('open')->andReturn(true);
        $mock->shouldReceive('close')->andReturn(true);
        $mock->shouldReceive('addFromString')->andReturn(true);
        $mock->shouldReceive('getStream')->andReturn($resource);
        $zip = new Zip('location', $mock);
        $this->assertTrue($zip->copy('old', 'new'));

        // Ensure the resource is closed internally
        $this->assertFalse(is_resource($resource));
    }

    public function testCopyFailed()
    {
        if (defined('HHVM_VERSION')) {
            $this->markTestSkipped('This test results in a fatal error on HHVM');
        }

        $mock = Mockery::mock('ZipArchive');
        $mock->shouldReceive('open')->andReturn(true);
        $mock->shouldReceive('close')->andReturn(true);
        $mock->shouldReceive('getStream')->andReturn(false);
        $zip = new Zip('location', $mock);
        $this->assertFalse($zip->copy('old', 'new'));
    }
}