|
2 | 2 |
|
3 | 3 | namespace MongoDB\Tests\Operation;
|
4 | 4 |
|
| 5 | +use MongoDB\Driver\BulkWrite; |
5 | 6 | use MongoDB\Operation\Count;
|
| 7 | +use MongoDB\Operation\CreateCollection; |
6 | 8 | use MongoDB\Operation\Distinct;
|
7 | 9 | use MongoDB\Operation\Explain;
|
| 10 | +use MongoDB\Operation\Find; |
8 | 11 | use MongoDB\Operation\FindAndModify;
|
| 12 | +use MongoDB\Operation\FindOne; |
9 | 13 | use MongoDB\Operation\InsertMany;
|
10 | 14 |
|
11 | 15 | class ExplainFunctionalTest extends FunctionalTestCase
|
@@ -219,4 +223,176 @@ public function testFindAndModifyQueryPlanner()
|
219 | 223 | $this->assertTrue(array_key_exists('queryPlanner', $result));
|
220 | 224 | $this->assertFalse(array_key_exists('executionStats', $result));
|
221 | 225 | }
|
| 226 | + |
| 227 | + public function testFindAllPlansExecution() |
| 228 | + { |
| 229 | + $this->createFixtures(3); |
| 230 | + |
| 231 | + $operation = new Find($this->getDatabaseName(), $this->getCollectionName(), [], ['readConcern' => $this->createDefaultReadConcern()]); |
| 232 | + |
| 233 | + $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => Explain::VERBOSITY_ALL_PLANS, 'typeMap' => ['root' => 'array']]); |
| 234 | + $result = $explainOperation->execute($this->getPrimaryServer()); |
| 235 | + |
| 236 | + $this->assertTrue(array_key_exists('queryPlanner', $result)); |
| 237 | + $this->assertTrue(array_key_exists('executionStats', $result)); |
| 238 | + $this->assertTrue(array_key_exists('allPlansExecution', $result['executionStats'])); |
| 239 | + } |
| 240 | + |
| 241 | + public function testFindDefaultVerbosity() |
| 242 | + { |
| 243 | + $this->createFixtures(3); |
| 244 | + |
| 245 | + $operation = new Find($this->getDatabaseName(), $this->getCollectionName(), [], ['readConcern' => $this->createDefaultReadConcern()]); |
| 246 | + |
| 247 | + $explainOperation = new Explain($this->getDatabaseName(), $operation, ['typeMap' => ['root' => 'array']]); |
| 248 | + $result = $explainOperation->execute($this->getPrimaryServer()); |
| 249 | + |
| 250 | + $this->assertTrue(array_key_exists('queryPlanner', $result)); |
| 251 | + $this->assertTrue(array_key_exists('executionStats', $result)); |
| 252 | + $this->assertTrue(array_key_exists('allPlansExecution', $result['executionStats'])); |
| 253 | + } |
| 254 | + |
| 255 | + public function testFindExecutionStats() |
| 256 | + { |
| 257 | + $this->createFixtures(3); |
| 258 | + |
| 259 | + $operation = new Find($this->getDatabaseName(), $this->getCollectionName(), [], ['readConcern' => $this->createDefaultReadConcern()]); |
| 260 | + |
| 261 | + $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => Explain::VERBOSITY_EXEC_STATS, 'typeMap' => ['root' => 'array']]); |
| 262 | + $result = $explainOperation->execute($this->getPrimaryServer()); |
| 263 | + |
| 264 | + $this->assertTrue(array_key_exists('queryPlanner', $result)); |
| 265 | + $this->assertTrue(array_key_exists('executionStats', $result)); |
| 266 | + $this->assertFalse(array_key_exists('allPlansExecution', $result['executionStats'])); |
| 267 | + } |
| 268 | + |
| 269 | + public function testFindQueryPlanner() |
| 270 | + { |
| 271 | + $this->createFixtures(3); |
| 272 | + |
| 273 | + $operation = new Find($this->getDatabaseName(), $this->getCollectionName(), [], ['readConcern' => $this->createDefaultReadConcern()]); |
| 274 | + |
| 275 | + $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => Explain::VERBOSITY_QUERY, 'typeMap' => ['root' => 'array']]); |
| 276 | + $result = $explainOperation->execute($this->getPrimaryServer()); |
| 277 | + |
| 278 | + $this->assertTrue(array_key_exists('queryPlanner', $result)); |
| 279 | + $this->assertFalse(array_key_exists('executionStats', $result)); |
| 280 | + } |
| 281 | + |
| 282 | + public function testFindMaxAwait() |
| 283 | + { |
| 284 | + if (version_compare($this->getServerVersion(), '3.2.0', '<')) { |
| 285 | + $this->markTestSkipped('maxAwaitTimeMS option is not supported'); |
| 286 | + } |
| 287 | + |
| 288 | + $maxAwaitTimeMS = 100; |
| 289 | + |
| 290 | + /* Calculate an approximate pivot to use for time assertions. We will |
| 291 | + * assert that the duration of blocking responses is greater than this |
| 292 | + * value, and vice versa. */ |
| 293 | + $pivot = ($maxAwaitTimeMS * 0.001) * 0.9; |
| 294 | + |
| 295 | + // Create a capped collection. |
| 296 | + $databaseName = $this->getDatabaseName(); |
| 297 | + $cappedCollectionName = $this->getCollectionName(); |
| 298 | + $cappedCollectionOptions = [ |
| 299 | + 'capped' => true, |
| 300 | + 'max' => 100, |
| 301 | + 'size' => 1048576, |
| 302 | + ]; |
| 303 | + |
| 304 | + $operation = new CreateCollection($databaseName, $cappedCollectionName, $cappedCollectionOptions); |
| 305 | + $operation->execute($this->getPrimaryServer()); |
| 306 | + |
| 307 | + // Insert documents into the capped collection. |
| 308 | + $bulkWrite = new BulkWrite(['ordered' => true]); |
| 309 | + $bulkWrite->insert(['_id' => 1]); |
| 310 | + $bulkWrite->insert(['_id' => 2]); |
| 311 | + $result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite); |
| 312 | + |
| 313 | + $operation = new Find($databaseName, $cappedCollectionName, [], ['cursorType' => Find::TAILABLE_AWAIT, 'maxAwaitTimeMS' => $maxAwaitTimeMS]); |
| 314 | + |
| 315 | + $explainOperation = new Explain($this->getDatabaseName(), $operation, ['typeMap' => ['root' => 'array']]); |
| 316 | + $result = $explainOperation->execute($this->getPrimaryServer()); |
| 317 | + |
| 318 | + $this->assertTrue(array_key_exists('queryPlanner', $result)); |
| 319 | + $this->assertTrue(array_key_exists('executionStats', $result)); |
| 320 | + $this->assertTrue(array_key_exists('allPlansExecution', $result['executionStats'])); |
| 321 | + } |
| 322 | + |
| 323 | + public function testFindOneAllPlansExecution() |
| 324 | + { |
| 325 | + $this->createFixtures(1); |
| 326 | + |
| 327 | + $operation = new FindOne($this->getDatabaseName(), $this->getCollectionName(), []); |
| 328 | + |
| 329 | + $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => Explain::VERBOSITY_ALL_PLANS, 'typeMap' => ['root' => 'array']]); |
| 330 | + $result = $explainOperation->execute($this->getPrimaryServer()); |
| 331 | + |
| 332 | + $this->assertTrue(array_key_exists('queryPlanner', $result)); |
| 333 | + $this->assertTrue(array_key_exists('executionStats', $result)); |
| 334 | + $this->assertTrue(array_key_exists('allPlansExecution', $result['executionStats'])); |
| 335 | + } |
| 336 | + |
| 337 | + public function testFindOneDefaultVerbosity() |
| 338 | + { |
| 339 | + $this->createFixtures(1); |
| 340 | + |
| 341 | + $operation = new FindOne($this->getDatabaseName(), $this->getCollectionName(), []); |
| 342 | + |
| 343 | + $explainOperation = new Explain($this->getDatabaseName(), $operation, ['typeMap' => ['root' => 'array']]); |
| 344 | + $result = $explainOperation->execute($this->getPrimaryServer()); |
| 345 | + |
| 346 | + $this->assertTrue(array_key_exists('queryPlanner', $result)); |
| 347 | + $this->assertTrue(array_key_exists('executionStats', $result)); |
| 348 | + $this->assertTrue(array_key_exists('allPlansExecution', $result['executionStats'])); |
| 349 | + } |
| 350 | + |
| 351 | + public function testFindOneExecutionStats() |
| 352 | + { |
| 353 | + $this->createFixtures(1); |
| 354 | + |
| 355 | + $operation = new FindOne($this->getDatabaseName(), $this->getCollectionName(), []); |
| 356 | + |
| 357 | + $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => Explain::VERBOSITY_EXEC_STATS, 'typeMap' => ['root' => 'array']]); |
| 358 | + $result = $explainOperation->execute($this->getPrimaryServer()); |
| 359 | + |
| 360 | + $this->assertTrue(array_key_exists('queryPlanner', $result)); |
| 361 | + $this->assertTrue(array_key_exists('executionStats', $result)); |
| 362 | + $this->assertFalse(array_key_exists('allPlansExecution', $result['executionStats'])); |
| 363 | + } |
| 364 | + |
| 365 | + public function testFindOneQueryPlanner() |
| 366 | + { |
| 367 | + $this->createFixtures(1); |
| 368 | + |
| 369 | + $operation = new FindOne($this->getDatabaseName(), $this->getCollectionName(), []); |
| 370 | + |
| 371 | + $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => Explain::VERBOSITY_QUERY, 'typeMap' => ['root' => 'array']]); |
| 372 | + $result = $explainOperation->execute($this->getPrimaryServer()); |
| 373 | + |
| 374 | + $this->assertTrue(array_key_exists('queryPlanner', $result)); |
| 375 | + $this->assertFalse(array_key_exists('executionStats', $result)); |
| 376 | + } |
| 377 | + |
| 378 | + /** |
| 379 | + * Create data fixtures. |
| 380 | + * |
| 381 | + * @param integer $n |
| 382 | + */ |
| 383 | + private function createFixtures($n) |
| 384 | + { |
| 385 | + $bulkWrite = new BulkWrite(['ordered' => true]); |
| 386 | + |
| 387 | + for ($i = 1; $i <= $n; $i++) { |
| 388 | + $bulkWrite->insert([ |
| 389 | + '_id' => $i, |
| 390 | + 'x' => (object) ['foo' => 'bar'], |
| 391 | + ]); |
| 392 | + } |
| 393 | + |
| 394 | + $result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite); |
| 395 | + |
| 396 | + $this->assertEquals($n, $result->getInsertedCount()); |
| 397 | + } |
222 | 398 | }
|
0 commit comments