ZipInfoTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace PhpZip\Tests;
  3. use PhpZip\Constants\ZipCompressionMethod;
  4. use PhpZip\Constants\ZipEncryptionMethod;
  5. use PhpZip\Constants\ZipPlatform;
  6. use PhpZip\Exception\ZipEntryNotFoundException;
  7. use PhpZip\Exception\ZipException;
  8. use PhpZip\Model\ZipInfo;
  9. use PhpZip\ZipFile;
  10. /**
  11. * Testing the {@see ZipInfo} class.
  12. *
  13. * {@see ZipInfo} is {@deprecated}. Use the {@see ZipEntry} class.
  14. *
  15. * @internal
  16. *
  17. * @small
  18. */
  19. final class ZipInfoTest extends ZipTestCase
  20. {
  21. public function testZipAllInfo()
  22. {
  23. $zipFile = new ZipFile();
  24. $zipFile['entry'] = 'contents';
  25. $zipFile['entry 2'] = 'contents';
  26. $zipAllInfo = $zipFile->getAllInfo();
  27. $zipFile->close();
  28. self::assertCount(2, $zipAllInfo);
  29. self::assertContainsOnlyInstancesOf(ZipInfo::class, $zipAllInfo);
  30. }
  31. /**
  32. * @throws ZipEntryNotFoundException
  33. * @throws ZipException
  34. */
  35. public function testZipEntryInfo()
  36. {
  37. $zipFile = new ZipFile();
  38. $zipFile['entry'] = 'contents';
  39. $zipFile['entry 2'] = 'contents';
  40. $zipInfo = $zipFile->getEntryInfo('entry');
  41. $zipFile->close();
  42. self::assertInstanceOf(ZipInfo::class, $zipInfo);
  43. }
  44. /**
  45. * @throws ZipEntryNotFoundException
  46. * @throws ZipException
  47. */
  48. public function testZipInfoEntryNotFound()
  49. {
  50. $this->setExpectedException(
  51. ZipEntryNotFoundException::class,
  52. 'Zip Entry "unknown.name" was not found in the archive.'
  53. );
  54. $zipFile = new ZipFile();
  55. $zipFile->getEntryInfo('unknown.name');
  56. }
  57. /**
  58. * @throws ZipEntryNotFoundException
  59. * @throws ZipException
  60. */
  61. public function testZipInfo()
  62. {
  63. $zipFile = new ZipFile();
  64. $zipFile->openFile(__DIR__ . '/resources/Advanced-v1.0.0.epub');
  65. $entryName = 'META-INF/container.xml';
  66. $zipEntry = $zipFile->getEntry($entryName);
  67. $zipInfo = $zipFile->getEntryInfo($entryName);
  68. $zipFile->close();
  69. self::assertSame($zipInfo->getName(), $zipEntry->getName());
  70. self::assertSame($zipInfo->isFolder(), $zipEntry->isDirectory());
  71. self::assertSame($zipInfo->getSize(), $zipEntry->getUncompressedSize());
  72. self::assertSame($zipInfo->getCompressedSize(), $zipEntry->getCompressedSize());
  73. self::assertSame($zipInfo->getMtime(), $zipEntry->getMTime()->getTimestamp());
  74. self::assertSame(
  75. $zipInfo->getCtime(),
  76. $zipEntry->getCTime() !== null ? $zipEntry->getCTime()->getTimestamp() : null
  77. );
  78. self::assertSame(
  79. $zipInfo->getAtime(),
  80. $zipEntry->getATime() !== null ? $zipEntry->getATime()->getTimestamp() : null
  81. );
  82. self::assertNotEmpty($zipInfo->getAttributes());
  83. self::assertSame($zipInfo->isEncrypted(), $zipEntry->isEncrypted());
  84. self::assertSame($zipInfo->getComment(), $zipEntry->getComment());
  85. self::assertSame($zipInfo->getCrc(), $zipEntry->getCrc());
  86. self::assertSame(
  87. $zipInfo->getMethod(),
  88. ZipCompressionMethod::getCompressionMethodName($zipEntry->getCompressionMethod())
  89. );
  90. self::assertSame(
  91. $zipInfo->getMethodName(),
  92. ZipCompressionMethod::getCompressionMethodName($zipEntry->getCompressionMethod())
  93. );
  94. self::assertSame(
  95. $zipInfo->getEncryptionMethodName(),
  96. ZipEncryptionMethod::getEncryptionMethodName($zipEntry->getEncryptionMethod())
  97. );
  98. self::assertSame($zipInfo->getPlatform(), ZipPlatform::getPlatformName($zipEntry->getExtractedOS()));
  99. self::assertSame(ZipInfo::getPlatformName($zipEntry), ZipPlatform::getPlatformName($zipEntry->getExtractedOS()));
  100. self::assertSame($zipInfo->getVersion(), $zipEntry->getExtractVersion());
  101. self::assertNull($zipInfo->getEncryptionMethod());
  102. self::assertSame($zipInfo->getCompressionLevel(), $zipEntry->getCompressionLevel());
  103. self::assertSame($zipInfo->getCompressionMethod(), $zipEntry->getCompressionMethod());
  104. self::assertNotEmpty($zipInfo->toArray());
  105. }
  106. }