|
1 | 1 | <?php
|
| 2 | +require __DIR__ . "/" . "tools.php"; |
2 | 3 |
|
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 = ''; |
15 | 4 |
|
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); |
62 | 5 | }
|
63 | 6 |
|
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 |
| - } |
134 | 7 |
|
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 |
| -} |
179 | 8 | $consts = array(
|
180 | 9 | "MONGODB_URI" => "mongodb://localhost:27017",
|
181 | 10 | "MONGODB_CLEANUP_URI" => "mongodb://localhost:27017",
|
|
0 commit comments