N°9875 - Error deprecated on component version object - propose boilerplate methods to handle ZipArchive

This commit is contained in:
odain
2026-08-18 17:03:07 +02:00
parent fac132f707
commit ff21eb3bc9
2 changed files with 140 additions and 0 deletions

View File

@@ -29,6 +29,7 @@ use ScssPhp\ScssPhp\Compiler;
use ScssPhp\ScssPhp\OutputStyle;
use ScssPhp\ScssPhp\ValueConverter;
use Soundasleep\Html2Text;
use ZipArchive;
/**
* Static class utils
@@ -3249,4 +3250,46 @@ TXT
return (int)$sLimit;
}
}
/**
* Open archive and raise appropriate exception.
* Warning: do not forget to close archive afterwhile
* @param string $sArchiveFilePath
* @param int|null $flags
* @return \ZipArchive
* @throws \Exception
*/
public static function ZipArchiveOpen(string $sArchiveFilePath, int|null $flags = null): \ZipArchive
{
$oZip = new \ZipArchive();
if (is_null($flags)) {
$code = $oZip->open($sArchiveFilePath);
} else {
$code = $oZip->open($sArchiveFilePath, $flags);
}
if (true !== $code) {
//ZipArchive::ZIP_ER_NOZIP : 19
if ($code === 19) {
throw new \Exception(sprintf('Cannot to open zip file due to inconsistent or empty content'));
}
throw new \Exception(sprintf('Cannot to open zip file due to error code %s', $code));
}
return $oZip;
}
/**
* @param string $sDirectory
* @param string $sPrefix
* @return array
* @throws \Exception
*/
public static function ZipArchiveOpenWithTempNam(string $sDirectory, string $sPrefix): array
{
$sTempnam = tempnam($sDirectory, $sPrefix);
unlink($sTempnam);
$sArchiveName = $sTempnam.'.zip';;
return [ self::ZipArchiveOpen($sArchiveName, \ZipArchive::CREATE), $sArchiveName ];
}
}

View File

@@ -1009,4 +1009,101 @@ INI;
utils::Unserialize($sData);
}
public static function ZipArchiveOpen_ValidZipFileProvider()
{
return [
"RDONLY" => [\ZipArchive::RDONLY],
"null" => [null],
];
}
/**
* @dataProvider ZipArchiveOpen_ValidZipFileProvider
*/
public function testZipArchiveOpen_ValidZipFile($flags)
{
$sArchiveName = tempnam(sys_get_temp_dir(), "testZipArchiveOpen_ValidZipFile_");
unlink($sArchiveName);
$oZip = new \ZipArchive();
$oZip->open($sArchiveName, \ZipArchive::CREATE);
$oZip->addFile(__FILE__);
$oZip->close();
$this->aFileToClean [] = $sArchiveName;
$oZip = utils::ZipArchiveOpen($sArchiveName, $flags);
self::assertNotNull($oZip);
$oZip->close();
}
public static function ZipArchiveOpen_EmptyExistingFileProvider()
{
return [
"RDONLY" => [\ZipArchive::RDONLY, 'Cannot to open zip file due to inconsistent or empty content'],
"OVERWRITE" => [\ZipArchive::OVERWRITE],
];
}
/**
* @dataProvider ZipArchiveOpen_EmptyExistingFileProvider
*/
public function testZipArchiveOpen_EmptyExistingFile($flags, $sExpectedMessage = null)
{
$sFolderPath = tempnam(sys_get_temp_dir(), "testZipArchiveOpen_ZipFile_");
$this->aFileToClean [] = $sFolderPath;
if (! is_null($sExpectedMessage)) {
$this->expectExceptionMessage($sExpectedMessage);
}
$oZip = utils::ZipArchiveOpen($sFolderPath, $flags);
if (is_null($sExpectedMessage)) {
self::assertNotNull($oZip);
$oZip->close();
touch($sFolderPath);
}
}
public static function ZipArchiveOpen_NotyExistingFileProvider()
{
return [
"CREATE" => [\ZipArchive::CREATE],
"null" => [null, 'Cannot to open zip file due to error code 9'],
];
}
/**
* @dataProvider ZipArchiveOpen_NotyExistingFileProvider
*/
public function testZipArchiveOpen_NotyExistingFile($flags, $sExpectedMessage = null)
{
$sFolderPath = tempnam(sys_get_temp_dir(), "testZipArchiveOpen_ZipFile_");
@unlink($sFolderPath);
if (! is_null($sExpectedMessage)) {
$this->expectExceptionMessage($sExpectedMessage);
}
$oZip = utils::ZipArchiveOpen($sFolderPath, $flags);
if (is_null($sExpectedMessage)) {
self::assertNotNull($oZip);
$oZip->close();
touch($sFolderPath);
}
}
public function testZipArchiveOpenWithTempNam()
{
list($oZip, $sFilePath) = utils::ZipArchiveOpenWithTempNam(sys_get_temp_dir(), "testZipArchiveOpenWithTempFile_");
self::assertNotNull($oZip);
self::assertFalse(is_file($sFilePath), $sFilePath);
$oZip->addEmptyDir('toto');
$oZip->addFile(__FILE__);
$oZip->close();
self::assertTrue(in_array($sFilePath, glob(sys_get_temp_dir() . '/**')));
self::assertTrue(is_file($sFilePath), $sFilePath);
unlink($sFilePath);
}
}