@@ -28,17 +28,31 @@ public function testConstructorExceptionEntity()
28
28
$ collection = new Collection ($ manager , 'test-db ' , 'test-collection ' , ['documentClass ' => \stdClass::class]);
29
29
}
30
30
31
+ /**
32
+ * Provide data for testing 'createIndex' method
33
+ *
34
+ * @return array
35
+ */
36
+ public function createIndexProvider ()
37
+ {
38
+ return [
39
+ [['foo ' => 1 ]],
40
+ [(object )['foo ' => 1 ]]
41
+ ];
42
+ }
43
+
31
44
/**
32
45
* Test 'createIndex' method with no deletion or exception
46
+ *
47
+ * @dataProvider createIndexProvider
33
48
*/
34
- public function testCreateIndex ()
49
+ public function testCreateIndex ($ keys )
35
50
{
36
- $ keys = ['foo ' => 1 ];
37
51
$ options = ['zoo ' => 'baz ' ];
38
52
$ expected = 'foo_result ' ;
39
53
40
54
$ collection = $ this ->createPartialMock (Collection::class, ['parent ' ]);
41
- $ collection ->expects ($ this ->once ())->method ('parent ' , $ keys , $ options )->willReturn ($ expected );
55
+ $ collection ->expects ($ this ->once ())->method ('parent ' , ( array ) $ keys , $ options )->willReturn ($ expected );
42
56
43
57
$ result = $ collection ->createIndex ($ keys , $ options );
44
58
@@ -67,7 +81,7 @@ public function createIndexExceptionProvider()
67
81
*/
68
82
public function testCreateIndexException ($ exception , $ options )
69
83
{
70
- $ keys = ['foo ' => ' bar ' ];
84
+ $ keys = ['foo ' => 1 ];
71
85
72
86
$ collection = $ this ->createPartialMock (Collection::class, ['parent ' ]);
73
87
$ collection ->expects ($ this ->once ())->method ('parent ' , $ keys , $ options )->willThrowException ($ exception );
@@ -78,10 +92,11 @@ public function testCreateIndexException($exception, $options)
78
92
79
93
/**
80
94
* Test 'createIndex' method with deleting existing index
95
+ *
96
+ * @dataProvider createIndexProvider
81
97
*/
82
- public function testCreateIndexForce ()
98
+ public function testCreateIndexForce ($ keys )
83
99
{
84
- $ keys = ['foo ' => 1 ];
85
100
$ options = ['force ' => true ];
86
101
$ callbackState = (object )['called ' => 0 ];
87
102
@@ -96,7 +111,7 @@ public function testCreateIndexForce()
96
111
};
97
112
98
113
$ collection = $ this ->createPartialMock (Collection::class, ['parent ' , 'dropIndex ' ]);
99
- $ collection ->expects ($ this ->exactly (2 ))->method ('parent ' , $ keys , $ options )->will ($ this ->returnCallback ($ callback ));
114
+ $ collection ->expects ($ this ->exactly (2 ))->method ('parent ' , ( array ) $ keys , $ options )->will ($ this ->returnCallback ($ callback ));
100
115
$ collection ->expects ($ this ->once ())->method ('dropIndex ' )->with ('foo ' );
101
116
102
117
$ result = $ collection ->createIndex ($ keys , $ options );
@@ -106,18 +121,19 @@ public function testCreateIndexForce()
106
121
107
122
/**
108
123
* Test 'createIndex' method with deleting existing index
124
+ *
125
+ * @dataProvider createIndexProvider
109
126
*/
110
- public function testCreateIndexIgnore ()
127
+ public function testCreateIndexIgnore ($ keys )
111
128
{
112
- $ keys = ['foo ' => 1 ];
113
129
$ options = ['ignore ' => true ];
114
130
115
131
$ callback = function ($ keysArg , $ optionsArg ) {
116
132
throw new \MongoDB \Driver \Exception \RuntimeException ('' , 85 );
117
133
};
118
134
119
135
$ collection = $ this ->createPartialMock (Collection::class, ['parent ' , 'dropIndex ' ]);
120
- $ collection ->expects ($ this ->once ())->method ('parent ' , $ keys , $ options )->will ($ this ->returnCallback ($ callback ));
136
+ $ collection ->expects ($ this ->once ())->method ('parent ' , ( array ) $ keys , $ options )->will ($ this ->returnCallback ($ callback ));
121
137
$ collection ->expects ($ this ->never ())->method ('dropIndex ' );
122
138
123
139
$ result = $ collection ->createIndex ($ keys , $ options );
@@ -167,8 +183,8 @@ public function testWithoutCasting()
167
183
public function insertOneProvider ()
168
184
{
169
185
return [
170
- [['foo ' => 'bar ' ], [ ' foo ' => ' bar ' , ' _id ' => ' foo_id ' ] ],
171
- [(object )['foo ' => 'bar ' ], ( object )[ ' foo ' => ' bar ' , ' _id ' => ' foo_id ' ] ],
186
+ [['foo ' => 'bar ' ]],
187
+ [(object )['foo ' => 'bar ' ]],
172
188
];
173
189
}
174
190
@@ -177,9 +193,8 @@ public function insertOneProvider()
177
193
*
178
194
* @dataProvider insertOneProvider
179
195
*/
180
- public function testInsertOne ($ document, $ expectedDocument )
196
+ public function testInsertOne ($ document )
181
197
{
182
- $ id = 'foo_id ' ;
183
198
$ options = ['opt1 ' => 'val1 ' ];
184
199
$ values = (array )$ document ;
185
200
$ queryResult = $ this ->createMock (\MongoDB \InsertOneResult::class);
@@ -190,11 +205,9 @@ public function testInsertOne($document, $expectedDocument)
190
205
$ collection ->expects ($ this ->once ())->method ('getTypeCaster ' )->willReturn ($ typeCast );
191
206
$ typeCast ->expects ($ this ->once ())->method ('toMongoType ' )->with ($ document , true )->willReturn ($ values );
192
207
$ collection ->expects ($ this ->once ())->method ('parent ' )->with ('insertOne ' , $ values , $ options )->willReturn ($ queryResult );
193
- $ queryResult ->expects ($ this ->once ())->method ('getInsertedId ' )->willReturn ($ id );
194
208
195
209
$ result = $ collection ->insertOne ($ document , $ options );
196
210
$ this ->assertSame ($ queryResult , $ result );
197
- $ this ->assertEquals ($ expectedDocument , $ document );
198
211
}
199
212
200
213
/**
@@ -215,12 +228,6 @@ public function testInsertMany()
215
228
['foo3 ' => 'bar3 ' ]
216
229
];
217
230
218
- $ expectedDocs = [
219
- ['foo ' => 'bar ' , '_id ' => 'a ' ],
220
- ['foo2 ' => 'bar2 ' , '_id ' => 'b ' ],
221
- (object )['foo3 ' => 'bar3 ' , '_id ' => 'c ' ]
222
- ];
223
-
224
231
$ queryResult = $ this ->createMock (\MongoDB \InsertManyResult::class);
225
232
226
233
$ typeCast = $ this ->createMock (DeepCast::class);
@@ -233,46 +240,18 @@ public function testInsertMany()
233
240
$ collection = $ this ->createPartialMock (Collection::class, ['getTypeCaster ' , 'parent ' ]);
234
241
$ collection ->expects ($ this ->once ())->method ('getTypeCaster ' )->willReturn ($ typeCast );
235
242
$ collection ->expects ($ this ->once ())->method ('parent ' )->with ('insertMany ' , $ data , $ options )->willReturn ($ queryResult );
236
- $ queryResult ->expects ($ this ->once ())->method ('getInsertedIds ' )->willReturn (['a ' , 'b ' , 'c ' ]);
237
243
238
244
$ result = $ collection ->insertMany ($ docs , $ options );
239
245
$ this ->assertSame ($ queryResult , $ result );
240
- $ this ->assertEquals ($ expectedDocs , $ docs );
241
246
}
242
247
243
248
/**
244
249
* Test 'replaceOne' method
245
250
*
246
251
* @dataProvider insertOneProvider
247
252
*/
248
- public function testReplaceOne ($ document , $ expectedDocument )
249
- {
250
- $ id = 'foo_id ' ;
251
- $ options = ['opt1 ' => 'val1 ' ];
252
- $ filter = ['match_key ' => 'val ' ];
253
- $ values = (array )$ document ;
254
- $ queryResult = $ this ->createMock (\MongoDB \UpdateResult::class);
255
-
256
- $ typeCast = $ this ->createMock (DeepCast::class);
257
-
258
- $ collection = $ this ->createPartialMock (Collection::class, ['getTypeCaster ' , 'parent ' ]);
259
- $ collection ->expects ($ this ->once ())->method ('getTypeCaster ' )->willReturn ($ typeCast );
260
- $ typeCast ->expects ($ this ->once ())->method ('toMongoType ' )->with ($ document , true )->willReturn ($ values );
261
- $ collection ->expects ($ this ->once ())->method ('parent ' )->with ('replaceOne ' , $ filter , $ values , $ options )->willReturn ($ queryResult );
262
- $ queryResult ->expects ($ this ->once ())->method ('getUpsertedId ' )->willReturn ($ id );
263
-
264
- $ result = $ collection ->replaceOne ($ filter , $ document , $ options );
265
- $ this ->assertSame ($ queryResult , $ result );
266
- $ this ->assertEquals ($ expectedDocument , $ document );
267
- }
268
-
269
- /**
270
- * Test 'replaceOne' method, if no record was replaced
271
- */
272
- public function testReplaceOneNoReplaced ()
253
+ public function testReplaceOne ($ document )
273
254
{
274
- $ id = 'foo_id ' ;
275
- $ document = ['foo ' => 'bar ' ];
276
255
$ options = ['opt1 ' => 'val1 ' ];
277
256
$ filter = ['match_key ' => 'val ' ];
278
257
$ values = (array )$ document ;
@@ -284,11 +263,9 @@ public function testReplaceOneNoReplaced()
284
263
$ collection ->expects ($ this ->once ())->method ('getTypeCaster ' )->willReturn ($ typeCast );
285
264
$ typeCast ->expects ($ this ->once ())->method ('toMongoType ' )->with ($ document , true )->willReturn ($ values );
286
265
$ collection ->expects ($ this ->once ())->method ('parent ' )->with ('replaceOne ' , $ filter , $ values , $ options )->willReturn ($ queryResult );
287
- $ queryResult ->expects ($ this ->once ())->method ('getUpsertedId ' )->willReturn (null );
288
266
289
267
$ result = $ collection ->replaceOne ($ filter , $ document , $ options );
290
268
$ this ->assertSame ($ queryResult , $ result );
291
- $ this ->assertEquals (['foo ' => 'bar ' ], $ document );
292
269
}
293
270
294
271
/**
0 commit comments