|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Tests\Collection\CrudSpec; |
| 4 | + |
| 5 | +use MongoDB\Collection; |
| 6 | + |
| 7 | +/** |
| 8 | + * CRUD spec functional tests for findOneAndReplace(). |
| 9 | + * |
| 10 | + * @see https://github.com/mongodb/specifications/tree/master/source/crud/tests |
| 11 | + */ |
| 12 | +class FindOneAndReplaceFunctionalTest extends FunctionalTestCase |
| 13 | +{ |
| 14 | + public function setUp() |
| 15 | + { |
| 16 | + parent::setUp(); |
| 17 | + |
| 18 | + $this->createFixtures(3); |
| 19 | + } |
| 20 | + |
| 21 | + public function testFindOneAndReplaceWhenManyDocumentsMatchReturningDocumentBeforeModification() |
| 22 | + { |
| 23 | + $filter = array('_id' => array('$gt' => 1)); |
| 24 | + $replacement = array('x' => 32); |
| 25 | + $options = array( |
| 26 | + 'projection' => array('x' => 1, '_id' => 0), |
| 27 | + 'sort' => array('x' => 1), |
| 28 | + ); |
| 29 | + |
| 30 | + $document = $this->collection->findOneAndReplace($filter, $replacement, $options); |
| 31 | + $this->assertSame(array('x' => 22), $document); |
| 32 | + |
| 33 | + $expected = array( |
| 34 | + array('_id' => 1, 'x' => 11), |
| 35 | + array('_id' => 2, 'x' => 32), |
| 36 | + array('_id' => 3, 'x' => 33), |
| 37 | + ); |
| 38 | + |
| 39 | + $this->assertSame($expected, $this->collection->find()->toArray()); |
| 40 | + } |
| 41 | + |
| 42 | + public function testFindOneAndReplaceWhenManyDocumentsMatchReturningDocumentAfterModification() |
| 43 | + { |
| 44 | + $filter = array('_id' => array('$gt' => 1)); |
| 45 | + $replacement = array('x' => 32); |
| 46 | + $options = array( |
| 47 | + 'projection' => array('x' => 1, '_id' => 0), |
| 48 | + 'sort' => array('x' => 1), |
| 49 | + 'returnDocument' => Collection::FIND_ONE_AND_RETURN_AFTER, |
| 50 | + ); |
| 51 | + |
| 52 | + $document = $this->collection->findOneAndReplace($filter, $replacement, $options); |
| 53 | + $this->assertSame(array('x' => 32), $document); |
| 54 | + |
| 55 | + $expected = array( |
| 56 | + array('_id' => 1, 'x' => 11), |
| 57 | + array('_id' => 2, 'x' => 32), |
| 58 | + array('_id' => 3, 'x' => 33), |
| 59 | + ); |
| 60 | + |
| 61 | + $this->assertSame($expected, $this->collection->find()->toArray()); |
| 62 | + } |
| 63 | + |
| 64 | + public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentBeforeModification() |
| 65 | + { |
| 66 | + $filter = array('_id' => 2); |
| 67 | + $replacement = array('x' => 32); |
| 68 | + $options = array( |
| 69 | + 'projection' => array('x' => 1, '_id' => 0), |
| 70 | + 'sort' => array('x' => 1), |
| 71 | + ); |
| 72 | + |
| 73 | + $document = $this->collection->findOneAndReplace($filter, $replacement, $options); |
| 74 | + $this->assertSame(array('x' => 22), $document); |
| 75 | + |
| 76 | + $expected = array( |
| 77 | + array('_id' => 1, 'x' => 11), |
| 78 | + array('_id' => 2, 'x' => 32), |
| 79 | + array('_id' => 3, 'x' => 33), |
| 80 | + ); |
| 81 | + |
| 82 | + $this->assertSame($expected, $this->collection->find()->toArray()); |
| 83 | + } |
| 84 | + |
| 85 | + public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentAfterModification() |
| 86 | + { |
| 87 | + $filter = array('_id' => 2); |
| 88 | + $replacement = array('x' => 32); |
| 89 | + $options = array( |
| 90 | + 'projection' => array('x' => 1, '_id' => 0), |
| 91 | + 'sort' => array('x' => 1), |
| 92 | + 'returnDocument' => Collection::FIND_ONE_AND_RETURN_AFTER, |
| 93 | + ); |
| 94 | + |
| 95 | + $document = $this->collection->findOneAndReplace($filter, $replacement, $options); |
| 96 | + $this->assertSame(array('x' => 32), $document); |
| 97 | + |
| 98 | + $expected = array( |
| 99 | + array('_id' => 1, 'x' => 11), |
| 100 | + array('_id' => 2, 'x' => 32), |
| 101 | + array('_id' => 3, 'x' => 33), |
| 102 | + ); |
| 103 | + |
| 104 | + $this->assertSame($expected, $this->collection->find()->toArray()); |
| 105 | + } |
| 106 | + |
| 107 | + public function testFindOneAndReplaceWhenNoDocumentsMatchReturningDocumentBeforeModification() |
| 108 | + { |
| 109 | + $filter = array('_id' => 4); |
| 110 | + $replacement = array('x' => 44); |
| 111 | + $options = array( |
| 112 | + 'projection' => array('x' => 1, '_id' => 0), |
| 113 | + 'sort' => array('x' => 1), |
| 114 | + ); |
| 115 | + |
| 116 | + $document = $this->collection->findOneAndReplace($filter, $replacement, $options); |
| 117 | + $this->assertNull($document); |
| 118 | + |
| 119 | + $expected = array( |
| 120 | + array('_id' => 1, 'x' => 11), |
| 121 | + array('_id' => 2, 'x' => 22), |
| 122 | + array('_id' => 3, 'x' => 33), |
| 123 | + ); |
| 124 | + |
| 125 | + $this->assertSame($expected, $this->collection->find()->toArray()); |
| 126 | + } |
| 127 | + |
| 128 | + public function testFindOneAndReplaceWithUpsertWhenNoDocumentsMatchReturningDocumentBeforeModification() |
| 129 | + { |
| 130 | + $filter = array('_id' => 4); |
| 131 | + $replacement = array('x' => 44); |
| 132 | + $options = array( |
| 133 | + 'projection' => array('x' => 1, '_id' => 0), |
| 134 | + 'sort' => array('x' => 1), |
| 135 | + 'upsert' => true, |
| 136 | + ); |
| 137 | + |
| 138 | + $document = $this->collection->findOneAndReplace($filter, $replacement, $options); |
| 139 | + $this->assertNull($document); |
| 140 | + |
| 141 | + $expected = array( |
| 142 | + array('_id' => 1, 'x' => 11), |
| 143 | + array('_id' => 2, 'x' => 22), |
| 144 | + array('_id' => 3, 'x' => 33), |
| 145 | + array('_id' => 4, 'x' => 44), |
| 146 | + ); |
| 147 | + |
| 148 | + $this->assertSame($expected, $this->collection->find()->toArray()); |
| 149 | + } |
| 150 | + |
| 151 | + public function testFindOneAndReplaceWhenNoDocumentsMatchReturningDocumentAfterModification() |
| 152 | + { |
| 153 | + $filter = array('_id' => 4); |
| 154 | + $replacement = array('x' => 44); |
| 155 | + $options = array( |
| 156 | + 'projection' => array('x' => 1, '_id' => 0), |
| 157 | + 'sort' => array('x' => 1), |
| 158 | + 'returnDocument' => Collection::FIND_ONE_AND_RETURN_AFTER, |
| 159 | + ); |
| 160 | + |
| 161 | + $document = $this->collection->findOneAndReplace($filter, $replacement, $options); |
| 162 | + $this->assertNull($document); |
| 163 | + |
| 164 | + $expected = array( |
| 165 | + array('_id' => 1, 'x' => 11), |
| 166 | + array('_id' => 2, 'x' => 22), |
| 167 | + array('_id' => 3, 'x' => 33), |
| 168 | + ); |
| 169 | + |
| 170 | + $this->assertSame($expected, $this->collection->find()->toArray()); |
| 171 | + } |
| 172 | + |
| 173 | + public function testFindOneAndReplaceWithUpsertWhenNoDocumentsMatchReturningDocumentAfterModification() |
| 174 | + { |
| 175 | + $filter = array('_id' => 4); |
| 176 | + $replacement = array('x' => 44); |
| 177 | + $options = array( |
| 178 | + 'projection' => array('x' => 1, '_id' => 0), |
| 179 | + 'sort' => array('x' => 1), |
| 180 | + 'returnDocument' => Collection::FIND_ONE_AND_RETURN_AFTER, |
| 181 | + 'upsert' => true, |
| 182 | + ); |
| 183 | + |
| 184 | + $document = $this->collection->findOneAndReplace($filter, $replacement, $options); |
| 185 | + $this->assertSame(array('x' => 44), $document); |
| 186 | + |
| 187 | + $expected = array( |
| 188 | + array('_id' => 1, 'x' => 11), |
| 189 | + array('_id' => 2, 'x' => 22), |
| 190 | + array('_id' => 3, 'x' => 33), |
| 191 | + array('_id' => 4, 'x' => 44), |
| 192 | + ); |
| 193 | + |
| 194 | + $this->assertSame($expected, $this->collection->find()->toArray()); |
| 195 | + } |
| 196 | +} |
0 commit comments