-
Notifications
You must be signed in to change notification settings - Fork 60
/
ZipMatcherTest.php
145 lines (124 loc) · 3.85 KB
/
ZipMatcherTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare(strict_types=1);
/*
* This file is part of the nelexa/zip package.
* (c) Ne-Lexa <https://github.com/Ne-Lexa/php-zip>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PhpZip\Tests;
use PHPUnit\Framework\TestCase;
use PhpZip\Exception\ZipEntryNotFoundException;
use PhpZip\Model\ZipEntry;
use PhpZip\ZipFile;
/**
* @internal
*
* @small
*/
class ZipMatcherTest extends TestCase
{
/**
* @throws ZipEntryNotFoundException
*/
public function testMatcher(): void
{
$zipFile = new ZipFile();
for ($i = 0; $i < 100; $i++) {
$zipFile[$i] = $i;
}
$matcher = $zipFile->matcher();
static::assertIsArray($matcher->getMatches());
static::assertCount(0, $matcher);
$matcher->add(1)->add(10)->add(20);
static::assertCount(3, $matcher);
static::assertSame($matcher->getMatches(), ['1', '10', '20']);
$matcher->delete();
static::assertCount(97, $zipFile);
static::assertCount(0, $matcher);
$matcher->match('~^[2][1-5]|[3][6-9]|40$~s');
static::assertCount(10, $matcher);
$actualMatches = [
'21',
'22',
'23',
'24',
'25',
'36',
'37',
'38',
'39',
'40',
];
static::assertSame($matcher->getMatches(), $actualMatches);
$matcher->setPassword('qwerty');
$zipEntries = $zipFile->getEntries();
array_walk(
$zipEntries,
function (ZipEntry $zipEntry) use ($actualMatches): void {
$this->assertSame($zipEntry->isEncrypted(), \in_array($zipEntry->getName(), $actualMatches, true));
}
);
$matcher->all();
static::assertCount(\count($zipFile), $matcher);
$expectedNames = [];
$matcher->invoke(
static function ($entryName) use (&$expectedNames): void {
$expectedNames[] = $entryName;
}
);
static::assertSame($expectedNames, $matcher->getMatches());
$zipFile->close();
}
/**
* @throws \Exception
*/
public function testDocsExample(): void
{
$zipFile = new ZipFile();
for ($i = 0; $i < 100; $i++) {
$zipFile['file_' . $i . '.jpg'] = random_bytes(100);
}
$renameEntriesArray = [
'file_10.jpg',
'file_11.jpg',
'file_12.jpg',
'file_13.jpg',
'file_14.jpg',
'file_15.jpg',
'file_16.jpg',
'file_17.jpg',
'file_18.jpg',
'file_19.jpg',
'file_50.jpg',
'file_51.jpg',
'file_52.jpg',
'file_53.jpg',
'file_54.jpg',
'file_55.jpg',
'file_56.jpg',
'file_57.jpg',
'file_58.jpg',
'file_59.jpg',
];
foreach ($renameEntriesArray as $name) {
static::assertTrue(isset($zipFile[$name]));
}
$matcher = $zipFile->matcher();
$matcher->match('~^file_(1|5)\d+~');
static::assertSame($matcher->getMatches(), $renameEntriesArray);
$matcher->invoke(
static function ($entryName) use ($zipFile): void {
$newName = preg_replace('~\.(jpe?g)$~i', '.no_optimize.$1', $entryName);
$zipFile->rename($entryName, $newName);
}
);
foreach ($renameEntriesArray as $name) {
static::assertFalse(isset($zipFile[$name]));
$pathInfo = pathinfo($name);
$newName = $pathInfo['filename'] . '.no_optimize.' . $pathInfo['extension'];
static::assertTrue(isset($zipFile[$newName]));
}
$zipFile->close();
}
}