Merge pull request #19552 from kamil-tekiela/Fix-default-values
[phpmyadmin.git] / tests / unit / ZipExtensionTest.php
blob265966a2f812e6599112b20c38a497b3ed893e3c
1 <?php
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;
11 use ZipArchive;
13 use function file_put_contents;
14 use function tempnam;
15 use function unlink;
17 #[CoversClass(ZipExtension::class)]
18 #[RequiresPhpExtension('zip')]
19 class ZipExtensionTest extends AbstractTestCase
21 private ZipExtension $zipExtension;
23 protected function setUp(): void
25 parent::setUp();
27 $this->zipExtension = new ZipExtension(new ZipArchive());
30 /**
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
41 self::assertSame(
42 $this->zipExtension->getContents($file, $specificEntry),
43 $output,
47 /**
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
53 return [
54 'null as specific entry' => [
55 './tests/test_data/test.zip',
56 null,
57 ['error' => '', 'data' => 'TEST FILE' . "\n"],
59 'an existent specific entry' => [
60 './tests/test_data/test.zip',
61 '/test.file/',
62 ['error' => '', 'data' => 'TEST FILE' . "\n"],
64 'a nonexistent specific entry' => [
65 './tests/test_data/test.zip',
66 '/foobar/',
67 ['error' => 'Error in ZIP archive: Could not find "/foobar/"', 'data' => ''],
72 /**
73 * Test for findFile
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
83 self::assertSame(
84 $this->zipExtension->findFile($file, $fileRegexp),
85 $output,
89 /**
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
97 return [
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
108 self::assertSame(
109 $this->zipExtension->getNumberOfFiles('./tests/test_data/test.zip'),
115 * Test for extract
117 public function testExtract(): void
119 self::assertFalse(
120 $this->zipExtension->extract(
121 './tests/test_data/test.zip',
122 'wrongName',
125 self::assertSame(
126 "TEST FILE\n",
127 $this->zipExtension->extract(
128 './tests/test_data/test.zip',
129 'test.file',
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();
148 self::assertTrue(
149 $zip->open($tmp),
152 self::assertSame(0, $zip->locateName('test.txt'));
154 $zip->close();
155 unlink($tmp);
159 * Test for createFile
161 public function testCreateFailure(): void
163 self::assertFalse(
164 $this->zipExtension->createFile(
165 'Content',
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();
188 self::assertTrue(
189 $zip->open($tmp),
192 self::assertSame(0, $zip->locateName('name1.txt'));
193 self::assertSame(1, $zip->locateName('name2.txt'));
195 $zip->close();
196 unlink($tmp);