Files
mongo-php-driver/tests/utils/basic.inc
Hannes Magnusson 173310ee7a api changes
2014-07-16 17:59:24 +00:00

137 lines
3.8 KiB
PHP

<?php
/* Stolen from the intertubes */
function hex_dump($data)
{
static $from = '';
static $to = '';
static $width = 16; # number of bytes per line
static $pad = '.'; # padding for non-visible characters
if ($from==='')
{
for ($i=0; $i<=0xFF; $i++)
{
$from .= chr($i);
$to .= ($i >= 0x20 && $i <= 0x7E) ? chr($i) : $pad;
}
}
$hex = str_split(bin2hex($data), $width*2);
$chars = str_split(strtr($data, $from, $to), $width);
$offset = 0;
foreach ($hex as $i => $line)
{
$length = $width * 3;
$dump = sprintf("%-{$length}s", implode(' ', str_split($line,2)));
echo sprintf('%6X',$offset).' : '.$dump . ' [' . $chars[$i] . ']'."\n";
$offset += $width;
}
}
function makeCollectionNameFromFilename($filename) {
$rp = realpath($filename);
$search = "tests". DIRECTORY_SEPARATOR;
// relative path from tests/
$base = substr($rp, strpos($rp, $search)+strlen($search));
$dir = dirname($base) . DIRECTORY_SEPARATOR;
// Its the root of tests/
if ($dir[0] == ".") {
$dir = "";
}
$basename = basename($filename);
/* SKIPIFs are named [basename].skip.php */
$base = strstr($basename, ".", true);
// directory/testname
$retval = $dir . $base;
// directory-testname
$retval = str_replace(DIRECTORY_SEPARATOR, "-", $retval);
return $retval;
}
function LOAD_FIXTURES() {
try {
$mc = new MongoDB\Manager(MONGODB_CLEANUP_URI);
$cmd = new MongoDB\Command(array("drop" => COLLECTION_NAME));
$rp = new MongoDB\ReadPreference(MongoDB\ReadPreference::RP_PRIMARY);
$retval = $mc->executeInsert(NS, array("my" => "demo", array("document" => "with", "data")));
} catch(Exception $e) {
echo "skip " . $e->getCode(), ": ", $e->getMessage();
}
}
function CLEANUP() {
try {
$mc = new MongoDB\Manager(MONGODB_CLEANUP_URI);
$cmd = new MongoDB\Command(array("drop" => COLLECTION_NAME));
$rp = new MongoDB\ReadPreference(MongoDB\ReadPreference::RP_PRIMARY);
try {
$mc->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 " . $e->getCode(), ": ", $e->getMessage();
}
}
function getInsertCount($retval) {
return $retval->getNumInserted();
}
function getModifiedCount($retval) {
return $retval->getNumModified();
}
function getRemovedCount($retval) {
return $retval->getNumRemoved();
}
function getUpsertedCount($retval) {
return $retval->getNumUpserted();
}
function getWriteErrors($retval) {
return (array)$retval->getWriteErrors();
}
function def($arr) {
foreach($arr as $const => $value) {
define($const, getenv("PHONGO_TEST_$const") ?: $value);
}
}
$consts = array(
"MONGODB_URI" => "mongodb://localhost:27017",
"MONGODB_CLEANUP_URI" => "mongodb://localhost:27017",
"DATABASE_NAME" => "TESTS",
"COLLECTION_NAME" => makeCollectionNameFromFilename($_SERVER["SCRIPT_FILENAME"]),
"DEBUG_DIR" => sys_get_temp_dir() . "/PHONGO-TESTS/",
);
def($consts);
// These use values from constants defined above
$consts = array(
"NS" => DATABASE_NAME . "." . COLLECTION_NAME,
"DEBUG_FILENAME" => DEBUG_DIR . DATABASE_NAME . "." . COLLECTION_NAME,
);
def($consts);
if (!is_dir(DEBUG_DIR)) {
mkdir(DEBUG_DIR, 0777, true);
}
ini_set("phongo.debug_log", DEBUG_FILENAME);
file_put_contents(DEBUG_FILENAME, sprintf("===> %s <=== %s\n", date(DATE_ISO8601), $_SERVER["SCRIPT_FILENAME"]), FILE_APPEND);