CustomZipFormatTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the nelexa/zip package.
  5. * (c) Ne-Lexa <https://github.com/Ne-Lexa/php-zip>
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace PhpZip\Tests;
  10. use PhpZip\Exception\ZipEntryNotFoundException;
  11. use PhpZip\Exception\ZipException;
  12. use PhpZip\Model\Extra\Fields\NewUnixExtraField;
  13. use PhpZip\Model\Extra\Fields\NtfsExtraField;
  14. use PhpZip\Tests\Internal\CustomZip\ZipFileCustomWriter;
  15. use PhpZip\Tests\Internal\CustomZip\ZipFileWithBeforeSave;
  16. use PhpZip\Tests\Internal\Epub\EpubFile;
  17. use PhpZip\ZipFile;
  18. /**
  19. * Checks the ability to create own file-type class, reader, writer and container.
  20. **.
  21. *
  22. * @internal
  23. *
  24. * @small
  25. */
  26. final class CustomZipFormatTest extends ZipTestCase
  27. {
  28. /**
  29. * @throws ZipException
  30. *
  31. * @see http://www.epubtest.org/test-books source epub files
  32. */
  33. public function testEpub(): void
  34. {
  35. $epubFile = new EpubFile();
  36. $epubFile->openFile(__DIR__ . '/resources/Advanced-v1.0.0.epub');
  37. self::assertSame($epubFile->getRootFile(), 'EPUB/package.opf');
  38. self::assertSame($epubFile->getMimeType(), 'application/epub+zip');
  39. $epubInfo = $epubFile->getEpubInfo();
  40. self::assertSame($epubInfo->toArray(), [
  41. 'title' => 'Advanced Accessibility Tests: Extended Descriptions',
  42. 'creator' => 'DAISY Consortium Transition to EPUB 3 and DIAGRAM Standards WG',
  43. 'language' => 'en-US',
  44. 'publisher' => 'DAISY Consortium and DIAGRAM Center',
  45. 'description' => 'Tests for accessible extended descriptions of images in EPUBs',
  46. 'rights' => 'This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike (CC BY-NC-SA) license.',
  47. 'date' => '2019-01-03',
  48. 'subject' => 'extended-descriptions',
  49. ]);
  50. $epubFile->deleteFromName('mimetype');
  51. self::assertFalse($epubFile->hasEntry('mimetype'));
  52. try {
  53. $epubFile->getMimeType();
  54. self::fail('deleted mimetype');
  55. } catch (ZipEntryNotFoundException $e) {
  56. self::assertSame('Zip Entry "mimetype" was not found in the archive.', $e->getMessage());
  57. }
  58. $epubFile->saveAsFile($this->outputFilename);
  59. self::assertFalse($epubFile->hasEntry('mimetype'));
  60. $epubFile->close();
  61. self::assertCorrectZipArchive($this->outputFilename);
  62. $epubFile->openFile($this->outputFilename);
  63. // file appended in EpubWriter before write
  64. self::assertTrue($epubFile->hasEntry('mimetype'));
  65. $epubFile->close();
  66. }
  67. /**
  68. * @throws \Exception
  69. */
  70. public function testBeforeSaveInZipWriter(): void
  71. {
  72. $zipFile = new ZipFileCustomWriter();
  73. for ($i = 0; $i < 10; $i++) {
  74. $zipFile['file ' . $i] = 'contents file ' . $i;
  75. }
  76. $this->existsExtraFields($zipFile, false);
  77. $zipFile->saveAsFile($this->outputFilename);
  78. $this->existsExtraFields($zipFile, false);
  79. $zipFile->close();
  80. self::assertCorrectZipArchive($this->outputFilename);
  81. $zipFile->openFile($this->outputFilename);
  82. $this->existsExtraFields($zipFile, true);
  83. $zipFile->close();
  84. }
  85. /**
  86. * @throws \Exception
  87. */
  88. public function testBeforeSaveInZipFile(): void
  89. {
  90. $zipFile = new ZipFileWithBeforeSave();
  91. for ($i = 0; $i < 10; $i++) {
  92. $zipFile['file ' . $i] = 'contents file ' . $i;
  93. }
  94. $this->existsExtraFields($zipFile, false);
  95. $zipFile->saveAsFile($this->outputFilename);
  96. $this->existsExtraFields($zipFile, true);
  97. $zipFile->close();
  98. self::assertCorrectZipArchive($this->outputFilename);
  99. $zipFile->openFile($this->outputFilename);
  100. $this->existsExtraFields($zipFile, true);
  101. $zipFile->close();
  102. }
  103. private function existsExtraFields(ZipFile $zipFile, bool $exists): void
  104. {
  105. foreach ($zipFile->getEntries() as $entry) {
  106. $localExtras = $entry->getLocalExtraFields();
  107. $cdExtras = $entry->getCdExtraFields();
  108. self::assertSame(isset($localExtras[NtfsExtraField::HEADER_ID]), $exists);
  109. self::assertSame(isset($cdExtras[NtfsExtraField::HEADER_ID]), $exists);
  110. self::assertSame(isset($localExtras[NewUnixExtraField::HEADER_ID]), $exists);
  111. self::assertSame(isset($cdExtras[NewUnixExtraField::HEADER_ID]), $exists);
  112. }
  113. }
  114. }