MongoUpdateBatchTest.php 3.4 KB

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