ext/zip: Add some tests for methods taking callbakcs

This commit is contained in:
Gina Peter Banyard 2025-01-10 12:46:57 +00:00
parent 72708f298b
commit 08784ed58a
5 changed files with 257 additions and 6 deletions

View file

@ -1,5 +1,5 @@
--TEST--
registerCancelCallback
ZipArchive::registerCancelCallback() with a normal callback
--EXTENSIONS--
zip
--SKIPIF--
@ -14,8 +14,6 @@ date.timezone=UTC
$dirname = dirname(__FILE__) . '/';
$file = $dirname . '__tmp_oo_cancel.zip';
@unlink($file);
$zip = new ZipArchive;
if (!$zip->open($file, ZIPARCHIVE::CREATE)) {
exit('failed');
@ -33,6 +31,13 @@ var_dump($zip->getStatusString());
@unlink($file);
?>
Done
--CLEAN--
<?php
$dirname = dirname(__FILE__) . '/';
$file = $dirname . '__tmp_oo_cancel.zip';
@unlink($file);
?>
--EXPECTF--
bool(true)
bool(true)

View file

@ -0,0 +1,47 @@
--TEST--
ZipArchive::registerCancelCallback() with a callback returning an incorrect type
--EXTENSIONS--
zip
--SKIPIF--
<?php
/* $Id$ */
if (!method_exists('ZipArchive', 'registerCancelCallback')) die('skip libzip too old');
?>
--INI--
date.timezone=UTC
--FILE--
<?php
$dirname = dirname(__FILE__) . '/';
$file = $dirname . '__tmp_oo_cancel_incorrect_return.zip';
$zip = new ZipArchive;
if (!$zip->open($file, ZIPARCHIVE::CREATE)) {
exit('failed');
}
/* Register a bogus callback */
var_dump($zip->registerCancelCallback(function () {
return [];
}));
var_dump($zip->addFromString(PHP_BINARY, 'entry #1'));
var_dump($zip->close());
var_dump($zip->status == ZipArchive::ER_CANCELLED);
var_dump($zip->getStatusString());
@unlink($file);
?>
Done
--CLEAN--
<?php
$dirname = dirname(__FILE__) . '/';
$file = $dirname . '__tmp_oo_cancel_incorrect_return.zip';
@unlink($file);
?>
--EXPECT--
bool(true)
bool(true)
bool(true)
bool(false)
string(8) "No error"
Done

View file

@ -0,0 +1,97 @@
--TEST--
ZipArchive::registerCancelCallback() with a trampoline
--EXTENSIONS--
zip
--SKIPIF--
<?php
/* $Id$ */
if (!method_exists('ZipArchive', 'registerCancelCallback')) die('skip libzip too old');
?>
--INI--
date.timezone=UTC
--FILE--
<?php
$dirname = dirname(__FILE__) . '/';
$file = $dirname . '__tmp_oo_cancel_trampoline.zip';
class TrampolineTest {
public function __call(string $name, array $arguments): int {
echo 'Trampoline for ', $name, PHP_EOL;
if ($name === 'trampolineThrow') {
throw new Exception('boo');
}
var_dump($arguments);
return -1;
}
}
$o = new TrampolineTest();
$callback = [$o, 'trampoline'];
$callbackThrow = [$o, 'trampolineThrow'];
$zip = new ZipArchive;
if (!$zip->open($file, ZIPARCHIVE::CREATE)) {
exit('failed');
}
var_dump($zip->registerCancelCallback($callback));
var_dump($zip->addFromString(PHP_BINARY, 'entry #1'));
var_dump($zip->close());
var_dump($zip->status == ZipArchive::ER_CANCELLED);
var_dump($zip->getStatusString());
echo "Set trampoline after closed Archive:\n";
try {
var_dump($zip->registerCancelCallback($callback));
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
@unlink($file);
unset($zip);
$zip = new ZipArchive;
if (!$zip->open($file, ZIPARCHIVE::CREATE)) {
exit('failed');
}
var_dump($zip->registerCancelCallback($callbackThrow));
var_dump($zip->addFromString(PHP_BINARY, 'entry #1'));
try {
var_dump($zip->close());
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
var_dump($zip->status == ZipArchive::ER_CANCELLED);
var_dump($zip->getStatusString());
@unlink($file);
?>
Done
--CLEAN--
<?php
$dirname = dirname(__FILE__) . '/';
$file = $dirname . '__tmp_oo_cancel_trampoline.zip';
@unlink($file);
?>
--EXPECTF--
bool(true)
bool(true)
Trampoline for trampoline
array(0) {
}
Warning: ZipArchive::close(): Operation cancelled in %s on line %d
bool(false)
bool(true)
string(19) "Operation cancelled"
Set trampoline after closed Archive:
ValueError: Invalid or uninitialized Zip object
bool(true)
bool(true)
Trampoline for trampolineThrow
Exception: boo
bool(false)
string(8) "No error"
Done

View file

@ -1,5 +1,5 @@
--TEST--
registerProgressCallback
ZipArchive::registerProgressCallback() with a normal callback
--EXTENSIONS--
zip
--SKIPIF--
@ -14,8 +14,6 @@ date.timezone=UTC
$dirname = dirname(__FILE__) . '/';
$file = $dirname . '__tmp_oo_progress.zip';
@unlink($file);
$zip = new ZipArchive;
if (!$zip->open($file, ZIPARCHIVE::CREATE)) {
exit('failed');
@ -32,6 +30,13 @@ var_dump($zip->close());
unlink($file);
?>
Done
--CLEAN--
<?php
$dirname = dirname(__FILE__) . '/';
$file = $dirname . '__tmp_oo_progress.zip';
@unlink($file);
?>
--EXPECT--
bool(true)
bool(true)

View file

@ -0,0 +1,97 @@
--TEST--
ZipArchive::registerProgressCallback() with a trampoline
--EXTENSIONS--
zip
--SKIPIF--
<?php
/* $Id$ */
if (!method_exists('ZipArchive', 'registerProgressCallback')) die('skip libzip too old');
?>
--INI--
date.timezone=UTC
--FILE--
<?php
$dirname = dirname(__FILE__) . '/';
$file = $dirname . '__tmp_oo_progress_trampoline.zip';
class TrampolineTest {
public function __call(string $name, array $arguments): void {
echo 'Trampoline for ', $name, PHP_EOL;
if ($name === 'trampolineThrow') {
throw new Exception('boo');
}
var_dump($arguments);
$r = $arguments[0];
if ($r == 0.0) echo "start\n";
if ($r == 1.0) echo "end\n";
}
}
$o = new TrampolineTest();
$callback = [$o, 'trampoline'];
$callbackThrow = [$o, 'trampolineThrow'];
$zip = new ZipArchive;
if (!$zip->open($file, ZIPARCHIVE::CREATE)) {
exit('failed');
}
var_dump($zip->registerProgressCallback(0.5, $callback));
var_dump($zip->addFromString('foo', 'entry #1'));
var_dump($zip->close());
echo "Set trampoline after closed Archive:\n";
try {
var_dump($zip->registerProgressCallback(0.5, $callback));
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
unlink($file);
unset($zip);
$zip = new ZipArchive;
if (!$zip->open($file, ZIPARCHIVE::CREATE)) {
exit('failed');
}
var_dump($zip->registerProgressCallback(0.5, $callbackThrow));
var_dump($zip->addFromString('foo', 'entry #1'));
try {
var_dump($zip->close());
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
unlink($file);
unset($zip);
?>
Done
--CLEAN--
<?php
$dirname = dirname(__FILE__) . '/';
$file = $dirname . '__tmp_oo_progress_trampoline.zip';
@unlink($file);
?>
--EXPECT--
bool(true)
bool(true)
Trampoline for trampoline
array(1) {
[0]=>
float(0)
}
start
Trampoline for trampoline
array(1) {
[0]=>
float(1)
}
end
bool(true)
Set trampoline after closed Archive:
ValueError: Invalid or uninitialized Zip object
bool(true)
bool(true)
Trampoline for trampolineThrow
Exception: boo
Done