From 7d979e4e60fac05b50da419195d9036513197d2d Mon Sep 17 00:00:00 2001 From: Jakub Zelenka Date: Thu, 14 Jun 2018 20:14:27 +0100 Subject: [PATCH] Add helpers for checking arrays in tests --- tests/gnupg_oo_keyinfo.phpt | 4 +- tests/gnupgt.inc | 74 ++++++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/tests/gnupg_oo_keyinfo.phpt b/tests/gnupg_oo_keyinfo.phpt index 79ad9c6..dc533ca 100644 --- a/tests/gnupg_oo_keyinfo.phpt +++ b/tests/gnupg_oo_keyinfo.phpt @@ -10,7 +10,9 @@ gnupgt::import_key(); $gpg = new gnupg(); $gpg->seterrormode(gnupg::ERROR_WARNING); $ret = $gpg->keyinfo($fingerprint); -var_dump($ret); +gnupgt::check_array(false, $ret, 0, 'disabled'); + +//var_dump($ret); ?> --EXPECT-- array(1) { diff --git a/tests/gnupgt.inc b/tests/gnupgt.inc index 2d6f514..4ee8556 100644 --- a/tests/gnupgt.inc +++ b/tests/gnupgt.inc @@ -3,7 +3,10 @@ require_once __DIR__ . "/vars.inc"; class gnupgt { - static function import_key() + /** + * Import all keys + */ + static public function import_key() { global $testkey; @@ -13,7 +16,10 @@ class gnupgt { $gpg->import($testkey); } - static function delete_key() + /** + * Delete all keys. + */ + static public function delete_key() { @unlink(__DIR__ . "/pubring.gpg"); @unlink(__DIR__ . "/secring.gpg"); @@ -29,4 +35,68 @@ class gnupgt { 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)); + } } \ No newline at end of file