= 0x20 && $i <= 0x7E) ? chr($i) : $pad; } } $hex = str_split(bin2hex($data), $width * 2); $chars = str_split(strtr($data, $from, $to), $width); $offset = 0; $length = $width * 3; foreach ($hex as $i => $line) { printf("%6X : %-{$length}s [%s]\n", $offset, implode(' ', str_split($line, 2)), $chars[$i]); $offset += $width; } } /** * Return a collection name to use for the test file. * * The filename will be stripped of the base path to the test suite (prefix) as * well as the PHP file extension (suffix). Special characters (including hyphen * for shell compatibility) will be replaced with underscores. * * @param string $filename * @return string */ function makeCollectionNameFromFilename($filename) { $filename = realpath($filename); $prefix = realpath(dirname(__FILE__) . '/..') . DIRECTORY_SEPARATOR; $replacements = array( // Strip test path prefix sprintf('/^%s/', preg_quote($prefix, '/')) => '', // Strip file extension suffix '/\.php$/' => '', // SKIPIFs add ".skip" between base name and extension '/\.skip$/' => '', // Replace special characters with underscores sprintf('/[%s]/', preg_quote('-$/\\', '/')) => '_', ); return preg_replace(array_keys($replacements), array_values($replacements), $filename); } function CLEANUP($uri) { try { $manager = new MongoDB\Driver\Manager($uri); $cmd = new MongoDB\Driver\Command(array("drop" => COLLECTION_NAME)); $rp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY); try { $manager->executeCommand(DATABASE_NAME, $cmd, $rp); } catch(Exception $e) { do { /* ns not found */ if ($e->getCode() == 59) { continue; } throw $e; } while (0); } } catch(Exception $e) { echo "skip (cleanup)" . $e->getCode(), ": ", $e->getMessage(); exit(1); } } function throws(callable $function, $exceptionname, $infunction = null) { try { $function(); } catch(Exception $e) { if ($e instanceof $exceptionname) { if ($infunction) { $function = $e->getTrace()[0]["function"]; if (strcasecmp($function, $infunction) == 0) { printf("OK: Got %s thrown from %s\n", $exceptionname, $infunction); } else { printf("ALMOST: Got %s - but was thrown in %s, not %s\n", $exceptionname, $function, $infunction); } return $e->getMessage(); } printf("OK: Got %s\n", $exceptionname); } else { printf("ALMOST: Got %s - expected %s\n", get_class($e), $exceptionname); } return $e->getMessage(); } echo "FAILED: Expected $exceptionname thrown!\n"; } function printServer(MongoDB\Driver\Server $server) { printf("server: %s:%d\n", $server->getHost(), $server->getPort()); } function printWriteResult(MongoDB\Driver\WriteResult $result, $details = true) { printServer($result->getServer()); printf("insertedCount: %d\n", $result->getInsertedCount()); printf("matchedCount: %d\n", $result->getMatchedCount()); printf("modifiedCount: %d\n", $result->getModifiedCount()); printf("upsertedCount: %d\n", $result->getUpsertedCount()); printf("deletedCount: %d\n", $result->getDeletedCount()); foreach ($result->getUpsertedIds() as $index => $id) { printf("upsertedId[%d]: ", $index); var_dump($id); } $writeConcernError = $result->getWriteConcernError(); printWriteConcernError($writeConcernError ? $writeConcernError : null, $details); foreach ($result->getWriteErrors() as $writeError) { printWriteError($writeError); } } function printWriteConcernError(MongoDB\Driver\WriteConcernError $error = null, $details) { if ($error) { /* This stuff is generated by the server, no need for us to test it */ if (!$details) { printf("writeConcernError: %s (%d)\n", $error->getMessage(), $error->getCode()); return; } var_dump($error); printf("writeConcernError.message: %s\n", $error->getMessage()); printf("writeConcernError.code: %d\n", $error->getCode()); printf("writeConcernError.info: "); var_dump($error->getInfo()); } } function printWriteError(MongoDB\Driver\WriteError $error) { var_dump($error); printf("writeError[%d].message: %s\n", $error->getIndex(), $error->getMessage()); printf("writeError[%d].code: %d\n", $error->getIndex(), $error->getCode()); } function getInsertCount($retval) { return $retval->getInsertedCount(); } function getModifiedCount($retval) { return $retval->getModifiedCount(); } function getDeletedCount($retval) { return $retval->getDeletedCount(); } function getUpsertedCount($retval) { return $retval->getUpsertedCount(); } function getWriteErrors($retval) { return (array)$retval->getWriteErrors(); } function def($arr) { foreach($arr as $const => $value) { define($const, getenv("PHONGO_TEST_$const") ?: $value); } } function configureFailPoint(MongoDB\Driver\Manager $manager, $failPoint, $mode, $data = array()) { $doc = array( "configureFailPoint" => $failPoint, "mode" => $mode, ); if ($data) { $doc["data"] = $data; } $cmd = new MongoDB\Driver\Command($doc); $result = $manager->executeCommand("admin", $cmd); if (empty($result->toArray()["ok"])) { var_dump($result); throw new RuntimeException("Failpoint failed"); } return true; } function failMaxTimeMS(MongoDB\Driver\Manager $manager) { return configureFailPoint($manager, "maxTimeAlwaysTimeOut", array("times" => 1)); }