OldUnixExtraFieldTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\Model\Extra\Fields\OldUnixExtraField;
  12. /**
  13. * Class OldUnixExtraFieldTest.
  14. *
  15. * @internal
  16. *
  17. * @small
  18. */
  19. final class OldUnixExtraFieldTest extends TestCase
  20. {
  21. /**
  22. * @dataProvider provideExtraField
  23. *
  24. * @noinspection PhpTooManyParametersInspection
  25. *
  26. * @param ?int $accessTime
  27. * @param ?int $modifyTime
  28. * @param ?int $uid
  29. * @param ?int $gid
  30. *
  31. * @throws \Exception
  32. */
  33. public function testExtraField(
  34. ?int $accessTime,
  35. ?int $modifyTime,
  36. ?int $uid,
  37. ?int $gid,
  38. string $localBinaryData,
  39. string $cdBinaryData
  40. ): void {
  41. $extraField = new OldUnixExtraField($accessTime, $modifyTime, $uid, $gid);
  42. self::assertSame($extraField->getHeaderId(), OldUnixExtraField::HEADER_ID);
  43. self::assertSame($extraField->getAccessTime(), $accessTime);
  44. self::assertSame($extraField->getModifyTime(), $modifyTime);
  45. self::assertSame($extraField->getUid(), $uid);
  46. self::assertSame($extraField->getGid(), $gid);
  47. if ($extraField->getModifyTime() !== null) {
  48. self::assertEquals(
  49. new \DateTimeImmutable('@' . $extraField->getModifyTime()),
  50. $extraField->getModifyDateTime()
  51. );
  52. }
  53. if ($extraField->getAccessTime() !== null) {
  54. self::assertEquals(
  55. new \DateTimeImmutable('@' . $extraField->getAccessTime()),
  56. $extraField->getAccessDateTime()
  57. );
  58. }
  59. self::assertEquals(OldUnixExtraField::unpackLocalFileData($localBinaryData), $extraField);
  60. self::assertSame($extraField->packLocalFileData(), $localBinaryData);
  61. $uid = null;
  62. $gid = null;
  63. $extraField = new OldUnixExtraField($accessTime, $modifyTime, $uid, $gid);
  64. self::assertSame($extraField->getHeaderId(), OldUnixExtraField::HEADER_ID);
  65. self::assertSame($extraField->getAccessTime(), $accessTime);
  66. self::assertSame($extraField->getModifyTime(), $modifyTime);
  67. self::assertNull($extraField->getUid());
  68. self::assertNull($extraField->getGid());
  69. if ($extraField->getModifyTime() !== null) {
  70. self::assertEquals(
  71. new \DateTimeImmutable('@' . $extraField->getModifyTime()),
  72. $extraField->getModifyDateTime()
  73. );
  74. }
  75. if ($extraField->getAccessTime() !== null) {
  76. self::assertEquals(
  77. new \DateTimeImmutable('@' . $extraField->getAccessTime()),
  78. $extraField->getAccessDateTime()
  79. );
  80. }
  81. self::assertEquals(OldUnixExtraField::unpackCentralDirData($cdBinaryData), $extraField);
  82. self::assertSame($extraField->packCentralDirData(), $cdBinaryData);
  83. }
  84. public function provideExtraField(): array
  85. {
  86. return [
  87. [
  88. 1213373265,
  89. 1213365834,
  90. 502,
  91. 502,
  92. "Q\x9BRHJ~RH\xF6\x01\xF6\x01",
  93. "Q\x9BRHJ~RH",
  94. ],
  95. [
  96. 935520420,
  97. 935520401,
  98. 501,
  99. 100,
  100. "\xA4\xE8\xC27\x91\xE8\xC27\xF5\x01d\x00",
  101. "\xA4\xE8\xC27\x91\xE8\xC27",
  102. ],
  103. [
  104. 1402666135,
  105. 1402666135,
  106. 501,
  107. 20,
  108. "\x97\xFC\x9AS\x97\xFC\x9AS\xF5\x01\x14\x00",
  109. "\x97\xFC\x9AS\x97\xFC\x9AS",
  110. ],
  111. [
  112. null,
  113. null,
  114. null,
  115. null,
  116. '',
  117. '',
  118. ],
  119. ];
  120. }
  121. public function testSetter(): void
  122. {
  123. $extraField = new OldUnixExtraField(null, null, null, null);
  124. self::assertNull($extraField->getAccessTime());
  125. self::assertNull($extraField->getAccessDateTime());
  126. self::assertNull($extraField->getModifyTime());
  127. self::assertNull($extraField->getModifyDateTime());
  128. self::assertNull($extraField->getUid());
  129. self::assertNull($extraField->getGid());
  130. $extraField->setModifyTime(1402666135);
  131. self::assertSame($extraField->getModifyTime(), 1402666135);
  132. $extraField->setAccessTime(1213365834);
  133. self::assertSame($extraField->getAccessTime(), 1213365834);
  134. $extraField->setUid(500);
  135. self::assertSame($extraField->getUid(), 500);
  136. $extraField->setGid(100);
  137. self::assertSame($extraField->getGid(), 100);
  138. }
  139. }