ZipMatcherTest.php 3.4 KB

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