MongoUpdateBatchTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests;
  3. class MongoUpdateBatchTest extends TestCase
  4. {
  5. public function testUpdateOne()
  6. {
  7. $collection = $this->getCollection();
  8. $batch = new \MongoUpdateBatch($collection);
  9. $document = ['foo' => 'bar'];
  10. $collection->insert($document);
  11. $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'u' => ['$set' => ['foo' => 'foo']]]));
  12. $expected = [
  13. 'nMatched' => 1,
  14. 'nModified' => 1,
  15. 'nUpserted' => 0,
  16. 'ok' => true,
  17. ];
  18. $this->assertSame($expected, $batch->execute());
  19. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  20. $this->assertSame(1, $newCollection->count());
  21. $record = $newCollection->findOne();
  22. $this->assertNotNull($record);
  23. $this->assertObjectHasAttribute('foo', $record);
  24. $this->assertAttributeSame('foo', 'foo', $record);
  25. }
  26. public function testUpdateMany()
  27. {
  28. $collection = $this->getCollection();
  29. $batch = new \MongoUpdateBatch($collection);
  30. $document = ['foo' => 'bar'];
  31. $collection->insert($document);
  32. unset($document['_id']);
  33. $collection->insert($document);
  34. $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'u' => ['$set' => ['foo' => 'foo']], 'multi' => true]));
  35. $expected = [
  36. 'nMatched' => 2,
  37. 'nModified' => 2,
  38. 'nUpserted' => 0,
  39. 'ok' => true,
  40. ];
  41. $this->assertSame($expected, $batch->execute());
  42. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  43. $this->assertSame(2, $newCollection->count());
  44. $record = $newCollection->findOne();
  45. $this->assertNotNull($record);
  46. $this->assertObjectHasAttribute('foo', $record);
  47. $this->assertAttributeSame('foo', 'foo', $record);
  48. }
  49. public function testUpsert()
  50. {
  51. $document = ['foo' => 'foo'];
  52. $this->getCollection()->insert($document);
  53. $batch = new \MongoUpdateBatch($this->getCollection());
  54. $this->assertTrue($batch->add(['q' => ['foo' => 'foo'], 'u' => ['$set' => ['foo' => 'bar']], 'upsert' => true]));
  55. $this->assertTrue($batch->add(['q' => ['bar' => 'foo'], 'u' => ['$set' => ['foo' => 'bar']], 'upsert' => true]));
  56. $expected = [
  57. 'upserted' => [
  58. [
  59. 'index' => 1,
  60. ]
  61. ],
  62. 'nMatched' => 1,
  63. 'nModified' => 1,
  64. 'nUpserted' => 1,
  65. 'ok' => true,
  66. ];
  67. $result = $batch->execute();
  68. $this->assertArraySubset($expected, $result);
  69. $this->assertInstanceOf('MongoId', $result['upserted'][0]['_id']);
  70. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  71. $this->assertSame(0, $newCollection->count(['foo' => 'foo']));
  72. $this->assertSame(2, $newCollection->count());
  73. $record = $newCollection->findOne();
  74. $this->assertNotNull($record);
  75. $this->assertObjectHasAttribute('foo', $record);
  76. $this->assertAttributeSame('bar', 'foo', $record);
  77. }
  78. public function testValidateItem()
  79. {
  80. $collection = $this->getCollection();
  81. $batch = new \MongoUpdateBatch($collection);
  82. $this->setExpectedException('Exception', "Expected \$item to contain 'q' key");
  83. $batch->add([]);
  84. }
  85. }