BulkWriteFunctionalTest.php 7.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<?php

namespace MongoDB\Tests\Collection;

use MongoDB\Driver\BulkWrite;

class BulkWriteFunctionalTest extends FunctionalTestCase
{
    private $omitModifiedCount;

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

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

    public function testInserts()
    {
        $ops = array(
            array('insertOne' => array(array('_id' => 1, 'x' => 11))),
            array('insertOne' => array(array('x' => 22))),
        );

        $result = $this->collection->bulkWrite($ops);
        $this->assertInstanceOf('MongoDB\BulkWriteResult', $result);
        $this->assertSame(2, $result->getInsertedCount());

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

        $expected = array(
            array('_id' => $insertedIds[0], 'x' => 11),
            array('_id' => $insertedIds[1], 'x' => 22),
        );

38
        $this->assertSameDocuments($expected, $this->collection->find());
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    }

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

        $ops = array(
            array('updateOne' => array(array('_id' => 2), array('$inc' => array('x' => 1)))),
            array('updateMany' => array(array('_id' => array('$gt' => 2)), array('$inc' => array('x' => -1)))),
            array('updateOne' => array(array('_id' => 5), array('$set' => array('x' => 55)), array('upsert' => true))),
            array('updateOne' => array(array('x' => 66), array('$set' => array('x' => 66)), array('upsert' => true))),
            array('updateMany' => array(array('x' => array('$gt' => 50)), array('$inc' => array('x' => 1)))),
        );

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

        $expected = array(
            array('_id' => 1, 'x' => 11),
            array('_id' => 2, 'x' => 23),
            array('_id' => 3, 'x' => 32),
            array('_id' => 4, 'x' => 43),
            array('_id' => 5, 'x' => 56),
            array('_id' => $upsertedIds[3], 'x' => 67),
        );

72
        $this->assertSameDocuments($expected, $this->collection->find());
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    }

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

        $ops = array(
            array('deleteOne' => array(array('_id' => 1))),
            array('deleteMany' => array(array('_id' => array('$gt' => 2)))),
        );

        $result = $this->collection->bulkWrite($ops);
        $this->assertInstanceOf('MongoDB\BulkWriteResult', $result);
        $this->assertSame(3, $result->getDeletedCount());

        $expected = array(
            array('_id' => 2, 'x' => 22),
        );

92
        $this->assertSameDocuments($expected, $this->collection->find());
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    }

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

        $ops = array(
            array('updateOne' => array(array('_id' => array('$gt' => 1)), array('$inc' => array('x' => 1)))),
            array('updateMany' => array(array('_id' => array('$gt' => 1)), array('$inc' => array('x' => 1)))),
            array('insertOne' => array(array('_id' => 4, 'x' => 44))),
            array('deleteMany' => array(array('x' => array('$nin' => array(24, 34))))),
            array('replaceOne' => array(array('_id' => 4), array('_id' => 4, 'x' => 44), array('upsert' => true))),
        );

        $result = $this->collection->bulkWrite($ops);
        $this->assertInstanceOf('MongoDB\BulkWriteResult', $result);

        $this->assertSame(1, $result->getInsertedCount());
        $this->assertSame(array(2 => 4), $result->getInsertedIds());

        $this->assertSame(3, $result->getMatchedCount());
        $this->omitModifiedCount or $this->assertSame(3, $result->getModifiedCount());
        $this->assertSame(1, $result->getUpsertedCount());
        $this->assertSame(array(4 => 4), $result->getUpsertedIds());

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

        $expected = array(
            array('_id' => 2, 'x' => 24),
            array('_id' => 3, 'x' => 34),
            array('_id' => 4, 'x' => 44),
        );

126
        $this->assertSameDocuments($expected, $this->collection->find());
127 128 129 130
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
131
     * @expectedExceptionMessage Unknown operation type "foo" in $operations[0]
132 133 134 135 136 137 138 139 140 141
     */
    public function testUnknownOperation()
    {
        $this->collection->bulkWrite(array(
            array('foo' => array(array('_id' => 1))),
        ));
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
142
     * @expectedExceptionMessageRegExp /Missing (first|second) argument for \$operations\[\d+\]\["\w+\"]/
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
     * @dataProvider provideOpsWithMissingArguments
     */
    public function testMissingArguments(array $ops)
    {
        $this->collection->bulkWrite($ops);
    }

    public function provideOpsWithMissingArguments()
    {
        return array(
            array(array(array('insertOne' => array()))),
            array(array(array('updateOne' => array()))),
            array(array(array('updateOne' => array(array('_id' => 1))))),
            array(array(array('updateMany' => array()))),
            array(array(array('updateMany' => array(array('_id' => 1))))),
            array(array(array('replaceOne' => array()))),
            array(array(array('replaceOne' => array(array('_id' => 1))))),
            array(array(array('deleteOne' => array()))),
            array(array(array('deleteMany' => array()))),
        );
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
167
     * @expectedExceptionMessage First key in $operations[0]["updateOne"][1] is not an update operator
168 169 170 171 172 173 174 175 176 177
     */
    public function testUpdateOneRequiresUpdateOperators()
    {
        $this->collection->bulkWrite(array(
            array('updateOne' => array(array('_id' => 1), array('x' => 1))),
        ));
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
178
     * @expectedExceptionMessage First key in $operations[0]["updateMany"][1] is not an update operator
179 180 181 182 183 184 185 186 187 188
     */
    public function testUpdateManyRequiresUpdateOperators()
    {
        $this->collection->bulkWrite(array(
            array('updateMany' => array(array('_id' => array('$gt' => 1)), array('x' => 1))),
        ));
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
189
     * @expectedExceptionMessage First key in $operations[0]["replaceOne"][1] is an update operator
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 testReplaceOneRequiresReplacementDocument()
    {
        $this->collection->bulkWrite(array(
            array('replaceOne' => array(array('_id' => 1), array('$inc' => array('x' => 1)))),
        ));
    }

    /**
     * Create data fixtures.
     *
     * @param integer $n
     */
    private function createFixtures($n)
    {
        $bulkWrite = new BulkWrite(true);

        for ($i = 1; $i <= $n; $i++) {
            $bulkWrite->insert(array(
                '_id' => $i,
                'x' => (integer) ($i . $i),
            ));
        }

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

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