MongoUpdateBatchTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
  3. use Alcaeus\MongoDbAdapter\Tests\TestCase;
  4. class MongoUpdateBatchTest extends TestCase
  5. {
  6. public function testSerialize()
  7. {
  8. $batch = new \MongoUpdateBatch($this->getCollection());
  9. $this->assertIsString(serialize($batch));
  10. }
  11. public function testUpdateOne()
  12. {
  13. $collection = $this->getCollection();
  14. $batch = new \MongoUpdateBatch($collection);
  15. $document = ['foo' => 'bar'];
  16. $collection->insert($document);
  17. $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'u' => ['$set' => ['foo' => 'foo']]]));
  18. $expected = [
  19. 'nMatched' => 1,
  20. 'nModified' => 1,
  21. 'nUpserted' => 0,
  22. 'ok' => true,
  23. ];
  24. $this->assertSame($expected, $batch->execute());
  25. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  26. $this->assertSame(1, $newCollection->count());
  27. $record = $newCollection->findOne();
  28. $this->assertNotNull($record);
  29. $this->assertSame('foo', $record->foo);
  30. }
  31. public function testUpdateOneException()
  32. {
  33. $collection = $this->getCollection();
  34. $batch = new \MongoUpdateBatch($collection);
  35. $document = ['foo' => 'bar'];
  36. $collection->insert($document);
  37. $document = ['foo' => 'foo'];
  38. $collection->insert($document);
  39. $collection->createIndex(['foo' => 1], ['unique' => true]);
  40. $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'u' => ['$set' => ['foo' => 'foo']]]));
  41. $expected = [
  42. 'writeErrors' => [
  43. [
  44. 'index' => 0,
  45. 'code' => 11000,
  46. ]
  47. ],
  48. 'nMatched' => 0,
  49. 'nModified' => 0,
  50. 'nUpserted' => 0,
  51. 'ok' => true,
  52. ];
  53. try {
  54. $batch->execute();
  55. $this->fail('Expected MongoWriteConcernException');
  56. } catch (\MongoWriteConcernException $e) {
  57. $this->assertSame('Failed write', $e->getMessage());
  58. $this->assertSame(911, $e->getCode());
  59. $this->assertMatches($expected, $e->getDocument());
  60. }
  61. }
  62. public function testUpdateMany()
  63. {
  64. $collection = $this->getCollection();
  65. $batch = new \MongoUpdateBatch($collection);
  66. $document = ['foo' => 'bar'];
  67. $collection->insert($document);
  68. unset($document['_id']);
  69. $collection->insert($document);
  70. $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'u' => ['$set' => ['foo' => 'foo']], 'multi' => true]));
  71. $expected = [
  72. 'nMatched' => 2,
  73. 'nModified' => 2,
  74. 'nUpserted' => 0,
  75. 'ok' => true,
  76. ];
  77. $this->assertSame($expected, $batch->execute());
  78. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  79. $this->assertSame(2, $newCollection->count());
  80. $record = $newCollection->findOne();
  81. $this->assertNotNull($record);
  82. $this->assertSame('foo', $record->foo);
  83. }
  84. public function testUpdateManyWithoutAck()
  85. {
  86. $collection = $this->getCollection();
  87. $batch = new \MongoUpdateBatch($collection);
  88. $document = ['foo' => 'bar'];
  89. $collection->insert($document);
  90. unset($document['_id']);
  91. $collection->insert($document);
  92. $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'u' => ['$set' => ['foo' => 'foo']], 'multi' => true]));
  93. $expected = [
  94. 'nMatched' => 0,
  95. 'nModified' => 0,
  96. 'nUpserted' => 0,
  97. 'ok' => true,
  98. ];
  99. $this->assertSame($expected, $batch->execute(['w' => 0]));
  100. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  101. $this->assertSame(2, $newCollection->count());
  102. $record = $newCollection->findOne();
  103. $this->assertNotNull($record);
  104. $this->assertSame('foo', $record->foo);
  105. }
  106. public function testUpdateManyException()
  107. {
  108. $collection = $this->getCollection();
  109. $batch = new \MongoUpdateBatch($collection);
  110. $document = ['foo' => 'bar', 'bar' => 'bar'];
  111. $collection->insert($document);
  112. $document = ['foo' => 'foobar', 'bar' => 'bar'];
  113. $collection->insert($document);
  114. $collection->createIndex(['foo' => 1], ['unique' => true]);
  115. $batch->add(['q' => ['bar' => 'bar'], 'u' => ['$set' => ['foo' => 'foo']], 'multi' => true]);
  116. $expected = [
  117. 'writeErrors' => [
  118. [
  119. 'index' => 0,
  120. 'code' => 11000,
  121. ]
  122. ],
  123. 'nMatched' => 0,
  124. 'nModified' => 0,
  125. 'nUpserted' => 0,
  126. 'ok' => true,
  127. ];
  128. try {
  129. $batch->execute();
  130. $this->fail('Expected MongoWriteConcernException');
  131. } catch (\MongoWriteConcernException $e) {
  132. $this->assertSame('Failed write', $e->getMessage());
  133. $this->assertSame(911, $e->getCode());
  134. $this->assertMatches($expected, $e->getDocument());
  135. }
  136. }
  137. public function testUpsert()
  138. {
  139. $document = ['foo' => 'foo'];
  140. $this->getCollection()->insert($document);
  141. $batch = new \MongoUpdateBatch($this->getCollection());
  142. $this->assertTrue($batch->add(['q' => ['foo' => 'foo'], 'u' => ['$set' => ['foo' => 'bar']], 'upsert' => true]));
  143. $this->assertTrue($batch->add(['q' => ['bar' => 'foo'], 'u' => ['$set' => ['foo' => 'bar']], 'upsert' => true]));
  144. $expected = [
  145. 'upserted' => [
  146. [
  147. 'index' => 1,
  148. ]
  149. ],
  150. 'nMatched' => 1,
  151. 'nModified' => 1,
  152. 'nUpserted' => 1,
  153. 'ok' => true,
  154. ];
  155. $result = $batch->execute();
  156. $this->assertMatches($expected, $result);
  157. $this->assertInstanceOf('MongoId', $result['upserted'][0]['_id']);
  158. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  159. $this->assertSame(0, $newCollection->count(['foo' => 'foo']));
  160. $this->assertSame(2, $newCollection->count());
  161. $record = $newCollection->findOne();
  162. $this->assertNotNull($record);
  163. $this->assertSame('bar', $record->foo);
  164. }
  165. public function testValidateItem()
  166. {
  167. $collection = $this->getCollection();
  168. $batch = new \MongoUpdateBatch($collection);
  169. $this->expectException(\Exception::class);
  170. $this->expectExceptionMessage("Expected \$item to contain 'q' key");
  171. $batch->add([]);
  172. }
  173. }