MongoUpdateBatchTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 testUpdateMany()
  33. {
  34. $collection = $this->getCollection();
  35. $batch = new \MongoUpdateBatch($collection);
  36. $document = ['foo' => 'bar'];
  37. $collection->insert($document);
  38. unset($document['_id']);
  39. $collection->insert($document);
  40. $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'u' => ['$set' => ['foo' => 'foo']], 'multi' => true]));
  41. $expected = [
  42. 'nMatched' => 2,
  43. 'nModified' => 2,
  44. 'nUpserted' => 0,
  45. 'ok' => true,
  46. ];
  47. $this->assertSame($expected, $batch->execute());
  48. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  49. $this->assertSame(2, $newCollection->count());
  50. $record = $newCollection->findOne();
  51. $this->assertNotNull($record);
  52. $this->assertObjectHasAttribute('foo', $record);
  53. $this->assertAttributeSame('foo', 'foo', $record);
  54. }
  55. public function testUpsert()
  56. {
  57. $document = ['foo' => 'foo'];
  58. $this->getCollection()->insert($document);
  59. $batch = new \MongoUpdateBatch($this->getCollection());
  60. $this->assertTrue($batch->add(['q' => ['foo' => 'foo'], 'u' => ['$set' => ['foo' => 'bar']], 'upsert' => true]));
  61. $this->assertTrue($batch->add(['q' => ['bar' => 'foo'], 'u' => ['$set' => ['foo' => 'bar']], 'upsert' => true]));
  62. $expected = [
  63. 'upserted' => [
  64. [
  65. 'index' => 1,
  66. ]
  67. ],
  68. 'nMatched' => 1,
  69. 'nModified' => 1,
  70. 'nUpserted' => 1,
  71. 'ok' => true,
  72. ];
  73. $result = $batch->execute();
  74. $this->assertArraySubset($expected, $result);
  75. $this->assertInstanceOf('MongoId', $result['upserted'][0]['_id']);
  76. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  77. $this->assertSame(0, $newCollection->count(['foo' => 'foo']));
  78. $this->assertSame(2, $newCollection->count());
  79. $record = $newCollection->findOne();
  80. $this->assertNotNull($record);
  81. $this->assertObjectHasAttribute('foo', $record);
  82. $this->assertAttributeSame('bar', 'foo', $record);
  83. }
  84. public function testValidateItem()
  85. {
  86. $collection = $this->getCollection();
  87. $batch = new \MongoUpdateBatch($collection);
  88. $this->setExpectedException('Exception', "Expected \$item to contain 'q' key");
  89. $batch->add([]);
  90. }
  91. }