BulkWriteFunctionalTest.php 10.6 KB
Newer Older
1 2 3 4
<?php

namespace MongoDB\Tests\Collection;

5
use MongoDB\BulkWriteResult;
6
use MongoDB\Driver\BulkWrite as Bulk;
7
use MongoDB\Driver\WriteConcern;
8
use MongoDB\Model\BSONDocument;
9
use MongoDB\Operation\BulkWrite;
10 11 12 13 14 15 16 17 18 19 20 21 22 23

class BulkWriteFunctionalTest extends FunctionalTestCase
{
    private $omitModifiedCount;

    public function setUp()
    {
        parent::setUp();

        $this->omitModifiedCount = version_compare($this->getServerVersion(), '2.6.0', '<');
    }

    public function testInserts()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
24 25 26
        $ops = [
            ['insertOne' => [['_id' => 1, 'x' => 11]]],
            ['insertOne' => [['x' => 22]]],
27 28
            ['insertOne' => [(object) ['_id' => 'foo', 'x' => 33]]],
            ['insertOne' => [new BSONDocument(['_id' => 'bar', 'x' => 44])]],
Jeremy Mikola's avatar
Jeremy Mikola committed
29
        ];
30

31 32 33
        $operation = new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
        $result = $operation->execute($this->getPrimaryServer());

34
        $this->assertInstanceOf('MongoDB\BulkWriteResult', $result);
35
        $this->assertSame(4, $result->getInsertedCount());
36 37 38

        $insertedIds = $result->getInsertedIds();
        $this->assertSame(1, $insertedIds[0]);
39
        $this->assertInstanceOf('MongoDB\BSON\ObjectId', $insertedIds[1]);
40 41
        $this->assertSame('foo', $insertedIds[2]);
        $this->assertSame('bar', $insertedIds[3]);
42

Jeremy Mikola's avatar
Jeremy Mikola committed
43
        $expected = [
44
            ['_id' => 1, 'x' => 11],
Jeremy Mikola's avatar
Jeremy Mikola committed
45
            ['_id' => $insertedIds[1], 'x' => 22],
46 47
            ['_id' => 'foo', 'x' => 33],
            ['_id' => 'bar', 'x' => 44],
Jeremy Mikola's avatar
Jeremy Mikola committed
48
        ];
49

50
        $this->assertSameDocuments($expected, $this->collection->find());
51 52 53 54 55 56
    }

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

Jeremy Mikola's avatar
Jeremy Mikola committed
57 58 59 60 61 62 63
        $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]]]],
        ];
64

65 66 67
        $operation = new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
        $result = $operation->execute($this->getPrimaryServer());

68 69 70 71 72 73 74
        $this->assertInstanceOf('MongoDB\BulkWriteResult', $result);
        $this->assertSame(5, $result->getMatchedCount());
        $this->omitModifiedCount or $this->assertSame(5, $result->getModifiedCount());
        $this->assertSame(2, $result->getUpsertedCount());

        $upsertedIds = $result->getUpsertedIds();
        $this->assertSame(5, $upsertedIds[2]);
75
        $this->assertInstanceOf('MongoDB\BSON\ObjectId', $upsertedIds[3]);
76

Jeremy Mikola's avatar
Jeremy Mikola committed
77 78 79 80 81 82 83 84
        $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],
        ];
85

86
        $this->assertSameDocuments($expected, $this->collection->find());
87 88 89 90 91 92
    }

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

Jeremy Mikola's avatar
Jeremy Mikola committed
93 94 95 96
        $ops = [
            ['deleteOne' => [['_id' => 1]]],
            ['deleteMany' => [['_id' => ['$gt' => 2]]]],
        ];
97

98 99 100
        $operation = new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
        $result = $operation->execute($this->getPrimaryServer());

101 102 103
        $this->assertInstanceOf('MongoDB\BulkWriteResult', $result);
        $this->assertSame(3, $result->getDeletedCount());

Jeremy Mikola's avatar
Jeremy Mikola committed
104 105 106
        $expected = [
            ['_id' => 2, 'x' => 22],
        ];
107

108
        $this->assertSameDocuments($expected, $this->collection->find());
109 110 111 112 113 114
    }

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

Jeremy Mikola's avatar
Jeremy Mikola committed
115 116 117 118 119 120 121
        $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]]],
        ];
122

123 124 125
        $operation = new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
        $result = $operation->execute($this->getPrimaryServer());

126 127 128
        $this->assertInstanceOf('MongoDB\BulkWriteResult', $result);

        $this->assertSame(1, $result->getInsertedCount());
Jeremy Mikola's avatar
Jeremy Mikola committed
129
        $this->assertSame([2 => 4], $result->getInsertedIds());
130 131 132 133

        $this->assertSame(3, $result->getMatchedCount());
        $this->omitModifiedCount or $this->assertSame(3, $result->getModifiedCount());
        $this->assertSame(1, $result->getUpsertedCount());
Jeremy Mikola's avatar
Jeremy Mikola committed
134
        $this->assertSame([4 => 4], $result->getUpsertedIds());
135 136 137

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

Jeremy Mikola's avatar
Jeremy Mikola committed
138 139 140 141 142
        $expected = [
            ['_id' => 2, 'x' => 24],
            ['_id' => 3, 'x' => 34],
            ['_id' => 4, 'x' => 44],
        ];
143

144
        $this->assertSameDocuments($expected, $this->collection->find());
145 146
    }

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    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
     * @expectedException MongoDB\Exception\BadMethodCallException
     * @expectedExceptionMessageRegExp /[\w:\\]+ should not be called for an unacknowledged write result/
     */
    public function testUnacknowledgedWriteConcernAccessesDeletedCount(BulkWriteResult $result)
    {
        $result->getDeletedCount();
    }

    /**
     * @depends testUnacknowledgedWriteConcern
     * @expectedException MongoDB\Exception\BadMethodCallException
     * @expectedExceptionMessageRegExp /[\w:\\]+ should not be called for an unacknowledged write result/
     */
    public function testUnacknowledgedWriteConcernAccessesInsertCount(BulkWriteResult $result)
    {
        $result->getInsertedCount();
    }

    /**
     * @depends testUnacknowledgedWriteConcern
     * @expectedException MongoDB\Exception\BadMethodCallException
     * @expectedExceptionMessageRegExp /[\w:\\]+ should not be called for an unacknowledged write result/
     */
    public function testUnacknowledgedWriteConcernAccessesMatchedCount(BulkWriteResult $result)
    {
        $result->getMatchedCount();
    }

    /**
     * @depends testUnacknowledgedWriteConcern
     * @expectedException MongoDB\Exception\BadMethodCallException
     * @expectedExceptionMessageRegExp /[\w:\\]+ should not be called for an unacknowledged write result/
     */
    public function testUnacknowledgedWriteConcernAccessesModifiedCount(BulkWriteResult $result)
    {
        $result->getModifiedCount();
    }

    /**
     * @depends testUnacknowledgedWriteConcern
     * @expectedException MongoDB\Exception\BadMethodCallException
     * @expectedExceptionMessageRegExp /[\w:\\]+ should not be called for an unacknowledged write result/
     */
    public function testUnacknowledgedWriteConcernAccessesUpsertedCount(BulkWriteResult $result)
    {
        $result->getUpsertedCount();
    }

    /**
     * @depends testUnacknowledgedWriteConcern
     * @expectedException MongoDB\Exception\BadMethodCallException
     * @expectedExceptionMessageRegExp /[\w:\\]+ should not be called for an unacknowledged write result/
     */
    public function testUnacknowledgedWriteConcernAccessesUpsertedIds(BulkWriteResult $result)
    {
        $result->getUpsertedIds();
    }

219 220
    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
221
     * @expectedExceptionMessage Unknown operation type "foo" in $operations[0]
222 223 224
     */
    public function testUnknownOperation()
    {
225
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
Jeremy Mikola's avatar
Jeremy Mikola committed
226 227
            ['foo' => [['_id' => 1]]],
        ]);
228 229 230 231
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
232
     * @expectedExceptionMessageRegExp /Missing (first|second) argument for \$operations\[\d+\]\["\w+\"]/
233 234 235 236
     * @dataProvider provideOpsWithMissingArguments
     */
    public function testMissingArguments(array $ops)
    {
237
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
238 239 240 241
    }

    public function provideOpsWithMissingArguments()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
242 243 244 245 246 247 248 249 250 251 252
        return [
            [[['insertOne' => []]]],
            [[['updateOne' => []]]],
            [[['updateOne' => [['_id' => 1]]]]],
            [[['updateMany' => []]]],
            [[['updateMany' => [['_id' => 1]]]]],
            [[['replaceOne' => []]]],
            [[['replaceOne' => [['_id' => 1]]]]],
            [[['deleteOne' => []]]],
            [[['deleteMany' => []]]],
        ];
253 254 255 256
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
257
     * @expectedExceptionMessage First key in $operations[0]["updateOne"][1] is not an update operator
258 259 260
     */
    public function testUpdateOneRequiresUpdateOperators()
    {
261
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
Jeremy Mikola's avatar
Jeremy Mikola committed
262 263
            ['updateOne' => [['_id' => 1], ['x' => 1]]],
        ]);
264 265 266 267
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
268
     * @expectedExceptionMessage First key in $operations[0]["updateMany"][1] is not an update operator
269 270 271
     */
    public function testUpdateManyRequiresUpdateOperators()
    {
272
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
Jeremy Mikola's avatar
Jeremy Mikola committed
273 274
            ['updateMany' => [['_id' => ['$gt' => 1]], ['x' => 1]]],
        ]);
275 276 277 278
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
279
     * @expectedExceptionMessage First key in $operations[0]["replaceOne"][1] is an update operator
280 281 282
     */
    public function testReplaceOneRequiresReplacementDocument()
    {
283
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
Jeremy Mikola's avatar
Jeremy Mikola committed
284 285
            ['replaceOne' => [['_id' => 1], ['$inc' => ['x' => 1]]]],
        ]);
286 287 288 289 290 291 292 293 294
    }

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

        for ($i = 1; $i <= $n; $i++) {
Jeremy Mikola's avatar
Jeremy Mikola committed
298
            $bulkWrite->insert([
299 300
                '_id' => $i,
                'x' => (integer) ($i . $i),
Jeremy Mikola's avatar
Jeremy Mikola committed
301
            ]);
302 303 304 305 306 307
        }

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

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