1
0
mirror of https://github.com/Ne-Lexa/php-zip.git synced 2025-01-17 12:48:28 +01:00
php-zip/tests/ZipStreamOpenTest.php
2021-02-22 13:12:01 +03:00

89 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
/*
* This file is part of the nelexa/zip package.
* (c) Ne-Lexa <https://github.com/Ne-Lexa/php-zip>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
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 $exceptionClass
* @param ?string $exceptionMessage
*
* @throws ZipException
*/
public function testOpenStream($resource, ?string $exceptionClass = null, ?string $exceptionMessage = null): void
{
if ($resource === null || $resource === false) {
static::markTestSkipped('skip resource');
}
if ($exceptionClass !== null) {
$this->expectException($exceptionClass);
$this->expectExceptionMessage($exceptionMessage);
}
static::assertIsResource($resource);
$zipFile = new ZipFile();
$zipFile->openFromStream($resource);
static::assertTrue(fclose($resource));
}
public function provideStreams(): array
{
return [
[@fopen(__DIR__ . '/resources/apk.zip', 'rb'), null, null],
[
@fopen(__DIR__, 'rb'),
InvalidArgumentException::class,
'Directory stream not supported',
],
[$this->getTempResource('php://temp'), null, null],
[$this->getTempResource('php://memory'), null, null],
[
@fopen('https://github.com/Ne-Lexa/php-zip/archive/master.zip', 'rb'),
InvalidArgumentException::class,
'The stream wrapper type "http" is not supported.',
],
];
}
/**
* @return resource
*/
private function getTempResource(string $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;
}
}