|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Tests\Collection; |
| 4 | + |
| 5 | +use MongoDB\Driver\BulkWrite; |
| 6 | +use MongoDB\Operation\Update; |
| 7 | + |
| 8 | +class UpdateFunctionalTest extends FunctionalTestCase |
| 9 | +{ |
| 10 | + private $omitModifiedCount; |
| 11 | + |
| 12 | + public function setUp() |
| 13 | + { |
| 14 | + parent::setUp(); |
| 15 | + |
| 16 | + $this->omitModifiedCount = version_compare($this->getServerVersion(), '2.6.0', '<'); |
| 17 | + } |
| 18 | + |
| 19 | + public function testUpdateOne() |
| 20 | + { |
| 21 | + $this->createFixtures(3); |
| 22 | + |
| 23 | + $filter = ['_id' => ['$gt' => 1]]; |
| 24 | + $update = ['$inc' => ['x' => 1]]; |
| 25 | + |
| 26 | + $operation = new Update($this->getDatabaseName(), $this->getCollectionName(), $filter, $update); |
| 27 | + $result = $operation->execute($this->getPrimaryServer()); |
| 28 | + |
| 29 | + $this->assertInstanceOf('MongoDB\UpdateResult', $result); |
| 30 | + $this->assertSame(1, $result->getMatchedCount()); |
| 31 | + $this->omitModifiedCount or $this->assertSame(1, $result->getModifiedCount()); |
| 32 | + $this->assertSame(0, $result->getUpsertedCount()); |
| 33 | + $this->assertNull($result->getUpsertedId()); |
| 34 | + |
| 35 | + $expected = [ |
| 36 | + ['_id' => 1, 'x' => 11], |
| 37 | + ['_id' => 2, 'x' => 23], |
| 38 | + ['_id' => 3, 'x' => 33], |
| 39 | + ]; |
| 40 | + |
| 41 | + $this->assertSameDocuments($expected, $this->collection->find()); |
| 42 | + } |
| 43 | + |
| 44 | + public function testUpdateMany() |
| 45 | + { |
| 46 | + $this->createFixtures(3); |
| 47 | + |
| 48 | + $filter = ['_id' => ['$gt' => 1]]; |
| 49 | + $update = ['$inc' => ['x' => 1]]; |
| 50 | + $options = ['multi' => true]; |
| 51 | + |
| 52 | + $operation = new Update($this->getDatabaseName(), $this->getCollectionName(), $filter, $update, $options); |
| 53 | + $result = $operation->execute($this->getPrimaryServer()); |
| 54 | + |
| 55 | + $this->assertInstanceOf('MongoDB\UpdateResult', $result); |
| 56 | + $this->assertSame(2, $result->getMatchedCount()); |
| 57 | + $this->omitModifiedCount or $this->assertSame(2, $result->getModifiedCount()); |
| 58 | + $this->assertSame(0, $result->getUpsertedCount()); |
| 59 | + $this->assertNull($result->getUpsertedId()); |
| 60 | + |
| 61 | + $expected = [ |
| 62 | + ['_id' => 1, 'x' => 11], |
| 63 | + ['_id' => 2, 'x' => 23], |
| 64 | + ['_id' => 3, 'x' => 34], |
| 65 | + ]; |
| 66 | + |
| 67 | + $this->assertSameDocuments($expected, $this->collection->find()); |
| 68 | + } |
| 69 | + |
| 70 | + public function testUpdateManyWithExistingId() |
| 71 | + { |
| 72 | + $this->createFixtures(3); |
| 73 | + |
| 74 | + $filter = ['_id' => 5]; |
| 75 | + $update = ['$set' => ['x' => 55]]; |
| 76 | + $options = ['upsert' => true]; |
| 77 | + |
| 78 | + $operation = new Update($this->getDatabaseName(), $this->getCollectionName(), $filter, $update, $options); |
| 79 | + $result = $operation->execute($this->getPrimaryServer()); |
| 80 | + |
| 81 | + $this->assertInstanceOf('MongoDB\UpdateResult', $result); |
| 82 | + $this->assertSame(0, $result->getMatchedCount()); |
| 83 | + $this->omitModifiedCount or $this->assertSame(0, $result->getModifiedCount()); |
| 84 | + $this->assertSame(1, $result->getUpsertedCount()); |
| 85 | + $this->assertSame(5, $result->getUpsertedId()); |
| 86 | + |
| 87 | + $expected = [ |
| 88 | + ['_id' => 1, 'x' => 11], |
| 89 | + ['_id' => 2, 'x' => 22], |
| 90 | + ['_id' => 3, 'x' => 33], |
| 91 | + ['_id' => 5, 'x' => 55], |
| 92 | + ]; |
| 93 | + |
| 94 | + $this->assertSameDocuments($expected, $this->collection->find()); |
| 95 | + } |
| 96 | + |
| 97 | + public function testUpdateManyWithGeneratedId() |
| 98 | + { |
| 99 | + $this->createFixtures(3); |
| 100 | + |
| 101 | + $filter = ['x' => 66]; |
| 102 | + $update = ['$set' => ['x' => 66]]; |
| 103 | + $options = ['upsert' => true]; |
| 104 | + |
| 105 | + $operation = new Update($this->getDatabaseName(), $this->getCollectionName(), $filter, $update, $options); |
| 106 | + $result = $operation->execute($this->getPrimaryServer()); |
| 107 | + |
| 108 | + $this->assertInstanceOf('MongoDB\UpdateResult', $result); |
| 109 | + $this->assertSame(0, $result->getMatchedCount()); |
| 110 | + $this->omitModifiedCount or $this->assertSame(0, $result->getModifiedCount()); |
| 111 | + $this->assertSame(1, $result->getUpsertedCount()); |
| 112 | + $this->assertInstanceOf('MongoDB\BSON\ObjectId', $result->getUpsertedId()); |
| 113 | + |
| 114 | + $expected = [ |
| 115 | + ['_id' => 1, 'x' => 11], |
| 116 | + ['_id' => 2, 'x' => 22], |
| 117 | + ['_id' => 3, 'x' => 33], |
| 118 | + ['_id' => $result->getUpsertedId(), 'x' => 66], |
| 119 | + ]; |
| 120 | + |
| 121 | + $this->assertSameDocuments($expected, $this->collection->find()); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Create data fixtures. |
| 126 | + * |
| 127 | + * @param integer $n |
| 128 | + */ |
| 129 | + private function createFixtures($n) |
| 130 | + { |
| 131 | + $bulkWrite = new BulkWrite(['ordered' => true]); |
| 132 | + |
| 133 | + for ($i = 1; $i <= $n; $i++) { |
| 134 | + $bulkWrite->insert([ |
| 135 | + '_id' => $i, |
| 136 | + 'x' => (integer) ($i . $i), |
| 137 | + ]); |
| 138 | + } |
| 139 | + |
| 140 | + $result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite); |
| 141 | + |
| 142 | + $this->assertEquals($n, $result->getInsertedCount()); |
| 143 | + } |
| 144 | +} |
0 commit comments