/vendor/mongodb/mongodb/tests/Operation/BulkWriteFunctionalTest.php

https://gitlab.com/vnsoftdev/sna · PHP · 308 lines · 200 code · 52 blank · 56 comment · 1 complexity · c4fd36ef03cc14cfc8efd6051865c990 MD5 · raw file

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