MongoUpdateBatchTest.php 6.9 KB

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