AggregateFunctionalTest.php 13.3 KB
Newer Older
1 2 3 4
<?php

namespace MongoDB\Tests\Operation;

5
use ArrayIterator;
6
use MongoDB\Collection;
7
use MongoDB\Driver\BulkWrite;
8
use MongoDB\Driver\Exception\RuntimeException;
9
use MongoDB\Driver\ReadPreference;
10
use MongoDB\Driver\WriteConcern;
11
use MongoDB\Operation\Aggregate;
12 13
use MongoDB\Tests\CommandObserver;
use stdClass;
14 15 16
use function current;
use function iterator_to_array;
use function version_compare;
17 18 19

class AggregateFunctionalTest extends FunctionalTestCase
{
20 21
    public function testBatchSizeIsIgnoredIfPipelineIncludesOutStage()
    {
22 23
        (new CommandObserver())->observe(
            function () {
24 25 26 27 28 29 30 31 32
                $operation = new Aggregate(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    [['$out' => $this->getCollectionName() . '.output']],
                    ['batchSize' => 0]
                );

                $operation->execute($this->getPrimaryServer());
            },
33 34
            function (array $event) {
                $this->assertEquals(new stdClass(), $event['started']->getCommand()->cursor);
35 36 37 38 39 40 41
            }
        );

        $outCollection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName() . '.output');
        $outCollection->drop();
    }

42 43 44 45 46 47
    public function testCurrentOpCommand()
    {
        if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
            $this->markTestSkipped('$currentOp is not supported');
        }

48 49
        (new CommandObserver())->observe(
            function () {
50 51 52 53 54 55 56 57
                $operation = new Aggregate(
                    'admin',
                    null,
                    [['$currentOp' => (object) []]]
                );

                $operation->execute($this->getPrimaryServer());
            },
58
            function (array $event) {
59
                $this->assertSame(1, $event['started']->getCommand()->aggregate);
60 61 62 63
            }
        );
    }

64 65
    public function testDefaultReadConcernIsOmitted()
    {
66 67
        (new CommandObserver())->observe(
            function () {
68 69 70 71 72 73 74 75 76
                $operation = new Aggregate(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    [['$match' => ['x' => 1]]],
                    ['readConcern' => $this->createDefaultReadConcern()]
                );

                $operation->execute($this->getPrimaryServer());
            },
77
            function (array $event) {
78
                $this->assertObjectNotHasAttribute('readConcern', $event['started']->getCommand());
79 80 81 82 83 84
            }
        );
    }

    public function testDefaultWriteConcernIsOmitted()
    {
85 86
        (new CommandObserver())->observe(
            function () {
87 88 89 90 91 92 93 94 95
                $operation = new Aggregate(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    [['$out' => $this->getCollectionName() . '.output']],
                    ['writeConcern' => $this->createDefaultWriteConcern()]
                );

                $operation->execute($this->getPrimaryServer());
            },
96
            function (array $event) {
97
                $this->assertObjectNotHasAttribute('writeConcern', $event['started']->getCommand());
98 99
            }
        );
100 101 102

        $outCollection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName() . '.output');
        $outCollection->drop();
103 104
    }

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    public function testEmptyPipelineReturnsAllDocuments()
    {
        $this->createFixtures(3);

        $operation = new Aggregate($this->getDatabaseName(), $this->getCollectionName(), []);
        $results = iterator_to_array($operation->execute($this->getPrimaryServer()));

        $expectedDocuments = [
            (object) ['_id' => 1, 'x' => (object) ['foo' => 'bar']],
            (object) ['_id' => 2, 'x' => (object) ['foo' => 'bar']],
            (object) ['_id' => 3, 'x' => (object) ['foo' => 'bar']],
        ];

        $this->assertEquals($expectedDocuments, $results);
    }

121 122 123
    public function testUnrecognizedPipelineState()
    {
        $operation = new Aggregate($this->getDatabaseName(), $this->getCollectionName(), [['$foo' => 1]]);
124
        $this->expectException(RuntimeException::class);
125 126 127
        $operation->execute($this->getPrimaryServer());
    }

128 129 130 131 132 133
    public function testSessionOption()
    {
        if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
            $this->markTestSkipped('Sessions are not supported');
        }

134 135
        (new CommandObserver())->observe(
            function () {
136 137 138 139 140 141 142 143 144
                $operation = new Aggregate(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    [],
                    ['session' => $this->createSession()]
                );

                $operation->execute($this->getPrimaryServer());
            },
145
            function (array $event) {
146
                $this->assertObjectHasAttribute('lsid', $event['started']->getCommand());
147 148 149 150
            }
        );
    }

151
    /**
152
     * @dataProvider provideTypeMapOptionsAndExpectedDocuments
153
     */
154
    public function testTypeMapOption(array $typeMap = null, array $expectedDocuments)
155 156 157 158
    {
        $this->createFixtures(3);

        $pipeline = [['$match' => ['_id' => ['$ne' => 2]]]];
159

160
        $operation = new Aggregate($this->getDatabaseName(), $this->getCollectionName(), $pipeline, ['typeMap' => $typeMap]);
161
        $results = iterator_to_array($operation->execute($this->getPrimaryServer()));
162

163
        $this->assertEquals($expectedDocuments, $results);
164 165
    }

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    /**
     * @dataProvider provideTypeMapOptionsAndExpectedDocuments
     */
    public function testTypeMapOptionWithoutCursor(array $typeMap = null, array $expectedDocuments)
    {
        if (version_compare($this->getServerVersion(), '3.6.0', '>=')) {
            $this->markTestSkipped('Aggregations with useCursor == false are not supported');
        }

        $this->createFixtures(3);

        $pipeline = [['$match' => ['_id' => ['$ne' => 2]]]];

        $operation = new Aggregate($this->getDatabaseName(), $this->getCollectionName(), $pipeline, ['typeMap' => $typeMap, 'useCursor' => false]);
        $results = $operation->execute($this->getPrimaryServer());

        $this->assertInstanceOf(ArrayIterator::class, $results);
        $this->assertEquals($expectedDocuments, iterator_to_array($results));
    }

186 187 188 189 190 191 192 193 194
    public function testExplainOption()
    {
        $this->createFixtures(3);

        $pipeline = [['$match' => ['_id' => ['$ne' => 2]]]];
        $operation = new Aggregate($this->getDatabaseName(), $this->getCollectionName(), $pipeline, ['explain' => true, 'typeMap' => ['root' => 'array']]);
        $results = iterator_to_array($operation->execute($this->getPrimaryServer()));

        $this->assertCount(1, $results);
195 196 197 198 199 200 201

        /* MongoDB 4.2 may optimize aggregate pipelines into queries, which can
         * result in different explain output (see: SERVER-24860) */
        $this->assertThat($results[0], $this->logicalOr(
            $this->arrayHasKey('stages'),
            $this->arrayHasKey('queryPlanner')
        ));
202 203 204 205 206 207
    }

    public function testExplainOptionWithWriteConcern()
    {
        if (version_compare($this->getServerVersion(), '3.4.0', '<')) {
            $this->markTestSkipped('The writeConcern option is not supported');
208
        }
209 210 211 212 213 214

        $this->createFixtures(3);

        $pipeline = [['$match' => ['_id' => ['$ne' => 2]]], ['$out' => $this->getCollectionName() . '.output']];
        $options = ['explain' => true, 'writeConcern' => new WriteConcern(1)];

215 216
        (new CommandObserver())->observe(
            function () use ($pipeline, $options) {
217 218 219 220 221
                $operation = new Aggregate($this->getDatabaseName(), $this->getCollectionName(), $pipeline, $options);

                $results = iterator_to_array($operation->execute($this->getPrimaryServer()));

                $this->assertCount(1, $results);
222 223 224 225 226 227 228 229 230 231 232
                $result = current($results);

                if ($this->isShardedCluster()) {
                    $this->assertObjectHasAttribute('shards', $result);

                    foreach ($result->shards as $shard) {
                        $this->assertObjectHasAttribute('stages', $shard);
                    }
                } else {
                    $this->assertObjectHasAttribute('stages', $result);
                }
233
            },
234
            function (array $event) {
235
                $this->assertObjectNotHasAttribute('writeConcern', $event['started']->getCommand());
236 237 238 239 240 241
            }
        );

        $this->assertCollectionCount($this->getCollectionName() . '.output', 0);
    }

242 243
    public function testBypassDocumentValidationSetWhenTrue()
    {
244 245 246 247
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
            $this->markTestSkipped('bypassDocumentValidation is not supported');
        }

248 249
        (new CommandObserver())->observe(
            function () {
250 251 252 253 254 255 256 257 258
                $operation = new Aggregate(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    [['$match' => ['x' => 1]]],
                    ['bypassDocumentValidation' => true]
                );

                $operation->execute($this->getPrimaryServer());
            },
259
            function (array $event) {
260 261 262 263 264 265 266 267
                $this->assertObjectHasAttribute('bypassDocumentValidation', $event['started']->getCommand());
                $this->assertEquals(true, $event['started']->getCommand()->bypassDocumentValidation);
            }
        );
    }

    public function testBypassDocumentValidationUnsetWhenFalse()
    {
268 269 270 271
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
            $this->markTestSkipped('bypassDocumentValidation is not supported');
        }

272 273
        (new CommandObserver())->observe(
            function () {
274 275 276 277 278 279 280 281 282
                $operation = new Aggregate(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    [['$match' => ['x' => 1]]],
                    ['bypassDocumentValidation' => false]
                );

                $operation->execute($this->getPrimaryServer());
            },
283
            function (array $event) {
284 285 286 287 288
                $this->assertObjectNotHasAttribute('bypassDocumentValidation', $event['started']->getCommand());
            }
        );
    }

289
    public function provideTypeMapOptionsAndExpectedDocuments()
290 291
    {
        return [
292 293 294 295 296 297 298
            [
                null,
                [
                    (object) ['_id' => 1, 'x' => (object) ['foo' => 'bar']],
                    (object) ['_id' => 3, 'x' => (object) ['foo' => 'bar']],
                ],
            ],
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
            [
                ['root' => 'array', 'document' => 'array'],
                [
                    ['_id' => 1, 'x' => ['foo' => 'bar']],
                    ['_id' => 3, 'x' => ['foo' => 'bar']],
                ],
            ],
            [
                ['root' => 'object', 'document' => 'array'],
                [
                    (object) ['_id' => 1, 'x' => ['foo' => 'bar']],
                    (object) ['_id' => 3, 'x' => ['foo' => 'bar']],
                ],
            ],
            [
                ['root' => 'array', 'document' => 'stdClass'],
                [
                    ['_id' => 1, 'x' => (object) ['foo' => 'bar']],
                    ['_id' => 3, 'x' => (object) ['foo' => 'bar']],
                ],
            ],
320 321 322 323 324 325 326
            [
                ['root' => 'array', 'document' => 'stdClass', 'fieldPaths' => ['x' => 'array']],
                [
                    ['_id' => 1, 'x' => ['foo' => 'bar']],
                    ['_id' => 3, 'x' => ['foo' => 'bar']],
                ],
            ],
327 328 329
        ];
    }

330 331 332 333 334
    public function testReadPreferenceWithinTransaction()
    {
        $this->skipIfTransactionsAreNotSupported();

        // Collection must be created before the transaction starts
335
        $this->createCollection();
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

        $session = $this->manager->startSession();
        $session->startTransaction();

        try {
            $this->createFixtures(3, ['session' => $session]);

            $pipeline = [['$match' => ['_id' => ['$lt' => 3]]]];
            $options = [
                'readPreference' => new ReadPreference('primary'),
                'session' => $session,
            ];

            $operation = new Aggregate($this->getDatabaseName(), $this->getCollectionName(), $pipeline, $options);
            $cursor = $operation->execute($this->getPrimaryServer());

            $expected = [
                ['_id' => 1, 'x' => ['foo' => 'bar']],
                ['_id' => 2, 'x' => ['foo' => 'bar']],
            ];

            $this->assertSameDocuments($expected, $cursor);

            $session->commitTransaction();
        } finally {
            $session->endSession();
        }
    }

365 366 367 368
    /**
     * Create data fixtures.
     *
     * @param integer $n
369
     * @param array   $executeBulkWriteOptions
370
     */
371
    private function createFixtures($n, array $executeBulkWriteOptions = [])
372 373 374 375 376 377 378 379 380 381
    {
        $bulkWrite = new BulkWrite(['ordered' => true]);

        for ($i = 1; $i <= $n; $i++) {
            $bulkWrite->insert([
                '_id' => $i,
                'x' => (object) ['foo' => 'bar'],
            ]);
        }

382
        $result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite, $executeBulkWriteOptions);
383 384

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