ZipMatcherTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace PhpZip\Tests;
  3. use PHPUnit\Framework\TestCase;
  4. use PhpZip\Model\ZipEntryMatcher;
  5. use PhpZip\Model\ZipInfo;
  6. use PhpZip\ZipFile;
  7. /**
  8. * @internal
  9. *
  10. * @small
  11. */
  12. class ZipMatcherTest extends TestCase
  13. {
  14. public function testMatcher()
  15. {
  16. $zipFile = new ZipFile();
  17. for ($i = 0; $i < 100; $i++) {
  18. $zipFile[$i] = $i;
  19. }
  20. $matcher = $zipFile->matcher();
  21. static::assertInstanceOf(ZipEntryMatcher::class, $matcher);
  22. static::assertIsArray($matcher->getMatches());
  23. static::assertCount(0, $matcher);
  24. $matcher->add(1)->add(10)->add(20);
  25. static::assertCount(3, $matcher);
  26. static::assertEquals($matcher->getMatches(), ['1', '10', '20']);
  27. $matcher->delete();
  28. static::assertCount(97, $zipFile);
  29. static::assertCount(0, $matcher);
  30. $matcher->match('~^[2][1-5]|[3][6-9]|40$~s');
  31. static::assertCount(10, $matcher);
  32. $actualMatches = [
  33. '21',
  34. '22',
  35. '23',
  36. '24',
  37. '25',
  38. '36',
  39. '37',
  40. '38',
  41. '39',
  42. '40',
  43. ];
  44. static::assertSame($matcher->getMatches(), $actualMatches);
  45. $matcher->setPassword('qwerty');
  46. $info = $zipFile->getAllInfo();
  47. array_walk(
  48. $info,
  49. function (ZipInfo $zipInfo) use ($actualMatches) {
  50. $this->assertSame($zipInfo->isEncrypted(), \in_array($zipInfo->getName(), $actualMatches, true));
  51. }
  52. );
  53. $matcher->all();
  54. static::assertCount(\count($zipFile), $matcher);
  55. $expectedNames = [];
  56. $matcher->invoke(
  57. static function ($entryName) use (&$expectedNames) {
  58. $expectedNames[] = $entryName;
  59. }
  60. );
  61. static::assertSame($expectedNames, $matcher->getMatches());
  62. $zipFile->close();
  63. }
  64. /**
  65. * @throws \Exception
  66. */
  67. public function testDocsExample()
  68. {
  69. $zipFile = new ZipFile();
  70. for ($i = 0; $i < 100; $i++) {
  71. $zipFile['file_' . $i . '.jpg'] = random_bytes(100);
  72. }
  73. $renameEntriesArray = [
  74. 'file_10.jpg',
  75. 'file_11.jpg',
  76. 'file_12.jpg',
  77. 'file_13.jpg',
  78. 'file_14.jpg',
  79. 'file_15.jpg',
  80. 'file_16.jpg',
  81. 'file_17.jpg',
  82. 'file_18.jpg',
  83. 'file_19.jpg',
  84. 'file_50.jpg',
  85. 'file_51.jpg',
  86. 'file_52.jpg',
  87. 'file_53.jpg',
  88. 'file_54.jpg',
  89. 'file_55.jpg',
  90. 'file_56.jpg',
  91. 'file_57.jpg',
  92. 'file_58.jpg',
  93. 'file_59.jpg',
  94. ];
  95. foreach ($renameEntriesArray as $name) {
  96. static::assertTrue(isset($zipFile[$name]));
  97. }
  98. $matcher = $zipFile->matcher();
  99. $matcher->match('~^file_(1|5)\d+~');
  100. static::assertSame($matcher->getMatches(), $renameEntriesArray);
  101. $matcher->invoke(
  102. static function ($entryName) use ($zipFile) {
  103. $newName = preg_replace('~\.(jpe?g)$~i', '.no_optimize.$1', $entryName);
  104. $zipFile->rename($entryName, $newName);
  105. }
  106. );
  107. foreach ($renameEntriesArray as $name) {
  108. static::assertFalse(isset($zipFile[$name]));
  109. $pathInfo = pathinfo($name);
  110. $newName = $pathInfo['filename'] . '.no_optimize.' . $pathInfo['extension'];
  111. static::assertTrue(isset($zipFile[$newName]));
  112. }
  113. $zipFile->close();
  114. }
  115. }