| 1 |
<?php |
| 2 |
|
| 3 |
use League\Flysystem\Config; |
| 4 |
use League\Flysystem\ZipArchive\ZipArchiveAdapter as Zip; |
| 5 |
|
| 6 |
class ZipArchiveTests extends PHPUnit_Framework_TestCase |
| 7 |
{ |
| 8 |
/** |
| 9 |
* @return Zip[] |
| 10 |
*/ |
| 11 |
public function zipProvider() |
| 12 |
{ |
| 13 |
if (! is_dir($folder = __DIR__.'/files')) { |
| 14 |
mkdir($folder, 0777); |
| 15 |
} |
| 16 |
|
| 17 |
if (is_file($location = __DIR__.'/files/tester.zip')) { |
| 18 |
unlink($location); |
| 19 |
} |
| 20 |
|
| 21 |
return [ |
| 22 |
[new Zip($location, new ZipArchive())], |
| 23 |
]; |
| 24 |
} |
| 25 |
|
| 26 |
public function testInstance() |
| 27 |
{ |
| 28 |
$adapter = new Zip(__DIR__.'/files/tester.zip', new ZipArchive()); |
| 29 |
$this->assertInstanceOf('League\Flysystem\AdapterInterface', $adapter); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @dataProvider zipProvider |
| 34 |
*/ |
| 35 |
public function testGetArchive(Zip $zip) |
| 36 |
{ |
| 37 |
$this->assertInstanceOf('ZipArchive', $zip->getArchive()); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @dataProvider zipProvider |
| 42 |
*/ |
| 43 |
public function testZip(Zip $zip) |
| 44 |
{ |
| 45 |
$this->assertCount(0, $zip->listContents()); |
| 46 |
$this->assertInternalType('array', $zip->write('file.txt', 'contents', new Config())); |
| 47 |
$this->assertCount(1, $zip->listContents()); |
| 48 |
$this->assertInternalType('array', $zip->write('nested/file.txt', 'contents', new Config())); |
| 49 |
$this->assertCount(3, $zip->listContents()); |
| 50 |
$zip->setPathPrefix('nested/'); |
| 51 |
$this->assertCount(1, $zip->listContents()); |
| 52 |
$zip->setPathPrefix(''); |
| 53 |
$result = $zip->read('nested/file.txt'); |
| 54 |
$this->assertEquals('contents', $result['contents']); |
| 55 |
$zip->update('nested/file.txt', 'new contents', new Config()); |
| 56 |
$result = $zip->read('nested/file.txt'); |
| 57 |
$this->assertEquals('new contents', $result['contents']); |
| 58 |
$result = $zip->readStream('nested/file.txt'); |
| 59 |
$this->assertArrayHasKey('stream', $result); |
| 60 |
$result = $zip->getSize('nested/file.txt'); |
| 61 |
$this->assertEquals(12, $result['size']); |
| 62 |
$result = $zip->getTimestamp('nested/file.txt'); |
| 63 |
$this->assertInternalType('integer', $result['timestamp']); |
| 64 |
$result = $zip->getMimetype('nested/file.txt'); |
| 65 |
$this->assertEquals('text/plain', $result['mimetype']); |
| 66 |
$zip->deleteDir('nested'); |
| 67 |
$this->assertCount(1, $zip->listContents()); |
| 68 |
$zip->rename('file.txt', 'renamed.txt'); |
| 69 |
$this->assertFalse($zip->has('file.txt')); |
| 70 |
|
| 71 |
$zip->createDir('empty_dir', new Config()); |
| 72 |
$this->assertInternalType('array', $zip->getMetadata('empty_dir')); |
| 73 |
|
| 74 |
$stream = tmpfile(); |
| 75 |
fwrite($stream, 'something'); |
| 76 |
rewind($stream); |
| 77 |
$zip->writeStream('streamed.txt', $stream, new Config()); |
| 78 |
fclose($stream); |
| 79 |
$this->assertInternalType('array', $zip->has('streamed.txt')); |
| 80 |
|
| 81 |
$stream = tmpfile(); |
| 82 |
fwrite($stream, 'something'); |
| 83 |
rewind($stream); |
| 84 |
$zip->updateStream('streamed-other.txt', $stream, new Config()); |
| 85 |
fclose($stream); |
| 86 |
$this->assertInternalType('array', $zip->has('streamed-other.txt')); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* @dataProvider zipProvider |
| 91 |
* @expectedException LogicException |
| 92 |
*/ |
| 93 |
public function testWriteStreamFail(Zip $zip) |
| 94 |
{ |
| 95 |
$zip->writeStream('file.txt', tmpfile(), new Config(['visibility' => 'private'])); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @expectedException LogicException |
| 100 |
* @dataProvider zipProvider |
| 101 |
*/ |
| 102 |
public function testGetVisibility($zip) |
| 103 |
{ |
| 104 |
$zip->getVisibility('path'); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* @expectedException LogicException |
| 109 |
* @dataProvider zipProvider |
| 110 |
*/ |
| 111 |
public function testSetVisibility($zip) |
| 112 |
{ |
| 113 |
$zip->setVisibility('path', 'public'); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* @expectedException LogicException |
| 118 |
* @dataProvider zipProvider |
| 119 |
*/ |
| 120 |
public function testSetVisibilityWrite($zip) |
| 121 |
{ |
| 122 |
$zip->write('path', 'contents', new Config(['visibility' => 'private'])); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* @expectedException LogicException |
| 127 |
*/ |
| 128 |
public function testZipOpenFails() |
| 129 |
{ |
| 130 |
if (defined('HHVM_VERSION')) { |
| 131 |
$this->markTestSkipped('This test results in a fatal error on HHVM'); |
| 132 |
} |
| 133 |
|
| 134 |
$mock = Mockery::mock('ZipArchive'); |
| 135 |
$mock->shouldReceive('open')->andReturn(false); |
| 136 |
$zip = new Zip('location', $mock); |
| 137 |
} |
| 138 |
|
| 139 |
public function testZipReadWriteFails() |
| 140 |
{ |
| 141 |
if (defined('HHVM_VERSION')) { |
| 142 |
$this->markTestSkipped('HHVM does not support mocking of ZipArchive'); |
| 143 |
} |
| 144 |
|
| 145 |
$mock = Mockery::mock('ZipArchive'); |
| 146 |
$mock->shouldReceive('open')->andReturn(true); |
| 147 |
$mock->shouldReceive('close')->andReturn(true); |
| 148 |
$mock->shouldReceive('addFromString')->andReturn(false); |
| 149 |
$mock->shouldReceive('getFromName')->andReturn(false); |
| 150 |
$mock->shouldReceive('getStream')->andReturn(false); |
| 151 |
$zip = new Zip('location', $mock); |
| 152 |
|
| 153 |
$this->assertFalse($zip->write('file', 'contents', new Config())); |
| 154 |
$this->assertFalse($zip->read('file')); |
| 155 |
$this->assertFalse($zip->getMimetype('file')); |
| 156 |
$this->assertFalse($zip->readStream('file')); |
| 157 |
} |
| 158 |
|
| 159 |
public function testCopy() |
| 160 |
{ |
| 161 |
if (defined('HHVM_VERSION')) { |
| 162 |
$this->markTestSkipped('This test results in a fatal error on HHVM'); |
| 163 |
} |
| 164 |
|
| 165 |
$resource = fopen(__DIR__.'/../readme.md', 'r+'); |
| 166 |
$mock = Mockery::mock('ZipArchive'); |
| 167 |
$mock->shouldReceive('open')->andReturn(true); |
| 168 |
$mock->shouldReceive('close')->andReturn(true); |
| 169 |
$mock->shouldReceive('addFromString')->andReturn(true); |
| 170 |
$mock->shouldReceive('getStream')->andReturn($resource); |
| 171 |
$zip = new Zip('location', $mock); |
| 172 |
$this->assertTrue($zip->copy('old', 'new')); |
| 173 |
|
| 174 |
// Ensure the resource is closed internally |
| 175 |
$this->assertFalse(is_resource($resource)); |
| 176 |
} |
| 177 |
|
| 178 |
public function testCopyFailed() |
| 179 |
{ |
| 180 |
if (defined('HHVM_VERSION')) { |
| 181 |
$this->markTestSkipped('This test results in a fatal error on HHVM'); |
| 182 |
} |
| 183 |
|
| 184 |
$mock = Mockery::mock('ZipArchive'); |
| 185 |
$mock->shouldReceive('open')->andReturn(true); |
| 186 |
$mock->shouldReceive('close')->andReturn(true); |
| 187 |
$mock->shouldReceive('getStream')->andReturn(false); |
| 188 |
$zip = new Zip('location', $mock); |
| 189 |
$this->assertFalse($zip->copy('old', 'new')); |
| 190 |
} |
| 191 |
} |
| 192 |
|