1
0
mirror of https://github.com/Ne-Lexa/php-zip.git synced 2025-07-30 20:20:11 +02:00

ZipInfo tests

This commit is contained in:
Ne-Lexa
2020-01-31 13:42:19 +03:00
parent 2235de6b35
commit 820c63c30f
3 changed files with 142 additions and 0 deletions

View File

@@ -142,7 +142,9 @@ class ZipFile implements ZipFileInterface
}
if (!($handle = fopen('php://temp', 'r+b'))) {
// @codeCoverageIgnoreStart
throw new ZipException("Can't open temp stream.");
// @codeCoverageIgnoreEnd
}
fwrite($handle, $data);
rewind($handle);
@@ -459,7 +461,9 @@ class ZipFile implements ZipFileInterface
}
if (!mkdir($dir, $dirMode, true) && !is_dir($dir)) {
// @codeCoverageIgnoreStart
throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir));
// @codeCoverageIgnoreEnd
}
chmod($dir, $dirMode);
}
@@ -495,6 +499,7 @@ class ZipFile implements ZipFileInterface
/** @noinspection PhpUsageOfSilenceOperatorInspection */
if (!($handle = @fopen($file, 'w+b'))) {
// @codeCoverageIgnoreStart
throw new ZipException(
sprintf(
'Cannot extract zip entry %s. File %s cannot open for write.',
@@ -502,6 +507,7 @@ class ZipFile implements ZipFileInterface
$file
)
);
// @codeCoverageIgnoreEnd
}
try {

View File

@@ -2458,4 +2458,22 @@ class ZipFileTest extends ZipTestCase
}
$zipFile->close();
}
public function testNoData()
{
$this->setExpectedException(ZipException::class, 'No data for zip entry file');
$entryName = 'file';
$zipFile = new ZipFile();
try {
$zipFile[$entryName] = '';
$zipEntry = $zipFile->getEntry($entryName);
$zipEntry->setData(null);
$zipFile->getEntryContents($entryName);
} finally {
$zipFile->close();
}
}
}

118
tests/ZipInfoTest.php Normal file
View File

@@ -0,0 +1,118 @@
<?php
namespace PhpZip\Tests;
use PhpZip\Constants\ZipCompressionMethod;
use PhpZip\Constants\ZipEncryptionMethod;
use PhpZip\Constants\ZipPlatform;
use PhpZip\Exception\ZipEntryNotFoundException;
use PhpZip\Exception\ZipException;
use PhpZip\Model\ZipInfo;
use PhpZip\ZipFile;
/**
* Testing the {@see ZipInfo} class.
*
* {@see ZipInfo} is {@deprecated}. Use the {@see ZipEntry} class.
*
* @internal
*
* @small
*/
final class ZipInfoTest extends ZipTestCase
{
public function testZipAllInfo()
{
$zipFile = new ZipFile();
$zipFile['entry'] = 'contents';
$zipFile['entry 2'] = 'contents';
$zipAllInfo = $zipFile->getAllInfo();
$zipFile->close();
self::assertCount(2, $zipAllInfo);
self::assertContainsOnlyInstancesOf(ZipInfo::class, $zipAllInfo);
}
/**
* @throws ZipEntryNotFoundException
* @throws ZipException
*/
public function testZipEntryInfo()
{
$zipFile = new ZipFile();
$zipFile['entry'] = 'contents';
$zipFile['entry 2'] = 'contents';
$zipInfo = $zipFile->getEntryInfo('entry');
$zipFile->close();
self::assertInstanceOf(ZipInfo::class, $zipInfo);
}
/**
* @throws ZipEntryNotFoundException
* @throws ZipException
*/
public function testZipInfoEntryNotFound()
{
$this->setExpectedException(
ZipEntryNotFoundException::class,
'Zip Entry "unknown.name" was not found in the archive.'
);
$zipFile = new ZipFile();
$zipFile->getEntryInfo('unknown.name');
}
/**
* @throws ZipEntryNotFoundException
* @throws ZipException
*/
public function testZipInfo()
{
$zipFile = new ZipFile();
$zipFile->openFile(__DIR__ . '/resources/Advanced-v1.0.0.epub');
$entryName = 'META-INF/container.xml';
$zipEntry = $zipFile->getEntry($entryName);
$zipInfo = $zipFile->getEntryInfo($entryName);
$zipFile->close();
self::assertSame($zipInfo->getName(), $zipEntry->getName());
self::assertSame($zipInfo->isFolder(), $zipEntry->isDirectory());
self::assertSame($zipInfo->getSize(), $zipEntry->getUncompressedSize());
self::assertSame($zipInfo->getCompressedSize(), $zipEntry->getCompressedSize());
self::assertSame($zipInfo->getMtime(), $zipEntry->getMTime()->getTimestamp());
self::assertSame(
$zipInfo->getCtime(),
$zipEntry->getCTime() !== null ? $zipEntry->getCTime()->getTimestamp() : null
);
self::assertSame(
$zipInfo->getAtime(),
$zipEntry->getATime() !== null ? $zipEntry->getATime()->getTimestamp() : null
);
self::assertNotEmpty($zipInfo->getAttributes());
self::assertSame($zipInfo->isEncrypted(), $zipEntry->isEncrypted());
self::assertSame($zipInfo->getComment(), $zipEntry->getComment());
self::assertSame($zipInfo->getCrc(), $zipEntry->getCrc());
self::assertSame(
$zipInfo->getMethod(),
ZipCompressionMethod::getCompressionMethodName($zipEntry->getCompressionMethod())
);
self::assertSame(
$zipInfo->getMethodName(),
ZipCompressionMethod::getCompressionMethodName($zipEntry->getCompressionMethod())
);
self::assertSame(
$zipInfo->getEncryptionMethodName(),
ZipEncryptionMethod::getEncryptionMethodName($zipEntry->getEncryptionMethod())
);
self::assertSame($zipInfo->getPlatform(), ZipPlatform::getPlatformName($zipEntry->getExtractedOS()));
self::assertSame(ZipInfo::getPlatformName($zipEntry), ZipPlatform::getPlatformName($zipEntry->getExtractedOS()));
self::assertSame($zipInfo->getVersion(), $zipEntry->getExtractVersion());
self::assertNull($zipInfo->getEncryptionMethod());
self::assertSame($zipInfo->getCompressionLevel(), $zipEntry->getCompressionLevel());
self::assertSame($zipInfo->getCompressionMethod(), $zipEntry->getCompressionMethod());
self::assertNotEmpty($zipInfo->toArray());
self::assertSame((string) $zipInfo, 'PhpZip\Model\ZipInfo {Name="META-INF/container.xml", Size="249 bytes", Compressed size="169 bytes", Modified time="2019-04-08T14:59:08+00:00", Comment="", Method name="Deflated", Attributes="------", Platform="MS-DOS", Version=20}');
}
}