mirror of
https://github.com/Ne-Lexa/php-zip.git
synced 2025-08-06 15:36:28 +02:00
solving problems with lack of memory, the new ZipReader and ZipWriter class, adds .phpstorm.meta.php
#13 #16 #27 #31 #41
This commit is contained in:
112
tests/ZipStreamOpenTest.php
Normal file
112
tests/ZipStreamOpenTest.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace PhpZip\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PhpZip\Exception\InvalidArgumentException;
|
||||
use PhpZip\Exception\ZipException;
|
||||
use PhpZip\ZipFile;
|
||||
|
||||
/**
|
||||
* Class ZipStreamOpenTest.
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @medium
|
||||
*/
|
||||
class ZipStreamOpenTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideStreams
|
||||
*
|
||||
* @param resource $resource
|
||||
* @param string|null $exceptionClass
|
||||
* @param string|null $exceptionMessage
|
||||
*
|
||||
* @throws ZipException
|
||||
*/
|
||||
public function testOpenStream($resource, $exceptionClass = null, $exceptionMessage = null)
|
||||
{
|
||||
if ($resource === null) {
|
||||
static::markTestSkipped('skip null resource');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($exceptionClass !== null) {
|
||||
$this->setExpectedException(
|
||||
$exceptionClass,
|
||||
$exceptionMessage
|
||||
);
|
||||
}
|
||||
|
||||
static::assertInternalType('resource', $resource);
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->openFromStream($resource);
|
||||
|
||||
static::assertTrue(fclose($resource));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|\Generator
|
||||
*/
|
||||
public function provideStreams()
|
||||
{
|
||||
return [
|
||||
'file' => yield [fopen(__DIR__ . '/resources/apk.zip', 'rb'), null, null],
|
||||
'directory' => yield [
|
||||
fopen(__DIR__, 'rb'),
|
||||
InvalidArgumentException::class,
|
||||
'Directory stream not supported',
|
||||
],
|
||||
'temp' => yield [$this->getTempResource('php://temp'), null, null],
|
||||
'memory' => yield [$this->getTempResource('php://memory'), null, null],
|
||||
'bz' => yield [
|
||||
$this->getBzResource(),
|
||||
InvalidArgumentException::class,
|
||||
'The stream wrapper type "Unknown" is not supported.',
|
||||
],
|
||||
'url' => yield [
|
||||
fopen('https://github.com/Ne-Lexa/php-zip/archive/master.zip', 'rb'),
|
||||
InvalidArgumentException::class,
|
||||
'The stream wrapper type "http" is not supported.',
|
||||
],
|
||||
'ftp' => yield [
|
||||
fopen('ftp://ftp.ripe.net/pub/stats/ripencc/delegated-ripencc-latest.md5', 'rb'),
|
||||
InvalidArgumentException::class,
|
||||
'The stream wrapper type "ftp" is not supported.',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filename
|
||||
*
|
||||
* @return resource
|
||||
*/
|
||||
private function getTempResource($filename)
|
||||
{
|
||||
$fp = fopen(__DIR__ . '/resources/apk.zip', 'rb');
|
||||
$stream = fopen($filename, 'r+b');
|
||||
stream_copy_to_stream($fp, $stream);
|
||||
fclose($fp);
|
||||
rewind($stream);
|
||||
|
||||
return $stream;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return resource|null
|
||||
*/
|
||||
private function getBzResource()
|
||||
{
|
||||
if (!\extension_loaded('bz2')) {
|
||||
return null;
|
||||
}
|
||||
$stream = bzopen('php://temp', 'w');
|
||||
bzwrite($stream, 'some input here');
|
||||
|
||||
return $stream;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user