BulkWriteFunctionalTest.php 10.2 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\Operation\BulkWrite;
9 10 11 12 13 14 15 16 17 18 19 20 21 22

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
23 24 25 26
        $ops = [
            ['insertOne' => [['_id' => 1, 'x' => 11]]],
            ['insertOne' => [['x' => 22]]],
        ];
27

28 29 30
        $operation = new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
        $result = $operation->execute($this->getPrimaryServer());

31 32 33 34 35
        $this->assertInstanceOf('MongoDB\BulkWriteResult', $result);
        $this->assertSame(2, $result->getInsertedCount());

        $insertedIds = $result->getInsertedIds();
        $this->assertSame(1, $insertedIds[0]);
36
        $this->assertInstanceOf('MongoDB\BSON\ObjectId', $insertedIds[1]);
37

Jeremy Mikola's avatar
Jeremy Mikola committed
38 39 40 41
        $expected = [
            ['_id' => $insertedIds[0], 'x' => 11],
            ['_id' => $insertedIds[1], 'x' => 22],
        ];
42

43
        $this->assertSameDocuments($expected, $this->collection->find());
44 45 46 47 48 49
    }

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

Jeremy Mikola's avatar
Jeremy Mikola committed
50 51 52 53 54 55 56
        $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]]]],
        ];
57

58 59 60
        $operation = new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
        $result = $operation->execute($this->getPrimaryServer());

61 62 63 64 65 66 67
        $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]);
68
        $this->assertInstanceOf('MongoDB\BSON\ObjectId', $upsertedIds[3]);
69

Jeremy Mikola's avatar
Jeremy Mikola committed
70 71 72 73 74 75 76 77
        $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],
        ];
78

79
        $this->assertSameDocuments($expected, $this->collection->find());
80 81 82 83 84 85
    }

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

Jeremy Mikola's avatar
Jeremy Mikola committed
86 87 88 89
        $ops = [
            ['deleteOne' => [['_id' => 1]]],
            ['deleteMany' => [['_id' => ['$gt' => 2]]]],
        ];
90

91 92 93
        $operation = new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
        $result = $operation->execute($this->getPrimaryServer());

94 95 96
        $this->assertInstanceOf('MongoDB\BulkWriteResult', $result);
        $this->assertSame(3, $result->getDeletedCount());

Jeremy Mikola's avatar
Jeremy Mikola committed
97 98 99
        $expected = [
            ['_id' => 2, 'x' => 22],
        ];
100

101
        $this->assertSameDocuments($expected, $this->collection->find());
102 103 104 105 106 107
    }

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

Jeremy Mikola's avatar
Jeremy Mikola committed
108 109 110 111 112 113 114
        $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]]],
        ];
115

116 117 118
        $operation = new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
        $result = $operation->execute($this->getPrimaryServer());

119 120 121
        $this->assertInstanceOf('MongoDB\BulkWriteResult', $result);

        $this->assertSame(1, $result->getInsertedCount());
Jeremy Mikola's avatar
Jeremy Mikola committed
122
        $this->assertSame([2 => 4], $result->getInsertedIds());
123 124 125 126

        $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
127
        $this->assertSame([4 => 4], $result->getUpsertedIds());
128 129 130

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

Jeremy Mikola's avatar
Jeremy Mikola committed
131 132 133 134 135
        $expected = [
            ['_id' => 2, 'x' => 24],
            ['_id' => 3, 'x' => 34],
            ['_id' => 4, 'x' => 44],
        ];
136

137
        $this->assertSameDocuments($expected, $this->collection->find());
138 139
    }

140 141 142 143 144 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
    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();
    }

212 213
    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
214
     * @expectedExceptionMessage Unknown operation type "foo" in $operations[0]
215 216 217
     */
    public function testUnknownOperation()
    {
218
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
Jeremy Mikola's avatar
Jeremy Mikola committed
219 220
            ['foo' => [['_id' => 1]]],
        ]);
221 222 223 224
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
225
     * @expectedExceptionMessageRegExp /Missing (first|second) argument for \$operations\[\d+\]\["\w+\"]/
226 227 228 229
     * @dataProvider provideOpsWithMissingArguments
     */
    public function testMissingArguments(array $ops)
    {
230
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
231 232 233 234
    }

    public function provideOpsWithMissingArguments()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
235 236 237 238 239 240 241 242 243 244 245
        return [
            [[['insertOne' => []]]],
            [[['updateOne' => []]]],
            [[['updateOne' => [['_id' => 1]]]]],
            [[['updateMany' => []]]],
            [[['updateMany' => [['_id' => 1]]]]],
            [[['replaceOne' => []]]],
            [[['replaceOne' => [['_id' => 1]]]]],
            [[['deleteOne' => []]]],
            [[['deleteMany' => []]]],
        ];
246 247 248 249
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
250
     * @expectedExceptionMessage First key in $operations[0]["updateOne"][1] is not an update operator
251 252 253
     */
    public function testUpdateOneRequiresUpdateOperators()
    {
254
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
Jeremy Mikola's avatar
Jeremy Mikola committed
255 256
            ['updateOne' => [['_id' => 1], ['x' => 1]]],
        ]);
257 258 259 260
    }

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

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

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

        for ($i = 1; $i <= $n; $i++) {
Jeremy Mikola's avatar
Jeremy Mikola committed
291
            $bulkWrite->insert([
292 293
                '_id' => $i,
                'x' => (integer) ($i . $i),
Jeremy Mikola's avatar
Jeremy Mikola committed
294
            ]);
295 296 297 298 299 300
        }

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

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