MongoUpdateBatchTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. 'ok' => 1.0,
  14. 'nInserted' => 0,
  15. 'nMatched' => 1,
  16. 'nModified' => 1,
  17. 'nUpserted' => 0,
  18. 'nRemoved' => 0,
  19. ];
  20. $this->assertSame($expected, $batch->execute());
  21. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  22. $this->assertSame(1, $newCollection->count());
  23. $record = $newCollection->findOne();
  24. $this->assertNotNull($record);
  25. $this->assertObjectHasAttribute('foo', $record);
  26. $this->assertAttributeSame('foo', 'foo', $record);
  27. }
  28. public function testUpdateMany()
  29. {
  30. $collection = $this->getCollection();
  31. $batch = new \MongoUpdateBatch($collection);
  32. $document = ['foo' => 'bar'];
  33. $collection->insert($document);
  34. unset($document['_id']);
  35. $collection->insert($document);
  36. $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'u' => ['$set' => ['foo' => 'foo']], 'multi' => true]));
  37. $expected = [
  38. 'ok' => 1.0,
  39. 'nInserted' => 0,
  40. 'nMatched' => 2,
  41. 'nModified' => 2,
  42. 'nUpserted' => 0,
  43. 'nRemoved' => 0,
  44. ];
  45. $this->assertSame($expected, $batch->execute());
  46. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  47. $this->assertSame(2, $newCollection->count());
  48. $record = $newCollection->findOne();
  49. $this->assertNotNull($record);
  50. $this->assertObjectHasAttribute('foo', $record);
  51. $this->assertAttributeSame('foo', 'foo', $record);
  52. }
  53. public function testUpsert()
  54. {
  55. $batch = new \MongoUpdateBatch($this->getCollection());
  56. $this->assertTrue($batch->add(['q' => [], 'u' => ['$set' => ['foo' => 'bar']], 'upsert' => true]));
  57. $expected = [
  58. 'ok' => 1.0,
  59. 'nInserted' => 0,
  60. 'nMatched' => 0,
  61. 'nModified' => 0,
  62. 'nUpserted' => 1,
  63. 'nRemoved' => 0,
  64. ];
  65. $this->assertSame($expected, $batch->execute());
  66. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  67. $this->assertSame(1, $newCollection->count());
  68. $record = $newCollection->findOne();
  69. $this->assertNotNull($record);
  70. $this->assertObjectHasAttribute('foo', $record);
  71. $this->assertAttributeSame('bar', 'foo', $record);
  72. }
  73. public function testValidateItem()
  74. {
  75. $collection = $this->getCollection();
  76. $batch = new \MongoUpdateBatch($collection);
  77. $this->setExpectedException('Exception', 'invalid item');
  78. $batch->add([]);
  79. }
  80. }