ZipInfoTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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->expectException(
  51. ZipEntryNotFoundException::class
  52. );
  53. $this->expectExceptionMessage(
  54. 'Zip Entry "unknown.name" was not found in the archive.'
  55. );
  56. $zipFile = new ZipFile();
  57. $zipFile->getEntryInfo('unknown.name');
  58. }
  59. /**
  60. * @throws ZipEntryNotFoundException
  61. * @throws ZipException
  62. */
  63. public function testZipInfo()
  64. {
  65. $zipFile = new ZipFile();
  66. $zipFile->openFile(__DIR__ . '/resources/Advanced-v1.0.0.epub');
  67. $entryName = 'META-INF/container.xml';
  68. $zipEntry = $zipFile->getEntry($entryName);
  69. $zipInfo = $zipFile->getEntryInfo($entryName);
  70. $zipFile->close();
  71. self::assertSame($zipInfo->getName(), $zipEntry->getName());
  72. self::assertSame($zipInfo->isFolder(), $zipEntry->isDirectory());
  73. self::assertSame($zipInfo->getSize(), $zipEntry->getUncompressedSize());
  74. self::assertSame($zipInfo->getCompressedSize(), $zipEntry->getCompressedSize());
  75. self::assertSame($zipInfo->getMtime(), $zipEntry->getMTime()->getTimestamp());
  76. self::assertSame(
  77. $zipInfo->getCtime(),
  78. $zipEntry->getCTime() !== null ? $zipEntry->getCTime()->getTimestamp() : null
  79. );
  80. self::assertSame(
  81. $zipInfo->getAtime(),
  82. $zipEntry->getATime() !== null ? $zipEntry->getATime()->getTimestamp() : null
  83. );
  84. self::assertNotEmpty($zipInfo->getAttributes());
  85. self::assertSame($zipInfo->isEncrypted(), $zipEntry->isEncrypted());
  86. self::assertSame($zipInfo->getComment(), $zipEntry->getComment());
  87. self::assertSame($zipInfo->getCrc(), $zipEntry->getCrc());
  88. self::assertSame(
  89. $zipInfo->getMethod(),
  90. ZipCompressionMethod::getCompressionMethodName($zipEntry->getCompressionMethod())
  91. );
  92. self::assertSame(
  93. $zipInfo->getMethodName(),
  94. ZipCompressionMethod::getCompressionMethodName($zipEntry->getCompressionMethod())
  95. );
  96. self::assertSame(
  97. $zipInfo->getEncryptionMethodName(),
  98. ZipEncryptionMethod::getEncryptionMethodName($zipEntry->getEncryptionMethod())
  99. );
  100. self::assertSame($zipInfo->getPlatform(), ZipPlatform::getPlatformName($zipEntry->getExtractedOS()));
  101. self::assertSame(ZipInfo::getPlatformName($zipEntry), ZipPlatform::getPlatformName($zipEntry->getExtractedOS()));
  102. self::assertSame($zipInfo->getVersion(), $zipEntry->getExtractVersion());
  103. self::assertNull($zipInfo->getEncryptionMethod());
  104. self::assertSame($zipInfo->getCompressionLevel(), $zipEntry->getCompressionLevel());
  105. self::assertSame($zipInfo->getCompressionMethod(), $zipEntry->getCompressionMethod());
  106. self::assertNotEmpty($zipInfo->toArray());
  107. }
  108. }