2
0

AbstractUnicodeExtraFieldTest.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\Extra\Fields;
  10. use PHPUnit\Framework\TestCase;
  11. use PhpZip\Exception\ZipException;
  12. use PhpZip\Model\Extra\Fields\AbstractUnicodeExtraField;
  13. abstract class AbstractUnicodeExtraFieldTest extends TestCase
  14. {
  15. /**
  16. * @return string|AbstractUnicodeExtraField
  17. *
  18. * @psalm-var class-string<AbstractUnicodeExtraField>
  19. */
  20. abstract protected function getUnicodeExtraFieldClassName();
  21. /**
  22. * @dataProvider provideExtraField
  23. *
  24. * @throws ZipException
  25. */
  26. public function testExtraField(int $crc32, string $unicodePath, string $originalPath, string $binaryData): void
  27. {
  28. $className = $this->getUnicodeExtraFieldClassName();
  29. /** @var AbstractUnicodeExtraField $extraField */
  30. $extraField = new $className($crc32, $unicodePath);
  31. static::assertSame($extraField->getCrc32(), $crc32);
  32. static::assertSame($extraField->getUnicodeValue(), $unicodePath);
  33. static::assertSame(crc32($originalPath), $crc32);
  34. static::assertSame($binaryData, $extraField->packLocalFileData());
  35. static::assertSame($binaryData, $extraField->packCentralDirData());
  36. static::assertEquals($className::unpackLocalFileData($binaryData), $extraField);
  37. static::assertEquals($className::unpackCentralDirData($binaryData), $extraField);
  38. }
  39. abstract public function provideExtraField(): array;
  40. public function testSetter(): void
  41. {
  42. $className = $this->getUnicodeExtraFieldClassName();
  43. $entryName = '11111';
  44. /** @var AbstractUnicodeExtraField $extraField */
  45. $extraField = new $className(crc32($entryName), '22222');
  46. static::assertSame($extraField->getHeaderId(), $className::HEADER_ID);
  47. static::assertSame($extraField->getCrc32(), crc32($entryName));
  48. static::assertSame($extraField->getUnicodeValue(), '22222');
  49. $crc32 = 1234567;
  50. $extraField->setCrc32($crc32);
  51. static::assertSame($extraField->getCrc32(), $crc32);
  52. $extraField->setUnicodeValue('44444');
  53. static::assertSame($extraField->getUnicodeValue(), '44444');
  54. }
  55. /**
  56. * @throws ZipException
  57. */
  58. public function testUnicodeErrorParse(): void
  59. {
  60. $this->expectException(ZipException::class);
  61. $this->expectExceptionMessage('Unicode path extra data must have at least 5 bytes.');
  62. $className = $this->getUnicodeExtraFieldClassName();
  63. $className::unpackLocalFileData('');
  64. }
  65. /**
  66. * @throws ZipException
  67. */
  68. public function testUnknownVersionParse(): void
  69. {
  70. $this->expectException(ZipException::class);
  71. $this->expectExceptionMessage('Unsupported version [2] for Unicode path extra data.');
  72. $className = $this->getUnicodeExtraFieldClassName();
  73. $className::unpackLocalFileData("\x02\x04a\xD28\xC3\xA4\\\xC3\xBC.txt");
  74. }
  75. }