21use PHPUnit\Framework\Attributes\BackupGlobals;
22use PHPUnit\Framework\Attributes\BackupStaticProperties;
23use PHPUnit\Framework\Attributes\PreserveGlobalState;
24use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
25use PHPUnit\Framework\Attributes\DataProvider;
30use PHPUnit\Framework\TestCase;
36#[BackupGlobals(false)]
37#[BackupStaticProperties(false)]
38#[PreserveGlobalState(false)]
39#[RunTestsInSeparateProcesses]
47 if (file_exists($this->unzips_dir)) {
48 rmdir($this->unzips_dir);
56 #[DataProvider('getZips')]
57 public function testUnzip(
59 bool $has_multiple_root_entries,
60 int $expected_amount_directories,
61 array $expected_directories,
62 int $expected_amount_files,
65 $this->assertStringContainsString(
'.zip', $zip);
66 $zip_path = $this->zips_dir . $zip;
67 $this->assertFileExists($zip_path);
71 $unzip =
new Unzip($options, $stream);
73 $this->assertFalse($unzip->hasZipReadingError());
74 $this->assertSame($has_multiple_root_entries, $unzip->hasMultipleRootEntriesInZip());
75 $this->assertSame($expected_amount_directories, $unzip->getAmountOfDirectories());
76 $this->assertEquals($expected_directories, iterator_to_array($unzip->getDirectories()));
77 $this->assertSame($expected_amount_files, $unzip->getAmountOfFiles());
78 $this->assertEquals($expected_files, iterator_to_array($unzip->getFiles()));
81 $one_file = iterator_to_array($unzip->getFileStreams())[0];
84 $this->assertGreaterThan(0, preg_match(
'~[^\x20-\x7E\t\r\n]~', $one_file->getContents()));
91 $unzip =
new Unzip($options, $stream);
92 $this->assertTrue($unzip->hasZipReadingError());
93 $this->assertFalse($unzip->hasMultipleRootEntriesInZip());
94 $this->assertCount(0, iterator_to_array($unzip->getFiles()));
95 $this->assertCount(0, iterator_to_array($unzip->getDirectories()));
96 $this->assertCount(0, iterator_to_array($unzip->getPaths()));
97 $this->assertSame([], iterator_to_array($unzip->getDirectories()));
98 $this->assertSame([], iterator_to_array($unzip->getFiles()));
105 #[DataProvider('getZips')]
108 bool $has_multiple_root_entries,
109 int $expected_amount_directories,
110 array $expected_directories,
111 int $expected_amount_files,
112 array $expected_files
116 $this->assertStringContainsString(
'.zip', $zip);
117 $zip_path = $this->zips_dir . $zip;
118 $this->assertFileExists($zip_path);
120 $temp_unzip_path = $this->unzips_dir . uniqid(
'unzip',
true);
122 $return = $legacy->unzip(
127 $this->assertTrue($return);
129 $unzipped_files = $this->directoryToArray($temp_unzip_path);
130 $expected_paths = array_merge($expected_directories, $expected_files);
131 sort($expected_paths);
132 $this->assertEquals($expected_paths, $unzipped_files);
133 $this->assertTrue($this->recurseRmdir($temp_unzip_path));
139 $zip_path = $this->zips_dir .
'3_folders_mac.zip';
140 $this->assertFileExists($zip_path);
142 $temp_unzip_path = $this->unzips_dir . uniqid(
'unzip',
true);
144 $return = $legacy->unzip(
152 $this->assertTrue($return);
154 $unzipped_files = $this->directoryToArray($temp_unzip_path);
156 $this->assertSame(self::$top_directory_tree, $unzipped_files);
157 $this->assertTrue($this->recurseRmdir($temp_unzip_path));
163 $zip_path = $this->zips_dir .
'3_folders_mac.zip';
164 $this->assertFileExists($zip_path);
166 $temp_unzip_path = $this->unzips_dir . uniqid(
'unzip',
true);
168 $return = $legacy->unzip(
175 $this->assertTrue($return);
177 $unzipped_files = $this->directoryToArray($temp_unzip_path);
179 $this->assertSame(self::$expected_flat_files, $unzipped_files);
180 $this->assertTrue($this->recurseRmdir($temp_unzip_path));
185 $files = array_diff(scandir($path_to_directory), [
'.',
'..']);
186 foreach ($files as $file) {
187 (is_dir(
"$path_to_directory/$file") && !is_link(
"$path_to_directory/$file")) ? $this->recurseRmdir(
188 "$path_to_directory/$file"
189 ) : unlink(
"$path_to_directory/$file");
191 return rmdir($path_to_directory);
199 $iterator = new \RecursiveIteratorIterator(
200 new \RecursiveDirectoryIterator($path_to_directory, \RecursiveDirectoryIterator::SKIP_DOTS),
201 \RecursiveIteratorIterator::SELF_FIRST,
202 \RecursiveIteratorIterator::CATCH_GET_CHILD
205 foreach ($iterator as $item) {
206 $relative_path = str_replace($path_to_directory .
'/',
'', $item->getPathname());
207 $paths[] = $item->isDir() ? $relative_path .
'/' : $relative_path;
219 yield [
'1_folder_mac.zip',
false, 10, self::$directories_one, 15, self::$files_one];
220 yield [
'1_folder_win.zip',
false, 10, self::$directories_one, 15, self::$files_one];
221 yield [
'3_folders_mac.zip',
true, 9, self::$directories_three, 12, self::$files_three];
222 yield [
'3_folders_win.zip',
true, 9, self::$directories_three, 12, self::$files_three];
223 yield [
'1_folder_1_file_mac.zip',
true, 3, self::$directories_mixed, 5, self::$files_mixed];
226 protected static array $files_mixed = [
228 1 =>
'Ordner A/01_Test.pdf',
229 2 =>
'Ordner A/02_Test.pdf',
230 3 =>
'Ordner A/Ordner A_2/07_Test.pdf',
231 4 =>
'Ordner A/Ordner A_2/08_Test.pdf'
234 protected static array $directories_mixed = [
236 1 =>
'Ordner A/Ordner A_1/',
237 2 =>
'Ordner A/Ordner A_2/'
240 protected static array $directories_one = [
242 1 =>
'Ordner 0/Ordner A/',
243 2 =>
'Ordner 0/Ordner A/Ordner A_1/',
244 3 =>
'Ordner 0/Ordner A/Ordner A_2/',
245 4 =>
'Ordner 0/Ordner B/',
246 5 =>
'Ordner 0/Ordner B/Ordner B_1/',
247 6 =>
'Ordner 0/Ordner B/Ordner B_2/',
248 7 =>
'Ordner 0/Ordner C/',
249 8 =>
'Ordner 0/Ordner C/Ordner C_1/',
250 9 =>
'Ordner 0/Ordner C/Ordner C_2/'
252 protected static array $directories_three = [
254 1 =>
'Ordner A/Ordner A_1/',
255 2 =>
'Ordner A/Ordner A_2/',
257 4 =>
'Ordner B/Ordner B_1/',
258 5 =>
'Ordner B/Ordner B_2/',
260 7 =>
'Ordner C/Ordner C_1/',
261 8 =>
'Ordner C/Ordner C_2/'
264 protected static array $files_one = [
265 0 =>
'Ordner 0/13_Test.pdf',
266 1 =>
'Ordner 0/14_Test.pdf',
267 2 =>
'Ordner 0/15_Test.pdf',
268 3 =>
'Ordner 0/Ordner A/01_Test.pdf',
269 4 =>
'Ordner 0/Ordner A/02_Test.pdf',
270 5 =>
'Ordner 0/Ordner A/Ordner A_2/07_Test.pdf',
271 6 =>
'Ordner 0/Ordner A/Ordner A_2/08_Test.pdf',
272 7 =>
'Ordner 0/Ordner B/03_Test.pdf',
273 8 =>
'Ordner 0/Ordner B/04_Test.pdf',
274 9 =>
'Ordner 0/Ordner B/Ordner B_2/09_Test.pdf',
275 10 =>
'Ordner 0/Ordner B/Ordner B_2/10_Test.pdf',
276 11 =>
'Ordner 0/Ordner C/05_Test.pdf',
277 12 =>
'Ordner 0/Ordner C/06_Test.pdf',
278 13 =>
'Ordner 0/Ordner C/Ordner C_2/11_Test.pdf',
279 14 =>
'Ordner 0/Ordner C/Ordner C_2/12_Test.pdf'
282 protected static array $files_three = [
283 0 =>
'Ordner A/01_Test.pdf',
284 1 =>
'Ordner A/02_Test.pdf',
285 2 =>
'Ordner A/Ordner A_2/07_Test.pdf',
286 3 =>
'Ordner A/Ordner A_2/08_Test.pdf',
287 4 =>
'Ordner B/03_Test.pdf',
288 5 =>
'Ordner B/04_Test.pdf',
289 6 =>
'Ordner B/Ordner B_2/09_Test.pdf',
290 7 =>
'Ordner B/Ordner B_2/10_Test.pdf',
291 8 =>
'Ordner C/05_Test.pdf',
292 9 =>
'Ordner C/06_Test.pdf',
293 10 =>
'Ordner C/Ordner C_2/11_Test.pdf',
294 11 =>
'Ordner C/Ordner C_2/12_Test.pdf',
297 protected static array $top_directory_tree = [
298 0 =>
'3_folders_mac/',
299 1 =>
'3_folders_mac/Ordner A/',
300 2 =>
'3_folders_mac/Ordner A/01_Test.pdf',
301 3 =>
'3_folders_mac/Ordner A/02_Test.pdf',
302 4 =>
'3_folders_mac/Ordner A/Ordner A_1/',
303 5 =>
'3_folders_mac/Ordner A/Ordner A_2/',
304 6 =>
'3_folders_mac/Ordner A/Ordner A_2/07_Test.pdf',
305 7 =>
'3_folders_mac/Ordner A/Ordner A_2/08_Test.pdf',
306 8 =>
'3_folders_mac/Ordner B/',
307 9 =>
'3_folders_mac/Ordner B/03_Test.pdf',
308 10 =>
'3_folders_mac/Ordner B/04_Test.pdf',
309 11 =>
'3_folders_mac/Ordner B/Ordner B_1/',
310 12 =>
'3_folders_mac/Ordner B/Ordner B_2/',
311 13 =>
'3_folders_mac/Ordner B/Ordner B_2/09_Test.pdf',
312 14 =>
'3_folders_mac/Ordner B/Ordner B_2/10_Test.pdf',
313 15 =>
'3_folders_mac/Ordner C/',
314 16 =>
'3_folders_mac/Ordner C/05_Test.pdf',
315 17 =>
'3_folders_mac/Ordner C/06_Test.pdf',
316 18 =>
'3_folders_mac/Ordner C/Ordner C_1/',
317 19 =>
'3_folders_mac/Ordner C/Ordner C_2/',
318 20 =>
'3_folders_mac/Ordner C/Ordner C_2/11_Test.pdf',
319 21 =>
'3_folders_mac/Ordner C/Ordner C_2/12_Test.pdf',
322 private static array $expected_flat_files = [
Stream factory which enables the user to create streams without the knowledge of the concrete class.
static ofResource($resource)
Wraps an already created resource with the stream abstraction.
directoryToArray(string $path_to_directory)
recurseRmdir(string $path_to_directory)
testLegacyUnzip(string $zip, bool $has_multiple_root_entries, int $expected_amount_directories, array $expected_directories, int $expected_amount_files, array $expected_files)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...