MongoUpdateBatchTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 testUpdateManyException()
  87. {
  88. $collection = $this->getCollection();
  89. $batch = new \MongoUpdateBatch($collection);
  90. $document = ['foo' => 'bar', 'bar' => 'bar'];
  91. $collection->insert($document);
  92. $document = ['foo' => 'foobar', 'bar' => 'bar'];
  93. $collection->insert($document);
  94. $collection->createIndex(['foo' => 1], ['unique' => true]);
  95. $batch->add(['q' => ['bar' => 'bar'], 'u' => ['$set' => ['foo' => 'foo']], 'multi' => true]);
  96. $expected = [
  97. 'writeErrors' => [
  98. [
  99. 'index' => 0,
  100. 'code' => 11000,
  101. ]
  102. ],
  103. 'nMatched' => 0,
  104. 'nModified' => 0,
  105. 'nUpserted' => 0,
  106. 'ok' => true,
  107. ];
  108. try {
  109. $batch->execute();
  110. $this->fail('Expected MongoWriteConcernException');
  111. } catch (\MongoWriteConcernException $e) {
  112. $this->assertSame('Failed write', $e->getMessage());
  113. $this->assertSame(911, $e->getCode());
  114. $this->assertArraySubset($expected, $e->getDocument());
  115. }
  116. }
  117. public function testUpsert()
  118. {
  119. $document = ['foo' => 'foo'];
  120. $this->getCollection()->insert($document);
  121. $batch = new \MongoUpdateBatch($this->getCollection());
  122. $this->assertTrue($batch->add(['q' => ['foo' => 'foo'], 'u' => ['$set' => ['foo' => 'bar']], 'upsert' => true]));
  123. $this->assertTrue($batch->add(['q' => ['bar' => 'foo'], 'u' => ['$set' => ['foo' => 'bar']], 'upsert' => true]));
  124. $expected = [
  125. 'upserted' => [
  126. [
  127. 'index' => 1,
  128. ]
  129. ],
  130. 'nMatched' => 1,
  131. 'nModified' => 1,
  132. 'nUpserted' => 1,
  133. 'ok' => true,
  134. ];
  135. $result = $batch->execute();
  136. $this->assertArraySubset($expected, $result);
  137. $this->assertInstanceOf('MongoId', $result['upserted'][0]['_id']);
  138. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  139. $this->assertSame(0, $newCollection->count(['foo' => 'foo']));
  140. $this->assertSame(2, $newCollection->count());
  141. $record = $newCollection->findOne();
  142. $this->assertNotNull($record);
  143. $this->assertObjectHasAttribute('foo', $record);
  144. $this->assertAttributeSame('bar', 'foo', $record);
  145. }
  146. public function testValidateItem()
  147. {
  148. $collection = $this->getCollection();
  149. $batch = new \MongoUpdateBatch($collection);
  150. $this->setExpectedException('Exception', "Expected \$item to contain 'q' key");
  151. $batch->add([]);
  152. }
  153. }