MongoUpdateBatchTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. $collection->insert(['foo' => 'bar']);
  10. $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'u' => ['$set' => ['foo' => 'foo']]]));
  11. $expected = [
  12. 'ok' => 1.0,
  13. 'nInserted' => 0,
  14. 'nMatched' => 1,
  15. 'nModified' => 1,
  16. 'nUpserted' => 0,
  17. 'nRemoved' => 0,
  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. $collection->insert(['foo' => 'bar']);
  32. $collection->insert(['foo' => 'bar']);
  33. $this->assertTrue($batch->add(['q' => ['foo' => 'bar'], 'u' => ['$set' => ['foo' => 'foo']], 'multi' => true]));
  34. $expected = [
  35. 'ok' => 1.0,
  36. 'nInserted' => 0,
  37. 'nMatched' => 2,
  38. 'nModified' => 2,
  39. 'nUpserted' => 0,
  40. 'nRemoved' => 0,
  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. $batch = new \MongoUpdateBatch($this->getCollection());
  53. $this->assertTrue($batch->add(['q' => [], 'u' => ['$set' => ['foo' => 'bar']], 'upsert' => true]));
  54. $expected = [
  55. 'ok' => 1.0,
  56. 'nInserted' => 0,
  57. 'nMatched' => 0,
  58. 'nModified' => 0,
  59. 'nUpserted' => 1,
  60. 'nRemoved' => 0,
  61. ];
  62. $this->assertSame($expected, $batch->execute());
  63. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  64. $this->assertSame(1, $newCollection->count());
  65. $record = $newCollection->findOne();
  66. $this->assertNotNull($record);
  67. $this->assertObjectHasAttribute('foo', $record);
  68. $this->assertAttributeSame('bar', 'foo', $record);
  69. }
  70. public function testValidateItem()
  71. {
  72. $collection = $this->getCollection();
  73. $batch = new \MongoUpdateBatch($collection);
  74. $this->setExpectedException('Exception', 'invalid item');
  75. $batch->add([]);
  76. }
  77. }