2
0

ZipMatcherTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace PhpZip;
  3. use PHPUnit\Framework\TestCase;
  4. use PhpZip\Model\ZipEntryMatcher;
  5. use PhpZip\Model\ZipInfo;
  6. use PhpZip\Util\CryptoUtil;
  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::assertInternalType('array', $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', '22', '23', '24', '25',
  34. '36', '37', '38', '39', '40',
  35. ];
  36. static::assertSame($matcher->getMatches(), $actualMatches);
  37. $matcher->setPassword('qwerty');
  38. $info = $zipFile->getAllInfo();
  39. array_walk(
  40. $info,
  41. function (ZipInfo $zipInfo) use ($actualMatches) {
  42. $this->assertSame($zipInfo->isEncrypted(), \in_array($zipInfo->getName(), $actualMatches, true));
  43. }
  44. );
  45. $matcher->all();
  46. static::assertCount(\count($zipFile), $matcher);
  47. $expectedNames = [];
  48. $matcher->invoke(
  49. static function ($entryName) use (&$expectedNames) {
  50. $expectedNames[] = $entryName;
  51. }
  52. );
  53. static::assertSame($expectedNames, $matcher->getMatches());
  54. $zipFile->close();
  55. }
  56. public function testDocsExample()
  57. {
  58. $zipFile = new ZipFile();
  59. for ($i = 0; $i < 100; $i++) {
  60. $zipFile['file_' . $i . '.jpg'] = CryptoUtil::randomBytes(100);
  61. }
  62. $renameEntriesArray = [
  63. 'file_10.jpg',
  64. 'file_11.jpg',
  65. 'file_12.jpg',
  66. 'file_13.jpg',
  67. 'file_14.jpg',
  68. 'file_15.jpg',
  69. 'file_16.jpg',
  70. 'file_17.jpg',
  71. 'file_18.jpg',
  72. 'file_19.jpg',
  73. 'file_50.jpg',
  74. 'file_51.jpg',
  75. 'file_52.jpg',
  76. 'file_53.jpg',
  77. 'file_54.jpg',
  78. 'file_55.jpg',
  79. 'file_56.jpg',
  80. 'file_57.jpg',
  81. 'file_58.jpg',
  82. 'file_59.jpg',
  83. ];
  84. foreach ($renameEntriesArray as $name) {
  85. static::assertTrue(isset($zipFile[$name]));
  86. }
  87. $matcher = $zipFile->matcher();
  88. $matcher->match('~^file_(1|5)\d+~');
  89. static::assertSame($matcher->getMatches(), $renameEntriesArray);
  90. $matcher->invoke(
  91. static function ($entryName) use ($zipFile) {
  92. $newName = preg_replace('~\.(jpe?g)$~i', '.no_optimize.$1', $entryName);
  93. $zipFile->rename($entryName, $newName);
  94. }
  95. );
  96. foreach ($renameEntriesArray as $name) {
  97. static::assertFalse(isset($zipFile[$name]));
  98. $pathInfo = pathinfo($name);
  99. $newName = $pathInfo['filename'] . '.no_optimize.' . $pathInfo['extension'];
  100. static::assertTrue(isset($zipFile[$newName]));
  101. }
  102. $zipFile->close();
  103. }
  104. }