BulkWriteFunctionalTest.php 13.3 KB
Newer Older
1 2
<?php

3
namespace MongoDB\Tests\Operation;
4

5
use MongoDB\BSON\ObjectId;
6
use MongoDB\BulkWriteResult;
7
use MongoDB\Collection;
8
use MongoDB\Driver\BulkWrite as Bulk;
9
use MongoDB\Driver\WriteConcern;
10 11
use MongoDB\Exception\BadMethodCallException;
use MongoDB\Exception\InvalidArgumentException;
12
use MongoDB\Model\BSONDocument;
13
use MongoDB\Operation\BulkWrite;
14
use MongoDB\Tests\CommandObserver;
15
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
16
use function version_compare;
17 18 19

class BulkWriteFunctionalTest extends FunctionalTestCase
{
20 21
    use SetUpTearDownTrait;

22
    /** @var Collection */
23
    private $collection;
24

25
    private function doSetUp()
26 27 28
    {
        parent::setUp();

29
        $this->collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
30 31 32 33
    }

    public function testInserts()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
34 35 36
        $ops = [
            ['insertOne' => [['_id' => 1, 'x' => 11]]],
            ['insertOne' => [['x' => 22]]],
37 38
            ['insertOne' => [(object) ['_id' => 'foo', 'x' => 33]]],
            ['insertOne' => [new BSONDocument(['_id' => 'bar', 'x' => 44])]],
Jeremy Mikola's avatar
Jeremy Mikola committed
39
        ];
40

41 42 43
        $operation = new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
        $result = $operation->execute($this->getPrimaryServer());

44
        $this->assertInstanceOf(BulkWriteResult::class, $result);
45
        $this->assertSame(4, $result->getInsertedCount());
46 47 48

        $insertedIds = $result->getInsertedIds();
        $this->assertSame(1, $insertedIds[0]);
49
        $this->assertInstanceOf(ObjectId::class, $insertedIds[1]);
50 51
        $this->assertSame('foo', $insertedIds[2]);
        $this->assertSame('bar', $insertedIds[3]);
52

Jeremy Mikola's avatar
Jeremy Mikola committed
53
        $expected = [
54
            ['_id' => 1, 'x' => 11],
Jeremy Mikola's avatar
Jeremy Mikola committed
55
            ['_id' => $insertedIds[1], 'x' => 22],
56 57
            ['_id' => 'foo', 'x' => 33],
            ['_id' => 'bar', 'x' => 44],
Jeremy Mikola's avatar
Jeremy Mikola committed
58
        ];
59

60
        $this->assertSameDocuments($expected, $this->collection->find());
61 62 63 64 65 66
    }

    public function testUpdates()
    {
        $this->createFixtures(4);

Jeremy Mikola's avatar
Jeremy Mikola committed
67 68 69 70 71 72 73
        $ops = [
            ['updateOne' => [['_id' => 2], ['$inc' => ['x' => 1]]]],
            ['updateMany' => [['_id' => ['$gt' => 2]], ['$inc' => ['x' => -1]]]],
            ['updateOne' => [['_id' => 5], ['$set' => ['x' => 55]], ['upsert' => true]]],
            ['updateOne' => [['x' => 66], ['$set' => ['x' => 66]], ['upsert' => true]]],
            ['updateMany' => [['x' => ['$gt' => 50]], ['$inc' => ['x' => 1]]]],
        ];
74

75 76 77
        $operation = new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
        $result = $operation->execute($this->getPrimaryServer());

78
        $this->assertInstanceOf(BulkWriteResult::class, $result);
79
        $this->assertSame(5, $result->getMatchedCount());
80
        $this->assertSame(5, $result->getModifiedCount());
81 82 83 84
        $this->assertSame(2, $result->getUpsertedCount());

        $upsertedIds = $result->getUpsertedIds();
        $this->assertSame(5, $upsertedIds[2]);
85
        $this->assertInstanceOf(ObjectId::class, $upsertedIds[3]);
86

Jeremy Mikola's avatar
Jeremy Mikola committed
87 88 89 90 91 92 93 94
        $expected = [
            ['_id' => 1, 'x' => 11],
            ['_id' => 2, 'x' => 23],
            ['_id' => 3, 'x' => 32],
            ['_id' => 4, 'x' => 43],
            ['_id' => 5, 'x' => 56],
            ['_id' => $upsertedIds[3], 'x' => 67],
        ];
95

96
        $this->assertSameDocuments($expected, $this->collection->find());
97 98 99 100 101 102
    }

    public function testDeletes()
    {
        $this->createFixtures(4);

Jeremy Mikola's avatar
Jeremy Mikola committed
103 104 105 106
        $ops = [
            ['deleteOne' => [['_id' => 1]]],
            ['deleteMany' => [['_id' => ['$gt' => 2]]]],
        ];
107

108 109 110
        $operation = new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
        $result = $operation->execute($this->getPrimaryServer());

111
        $this->assertInstanceOf(BulkWriteResult::class, $result);
112 113
        $this->assertSame(3, $result->getDeletedCount());

Jeremy Mikola's avatar
Jeremy Mikola committed
114 115 116
        $expected = [
            ['_id' => 2, 'x' => 22],
        ];
117

118
        $this->assertSameDocuments($expected, $this->collection->find());
119 120 121 122 123 124
    }

    public function testMixedOrderedOperations()
    {
        $this->createFixtures(3);

Jeremy Mikola's avatar
Jeremy Mikola committed
125 126 127 128 129 130 131
        $ops = [
            ['updateOne' => [['_id' => ['$gt' => 1]], ['$inc' => ['x' => 1]]]],
            ['updateMany' => [['_id' => ['$gt' => 1]], ['$inc' => ['x' => 1]]]],
            ['insertOne' => [['_id' => 4, 'x' => 44]]],
            ['deleteMany' => [['x' => ['$nin' => [24, 34]]]]],
            ['replaceOne' => [['_id' => 4], ['_id' => 4, 'x' => 44], ['upsert' => true]]],
        ];
132

133 134 135
        $operation = new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
        $result = $operation->execute($this->getPrimaryServer());

136
        $this->assertInstanceOf(BulkWriteResult::class, $result);
137 138

        $this->assertSame(1, $result->getInsertedCount());
Jeremy Mikola's avatar
Jeremy Mikola committed
139
        $this->assertSame([2 => 4], $result->getInsertedIds());
140 141

        $this->assertSame(3, $result->getMatchedCount());
142
        $this->assertSame(3, $result->getModifiedCount());
143
        $this->assertSame(1, $result->getUpsertedCount());
Jeremy Mikola's avatar
Jeremy Mikola committed
144
        $this->assertSame([4 => 4], $result->getUpsertedIds());
145 146 147

        $this->assertSame(2, $result->getDeletedCount());

Jeremy Mikola's avatar
Jeremy Mikola committed
148 149 150 151 152
        $expected = [
            ['_id' => 2, 'x' => 24],
            ['_id' => 3, 'x' => 34],
            ['_id' => 4, 'x' => 44],
        ];
153

154
        $this->assertSameDocuments($expected, $this->collection->find());
155 156
    }

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    public function testUnacknowledgedWriteConcern()
    {
        $ops = [['insertOne' => [['_id' => 1]]]];
        $options = ['writeConcern' => new WriteConcern(0)];
        $operation = new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops, $options);
        $result = $operation->execute($this->getPrimaryServer());

        $this->assertFalse($result->isAcknowledged());

        return $result;
    }

    /**
     * @depends testUnacknowledgedWriteConcern
     */
    public function testUnacknowledgedWriteConcernAccessesDeletedCount(BulkWriteResult $result)
    {
174 175
        $this->expectException(BadMethodCallException::class);
        $this->expectExceptionMessageRegExp('/[\w:\\\\]+ should not be called for an unacknowledged write result/');
176 177 178 179 180 181 182 183
        $result->getDeletedCount();
    }

    /**
     * @depends testUnacknowledgedWriteConcern
     */
    public function testUnacknowledgedWriteConcernAccessesInsertCount(BulkWriteResult $result)
    {
184 185
        $this->expectException(BadMethodCallException::class);
        $this->expectExceptionMessageRegExp('/[\w:\\\\]+ should not be called for an unacknowledged write result/');
186 187 188 189 190 191 192 193
        $result->getInsertedCount();
    }

    /**
     * @depends testUnacknowledgedWriteConcern
     */
    public function testUnacknowledgedWriteConcernAccessesMatchedCount(BulkWriteResult $result)
    {
194 195
        $this->expectException(BadMethodCallException::class);
        $this->expectExceptionMessageRegExp('/[\w:\\\\]+ should not be called for an unacknowledged write result/');
196 197 198 199 200 201 202 203
        $result->getMatchedCount();
    }

    /**
     * @depends testUnacknowledgedWriteConcern
     */
    public function testUnacknowledgedWriteConcernAccessesModifiedCount(BulkWriteResult $result)
    {
204 205
        $this->expectException(BadMethodCallException::class);
        $this->expectExceptionMessageRegExp('/[\w:\\\\]+ should not be called for an unacknowledged write result/');
206 207 208 209 210 211 212 213
        $result->getModifiedCount();
    }

    /**
     * @depends testUnacknowledgedWriteConcern
     */
    public function testUnacknowledgedWriteConcernAccessesUpsertedCount(BulkWriteResult $result)
    {
214 215
        $this->expectException(BadMethodCallException::class);
        $this->expectExceptionMessageRegExp('/[\w:\\\\]+ should not be called for an unacknowledged write result/');
216 217 218 219 220 221 222 223
        $result->getUpsertedCount();
    }

    /**
     * @depends testUnacknowledgedWriteConcern
     */
    public function testUnacknowledgedWriteConcernAccessesUpsertedIds(BulkWriteResult $result)
    {
224 225
        $this->expectException(BadMethodCallException::class);
        $this->expectExceptionMessageRegExp('/[\w:\\\\]+ should not be called for an unacknowledged write result/');
226 227 228
        $result->getUpsertedIds();
    }

229 230
    public function testUnknownOperation()
    {
231 232
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Unknown operation type "foo" in $operations[0]');
233
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
Jeremy Mikola's avatar
Jeremy Mikola committed
234 235
            ['foo' => [['_id' => 1]]],
        ]);
236 237 238 239 240 241 242
    }

    /**
     * @dataProvider provideOpsWithMissingArguments
     */
    public function testMissingArguments(array $ops)
    {
243 244
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessageRegExp('/Missing (first|second) argument for \$operations\[\d+\]\["\w+\"]/');
245
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
246 247 248 249
    }

    public function provideOpsWithMissingArguments()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
250 251 252 253 254 255 256 257 258 259 260
        return [
            [[['insertOne' => []]]],
            [[['updateOne' => []]]],
            [[['updateOne' => [['_id' => 1]]]]],
            [[['updateMany' => []]]],
            [[['updateMany' => [['_id' => 1]]]]],
            [[['replaceOne' => []]]],
            [[['replaceOne' => [['_id' => 1]]]]],
            [[['deleteOne' => []]]],
            [[['deleteMany' => []]]],
        ];
261 262 263 264
    }

    public function testUpdateOneRequiresUpdateOperators()
    {
265 266
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('First key in $operations[0]["updateOne"][1] is not an update operator');
267
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
Jeremy Mikola's avatar
Jeremy Mikola committed
268 269
            ['updateOne' => [['_id' => 1], ['x' => 1]]],
        ]);
270 271 272 273
    }

    public function testUpdateManyRequiresUpdateOperators()
    {
274 275
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('First key in $operations[0]["updateMany"][1] is not an update operator');
276
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
Jeremy Mikola's avatar
Jeremy Mikola committed
277 278
            ['updateMany' => [['_id' => ['$gt' => 1]], ['x' => 1]]],
        ]);
279 280 281 282
    }

    public function testReplaceOneRequiresReplacementDocument()
    {
283 284
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('First key in $operations[0]["replaceOne"][1] is an update operator');
285
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
Jeremy Mikola's avatar
Jeremy Mikola committed
286 287
            ['replaceOne' => [['_id' => 1], ['$inc' => ['x' => 1]]]],
        ]);
288 289
    }

290 291 292 293 294 295
    public function testSessionOption()
    {
        if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
            $this->markTestSkipped('Sessions are not supported');
        }

296 297
        (new CommandObserver())->observe(
            function () {
298 299 300 301 302 303 304 305 306
                $operation = new BulkWrite(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    [['insertOne' => [['_id' => 1]]]],
                    ['session' => $this->createSession()]
                );

                $operation->execute($this->getPrimaryServer());
            },
307
            function (array $event) {
308
                $this->assertObjectHasAttribute('lsid', $event['started']->getCommand());
309 310 311 312
            }
        );
    }

313 314
    public function testBypassDocumentValidationSetWhenTrue()
    {
315 316 317 318
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
            $this->markTestSkipped('bypassDocumentValidation is not supported');
        }

319 320
        (new CommandObserver())->observe(
            function () {
321 322 323 324 325 326 327 328 329
                $operation = new BulkWrite(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    [['insertOne' => [['_id' => 1]]]],
                    ['bypassDocumentValidation' => true]
                );

                $operation->execute($this->getPrimaryServer());
            },
330
            function (array $event) {
331 332 333 334 335 336 337 338
                $this->assertObjectHasAttribute('bypassDocumentValidation', $event['started']->getCommand());
                $this->assertEquals(true, $event['started']->getCommand()->bypassDocumentValidation);
            }
        );
    }

    public function testBypassDocumentValidationUnsetWhenFalse()
    {
339 340 341 342
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
            $this->markTestSkipped('bypassDocumentValidation is not supported');
        }

343 344
        (new CommandObserver())->observe(
            function () {
345 346 347 348 349 350 351 352 353
                $operation = new BulkWrite(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    [['insertOne' => [['_id' => 1]]]],
                    ['bypassDocumentValidation' => false]
                );

                $operation->execute($this->getPrimaryServer());
            },
354
            function (array $event) {
355 356 357 358 359
                $this->assertObjectNotHasAttribute('bypassDocumentValidation', $event['started']->getCommand());
            }
        );
    }

360 361 362 363 364 365 366
    /**
     * Create data fixtures.
     *
     * @param integer $n
     */
    private function createFixtures($n)
    {
367
        $bulkWrite = new Bulk(['ordered' => true]);
368 369

        for ($i = 1; $i <= $n; $i++) {
Jeremy Mikola's avatar
Jeremy Mikola committed
370
            $bulkWrite->insert([
371 372
                '_id' => $i,
                'x' => (integer) ($i . $i),
Jeremy Mikola's avatar
Jeremy Mikola committed
373
            ]);
374 375 376 377 378 379
        }

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

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