ExtendedTimestampExtraFieldTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\ExtendedTimestampExtraField;
  12. /**
  13. * Class ExtendedTimestampExtraFieldTest.
  14. *
  15. * @internal
  16. *
  17. * @small
  18. */
  19. final class ExtendedTimestampExtraFieldTest extends TestCase
  20. {
  21. /**
  22. * @dataProvider provideExtraField
  23. *
  24. * @noinspection PhpTooManyParametersInspection
  25. *
  26. * @param ?int $modifyTime
  27. * @param ?int $accessTime
  28. * @param ?int $createTime
  29. */
  30. public function testExtraField(
  31. int $flags,
  32. ?int $modifyTime,
  33. ?int $accessTime,
  34. ?int $createTime,
  35. string $localData,
  36. string $cdData
  37. ): void {
  38. $localExtraField = new ExtendedTimestampExtraField($flags, $modifyTime, $accessTime, $createTime);
  39. self::assertSame($localExtraField->getHeaderId(), ExtendedTimestampExtraField::HEADER_ID);
  40. self::assertSame($localExtraField->getFlags(), $flags);
  41. self::assertSame($localExtraField->getModifyTime(), $modifyTime);
  42. self::assertSame($localExtraField->getAccessTime(), $accessTime);
  43. self::assertSame($localExtraField->getCreateTime(), $createTime);
  44. self::assertSame($localExtraField->packLocalFileData(), $localData);
  45. self::assertEquals(ExtendedTimestampExtraField::unpackLocalFileData($localData), $localExtraField);
  46. $extTimeField = ExtendedTimestampExtraField::create($modifyTime, $accessTime, $createTime);
  47. self::assertEquals($extTimeField, $localExtraField);
  48. $accessTime = null;
  49. $createTime = null;
  50. $cdExtraField = new ExtendedTimestampExtraField($flags, $modifyTime, $accessTime, $createTime);
  51. self::assertSame($cdExtraField->getHeaderId(), ExtendedTimestampExtraField::HEADER_ID);
  52. self::assertSame($cdExtraField->getFlags(), $flags);
  53. self::assertSame($cdExtraField->getModifyTime(), $modifyTime);
  54. self::assertSame($cdExtraField->getAccessTime(), $accessTime);
  55. self::assertSame($cdExtraField->getCreateTime(), $createTime);
  56. self::assertSame($cdExtraField->packCentralDirData(), $cdData);
  57. self::assertEquals(ExtendedTimestampExtraField::unpackCentralDirData($cdData), $cdExtraField);
  58. self::assertSame($localExtraField->packCentralDirData(), $cdData);
  59. }
  60. public function provideExtraField(): array
  61. {
  62. return [
  63. [
  64. ExtendedTimestampExtraField::MODIFY_TIME_BIT
  65. | ExtendedTimestampExtraField::ACCESS_TIME_BIT
  66. | ExtendedTimestampExtraField::CREATE_TIME_BIT,
  67. 911512006,
  68. 911430000,
  69. 893709400,
  70. "\x07\xC6\x91T6pQS6X\xECD5",
  71. "\x07\xC6\x91T6",
  72. ],
  73. [
  74. ExtendedTimestampExtraField::MODIFY_TIME_BIT
  75. | ExtendedTimestampExtraField::ACCESS_TIME_BIT,
  76. 1492955702,
  77. 1492955638,
  78. null,
  79. "\x036\xB2\xFCX\xF6\xB1\xFCX",
  80. "\x036\xB2\xFCX",
  81. ],
  82. [
  83. ExtendedTimestampExtraField::MODIFY_TIME_BIT,
  84. 1470494391,
  85. null,
  86. null,
  87. "\x01\xB7\xF6\xA5W",
  88. "\x01\xB7\xF6\xA5W",
  89. ],
  90. ];
  91. }
  92. /**
  93. * @throws \Exception
  94. */
  95. public function testSetter(): void
  96. {
  97. $mtime = time();
  98. $atime = null;
  99. $ctime = null;
  100. $field = ExtendedTimestampExtraField::create($mtime, $atime, $ctime);
  101. self::assertSame($field->getFlags(), ExtendedTimestampExtraField::MODIFY_TIME_BIT);
  102. self::assertSame($field->getModifyTime(), $mtime);
  103. self::assertEquals($field->getModifyDateTime(), new \DateTimeImmutable('@' . $mtime));
  104. self::assertSame($field->getAccessTime(), $atime);
  105. self::assertSame($field->getCreateTime(), $ctime);
  106. $atime = strtotime('-1 min');
  107. $field->setAccessTime($atime);
  108. self::assertSame(
  109. $field->getFlags(),
  110. ExtendedTimestampExtraField::MODIFY_TIME_BIT
  111. | ExtendedTimestampExtraField::ACCESS_TIME_BIT
  112. );
  113. self::assertSame($field->getModifyTime(), $mtime);
  114. self::assertSame($field->getAccessTime(), $atime);
  115. self::assertEquals($field->getAccessDateTime(), new \DateTimeImmutable('@' . $atime));
  116. self::assertSame($field->getCreateTime(), $ctime);
  117. $ctime = strtotime('-1 hour');
  118. $field->setCreateTime($ctime);
  119. self::assertSame(
  120. $field->getFlags(),
  121. ExtendedTimestampExtraField::MODIFY_TIME_BIT
  122. | ExtendedTimestampExtraField::ACCESS_TIME_BIT
  123. | ExtendedTimestampExtraField::CREATE_TIME_BIT
  124. );
  125. self::assertSame($field->getModifyTime(), $mtime);
  126. self::assertSame($field->getAccessTime(), $atime);
  127. self::assertSame($field->getCreateTime(), $ctime);
  128. self::assertEquals($field->getCreateDateTime(), new \DateTimeImmutable('@' . $ctime));
  129. $field->setCreateTime(null);
  130. self::assertNull($field->getCreateTime());
  131. self::assertNull($field->getCreateDateTime());
  132. self::assertSame(
  133. $field->getFlags(),
  134. ExtendedTimestampExtraField::MODIFY_TIME_BIT
  135. | ExtendedTimestampExtraField::ACCESS_TIME_BIT
  136. );
  137. $field->setAccessTime(null);
  138. self::assertNull($field->getAccessTime());
  139. self::assertNull($field->getAccessDateTime());
  140. self::assertSame($field->getFlags(), ExtendedTimestampExtraField::MODIFY_TIME_BIT);
  141. $field->setModifyTime(null);
  142. self::assertNull($field->getModifyTime());
  143. self::assertNull($field->getModifyDateTime());
  144. self::assertSame($field->getFlags(), 0);
  145. }
  146. }