BulkWriteTest.php 13.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php

namespace MongoDB\Tests\Operation;

use MongoDB\Operation\BulkWrite;

class BulkWriteTest extends TestCase
{
    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @expectedExceptionMessage $operations is empty
     */
    public function testOperationsMustNotBeEmpty()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
15
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), []);
16 17 18 19 20 21 22 23
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @expectedExceptionMessage $operations is not a list (unexpected index: "1")
     */
    public function testOperationsMustBeAList()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
24 25 26
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            1 => [BulkWrite::INSERT_ONE => [['x' => 1]]],
        ]);
27 28 29 30 31 32 33 34
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @expectedExceptionMessage Expected one element in $operation[0], actually: 2
     */
    public function testMultipleOperationsInOneElement()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
35 36 37 38 39 40
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [
                BulkWrite::INSERT_ONE => [['x' => 1]],
                BulkWrite::DELETE_ONE => [['x' => 1]],
            ],
        ]);
41 42 43 44 45 46 47 48
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @expectedExceptionMessage Unknown operation type "foo" in $operations[0]
     */
    public function testUnknownOperation()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
49 50 51
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            ['foo' => [['_id' => 1]]],
        ]);
52 53 54 55 56 57 58 59
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @expectedExceptionMessage Missing first argument for $operations[0]["insertOne"]
     */
    public function testInsertOneDocumentArgumentMissing()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
60 61 62
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::INSERT_ONE => []],
        ]);
63 64 65
    }

    /**
66
     * @expectedException MongoDB\Exception\InvalidArgumentException
67 68
     * @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["insertOne"\]\[0\] to have type "array or object" but found "[\w ]+"/
     * @dataProvider provideInvalidDocumentValues
69
     */
70
    public function testInsertOneDocumentArgumentTypeCheck($document)
71
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
72 73 74
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::INSERT_ONE => [$document]],
        ]);
75 76 77 78 79 80 81 82
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @expectedExceptionMessage Missing first argument for $operations[0]["deleteMany"]
     */
    public function testDeleteManyFilterArgumentMissing()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
83 84 85
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::DELETE_MANY => []],
        ]);
86 87 88
    }

    /**
89
     * @expectedException MongoDB\Exception\InvalidArgumentException
90 91
     * @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["deleteMany"\]\[0\] to have type "array or object" but found "[\w ]+"/
     * @dataProvider provideInvalidDocumentValues
92
     */
93
    public function testDeleteManyFilterArgumentTypeCheck($document)
94
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
95 96 97
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::DELETE_MANY => [$document]],
        ]);
98 99 100 101 102 103 104 105
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @expectedExceptionMessage Missing first argument for $operations[0]["deleteOne"]
     */
    public function testDeleteOneFilterArgumentMissing()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
106 107 108
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::DELETE_ONE => []],
        ]);
109 110 111
    }

    /**
112
     * @expectedException MongoDB\Exception\InvalidArgumentException
113 114
     * @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["deleteOne"\]\[0\] to have type "array or object" but found "[\w ]+"/
     * @dataProvider provideInvalidDocumentValues
115
     */
116
    public function testDeleteOneFilterArgumentTypeCheck($document)
117
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
118 119 120
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::DELETE_ONE => [$document]],
        ]);
121 122 123 124 125 126 127 128
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @expectedExceptionMessage Missing first argument for $operations[0]["replaceOne"]
     */
    public function testReplaceOneFilterArgumentMissing()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
129 130 131
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::REPLACE_ONE => []],
        ]);
132 133 134
    }

    /**
135
     * @expectedException MongoDB\Exception\InvalidArgumentException
136 137
     * @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["replaceOne"\]\[0\] to have type "array or object" but found "[\w ]+"/
     * @dataProvider provideInvalidDocumentValues
138
     */
139
    public function testReplaceOneFilterArgumentTypeCheck($filter)
140
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
141 142 143
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::REPLACE_ONE => [$filter, ['y' => 1]]],
        ]);
144 145 146 147 148 149 150 151
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @expectedExceptionMessage Missing second argument for $operations[0]["replaceOne"]
     */
    public function testReplaceOneReplacementArgumentMissing()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
152 153 154
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::REPLACE_ONE => [['x' => 1]]],
        ]);
155 156 157
    }

    /**
158
     * @expectedException MongoDB\Exception\InvalidArgumentException
159 160
     * @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["replaceOne"\]\[1\] to have type "array or object" but found "[\w ]+"/
     * @dataProvider provideInvalidDocumentValues
161
     */
162
    public function testReplaceOneReplacementArgumentTypeCheck($replacement)
163
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
164 165 166
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::REPLACE_ONE => [['x' => 1], $replacement]],
        ]);
167 168 169 170 171 172 173 174
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @expectedExceptionMessage First key in $operations[0]["replaceOne"][1] is an update operator
     */
    public function testReplaceOneReplacementArgumentRequiresNoOperators()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
175 176 177
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::REPLACE_ONE => [['_id' => 1], ['$inc' => ['x' => 1]]]],
        ]);
178 179 180
    }

    /**
181
     * @expectedException MongoDB\Exception\InvalidArgumentException
182 183
     * @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["replaceOne"\]\[2\]\["upsert"\] to have type "boolean" but found "[\w ]+"/
     * @dataProvider provideInvalidBooleanValues
184
     */
185
    public function testReplaceOneUpsertOptionTypeCheck($upsert)
186
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
187 188 189
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::REPLACE_ONE => [['x' => 1], ['y' => 1], ['upsert' => $upsert]]],
        ]);
190 191 192 193 194 195 196 197
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @expectedExceptionMessage Missing first argument for $operations[0]["updateMany"]
     */
    public function testUpdateManyFilterArgumentMissing()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
198 199 200
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::UPDATE_MANY => []],
        ]);
201 202 203
    }

    /**
204
     * @expectedException MongoDB\Exception\InvalidArgumentException
205 206
     * @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateMany"\]\[0\] to have type "array or object" but found "[\w ]+"/
     * @dataProvider provideInvalidDocumentValues
207
     */
208
    public function testUpdateManyFilterArgumentTypeCheck($filter)
209
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
210 211 212
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::UPDATE_MANY => [$filter, ['$set' => ['x' => 1]]]],
        ]);
213 214 215 216 217 218 219 220
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @expectedExceptionMessage Missing second argument for $operations[0]["updateMany"]
     */
    public function testUpdateManyUpdateArgumentMissing()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
221 222 223
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::UPDATE_MANY => [['x' => 1]]],
        ]);
224 225 226
    }

    /**
227
     * @expectedException MongoDB\Exception\InvalidArgumentException
228 229
     * @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateMany"\]\[1\] to have type "array or object" but found "[\w ]+"/
     * @dataProvider provideInvalidDocumentValues
230
     */
231
    public function testUpdateManyUpdateArgumentTypeCheck($update)
232
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
233 234 235
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::UPDATE_MANY => [['x' => 1], $update]],
        ]);
236 237 238 239 240 241 242 243
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @expectedExceptionMessage First key in $operations[0]["updateMany"][1] is not an update operator
     */
    public function testUpdateManyUpdateArgumentRequiresOperators()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
244 245 246
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::UPDATE_MANY => [['_id' => ['$gt' => 1]], ['x' => 1]]],
        ]);
247 248 249
    }

    /**
250
     * @expectedException MongoDB\Exception\InvalidArgumentException
251 252
     * @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateMany"\]\[2\]\["upsert"\] to have type "boolean" but found "[\w ]+"/
     * @dataProvider provideInvalidBooleanValues
253
     */
254
    public function testUpdateManyUpsertOptionTypeCheck($upsert)
255
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
256 257 258
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::UPDATE_MANY => [['x' => 1], ['$set' => ['x' => 1]], ['upsert' => $upsert]]],
        ]);
259 260 261 262 263 264 265 266
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @expectedExceptionMessage Missing first argument for $operations[0]["updateOne"]
     */
    public function testUpdateOneFilterArgumentMissing()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
267 268 269
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::UPDATE_ONE => []],
        ]);
270 271 272
    }

    /**
273
     * @expectedException MongoDB\Exception\InvalidArgumentException
274 275
     * @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateOne"\]\[0\] to have type "array or object" but found "[\w ]+"/
     * @dataProvider provideInvalidDocumentValues
276
     */
277
    public function testUpdateOneFilterArgumentTypeCheck($filter)
278
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
279 280 281
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::UPDATE_ONE => [$filter, ['$set' => ['x' => 1]]]],
        ]);
282 283 284 285 286 287 288 289
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @expectedExceptionMessage Missing second argument for $operations[0]["updateOne"]
     */
    public function testUpdateOneUpdateArgumentMissing()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
290 291 292
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::UPDATE_ONE => [['x' => 1]]],
        ]);
293 294 295
    }

    /**
296
     * @expectedException MongoDB\Exception\InvalidArgumentException
297 298
     * @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateOne"\]\[1\] to have type "array or object" but found "[\w ]+"/
     * @dataProvider provideInvalidDocumentValues
299
     */
300
    public function testUpdateOneUpdateArgumentTypeCheck($update)
301
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
302 303 304
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::UPDATE_ONE => [['x' => 1], $update]],
        ]);
305 306 307 308 309 310 311 312
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @expectedExceptionMessage First key in $operations[0]["updateOne"][1] is not an update operator
     */
    public function testUpdateOneUpdateArgumentRequiresOperators()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
313 314 315
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::UPDATE_ONE => [['_id' => 1], ['x' => 1]]],
        ]);
316 317 318
    }

    /**
319
     * @expectedException MongoDB\Exception\InvalidArgumentException
320 321
     * @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateOne"\]\[2\]\["upsert"\] to have type "boolean" but found "[\w ]+"/
     * @dataProvider provideInvalidBooleanValues
322
     */
323
    public function testUpdateOneUpsertOptionTypeCheck($upsert)
324
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
325 326 327
        new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
            [BulkWrite::UPDATE_ONE => [['x' => 1], ['$set' => ['x' => 1]], ['upsert' => $upsert]]],
        ]);
328
    }
329 330

    /**
331
     * @expectedException MongoDB\Exception\InvalidArgumentException
332 333 334 335 336 337 338
     * @dataProvider provideInvalidConstructorOptions
     */
    public function testConstructorOptionTypeChecks(array $options)
    {
        new BulkWrite(
            $this->getDatabaseName(),
            $this->getCollectionName(),
Jeremy Mikola's avatar
Jeremy Mikola committed
339
            [[BulkWrite::INSERT_ONE => [['x' => 1]]]],
340 341 342 343
            $options
        );
    }

344 345 346 347 348
    public function provideInvalidBooleanValues()
    {
        return $this->wrapValuesForDataProvider($this->getInvalidBooleanValues());
    }

349 350
    public function provideInvalidConstructorOptions()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
351
        $options = [];
352

353 354 355 356
        foreach ($this->getInvalidBooleanValues() as $value) {
            $options[][] = ['bypassDocumentValidation' => $value];
        }

357
        foreach ($this->getInvalidBooleanValues() as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
358
            $options[][] = ['ordered' => $value];
359 360 361
        }

        foreach ($this->getInvalidWriteConcernValues() as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
362
            $options[][] = ['writeConcern' => $value];
363 364 365 366
        }

        return $options;
    }
367
}