Skip to content

Commit bbbda03

Browse files
committed
Seperate code from functions
1 parent 8ad0cd4 commit bbbda03

File tree

2 files changed

+171
-172
lines changed

2 files changed

+171
-172
lines changed

tests/utils/basic.inc

Lines changed: 1 addition & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -1,181 +1,10 @@
11
<?php
2+
require __DIR__ . "/" . "tools.php";
23

3-
/**
4-
* Prints a traditional hex dump of byte values and printable characters.
5-
*
6-
* @see http://stackoverflow.com/a/4225813/162228
7-
* @param string $data Binary data
8-
* @param integer $width Bytes displayed per line
9-
*/
10-
function hex_dump($data, $width = 16)
11-
{
12-
static $pad = '.'; // Placeholder for non-printable characters
13-
static $from = '';
14-
static $to = '';
154

16-
if ($from === '') {
17-
for ($i = 0; $i <= 0xFF; $i++) {
18-
$from .= chr($i);
19-
$to .= ($i >= 0x20 && $i <= 0x7E) ? chr($i) : $pad;
20-
}
21-
}
22-
23-
$hex = str_split(bin2hex($data), $width * 2);
24-
$chars = str_split(strtr($data, $from, $to), $width);
25-
26-
$offset = 0;
27-
$length = $width * 3;
28-
29-
foreach ($hex as $i => $line) {
30-
printf("%6X : %-{$length}s [%s]\n", $offset, implode(' ', str_split($line, 2)), $chars[$i]);
31-
$offset += $width;
32-
}
33-
}
34-
35-
/**
36-
* Return a collection name to use for the test file.
37-
*
38-
* The filename will be stripped of the base path to the test suite (prefix) as
39-
* well as the PHP file extension (suffix). Special characters (including hyphen
40-
* for shell compatibility) will be replaced with underscores.
41-
*
42-
* @param string $filename
43-
* @return string
44-
*/
45-
function makeCollectionNameFromFilename($filename)
46-
{
47-
$filename = realpath($filename);
48-
$prefix = realpath(dirname(__FILE__) . '/..') . DIRECTORY_SEPARATOR;
49-
50-
$replacements = array(
51-
// Strip test path prefix
52-
sprintf('/^%s/', preg_quote($prefix, '/')) => '',
53-
// Strip file extension suffix
54-
'/\.php$/' => '',
55-
// SKIPIFs add ".skip" between base name and extension
56-
'/\.skip$/' => '',
57-
// Replace special characters with underscores
58-
sprintf('/[%s]/', preg_quote('-$/\\', '/')) => '_',
59-
);
60-
61-
return preg_replace(array_keys($replacements), array_values($replacements), $filename);
625
}
636

64-
function LOAD_FIXTURES() {
65-
try {
66-
$mc = new MongoDB\Manager(MONGODB_CLEANUP_URI);
67-
$retval = $mc->executeInsert(NS, array("my" => "demo", array("document" => "with", "data")));
68-
} catch(Exception $e) {
69-
echo "skip " . $e->getCode(), ": ", $e->getMessage();
70-
}
71-
}
72-
function CLEANUP() {
73-
try {
74-
$mc = new MongoDB\Manager(MONGODB_CLEANUP_URI);
75-
$cmd = new MongoDB\Command(array("drop" => COLLECTION_NAME));
76-
$rp = new MongoDB\ReadPreference(MongoDB\ReadPreference::RP_PRIMARY);
77-
try {
78-
$mc->executeCommand(DATABASE_NAME, $cmd, $rp);
79-
} catch(Exception $e) {
80-
do {
81-
/* ns not found */
82-
if ($e->getCode() == 59) {
83-
continue;
84-
}
85-
throw $e;
86-
} while (0);
87-
}
88-
} catch(Exception $e) {
89-
echo "skip " . $e->getCode(), ": ", $e->getMessage();
90-
}
91-
}
92-
93-
function throws(callable $function, $exceptionname, $infunction = null) {
94-
try {
95-
$function();
96-
} catch(Exception $e) {
97-
if ($e instanceof $exceptionname) {
98-
if ($infunction) {
99-
$function = $e->getTrace()[0]["function"];
100-
if (strcasecmp($function, $infunction) == 0) {
101-
printf("OK: Got %s thrown from %s\n", $exceptionname, $infunction);
102-
} else {
103-
printf("ALMOST: Got %s - but was thrown in %s, not %s\n", $exceptionname, $function, $infunction);
104-
}
105-
return;
106-
}
107-
printf("OK: Got %s\n", $exceptionname);
108-
} else {
109-
printf("ALMOST: Got %s - expected %s\n", get_class($e), $exceptionname);
110-
}
111-
return;
112-
}
113-
echo "FAILED: Expected $exceptionname thrown!\n";
114-
}
115-
function printServer(MongoDB\Server $server)
116-
{
117-
printf("server: %s:%d\n", $server->getHost(), $server->getPort());
118-
}
119-
120-
function printWriteResult(MongoDB\WriteResult $result)
121-
{
122-
printServer($result->getServer());
123-
124-
printf("insertedCount: %d\n", $result->getInsertedCount());
125-
printf("matchedCount: %d\n", $result->getMatchedCount());
126-
printf("modifiedCount: %d\n", $result->getModifiedCount());
127-
printf("upsertedCount: %d\n", $result->getUpsertedCount());
128-
printf("deletedCount: %d\n", $result->getDeletedCount());
129-
130-
foreach ($result->getUpsertedIds() as $index => $id) {
131-
printf("upsertedId[%d]: ", $index);
132-
var_dump($id);
133-
}
1347

135-
foreach ($result->getWriteConcernErrors() as $writeConcernError) {
136-
printWriteConcernError($writeConcernError);
137-
}
138-
139-
foreach ($result->getWriteErrors() as $writeError) {
140-
printWriteError($writeError);
141-
}
142-
}
143-
144-
function printWriteConcernError(MongoDB\WriteConcernError $error)
145-
{
146-
printf("writeConcernError.message: %s\n", $error->getMessage());
147-
printf("writeConcernError.code: %d\n", $error->getCode());
148-
printf("writeConcernError.info: ");
149-
var_dump($error->info());
150-
}
151-
152-
function printWriteError(MongoDB\WriteError $error)
153-
{
154-
printf("writeError[%d].message: %s\n", $error->getIndex(), $error->getMessage());
155-
printf("writeError[%d].code: %d\n", $error->getIndex(), $error->getCode());
156-
}
157-
158-
function getInsertCount($retval) {
159-
return $retval->getInsertedCount();
160-
}
161-
function getModifiedCount($retval) {
162-
return $retval->getModifiedCount();
163-
}
164-
function getDeletedCount($retval) {
165-
return $retval->getDeletedCount();
166-
}
167-
function getUpsertedCount($retval) {
168-
return $retval->getUpsertedCount();
169-
}
170-
function getWriteErrors($retval) {
171-
return (array)$retval->getWriteErrors();
172-
}
173-
174-
function def($arr) {
175-
foreach($arr as $const => $value) {
176-
define($const, getenv("PHONGO_TEST_$const") ?: $value);
177-
}
178-
}
1798
$consts = array(
1809
"MONGODB_URI" => "mongodb://localhost:27017",
18110
"MONGODB_CLEANUP_URI" => "mongodb://localhost:27017",

tests/utils/tools.php

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
/**
3+
* Prints a traditional hex dump of byte values and printable characters.
4+
*
5+
* @see http://stackoverflow.com/a/4225813/162228
6+
* @param string $data Binary data
7+
* @param integer $width Bytes displayed per line
8+
*/
9+
function hex_dump($data, $width = 16)
10+
{
11+
static $pad = '.'; // Placeholder for non-printable characters
12+
static $from = '';
13+
static $to = '';
14+
15+
if ($from === '') {
16+
for ($i = 0; $i <= 0xFF; $i++) {
17+
$from .= chr($i);
18+
$to .= ($i >= 0x20 && $i <= 0x7E) ? chr($i) : $pad;
19+
}
20+
}
21+
22+
$hex = str_split(bin2hex($data), $width * 2);
23+
$chars = str_split(strtr($data, $from, $to), $width);
24+
25+
$offset = 0;
26+
$length = $width * 3;
27+
28+
foreach ($hex as $i => $line) {
29+
printf("%6X : %-{$length}s [%s]\n", $offset, implode(' ', str_split($line, 2)), $chars[$i]);
30+
$offset += $width;
31+
}
32+
}
33+
34+
/**
35+
* Return a collection name to use for the test file.
36+
*
37+
* The filename will be stripped of the base path to the test suite (prefix) as
38+
* well as the PHP file extension (suffix). Special characters (including hyphen
39+
* for shell compatibility) will be replaced with underscores.
40+
*
41+
* @param string $filename
42+
* @return string
43+
*/
44+
function makeCollectionNameFromFilename($filename)
45+
{
46+
$filename = realpath($filename);
47+
$prefix = realpath(dirname(__FILE__) . '/..') . DIRECTORY_SEPARATOR;
48+
49+
$replacements = array(
50+
// Strip test path prefix
51+
sprintf('/^%s/', preg_quote($prefix, '/')) => '',
52+
// Strip file extension suffix
53+
'/\.php$/' => '',
54+
// SKIPIFs add ".skip" between base name and extension
55+
'/\.skip$/' => '',
56+
// Replace special characters with underscores
57+
sprintf('/[%s]/', preg_quote('-$/\\', '/')) => '_',
58+
);
59+
60+
return preg_replace(array_keys($replacements), array_values($replacements), $filename);
61+
}
62+
63+
function CLEANUP() {
64+
try {
65+
$mc = new MongoDB\Manager(MONGODB_CLEANUP_URI);
66+
$cmd = new MongoDB\Command(array("drop" => COLLECTION_NAME));
67+
$rp = new MongoDB\ReadPreference(MongoDB\ReadPreference::RP_PRIMARY);
68+
try {
69+
$mc->executeCommand(DATABASE_NAME, $cmd, $rp);
70+
} catch(Exception $e) {
71+
do {
72+
/* ns not found */
73+
if ($e->getCode() == 59) {
74+
continue;
75+
}
76+
throw $e;
77+
} while (0);
78+
}
79+
} catch(Exception $e) {
80+
echo "skip " . $e->getCode(), ": ", $e->getMessage();
81+
}
82+
}
83+
84+
function throws(callable $function, $exceptionname, $infunction = null) {
85+
try {
86+
$function();
87+
} catch(Exception $e) {
88+
if ($e instanceof $exceptionname) {
89+
if ($infunction) {
90+
$function = $e->getTrace()[0]["function"];
91+
if (strcasecmp($function, $infunction) == 0) {
92+
printf("OK: Got %s thrown from %s\n", $exceptionname, $infunction);
93+
} else {
94+
printf("ALMOST: Got %s - but was thrown in %s, not %s\n", $exceptionname, $function, $infunction);
95+
}
96+
return;
97+
}
98+
printf("OK: Got %s\n", $exceptionname);
99+
} else {
100+
printf("ALMOST: Got %s - expected %s\n", get_class($e), $exceptionname);
101+
}
102+
return;
103+
}
104+
echo "FAILED: Expected $exceptionname thrown!\n";
105+
}
106+
function printServer(MongoDB\Server $server)
107+
{
108+
printf("server: %s:%d\n", $server->getHost(), $server->getPort());
109+
}
110+
111+
function printWriteResult(MongoDB\WriteResult $result)
112+
{
113+
printServer($result->getServer());
114+
115+
printf("insertedCount: %d\n", $result->getInsertedCount());
116+
printf("matchedCount: %d\n", $result->getMatchedCount());
117+
printf("modifiedCount: %d\n", $result->getModifiedCount());
118+
printf("upsertedCount: %d\n", $result->getUpsertedCount());
119+
printf("deletedCount: %d\n", $result->getDeletedCount());
120+
121+
foreach ($result->getUpsertedIds() as $index => $id) {
122+
printf("upsertedId[%d]: ", $index);
123+
var_dump($id);
124+
}
125+
126+
foreach ($result->getWriteConcernErrors() as $writeConcernError) {
127+
printWriteConcernError($writeConcernError);
128+
}
129+
130+
foreach ($result->getWriteErrors() as $writeError) {
131+
printWriteError($writeError);
132+
}
133+
}
134+
135+
function printWriteConcernError(MongoDB\WriteConcernError $error)
136+
{
137+
printf("writeConcernError.message: %s\n", $error->getMessage());
138+
printf("writeConcernError.code: %d\n", $error->getCode());
139+
printf("writeConcernError.info: ");
140+
var_dump($error->info());
141+
}
142+
143+
function printWriteError(MongoDB\WriteError $error)
144+
{
145+
printf("writeError[%d].message: %s\n", $error->getIndex(), $error->getMessage());
146+
printf("writeError[%d].code: %d\n", $error->getIndex(), $error->getCode());
147+
}
148+
149+
function getInsertCount($retval) {
150+
return $retval->getInsertedCount();
151+
}
152+
function getModifiedCount($retval) {
153+
return $retval->getModifiedCount();
154+
}
155+
function getDeletedCount($retval) {
156+
return $retval->getDeletedCount();
157+
}
158+
function getUpsertedCount($retval) {
159+
return $retval->getUpsertedCount();
160+
}
161+
function getWriteErrors($retval) {
162+
return (array)$retval->getWriteErrors();
163+
}
164+
165+
function def($arr) {
166+
foreach($arr as $const => $value) {
167+
define($const, getenv("PHONGO_TEST_$const") ?: $value);
168+
}
169+
}
170+

0 commit comments

Comments
 (0)