ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
AddDirectoryToZipTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
24use ZipArchive;
25
34final class AddDirectoryToZipTest extends TestCase
35{
36 private string $zip_file;
37 private string $source_directory;
38 private ZipArchive $zip;
39
40 protected function setUp(): void
41 {
43 $this->zip_file = tempnam(sys_get_temp_dir(), 'irss_dir_test_');
44 $this->zip = new ZipArchive();
45 $this->zip->open($this->zip_file, ZipArchive::OVERWRITE);
46
47 $this->source_directory = $this->zip_file . '_source';
48 mkdir($this->source_directory . '/docs/nested', 0777, true);
49 file_put_contents($this->source_directory . '/root.txt', 'root');
50 file_put_contents($this->source_directory . '/docs/one.txt', 'one');
51 file_put_contents($this->source_directory . '/docs/nested/two.txt', 'two');
52 }
53
54 protected function tearDown(): void
55 {
57 if ($this->zip->filename !== '') {
58 @$this->zip->close();
59 }
60 @unlink($this->zip_file);
61 $this->removeDirectory($this->source_directory);
62 }
63
64 private function removeDirectory(string $directory): void
65 {
66 if (!is_dir($directory)) {
67 return;
68 }
69 $iterator = new \RecursiveIteratorIterator(
70 new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS),
71 \RecursiveIteratorIterator::CHILD_FIRST
72 );
73 foreach ($iterator as $file) {
74 $file->isDir() ? @rmdir($file->getPathname()) : @unlink($file->getPathname());
75 }
76 @rmdir($directory);
77 }
78
79 private function invoke(string $source_directory, string $path_inside_container): int
80 {
81 $method = new \ReflectionMethod(ResourceBuilder::class, 'addDirectoryToZIP');
82 $builder = (new \ReflectionClass(ResourceBuilder::class))->newInstanceWithoutConstructor();
83
84 return $method->invoke($builder, $this->zip, $source_directory, $path_inside_container);
85 }
86
90 private function fileEntries(): array
91 {
92 $verify = new ZipArchive();
93 $verify->open($this->zip_file);
94 $names = [];
95 for ($i = 0; $i < $verify->numFiles; $i++) {
96 $name = $verify->getNameIndex($i);
97 if (!str_ends_with($name, '/')) {
98 $names[] = $name;
99 }
100 }
101 $verify->close();
102 sort($names);
103
104 return $names;
105 }
106
108 {
109 $added = $this->invoke($this->source_directory, '');
110 $this->zip->close();
111
112 $this->assertSame(3, $added);
113 $this->assertSame(
114 ['docs/nested/two.txt', 'docs/one.txt', 'root.txt'],
115 $this->fileEntries()
116 );
117 }
118
120 {
121 $added = $this->invoke($this->source_directory, 'target');
122 $this->zip->close();
123
124 $this->assertSame(3, $added);
125 $this->assertSame(
126 ['target/docs/nested/two.txt', 'target/docs/one.txt', 'target/root.txt'],
127 $this->fileEntries()
128 );
129 }
130
132 {
133 $this->invoke($this->source_directory . DIRECTORY_SEPARATOR, '');
134 $this->zip->close();
135
136 $this->assertSame(
137 ['docs/nested/two.txt', 'docs/one.txt', 'root.txt'],
138 $this->fileEntries()
139 );
140 }
141
142 public function testNoEntryStartsWithASlash(): void
143 {
144 $this->invoke($this->source_directory, '/');
145 $this->zip->close();
146
147 $entries = $this->fileEntries();
148 $this->assertNotEmpty($entries);
149 foreach ($entries as $entry) {
150 $this->assertStringStartsNotWith('/', $entry, "ZIP entry must not start with '/': {$entry}");
151 }
152 }
153
154 public function testExistingEntriesAreKept(): void
155 {
156 $this->zip->addFromString('existing.txt', 'already in the container');
157
158 $this->invoke($this->source_directory, '');
159 $this->zip->close();
160
161 $this->assertContains('existing.txt', $this->fileEntries());
162 }
163
164 public function testAnEmptyDirectoryAddsNothing(): void
165 {
166 $empty_directory = $this->source_directory . '/empty';
167 mkdir($empty_directory);
168
169 $this->assertSame(0, $this->invoke($empty_directory, ''));
170 }
171}
Mantis 48134: appending the contents of a ZIP to a container used to add one file per addStreamToCont...
invoke(string $source_directory, string $path_inside_container)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...