Add helpers for checking arrays in tests

This commit is contained in:
Jakub Zelenka 2018-06-14 20:14:27 +01:00
parent aa6fdf830b
commit 7d979e4e60
2 changed files with 75 additions and 3 deletions

View file

@ -10,7 +10,9 @@ gnupgt::import_key();
$gpg = new gnupg(); $gpg = new gnupg();
$gpg->seterrormode(gnupg::ERROR_WARNING); $gpg->seterrormode(gnupg::ERROR_WARNING);
$ret = $gpg->keyinfo($fingerprint); $ret = $gpg->keyinfo($fingerprint);
var_dump($ret); gnupgt::check_array(false, $ret, 0, 'disabled');
//var_dump($ret);
?> ?>
--EXPECT-- --EXPECT--
array(1) { array(1) {

View file

@ -3,7 +3,10 @@
require_once __DIR__ . "/vars.inc"; require_once __DIR__ . "/vars.inc";
class gnupgt { class gnupgt {
static function import_key() /**
* Import all keys
*/
static public function import_key()
{ {
global $testkey; global $testkey;
@ -13,7 +16,10 @@ class gnupgt {
$gpg->import($testkey); $gpg->import($testkey);
} }
static function delete_key() /**
* Delete all keys.
*/
static public function delete_key()
{ {
@unlink(__DIR__ . "/pubring.gpg"); @unlink(__DIR__ . "/pubring.gpg");
@unlink(__DIR__ . "/secring.gpg"); @unlink(__DIR__ . "/secring.gpg");
@ -29,4 +35,68 @@ class gnupgt {
rmdir($privKeyDir); rmdir($privKeyDir);
} }
} }
/**
* Print error message and return false.
*
* @param string $msg
* @return bool
*/
static private function error($msg)
{
echo "ERROR: " . $msg;
return false;
}
/**
* Check single array value.
*
* @param mixed $expected
* @param array $a
* @param string $key1
* @return bool
*/
static public function check_array($expected, $a, $key1)
{
$args = func_get_args();
$keys = array_splice($args, 2);
$value = $a;
foreach ($keys as $key) {
if (!isset($value[$key])) {
return self::error("key $key not found in the array");
}
$value = $value[$key];
}
if ($value !== $expected) {
return self::error(
sprintf(
"key %s value %s does not match expected %s",
$key,
var_export($value, true),
var_export($value, true)
)
);
}
return true;
}
/**
* Check single array value but only for GpgME version higher than supplied.
*
* @param mixed $expected
* @param array $a
* @param string $key1
* @return bool
*/
static public function check_array_from_version($version, $expected, $a, $key1)
{
if (version_compare(GNUPG_GPGME_VERSION, $version) > 0) {
return true;
}
$args = func_get_args();
return call_user_func_array('gnupgt::check_array', array_splice($args, 1));
}
} }