|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Tests\SpecTests; |
| 4 | + |
| 5 | +use stdClass; |
| 6 | + |
| 7 | +/** |
| 8 | + * Command monitoring spec tests. |
| 9 | + * |
| 10 | + * @see https://github.com/mongodb/specifications/tree/master/source/command-monitoring |
| 11 | + */ |
| 12 | +class CommandMonitoringSpecTest extends FunctionalTestCase |
| 13 | +{ |
| 14 | + /** |
| 15 | + * Assert that the expected and actual command documents match. |
| 16 | + * |
| 17 | + * Note: this method may modify the $expected object. |
| 18 | + * |
| 19 | + * @param stdClass $expected Expected command document |
| 20 | + * @param stdClass $actual Actual command document |
| 21 | + */ |
| 22 | + public static function assertCommandMatches(stdClass $expected, stdClass $actual) |
| 23 | + { |
| 24 | + if (isset($expected->getMore) && $expected->getMore === 42) { |
| 25 | + static::assertObjectHasAttribute('getMore', $actual); |
| 26 | + static::assertThat($actual->getMore, static::logicalOr( |
| 27 | + static::isInstanceOf(Int64::class), |
| 28 | + static::isType('integer') |
| 29 | + )); |
| 30 | + unset($expected->getMore); |
| 31 | + } |
| 32 | + |
| 33 | + if (isset($expected->killCursors) && isset($expected->cursors) && is_array($expected->cursors)) { |
| 34 | + static::assertObjectHasAttribute('cursors', $actual); |
| 35 | + static::assertInternalType('array', $actual->cursors); |
| 36 | + |
| 37 | + foreach ($expected->cursors as $i => $cursorId) { |
| 38 | + static::assertArrayHasKey($i, $actual->cursors); |
| 39 | + |
| 40 | + if ($cursorId === 42) { |
| 41 | + static::assertThat($actual->cursors[$i], static::logicalOr( |
| 42 | + static::isInstanceOf(Int64::class), |
| 43 | + static::isType('integer') |
| 44 | + )); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + unset($expected->cursors); |
| 49 | + } |
| 50 | + |
| 51 | + static::assertDocumentsMatch($expected, $actual); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Assert that the expected and actual command reply documents match. |
| 56 | + * |
| 57 | + * Note: this method may modify the $expectedReply object. |
| 58 | + * |
| 59 | + * @param stdClass $expected Expected command reply document |
| 60 | + * @param stdClass $actual Actual command reply document |
| 61 | + */ |
| 62 | + public static function assertCommandReplyMatches(stdClass $expected, stdClass $actual) |
| 63 | + { |
| 64 | + if (isset($expected->cursor->id) && $expected->cursor->id === 42) { |
| 65 | + static::assertObjectHasAttribute('cursor', $actual); |
| 66 | + static::assertInternalType('object', $actual->cursor); |
| 67 | + static::assertObjectHasAttribute('id', $actual->cursor); |
| 68 | + static::assertThat($actual->cursor->id, static::logicalOr( |
| 69 | + static::isInstanceOf(Int64::class), |
| 70 | + static::isType('integer') |
| 71 | + )); |
| 72 | + unset($expected->cursor->id); |
| 73 | + } |
| 74 | + |
| 75 | + if (isset($expected->cursorsUnknown) && is_array($expected->cursorsUnknown)) { |
| 76 | + static::assertObjectHasAttribute('cursorsUnknown', $actual); |
| 77 | + static::assertInternalType('array', $actual->cursorsUnknown); |
| 78 | + |
| 79 | + foreach ($expected->cursorsUnknown as $i => $cursorId) { |
| 80 | + static::assertArrayHasKey($i, $actual->cursorsUnknown); |
| 81 | + |
| 82 | + if ($cursorId === 42) { |
| 83 | + static::assertThat($actual->cursorsUnknown[$i], static::logicalOr( |
| 84 | + static::isInstanceOf(Int64::class), |
| 85 | + static::isType('integer') |
| 86 | + )); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + unset($expected->cursorsUnknown); |
| 91 | + } |
| 92 | + |
| 93 | + if (isset($expected->ok) && is_numeric($expected->ok)) { |
| 94 | + static::assertObjectHasAttribute('ok', $actual); |
| 95 | + static::assertInternalType('numeric', $actual->ok); |
| 96 | + static::assertEquals($expected->ok, $actual->ok); |
| 97 | + unset($expected->ok); |
| 98 | + } |
| 99 | + |
| 100 | + if (isset($expected->writeErrors) && is_array($expected->writeErrors)) { |
| 101 | + static::assertObjectHasAttribute('writeErrors', $actual); |
| 102 | + static::assertInternalType('array', $actual->writeErrors); |
| 103 | + |
| 104 | + foreach ($expected->writeErrors as $i => $expectedWriteError) { |
| 105 | + static::assertArrayHasKey($i, $actual->writeErrors); |
| 106 | + $actualWriteError = $actual->writeErrors[$i]; |
| 107 | + |
| 108 | + if (isset($expectedWriteError->code) && $expectedWriteError->code === 42) { |
| 109 | + static::assertObjectHasAttribute('code', $actualWriteError); |
| 110 | + static::assertThat($actualWriteError->code, static::logicalOr( |
| 111 | + static::isInstanceOf(Int64::class), |
| 112 | + static::isType('integer') |
| 113 | + )); |
| 114 | + unset($expected->writeErrors[$i]->code); |
| 115 | + } |
| 116 | + |
| 117 | + if (isset($expectedWriteError->errmsg) && $expectedWriteError->errmsg === '') { |
| 118 | + static::assertObjectHasAttribute('errmsg', $actualWriteError); |
| 119 | + static::assertInternalType('string', $actualWriteError->errmsg); |
| 120 | + static::assertNotEmpty($actualWriteError->errmsg); |
| 121 | + unset($expected->writeErrors[$i]->errmsg); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + static::assertDocumentsMatch($expected, $actual); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Execute an individual test case from the specification. |
| 131 | + * |
| 132 | + * @dataProvider provideTests |
| 133 | + * @param string $name Test name |
| 134 | + * @param stdClass $test Individual "tests[]" document |
| 135 | + * @param array $data Top-level "data" array to initialize collection |
| 136 | + * @param string $databaseName Name of database under test |
| 137 | + * @param string $collectionName Name of collection under test |
| 138 | + */ |
| 139 | + public function testCommandMonitoring($name, stdClass $test, array $data, $databaseName = null, $collectionName = null) |
| 140 | + { |
| 141 | + $this->setName($name); |
| 142 | + |
| 143 | + $this->checkServerRequirements($this->createRunOn($test)); |
| 144 | + |
| 145 | + $databaseName = isset($databaseName) ? $databaseName : $this->getDatabaseName(); |
| 146 | + $collectionName = isset($collectionName) ? $collectionName : $this->getCollectionName(); |
| 147 | + |
| 148 | + $context = Context::fromCommandMonitoring($test, $databaseName, $collectionName); |
| 149 | + $this->setContext($context); |
| 150 | + |
| 151 | + $this->dropTestAndOutcomeCollections(); |
| 152 | + $this->insertDataFixtures($data); |
| 153 | + |
| 154 | + if (isset($test->expectations)) { |
| 155 | + $commandExpectations = CommandExpectations::fromCommandMonitoring($test->expectations); |
| 156 | + $commandExpectations->startMonitoring(); |
| 157 | + } |
| 158 | + |
| 159 | + Operation::fromCommandMonitoring($test->operation)->assert($this, $context); |
| 160 | + |
| 161 | + if (isset($commandExpectations)) { |
| 162 | + $commandExpectations->stopMonitoring(); |
| 163 | + $commandExpectations->assert($this, $context); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + public function provideTests() |
| 168 | + { |
| 169 | + $testArgs = []; |
| 170 | + |
| 171 | + foreach (glob(__DIR__ . '/command-monitoring/*.json') as $filename) { |
| 172 | + $json = $this->decodeJson(file_get_contents($filename)); |
| 173 | + $group = basename($filename, '.json'); |
| 174 | + $data = isset($json->data) ? $json->data : []; |
| 175 | + $databaseName = isset($json->database_name) ? $json->database_name : null; |
| 176 | + $collectionName = isset($json->collection_name) ? $json->collection_name : null; |
| 177 | + |
| 178 | + foreach ($json->tests as $test) { |
| 179 | + $name = $group . ': ' . $test->description; |
| 180 | + $testArgs[] = [$name, $test, $data, $databaseName, $collectionName]; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + return $testArgs; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Convert the server and topology requirements to a standard "runOn" array |
| 189 | + * used by other specifications. |
| 190 | + * |
| 191 | + * @param stdClass $test |
| 192 | + * @return array |
| 193 | + */ |
| 194 | + private function createRunOn(stdClass $test) |
| 195 | + { |
| 196 | + $req = new stdClass; |
| 197 | + |
| 198 | + $topologies = [ |
| 199 | + self::TOPOLOGY_SINGLE, |
| 200 | + self::TOPOLOGY_REPLICASET, |
| 201 | + self::TOPOLOGY_SHARDED |
| 202 | + ]; |
| 203 | + |
| 204 | + /* Append ".99" as patch version, since command monitoring tests expect |
| 205 | + * the minor version to be an inclusive upper bound. */ |
| 206 | + if (isset($test->ignore_if_server_version_greater_than)) { |
| 207 | + $req->maxServerVersion = $test->ignore_if_server_version_greater_than . '.99'; |
| 208 | + } |
| 209 | + |
| 210 | + if (isset($test->ignore_if_server_version_less_than)) { |
| 211 | + $req->minServerVersion = $test->ignore_if_server_version_less_than; |
| 212 | + } |
| 213 | + |
| 214 | + if (isset($test->ignore_if_topology_type)) { |
| 215 | + $req->topology = array_diff($topologies, $test->ignore_if_topology_type); |
| 216 | + } |
| 217 | + |
| 218 | + return [$req]; |
| 219 | + } |
| 220 | +} |
0 commit comments