diff --git a/tests/utils/basic-skipif.inc b/tests/utils/basic-skipif.inc index 91cdbf66d..df23d43ca 100644 --- a/tests/utils/basic-skipif.inc +++ b/tests/utils/basic-skipif.inc @@ -1,9 +1,24 @@ getCode(), $e->getMessage())); +}); +register_shutdown_function(function() { + $lastError = error_get_last(); + if ($lastError !== null) { + exit(sprintf('skip %s: %s', errno_as_string($lastError['type']), $lastError['message'])); + } +}); diff --git a/tests/utils/basic.inc b/tests/utils/basic.inc index 1c10600f0..ac9cd1143 100644 --- a/tests/utils/basic.inc +++ b/tests/utils/basic.inc @@ -1,38 +1,8 @@ "", - "STANDALONE_30" => "", - "STANDALONE_SSL" => "", - "STANDALONE_AUTH" => "", - "STANDALONE_X509" => "", - "STANDALONE_PLAIN" => "", - "REPLICASET" => "", - "REPLICASET_30" => "", - "REPLICASET_DNS" => "", -); -$servers = array_merge($servers, $config); -def($servers); - -$consts = array( - "DATABASE_NAME" => "phongo", - "COLLECTION_NAME" => makeCollectionNameFromFilename($_SERVER["SCRIPT_FILENAME"]), -); -def($consts); - -// These use values from constants defined above -$consts = array( - "NS" => DATABASE_NAME . "." . COLLECTION_NAME, -); -def($consts); +define('URI', getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); +define('DATABASE_NAME', getenv('MONGODB_DATABASE') ?: 'phongo'); +define('COLLECTION_NAME', makeCollectionNameFromFilename($_SERVER['SCRIPT_FILENAME'])); +define('NS', DATABASE_NAME . '.' . COLLECTION_NAME); diff --git a/tests/utils/skipif.php b/tests/utils/skipif.php new file mode 100644 index 000000000..77741e197 --- /dev/null +++ b/tests/utils/skipif.php @@ -0,0 +1,155 @@ +getMessage()); + } +} + +/** + * Skips the test if the server version satisfies a comparison. + * + * @see http://php.net/version_compare + * @param string $operator Comparison operator + * @param string $version Version to compare against + */ +function skip_if_server_version($operator, $version) +{ + $serverVersion = get_server_version(URI); + + if (version_compare($serverVersion, $version, $operator)) { + exit("skip Server version '$serverVersion' $operator '$version'"); + } +} + +/** + * Skips the test if the server not using a particular storage engine. + * + * @param string $storageEngine Storage engine name + */ +function skip_if_not_server_storage_engine($storageEngine) +{ + $serverStorageEngine = get_server_storage_engine(URI); + + if ($serverStorageEngine !== $storageEngine) { + exit("skip Server storage engine is '$serverStorageEngine' (needed '$storageEngine')"); + } +} + +/** + * Skips the test if libmongoc does not support crypto. + * + * If one or more libaries are provided, additionally check that the reported + * library is in that array. Possible values are "libcrypto", "Common Crypto", + * and "CNG". + * + * @param array $libs Optional list of crypto libraries to require + */ +function skip_if_not_libmongoc_crypto(array $libs = []) +{ + $lib = get_module_info('libmongoc crypto library'); + + if ($lib === null) { + exit('skip libmongoc crypto is not enabled'); + } + + if (!empty($libs) && !in_array($lib, $libs)) { + exit('skip Needs libmongoc crypto library ' . implode(', ', $libs) . ', but found ' . $lib); + } +} + +/** + * Skips the test if libmongoc does not support SSL. + * + * If one or more libaries are provided, additionally check that the reported + * library is in that array. Possible values are "OpenSSL", "LibreSSL", + * "Secure Transport", and "Secure Channel". + * + * @param array $libs Optional list of SSL libraries to require + */ +function skip_if_not_libmongoc_ssl(array $libs = []) +{ + $lib = get_module_info('libmongoc SSL library'); + + if ($lib === null) { + exit('skip libmongoc SSL is not enabled'); + } + + if (!empty($libs) && !in_array($lib, $libs)) { + exit('skip Needs libmongoc SSL library ' . implode(', ', $libs) . ', but found ' . $lib); + } +} diff --git a/tests/utils/tools.php b/tests/utils/tools.php index 19e0cdeda..c416f9109 100644 --- a/tests/utils/tools.php +++ b/tests/utils/tools.php @@ -1,5 +1,189 @@ ')); + + if (preg_match($pattern, $info, $matches) !== 1) { + return null; + } + + return $matches[1]; +} + +/** + * Returns the primary server. + * + * @param string $uri Connection string + * @return Server + * @throws ConnectionException + */ +function get_primary_server($uri) +{ + return (new Manager($uri))->selectServer(new ReadPreference('primary')); +} + +/** + * Returns the storage engine of the primary server. + * + * @param string $uri Connection string + * @return string + * @throws RuntimeException + */ +function get_server_storage_engine($uri) +{ + $server = get_primary_server($uri); + $command = new Command(['serverStatus' => 1]); + $cursor = $server->executeCommand('admin', $command); + + return current($cursor->toArray())->storageEngine->name; +} + +/** + * Returns the version of the primary server. + * + * @param string $uri Connection string + * @return string + * @throws RuntimeException + */ +function get_server_version($uri) +{ + $server = get_primary_server($uri); + $command = new Command(['buildInfo' => 1]); + $cursor = $server->executeCommand('admin', $command); + + return current($cursor->toArray())->version; +} + +/** + * Checks that the topology is a sharded cluster. + * + * @param string $uri + * @return boolean + */ +function is_mongos($uri) +{ + return get_primary_server($uri)->getType() === Server::TYPE_MONGOS; +} + +/** + * Checks that the topology is a replica set. + * + * @param string $uri + * @return boolean + */ +function is_replica_set($uri) +{ + return get_primary_server($uri)->getType() === Server::TYPE_RS_PRIMARY; +} + +/** + * Checks if the connection string uses SSL. + * + * @param string $uri + * @return boolean + */ +function is_ssl($uri) +{ + return stripos($uri, 'ssl=true') !== false; +} + +/** + * Checks that the topology is a standalone. + * + * @param string $uri + * @return boolean + */ +function is_standalone($uri) +{ + return get_primary_server($uri)->getType() === Server::TYPE_STANDALONE; +} + +/** + * Converts the server type constant to a string. + * + * @see http://php.net/manual/en/class.mongodb-driver-server.php + * @param integer $type + * @return string + */ +function server_type_as_string($type) +{ + switch ($type) { + case Server::TYPE_STANDALONE: + return 'Standalone'; + case Server::TYPE_MONGOS: + return 'Mongos'; + case Server::TYPE_POSSIBLE_PRIMARY: + return 'PossiblePrimary'; + case Server::TYPE_RS_PRIMARY: + return 'RSPrimary'; + case Server::TYPE_RS_SECONDARY: + return 'RSSecondary'; + case Server::TYPE_RS_ARBITER: + return 'RSArbiter'; + case Server::TYPE_RS_OTHER: + return 'RSOther'; + case Server::TYPE_RS_GHOST: + return 'RSGhost'; + default: + return 'Unknown'; + } +} + +/** + * Converts an errno number to a string. + * + * @see http://php.net/manual/en/errorfunc.constants.php + * @param integer $errno + * @param string + */ +function errno_as_string($errno) +{ + $errors = [ + 'E_ERROR', + 'E_WARNING', + 'E_PARSE', + 'E_NOTICE', + 'E_CORE_ERROR', + 'E_CORE_WARNING', + 'E_COMPILE_ERROR', + 'E_COMPILE_WARNING', + 'E_USER_ERROR', + 'E_USER_WARNING', + 'E_USER_NOTICE', + 'E_STRICT', + 'E_RECOVERABLE_ERROR', + 'E_DEPRECATED', + 'E_USER_DEPRECATED', + 'E_ALL', + ]; + + foreach ($errors as $error) { + if ($errno === constant($error)) { + return $error; + } + } + + return 'Unknown'; +} + /** * Prints a traditional hex dump of byte values and printable characters. *