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

namespace MongoDB\Tests\Collection;

5 6
use MongoDB\Driver\BulkWrite as Bulk;
use MongoDB\Operation\BulkWrite;
7 8 9 10 11 12 13 14 15 16 17 18 19 20

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

117 118 119
        $this->assertInstanceOf('MongoDB\BulkWriteResult', $result);

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

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

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

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

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

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
140
     * @expectedExceptionMessage Unknown operation type "foo" in $operations[0]
141 142 143
     */
    public function testUnknownOperation()
    {
144
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
Jeremy Mikola's avatar
Jeremy Mikola committed
145 146
            ['foo' => [['_id' => 1]]],
        ]);
147 148 149 150
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
151
     * @expectedExceptionMessageRegExp /Missing (first|second) argument for \$operations\[\d+\]\["\w+\"]/
152 153 154 155
     * @dataProvider provideOpsWithMissingArguments
     */
    public function testMissingArguments(array $ops)
    {
156
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), $ops);
157 158 159 160
    }

    public function provideOpsWithMissingArguments()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
161 162 163 164 165 166 167 168 169 170 171
        return [
            [[['insertOne' => []]]],
            [[['updateOne' => []]]],
            [[['updateOne' => [['_id' => 1]]]]],
            [[['updateMany' => []]]],
            [[['updateMany' => [['_id' => 1]]]]],
            [[['replaceOne' => []]]],
            [[['replaceOne' => [['_id' => 1]]]]],
            [[['deleteOne' => []]]],
            [[['deleteMany' => []]]],
        ];
172 173 174 175
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
176
     * @expectedExceptionMessage First key in $operations[0]["updateOne"][1] is not an update operator
177 178 179
     */
    public function testUpdateOneRequiresUpdateOperators()
    {
180
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
Jeremy Mikola's avatar
Jeremy Mikola committed
181 182
            ['updateOne' => [['_id' => 1], ['x' => 1]]],
        ]);
183 184 185 186
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
187
     * @expectedExceptionMessage First key in $operations[0]["updateMany"][1] is not an update operator
188 189 190
     */
    public function testUpdateManyRequiresUpdateOperators()
    {
191
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
Jeremy Mikola's avatar
Jeremy Mikola committed
192 193
            ['updateMany' => [['_id' => ['$gt' => 1]], ['x' => 1]]],
        ]);
194 195 196 197
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
198
     * @expectedExceptionMessage First key in $operations[0]["replaceOne"][1] is an update operator
199 200 201
     */
    public function testReplaceOneRequiresReplacementDocument()
    {
202
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
Jeremy Mikola's avatar
Jeremy Mikola committed
203 204
            ['replaceOne' => [['_id' => 1], ['$inc' => ['x' => 1]]]],
        ]);
205 206 207 208 209 210 211 212 213
    }

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

        for ($i = 1; $i <= $n; $i++) {
Jeremy Mikola's avatar
Jeremy Mikola committed
217
            $bulkWrite->insert([
218 219
                '_id' => $i,
                'x' => (integer) ($i . $i),
Jeremy Mikola's avatar
Jeremy Mikola committed
220
            ]);
221 222 223 224 225 226
        }

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

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