From 820c63c30fdb2780eb5e1f89b2dda898afa21372 Mon Sep 17 00:00:00 2001 From: Ne-Lexa Date: Fri, 31 Jan 2020 13:42:19 +0300 Subject: [PATCH] ZipInfo tests --- src/ZipFile.php | 6 +++ tests/ZipFileTest.php | 18 +++++++ tests/ZipInfoTest.php | 118 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 tests/ZipInfoTest.php diff --git a/src/ZipFile.php b/src/ZipFile.php index b265a39..7c3d012 100644 --- a/src/ZipFile.php +++ b/src/ZipFile.php @@ -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 { diff --git a/tests/ZipFileTest.php b/tests/ZipFileTest.php index b51ed12..0f36686 100644 --- a/tests/ZipFileTest.php +++ b/tests/ZipFileTest.php @@ -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(); + } + } } diff --git a/tests/ZipInfoTest.php b/tests/ZipInfoTest.php new file mode 100644 index 0000000..67957d6 --- /dev/null +++ b/tests/ZipInfoTest.php @@ -0,0 +1,118 @@ +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}'); + } +}