ExplainFunctionalTest.php 17 KB
Newer Older
1 2 3 4
<?php

namespace MongoDB\Tests\Operation;

5
use MongoDB\Driver\BulkWrite;
6
use MongoDB\Operation\Count;
7
use MongoDB\Operation\CreateCollection;
8
use MongoDB\Operation\Delete;
9 10
use MongoDB\Operation\DeleteMany;
use MongoDB\Operation\DeleteOne;
11
use MongoDB\Operation\Distinct;
12
use MongoDB\Operation\Explain;
13
use MongoDB\Operation\Find;
14
use MongoDB\Operation\FindAndModify;
15
use MongoDB\Operation\FindOne;
16 17 18
use MongoDB\Operation\FindOneAndDelete;
use MongoDB\Operation\FindOneAndReplace;
use MongoDB\Operation\FindOneAndUpdate;
19
use MongoDB\Operation\Update;
20 21
use MongoDB\Operation\UpdateMany;
use MongoDB\Operation\UpdateOne;
22
use MongoDB\Tests\CommandObserver;
23
use function version_compare;
24 25 26

class ExplainFunctionalTest extends FunctionalTestCase
{
27 28 29 30
    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testCount($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
31
    {
32
        $this->createFixtures(3);
33 34

        $operation = new Count($this->getDatabaseName(), $this->getCollectionName(), ['x' => ['$gte' => 1]], []);
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
        $result = $explainOperation->execute($this->getPrimaryServer());

        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
    }

    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testDelete($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
    {
        $this->createFixtures(3);

        $filter = ['_id' => 1];

        $operation = new Delete($this->getDatabaseName(), $this->getCollectionName(), $filter, 1);

53
        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
54 55
        $result = $explainOperation->execute($this->getPrimaryServer());

56
        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
57 58
    }

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testDeleteMany($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
    {
        $this->createFixtures(3);

        $filter = ['_id' => ['$gt' => 1]];

        $operation = new DeleteMany($this->getDatabaseName(), $this->getCollectionName(), $filter);

        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
        $result = $explainOperation->execute($this->getPrimaryServer());

        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
    }

    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testDeleteOne($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
    {
        $this->createFixtures(3);

        $filter = ['_id' => 1];

        $operation = new DeleteOne($this->getDatabaseName(), $this->getCollectionName(), $filter);

        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
        $result = $explainOperation->execute($this->getPrimaryServer());

        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
    }

93 94 95 96
    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testDistinct($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
97 98
    {
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
99
            $this->markTestSkipped('Explaining distinct command requires server version >= 3.2');
100 101 102 103
        }

        $operation = new Distinct($this->getDatabaseName(), $this->getCollectionName(), 'x', []);

104
        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
105 106
        $result = $explainOperation->execute($this->getPrimaryServer());

107
        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
108 109
    }

110 111 112 113
    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testFindAndModify($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
114 115
    {
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
116
            $this->markTestSkipped('Explaining findAndModify command requires server version >= 3.2');
117 118 119 120
        }

        $operation = new FindAndModify($this->getDatabaseName(), $this->getCollectionName(), ['remove' => true]);

121
        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
122 123
        $result = $explainOperation->execute($this->getPrimaryServer());

124
        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
125
    }
126

127 128 129 130
    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testFind($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
131 132 133
    {
        $this->createFixtures(3);

134
        $operation = new Find($this->getDatabaseName(), $this->getCollectionName(), []);
135

136
        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
137 138
        $result = $explainOperation->execute($this->getPrimaryServer());

139
        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
140 141
    }

142
    public function testFindMaxAwait()
143 144 145 146 147 148 149 150 151 152
    {
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
            $this->markTestSkipped('maxAwaitTimeMS option is not supported');
        }

        $maxAwaitTimeMS = 100;

        /* Calculate an approximate pivot to use for time assertions. We will
         * assert that the duration of blocking responses is greater than this
         * value, and vice versa. */
153
        $pivot = $maxAwaitTimeMS * 0.001 * 0.9;
154 155 156 157 158 159 160 161 162 163 164 165 166

        // Create a capped collection.
        $databaseName = $this->getDatabaseName();
        $cappedCollectionName = $this->getCollectionName();
        $cappedCollectionOptions = [
            'capped' => true,
            'max' => 100,
            'size' => 1048576,
        ];

        $operation = new CreateCollection($databaseName, $cappedCollectionName, $cappedCollectionOptions);
        $operation->execute($this->getPrimaryServer());

167
        $this->createFixtures(2);
168 169 170

        $operation = new Find($databaseName, $cappedCollectionName, [], ['cursorType' => Find::TAILABLE_AWAIT, 'maxAwaitTimeMS' => $maxAwaitTimeMS]);

171 172
        (new CommandObserver())->observe(
            function () use ($operation) {
173 174 175
                $explainOperation = new Explain($this->getDatabaseName(), $operation, ['typeMap' => ['root' => 'array', 'document' => 'array']]);
                $explainOperation->execute($this->getPrimaryServer());
            },
176
            function (array $event) {
177
                $command = $event['started']->getCommand();
178 179 180 181 182 183
                $this->assertObjectNotHasAttribute('maxAwaitTimeMS', $command->explain);
                $this->assertObjectHasAttribute('tailable', $command->explain);
                $this->assertObjectHasAttribute('awaitData', $command->explain);
            }
        );
    }
184

185 186 187 188 189 190 191 192
    public function testFindModifiers()
    {
        $this->createFixtures(3);

        $operation = new Find(
            $this->getDatabaseName(),
            $this->getCollectionName(),
            [],
193
            ['modifiers' => ['$orderby' => ['_id' => 1]]]
194 195
        );

196 197
        (new CommandObserver())->observe(
            function () use ($operation) {
198 199 200
                $explainOperation = new Explain($this->getDatabaseName(), $operation, ['typeMap' => ['root' => 'array', 'document' => 'array']]);
                $explainOperation->execute($this->getPrimaryServer());
            },
201
            function (array $event) {
202
                $command = $event['started']->getCommand();
203
                $this->assertObjectHasAttribute('sort', $command->explain);
204 205 206
                $this->assertObjectNotHasAttribute('modifiers', $command->explain);
            }
        );
207 208
    }

209 210 211 212
    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testFindOne($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
213 214 215 216 217
    {
        $this->createFixtures(1);

        $operation = new FindOne($this->getDatabaseName(), $this->getCollectionName(), []);

218
        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
219 220
        $result = $explainOperation->execute($this->getPrimaryServer());

221
        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
222 223
    }

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testFindOneAndDelete($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
    {
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
            $this->markTestSkipped('Explaining findOneAndDelete command requires server version >= 3.2');
        }

        $operation = new FindOneAndDelete($this->getDatabaseName(), $this->getCollectionName(), []);

        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
        $result = $explainOperation->execute($this->getPrimaryServer());

        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
    }

    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testFindOneAndReplace($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
    {
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
            $this->markTestSkipped('Explaining findOneAndReplace command requires server version >= 3.2');
        }

        $operation = new FindOneAndReplace($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1.1], ['x' => 5]);

        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
        $result = $explainOperation->execute($this->getPrimaryServer());

        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
    }

    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testFindOneAndUpdate($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
    {
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
            $this->markTestSkipped('Explaining findOneAndUpdate command requires server version >= 3.2');
        }

        $operation = new FindOneAndUpdate($this->getDatabaseName(), $this->getCollectionName(), [], ['$rename' => ['x' => 'y']]);

        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
        $result = $explainOperation->execute($this->getPrimaryServer());

        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
    }

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testUpdate($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
    {
        $this->createFixtures(3);

        $filter = ['_id' => ['$gt' => 1]];
        $update = ['$inc' => ['x' => 1]];

        $operation = new Update($this->getDatabaseName(), $this->getCollectionName(), $filter, $update);

        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
        $result = $explainOperation->execute($this->getPrimaryServer());

        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
    }

293 294
    public function testUpdateBypassDocumentValidationSetWhenTrue()
    {
295 296 297 298
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
            $this->markTestSkipped('bypassDocumentValidation is not supported');
        }

299 300
        $this->createFixtures(3);

301 302
        (new CommandObserver())->observe(
            function () {
303 304 305 306 307 308 309 310 311 312 313
                $operation = new Update(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    ['_id' => ['$gt' => 1]],
                    ['$inc' => ['x' => 1]],
                    ['bypassDocumentValidation' => true]
                );

                $explainOperation = new Explain($this->getDatabaseName(), $operation);
                $result = $explainOperation->execute($this->getPrimaryServer());
            },
314
            function (array $event) {
315 316 317 318 319 320 321 322 323 324 325
                $this->assertObjectHasAttribute(
                    'bypassDocumentValidation',
                    $event['started']->getCommand()->explain
                );
                $this->assertEquals(true, $event['started']->getCommand()->explain->bypassDocumentValidation);
            }
        );
    }

    public function testUpdateBypassDocumentValidationUnsetWhenFalse()
    {
326 327 328 329
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
            $this->markTestSkipped('bypassDocumentValidation is not supported');
        }

330 331
        $this->createFixtures(3);

332 333
        (new CommandObserver())->observe(
            function () {
334 335 336 337 338 339 340 341 342 343 344
                $operation = new Update(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    ['_id' => ['$gt' => 1]],
                    ['$inc' => ['x' => 1]],
                    ['bypassDocumentValidation' => false]
                );

                $explainOperation = new Explain($this->getDatabaseName(), $operation);
                $result = $explainOperation->execute($this->getPrimaryServer());
            },
345
            function (array $event) {
346 347 348 349 350 351 352 353
                $this->assertObjectNotHasAttribute(
                    'bypassDocumentValidation',
                    $event['started']->getCommand()->explain
                );
            }
        );
    }

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testUpdateMany($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
    {
        $this->createFixtures(3);

        $filter = ['_id' => ['$gt' => 1]];
        $update = ['$inc' => ['x' => 1]];

        $operation = new UpdateMany($this->getDatabaseName(), $this->getCollectionName(), $filter, $update);

        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
        $result = $explainOperation->execute($this->getPrimaryServer());

        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
    }

    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testUpdateOne($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
    {
        $this->createFixtures(3);

        $filter = ['_id' => ['$lte' => 1]];
        $update = ['$inc' => ['x' => 1]];

        $operation = new UpdateOne($this->getDatabaseName(), $this->getCollectionName(), $filter, $update);

        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
        $result = $explainOperation->execute($this->getPrimaryServer());

        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
    }

390
    public function provideVerbosityInformation()
391
    {
392 393 394
        return [
            [Explain::VERBOSITY_ALL_PLANS, true, true],
            [Explain::VERBOSITY_EXEC_STATS, true, false],
395
            [Explain::VERBOSITY_QUERY, false, false],
396
        ];
397 398
    }

399
    private function assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected)
400
    {
401 402 403 404 405 406 407 408 409 410 411
        $this->assertArrayHasKey('queryPlanner', $result);
        if ($executionStatsExpected) {
            $this->assertArrayHasKey('executionStats', $result);
            if ($allPlansExecutionExpected) {
                $this->assertArrayHasKey('allPlansExecution', $result['executionStats']);
            } else {
                $this->assertArrayNotHasKey('allPlansExecution', $result['executionStats']);
            }
        } else {
            $this->assertArrayNotHasKey('executionStats', $result);
        }
412 413 414 415 416 417 418 419 420 421 422 423 424 425
    }

    /**
     * Create data fixtures.
     *
     * @param integer $n
     */
    private function createFixtures($n)
    {
        $bulkWrite = new BulkWrite(['ordered' => true]);

        for ($i = 1; $i <= $n; $i++) {
            $bulkWrite->insert([
                '_id' => $i,
426
                'x' => (integer) ($i . $i),
427 428 429 430 431 432 433
            ]);
        }

        $result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);

        $this->assertEquals($n, $result->getInsertedCount());
    }
434
}