ZipMatcherTest.php 3.5 KB

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