ZipMatcherTest.php 3.8 KB

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