1+ <?php
2+
3+ namespace MongoDB \Tests \Collection ;
4+
5+ use MongoDB \Driver \BulkWrite ;
6+
7+ class BulkWriteFunctionalTest extends FunctionalTestCase
8+ {
9+ private $ omitModifiedCount ;
10+
11+ public function setUp ()
12+ {
13+ parent ::setUp ();
14+
15+ $ this ->omitModifiedCount = version_compare ($ this ->getServerVersion (), '2.6.0 ' , '< ' );
16+ }
17+
18+ public function testInserts ()
19+ {
20+ $ ops = array (
21+ array ('insertOne ' => array (array ('_id ' => 1 , 'x ' => 11 ))),
22+ array ('insertOne ' => array (array ('x ' => 22 ))),
23+ );
24+
25+ $ result = $ this ->collection ->bulkWrite ($ ops );
26+ $ this ->assertInstanceOf ('MongoDB\BulkWriteResult ' , $ result );
27+ $ this ->assertSame (2 , $ result ->getInsertedCount ());
28+
29+ $ insertedIds = $ result ->getInsertedIds ();
30+ $ this ->assertSame (1 , $ insertedIds [0 ]);
31+ $ this ->assertInstanceOf ('BSON\ObjectId ' , $ insertedIds [1 ]);
32+
33+ $ expected = array (
34+ array ('_id ' => $ insertedIds [0 ], 'x ' => 11 ),
35+ array ('_id ' => $ insertedIds [1 ], 'x ' => 22 ),
36+ );
37+
38+ $ this ->assertEquals ($ expected , $ this ->collection ->find ()->toArray ());
39+ }
40+
41+ public function testUpdates ()
42+ {
43+ $ this ->createFixtures (4 );
44+
45+ $ ops = array (
46+ array ('updateOne ' => array (array ('_id ' => 2 ), array ('$inc ' => array ('x ' => 1 )))),
47+ array ('updateMany ' => array (array ('_id ' => array ('$gt ' => 2 )), array ('$inc ' => array ('x ' => -1 )))),
48+ array ('updateOne ' => array (array ('_id ' => 5 ), array ('$set ' => array ('x ' => 55 )), array ('upsert ' => true ))),
49+ array ('updateOne ' => array (array ('x ' => 66 ), array ('$set ' => array ('x ' => 66 )), array ('upsert ' => true ))),
50+ array ('updateMany ' => array (array ('x ' => array ('$gt ' => 50 )), array ('$inc ' => array ('x ' => 1 )))),
51+ );
52+
53+ $ result = $ this ->collection ->bulkWrite ($ ops );
54+ $ this ->assertInstanceOf ('MongoDB\BulkWriteResult ' , $ result );
55+ $ this ->assertSame (5 , $ result ->getMatchedCount ());
56+ $ this ->omitModifiedCount or $ this ->assertSame (5 , $ result ->getModifiedCount ());
57+ $ this ->assertSame (2 , $ result ->getUpsertedCount ());
58+
59+ $ upsertedIds = $ result ->getUpsertedIds ();
60+ $ this ->assertSame (5 , $ upsertedIds [2 ]);
61+ $ this ->assertInstanceOf ('BSON\ObjectId ' , $ upsertedIds [3 ]);
62+
63+ $ expected = array (
64+ array ('_id ' => 1 , 'x ' => 11 ),
65+ array ('_id ' => 2 , 'x ' => 23 ),
66+ array ('_id ' => 3 , 'x ' => 32 ),
67+ array ('_id ' => 4 , 'x ' => 43 ),
68+ array ('_id ' => 5 , 'x ' => 56 ),
69+ array ('_id ' => $ upsertedIds [3 ], 'x ' => 67 ),
70+ );
71+
72+ $ this ->assertEquals ($ expected , $ this ->collection ->find ()->toArray ());
73+ }
74+
75+ public function testDeletes ()
76+ {
77+ $ this ->createFixtures (4 );
78+
79+ $ ops = array (
80+ array ('deleteOne ' => array (array ('_id ' => 1 ))),
81+ array ('deleteMany ' => array (array ('_id ' => array ('$gt ' => 2 )))),
82+ );
83+
84+ $ result = $ this ->collection ->bulkWrite ($ ops );
85+ $ this ->assertInstanceOf ('MongoDB\BulkWriteResult ' , $ result );
86+ $ this ->assertSame (3 , $ result ->getDeletedCount ());
87+
88+ $ expected = array (
89+ array ('_id ' => 2 , 'x ' => 22 ),
90+ );
91+
92+ $ this ->assertEquals ($ expected , $ this ->collection ->find ()->toArray ());
93+ }
94+
95+ public function testMixedOrderedOperations ()
96+ {
97+ $ this ->createFixtures (3 );
98+
99+ $ ops = array (
100+ array ('updateOne ' => array (array ('_id ' => array ('$gt ' => 1 )), array ('$inc ' => array ('x ' => 1 )))),
101+ array ('updateMany ' => array (array ('_id ' => array ('$gt ' => 1 )), array ('$inc ' => array ('x ' => 1 )))),
102+ array ('insertOne ' => array (array ('_id ' => 4 , 'x ' => 44 ))),
103+ array ('deleteMany ' => array (array ('x ' => array ('$nin ' => array (24 , 34 ))))),
104+ array ('replaceOne ' => array (array ('_id ' => 4 ), array ('_id ' => 4 , 'x ' => 44 ), array ('upsert ' => true ))),
105+ );
106+
107+ $ result = $ this ->collection ->bulkWrite ($ ops );
108+ $ this ->assertInstanceOf ('MongoDB\BulkWriteResult ' , $ result );
109+
110+ $ this ->assertSame (1 , $ result ->getInsertedCount ());
111+ $ this ->assertSame (array (2 => 4 ), $ result ->getInsertedIds ());
112+
113+ $ this ->assertSame (3 , $ result ->getMatchedCount ());
114+ $ this ->omitModifiedCount or $ this ->assertSame (3 , $ result ->getModifiedCount ());
115+ $ this ->assertSame (1 , $ result ->getUpsertedCount ());
116+ $ this ->assertSame (array (4 => 4 ), $ result ->getUpsertedIds ());
117+
118+ $ this ->assertSame (2 , $ result ->getDeletedCount ());
119+
120+ $ expected = array (
121+ array ('_id ' => 2 , 'x ' => 24 ),
122+ array ('_id ' => 3 , 'x ' => 34 ),
123+ array ('_id ' => 4 , 'x ' => 44 ),
124+ );
125+
126+ $ this ->assertEquals ($ expected , $ this ->collection ->find ()->toArray ());
127+ }
128+
129+ /**
130+ * @expectedException MongoDB\Exception\InvalidArgumentException
131+ * @expectedExceptionMessage Unknown operation type called 'foo' (operation#0)
132+ */
133+ public function testUnknownOperation ()
134+ {
135+ $ this ->collection ->bulkWrite (array (
136+ array ('foo ' => array (array ('_id ' => 1 ))),
137+ ));
138+ }
139+
140+ /**
141+ * @expectedException MongoDB\Exception\InvalidArgumentException
142+ * @expectedExceptionMessageRegExp /Missing argument#\d+ for '\w+' \(operation#\d+\)/
143+ * @dataProvider provideOpsWithMissingArguments
144+ */
145+ public function testMissingArguments (array $ ops )
146+ {
147+ $ this ->collection ->bulkWrite ($ ops );
148+ }
149+
150+ public function provideOpsWithMissingArguments ()
151+ {
152+ return array (
153+ array (array (array ('insertOne ' => array ()))),
154+ array (array (array ('updateOne ' => array ()))),
155+ array (array (array ('updateOne ' => array (array ('_id ' => 1 ))))),
156+ array (array (array ('updateMany ' => array ()))),
157+ array (array (array ('updateMany ' => array (array ('_id ' => 1 ))))),
158+ array (array (array ('replaceOne ' => array ()))),
159+ array (array (array ('replaceOne ' => array (array ('_id ' => 1 ))))),
160+ array (array (array ('deleteOne ' => array ()))),
161+ array (array (array ('deleteMany ' => array ()))),
162+ );
163+ }
164+
165+ /**
166+ * @expectedException MongoDB\Exception\InvalidArgumentException
167+ * @expectedExceptionMessage First key in $update must be a $operator
168+ */
169+ public function testUpdateOneRequiresUpdateOperators ()
170+ {
171+ $ this ->collection ->bulkWrite (array (
172+ array ('updateOne ' => array (array ('_id ' => 1 ), array ('x ' => 1 ))),
173+ ));
174+ }
175+
176+ /**
177+ * @expectedException MongoDB\Exception\InvalidArgumentException
178+ * @expectedExceptionMessage First key in $update must be a $operator
179+ */
180+ public function testUpdateManyRequiresUpdateOperators ()
181+ {
182+ $ this ->collection ->bulkWrite (array (
183+ array ('updateMany ' => array (array ('_id ' => array ('$gt ' => 1 )), array ('x ' => 1 ))),
184+ ));
185+ }
186+
187+ /**
188+ * @expectedException MongoDB\Exception\InvalidArgumentException
189+ * @expectedExceptionMessage First key in $update must NOT be a $operator
190+ */
191+ public function testReplaceOneRequiresReplacementDocument ()
192+ {
193+ $ this ->collection ->bulkWrite (array (
194+ array ('replaceOne ' => array (array ('_id ' => 1 ), array ('$inc ' => array ('x ' => 1 )))),
195+ ));
196+ }
197+
198+ /**
199+ * Create data fixtures.
200+ *
201+ * @param integer $n
202+ */
203+ private function createFixtures ($ n )
204+ {
205+ $ bulkWrite = new BulkWrite (true );
206+
207+ for ($ i = 1 ; $ i <= $ n ; $ i ++) {
208+ $ bulkWrite ->insert (array (
209+ '_id ' => $ i ,
210+ 'x ' => (integer ) ($ i . $ i ),
211+ ));
212+ }
213+
214+ $ result = $ this ->manager ->executeBulkWrite ($ this ->getNamespace (), $ bulkWrite );
215+
216+ $ this ->assertEquals ($ n , $ result ->getInsertedCount ());
217+ }
218+ }
0 commit comments