3 declare(strict_types
=1);
5 namespace PhpMyAdmin\Tests
;
7 use PhpMyAdmin\ZipExtension
;
8 use PHPUnit\Framework\Attributes\CoversClass
;
9 use PHPUnit\Framework\Attributes\DataProvider
;
10 use PHPUnit\Framework\Attributes\RequiresPhpExtension
;
13 use function file_put_contents
;
17 #[CoversClass(ZipExtension::class)]
18 #[RequiresPhpExtension('zip')]
19 class ZipExtensionTest
extends AbstractTestCase
21 private ZipExtension
$zipExtension;
23 protected function setUp(): void
27 $this->zipExtension
= new ZipExtension(new ZipArchive());
31 * Test for getContents
33 * @param string $file path to zip file
34 * @param string|null $specificEntry regular expression to match a file
35 * @param array<string, string> $output expected output
36 * @psalm-param array{error: string, data: string} $output
38 #[DataProvider('provideTestGetContents')]
39 public function testGetContents(string $file, string|
null $specificEntry, array $output): void
42 $this->zipExtension
->getContents($file, $specificEntry),
48 * @return array<string, array<int, array<string, string>|string|null>>
49 * @psalm-return array<string, array{string, string|null, array{error: string, data: string}}>
51 public static function provideTestGetContents(): array
54 'null as specific entry' => [
55 './tests/test_data/test.zip',
57 ['error' => '', 'data' => 'TEST FILE' . "\n"],
59 'an existent specific entry' => [
60 './tests/test_data/test.zip',
62 ['error' => '', 'data' => 'TEST FILE' . "\n"],
64 'a nonexistent specific entry' => [
65 './tests/test_data/test.zip',
67 ['error' => 'Error in ZIP archive: Could not find "/foobar/"', 'data' => ''],
75 * @param string $file path to zip file
76 * @param string $fileRegexp regular expression for the file name to match
77 * @param string|bool $output expected output
78 * @psalm-param string|false $output
80 #[DataProvider('provideTestFindFile')]
81 public function testFindFile(string $file, string $fileRegexp, string|
bool $output): void
84 $this->zipExtension
->findFile($file, $fileRegexp),
90 * Provider for testFindFileFromZipArchive
92 * @return array<int, array<int, string|bool>>
93 * @psalm-return array<int, array{string, string, string|false}>
95 public static function provideTestFindFile(): array
98 ['./tests/test_data/test.zip', '/test/', 'test.file'],
99 ['./tests/test_data/test.zip', '/invalid/', false],
104 * Test for getNumberOfFiles
106 public function testGetNumberOfFiles(): void
109 $this->zipExtension
->getNumberOfFiles('./tests/test_data/test.zip'),
117 public function testExtract(): void
120 $this->zipExtension
->extract(
121 './tests/test_data/test.zip',
127 $this->zipExtension
->extract(
128 './tests/test_data/test.zip',
135 * Test for createFile
137 public function testCreateSingleFile(): void
139 $file = $this->zipExtension
->createFile('Test content', 'test.txt');
140 self
::assertIsString($file);
141 self
::assertNotEmpty($file);
143 $tmp = tempnam('./', 'zip-test');
144 self
::assertNotFalse($tmp);
145 self
::assertNotFalse(file_put_contents($tmp, $file));
147 $zip = new ZipArchive();
152 self
::assertSame(0, $zip->locateName('test.txt'));
159 * Test for createFile
161 public function testCreateFailure(): void
164 $this->zipExtension
->createFile(
166 ['name1.txt', 'name2.txt'],
172 * Test for createFile
174 public function testCreateMultiFile(): void
176 $file = $this->zipExtension
->createFile(
177 ['Content', 'Content2'],
178 ['name1.txt', 'name2.txt'],
180 self
::assertIsString($file);
181 self
::assertNotEmpty($file);
183 $tmp = tempnam('./', 'zip-test');
184 self
::assertNotFalse($tmp);
185 self
::assertNotFalse(file_put_contents($tmp, $file));
187 $zip = new ZipArchive();
192 self
::assertSame(0, $zip->locateName('name1.txt'));
193 self
::assertSame(1, $zip->locateName('name2.txt'));