123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of the nelexa/zip package.
- * (c) Ne-Lexa <https://github.com/Ne-Lexa/php-zip>
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace PhpZip\Tests;
- use PhpZip\Exception\ZipEntryNotFoundException;
- use PhpZip\Exception\ZipException;
- use PhpZip\Model\Extra\Fields\NewUnixExtraField;
- use PhpZip\Model\Extra\Fields\NtfsExtraField;
- use PhpZip\Tests\Internal\CustomZip\ZipFileCustomWriter;
- use PhpZip\Tests\Internal\CustomZip\ZipFileWithBeforeSave;
- use PhpZip\Tests\Internal\Epub\EpubFile;
- use PhpZip\ZipFile;
- /**
- * Checks the ability to create own file-type class, reader, writer and container.
- **.
- *
- * @internal
- *
- * @small
- */
- final class CustomZipFormatTest extends ZipTestCase
- {
- /**
- * @throws ZipException
- *
- * @see http://www.epubtest.org/test-books source epub files
- */
- public function testEpub(): void
- {
- $epubFile = new EpubFile();
- $epubFile->openFile(__DIR__ . '/resources/Advanced-v1.0.0.epub');
- self::assertSame($epubFile->getRootFile(), 'EPUB/package.opf');
- self::assertSame($epubFile->getMimeType(), 'application/epub+zip');
- $epubInfo = $epubFile->getEpubInfo();
- self::assertSame($epubInfo->toArray(), [
- 'title' => 'Advanced Accessibility Tests: Extended Descriptions',
- 'creator' => 'DAISY Consortium Transition to EPUB 3 and DIAGRAM Standards WG',
- 'language' => 'en-US',
- 'publisher' => 'DAISY Consortium and DIAGRAM Center',
- 'description' => 'Tests for accessible extended descriptions of images in EPUBs',
- 'rights' => 'This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike (CC BY-NC-SA) license.',
- 'date' => '2019-01-03',
- 'subject' => 'extended-descriptions',
- ]);
- $epubFile->deleteFromName('mimetype');
- self::assertFalse($epubFile->hasEntry('mimetype'));
- try {
- $epubFile->getMimeType();
- self::fail('deleted mimetype');
- } catch (ZipEntryNotFoundException $e) {
- self::assertSame('Zip Entry "mimetype" was not found in the archive.', $e->getMessage());
- }
- $epubFile->saveAsFile($this->outputFilename);
- self::assertFalse($epubFile->hasEntry('mimetype'));
- $epubFile->close();
- self::assertCorrectZipArchive($this->outputFilename);
- $epubFile->openFile($this->outputFilename);
- // file appended in EpubWriter before write
- self::assertTrue($epubFile->hasEntry('mimetype'));
- $epubFile->close();
- }
- /**
- * @throws \Exception
- */
- public function testBeforeSaveInZipWriter(): void
- {
- $zipFile = new ZipFileCustomWriter();
- for ($i = 0; $i < 10; $i++) {
- $zipFile['file ' . $i] = 'contents file ' . $i;
- }
- $this->existsExtraFields($zipFile, false);
- $zipFile->saveAsFile($this->outputFilename);
- $this->existsExtraFields($zipFile, false);
- $zipFile->close();
- self::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- $this->existsExtraFields($zipFile, true);
- $zipFile->close();
- }
- /**
- * @throws \Exception
- */
- public function testBeforeSaveInZipFile(): void
- {
- $zipFile = new ZipFileWithBeforeSave();
- for ($i = 0; $i < 10; $i++) {
- $zipFile['file ' . $i] = 'contents file ' . $i;
- }
- $this->existsExtraFields($zipFile, false);
- $zipFile->saveAsFile($this->outputFilename);
- $this->existsExtraFields($zipFile, true);
- $zipFile->close();
- self::assertCorrectZipArchive($this->outputFilename);
- $zipFile->openFile($this->outputFilename);
- $this->existsExtraFields($zipFile, true);
- $zipFile->close();
- }
- private function existsExtraFields(ZipFile $zipFile, bool $exists): void
- {
- foreach ($zipFile->getEntries() as $entry) {
- $localExtras = $entry->getLocalExtraFields();
- $cdExtras = $entry->getCdExtraFields();
- self::assertSame(isset($localExtras[NtfsExtraField::HEADER_ID]), $exists);
- self::assertSame(isset($cdExtras[NtfsExtraField::HEADER_ID]), $exists);
- self::assertSame(isset($localExtras[NewUnixExtraField::HEADER_ID]), $exists);
- self::assertSame(isset($cdExtras[NewUnixExtraField::HEADER_ID]), $exists);
- }
- }
- }
|