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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
126
127
128
129
130
131
132
133
134
135
136
137
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Operation\BulkWrite;
class BulkWriteTest extends TestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage $operations is empty
*/
public function testOperationsMustNotBeEmpty()
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), []);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage $operations is not a list (unexpected index: "1")
*/
public function testOperationsMustBeAList()
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
1 => [BulkWrite::INSERT_ONE => [['x' => 1]]],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage Expected one element in $operation[0], actually: 2
*/
public function testMultipleOperationsInOneElement()
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[
BulkWrite::INSERT_ONE => [['x' => 1]],
BulkWrite::DELETE_ONE => [['x' => 1]],
],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage Unknown operation type "foo" in $operations[0]
*/
public function testUnknownOperation()
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
['foo' => [['_id' => 1]]],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage Missing first argument for $operations[0]["insertOne"]
*/
public function testInsertOneDocumentArgumentMissing()
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::INSERT_ONE => []],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["insertOne"\]\[0\] to have type "array or object" but found "[\w ]+"/
* @dataProvider provideInvalidDocumentValues
*/
public function testInsertOneDocumentArgumentTypeCheck($document)
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::INSERT_ONE => [$document]],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage Missing first argument for $operations[0]["deleteMany"]
*/
public function testDeleteManyFilterArgumentMissing()
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::DELETE_MANY => []],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["deleteMany"\]\[0\] to have type "array or object" but found "[\w ]+"/
* @dataProvider provideInvalidDocumentValues
*/
public function testDeleteManyFilterArgumentTypeCheck($document)
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::DELETE_MANY => [$document]],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage Missing first argument for $operations[0]["deleteOne"]
*/
public function testDeleteOneFilterArgumentMissing()
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::DELETE_ONE => []],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["deleteOne"\]\[0\] to have type "array or object" but found "[\w ]+"/
* @dataProvider provideInvalidDocumentValues
*/
public function testDeleteOneFilterArgumentTypeCheck($document)
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::DELETE_ONE => [$document]],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage Missing first argument for $operations[0]["replaceOne"]
*/
public function testReplaceOneFilterArgumentMissing()
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::REPLACE_ONE => []],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["replaceOne"\]\[0\] to have type "array or object" but found "[\w ]+"/
* @dataProvider provideInvalidDocumentValues
*/
public function testReplaceOneFilterArgumentTypeCheck($filter)
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::REPLACE_ONE => [$filter, ['y' => 1]]],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage Missing second argument for $operations[0]["replaceOne"]
*/
public function testReplaceOneReplacementArgumentMissing()
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::REPLACE_ONE => [['x' => 1]]],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["replaceOne"\]\[1\] to have type "array or object" but found "[\w ]+"/
* @dataProvider provideInvalidDocumentValues
*/
public function testReplaceOneReplacementArgumentTypeCheck($replacement)
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::REPLACE_ONE => [['x' => 1], $replacement]],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage First key in $operations[0]["replaceOne"][1] is an update operator
*/
public function testReplaceOneReplacementArgumentRequiresNoOperators()
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::REPLACE_ONE => [['_id' => 1], ['$inc' => ['x' => 1]]]],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["replaceOne"\]\[2\]\["upsert"\] to have type "boolean" but found "[\w ]+"/
* @dataProvider provideInvalidBooleanValues
*/
public function testReplaceOneUpsertOptionTypeCheck($upsert)
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::REPLACE_ONE => [['x' => 1], ['y' => 1], ['upsert' => $upsert]]],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage Missing first argument for $operations[0]["updateMany"]
*/
public function testUpdateManyFilterArgumentMissing()
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::UPDATE_MANY => []],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateMany"\]\[0\] to have type "array or object" but found "[\w ]+"/
* @dataProvider provideInvalidDocumentValues
*/
public function testUpdateManyFilterArgumentTypeCheck($filter)
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::UPDATE_MANY => [$filter, ['$set' => ['x' => 1]]]],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage Missing second argument for $operations[0]["updateMany"]
*/
public function testUpdateManyUpdateArgumentMissing()
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::UPDATE_MANY => [['x' => 1]]],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateMany"\]\[1\] to have type "array or object" but found "[\w ]+"/
* @dataProvider provideInvalidDocumentValues
*/
public function testUpdateManyUpdateArgumentTypeCheck($update)
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::UPDATE_MANY => [['x' => 1], $update]],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage First key in $operations[0]["updateMany"][1] is not an update operator
*/
public function testUpdateManyUpdateArgumentRequiresOperators()
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::UPDATE_MANY => [['_id' => ['$gt' => 1]], ['x' => 1]]],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateMany"\]\[2\]\["upsert"\] to have type "boolean" but found "[\w ]+"/
* @dataProvider provideInvalidBooleanValues
*/
public function testUpdateManyUpsertOptionTypeCheck($upsert)
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::UPDATE_MANY => [['x' => 1], ['$set' => ['x' => 1]], ['upsert' => $upsert]]],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage Missing first argument for $operations[0]["updateOne"]
*/
public function testUpdateOneFilterArgumentMissing()
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::UPDATE_ONE => []],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateOne"\]\[0\] to have type "array or object" but found "[\w ]+"/
* @dataProvider provideInvalidDocumentValues
*/
public function testUpdateOneFilterArgumentTypeCheck($filter)
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::UPDATE_ONE => [$filter, ['$set' => ['x' => 1]]]],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage Missing second argument for $operations[0]["updateOne"]
*/
public function testUpdateOneUpdateArgumentMissing()
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::UPDATE_ONE => [['x' => 1]]],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateOne"\]\[1\] to have type "array or object" but found "[\w ]+"/
* @dataProvider provideInvalidDocumentValues
*/
public function testUpdateOneUpdateArgumentTypeCheck($update)
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::UPDATE_ONE => [['x' => 1], $update]],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage First key in $operations[0]["updateOne"][1] is not an update operator
*/
public function testUpdateOneUpdateArgumentRequiresOperators()
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::UPDATE_ONE => [['_id' => 1], ['x' => 1]]],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessageRegExp /Expected \$operations\[0\]\["updateOne"\]\[2\]\["upsert"\] to have type "boolean" but found "[\w ]+"/
* @dataProvider provideInvalidBooleanValues
*/
public function testUpdateOneUpsertOptionTypeCheck($upsert)
{
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[BulkWrite::UPDATE_ONE => [['x' => 1], ['$set' => ['x' => 1]], ['upsert' => $upsert]]],
]);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidConstructorOptions
*/
public function testConstructorOptionTypeChecks(array $options)
{
new BulkWrite(
$this->getDatabaseName(),
$this->getCollectionName(),
[[BulkWrite::INSERT_ONE => [['x' => 1]]]],
$options
);
}
public function provideInvalidBooleanValues()
{
return $this->wrapValuesForDataProvider($this->getInvalidBooleanValues());
}
public function provideInvalidConstructorOptions()
{
$options = [];
foreach ($this->getInvalidBooleanValues() as $value) {
$options[][] = ['bypassDocumentValidation' => $value];
}
foreach ($this->getInvalidBooleanValues() as $value) {
$options[][] = ['ordered' => $value];
}
foreach ($this->getInvalidWriteConcernValues() as $value) {
$options[][] = ['writeConcern' => $value];
}
return $options;
}
}