ZipMatcherTest.php 3.3 KB

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