FindOneAndUpdateFunctionalTest.php 6.01 KB
Newer Older
1 2 3 4
<?php

namespace MongoDB\Tests\Collection\CrudSpec;

5
use MongoDB\Operation\FindOneAndUpdate;
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

/**
 * CRUD spec functional tests for findOneAndUpdate().
 *
 * @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
 */
class FindOneAndUpdateFunctionalTest extends FunctionalTestCase
{
    public function setUp()
    {
        parent::setUp();

        $this->createFixtures(3);
    }

    public function testFindOneAndUpdateWhenManyDocumentsMatchReturningDocumentBeforeModification()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
23 24 25 26 27 28
        $filter = ['_id' => ['$gt' => 1]];
        $update = ['$inc' => ['x' => 1]];
        $options = [
            'projection' => ['x' => 1, '_id' => 0],
            'sort' => ['x' => 1],
        ];
29 30

        $document = $this->collection->findOneAndUpdate($filter, $update, $options);
Jeremy Mikola's avatar
Jeremy Mikola committed
31
        $this->assertSameDocument(['x' => 22], $document);
32

Jeremy Mikola's avatar
Jeremy Mikola committed
33 34 35 36 37
        $expected = [
            ['_id' => 1, 'x' => 11],
            ['_id' => 2, 'x' => 23],
            ['_id' => 3, 'x' => 33],
        ];
38

39
        $this->assertSameDocuments($expected, $this->collection->find());
40 41 42 43
    }

    public function testFindOneAndUpdateWhenManyDocumentsMatchReturningDocumentAfterModification()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
44 45 46 47 48
        $filter = ['_id' => ['$gt' => 1]];
        $update = ['$inc' => ['x' => 1]];
        $options = [
            'projection' => ['x' => 1, '_id' => 0],
            'sort' => ['x' => 1],
49
            'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
Jeremy Mikola's avatar
Jeremy Mikola committed
50
        ];
51 52

        $document = $this->collection->findOneAndUpdate($filter, $update, $options);
Jeremy Mikola's avatar
Jeremy Mikola committed
53
        $this->assertSameDocument(['x' => 23], $document);
54

Jeremy Mikola's avatar
Jeremy Mikola committed
55 56 57 58 59
        $expected = [
            ['_id' => 1, 'x' => 11],
            ['_id' => 2, 'x' => 23],
            ['_id' => 3, 'x' => 33],
        ];
60

61
        $this->assertSameDocuments($expected, $this->collection->find());
62 63 64 65
    }

    public function testFindOneAndUpdateWhenOneDocumentMatchesReturningDocumentBeforeModification()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
66 67 68 69 70 71
        $filter = ['_id' => 2];
        $update = ['$inc' => ['x' => 1]];
        $options = [
            'projection' => ['x' => 1, '_id' => 0],
            'sort' => ['x' => 1],
        ];
72 73

        $document = $this->collection->findOneAndUpdate($filter, $update, $options);
Jeremy Mikola's avatar
Jeremy Mikola committed
74
        $this->assertSameDocument(['x' => 22], $document);
75

Jeremy Mikola's avatar
Jeremy Mikola committed
76 77 78 79 80
        $expected = [
            ['_id' => 1, 'x' => 11],
            ['_id' => 2, 'x' => 23],
            ['_id' => 3, 'x' => 33],
        ];
81

82
        $this->assertSameDocuments($expected, $this->collection->find());
83 84 85 86
    }

    public function testFindOneAndUpdateWhenOneDocumentMatchesReturningDocumentAfterModification()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
87 88 89 90 91
        $filter = ['_id' => 2];
        $update = ['$inc' => ['x' => 1]];
        $options = [
            'projection' => ['x' => 1, '_id' => 0],
            'sort' => ['x' => 1],
92
            'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
Jeremy Mikola's avatar
Jeremy Mikola committed
93
        ];
94 95

        $document = $this->collection->findOneAndUpdate($filter, $update, $options);
Jeremy Mikola's avatar
Jeremy Mikola committed
96
        $this->assertSameDocument(['x' => 23], $document);
97

Jeremy Mikola's avatar
Jeremy Mikola committed
98 99 100 101 102
        $expected = [
            ['_id' => 1, 'x' => 11],
            ['_id' => 2, 'x' => 23],
            ['_id' => 3, 'x' => 33],
        ];
103

104
        $this->assertSameDocuments($expected, $this->collection->find());
105 106 107 108
    }

    public function testFindOneAndUpdateWhenNoDocumentsMatchReturningDocumentBeforeModification()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
109 110 111 112 113 114
        $filter = ['_id' => 4];
        $update = ['$inc' => ['x' => 1]];
        $options = [
            'projection' => ['x' => 1, '_id' => 0],
            'sort' => ['x' => 1],
        ];
115 116 117 118

        $document = $this->collection->findOneAndUpdate($filter, $update, $options);
        $this->assertNull($document);

Jeremy Mikola's avatar
Jeremy Mikola committed
119 120 121 122 123
        $expected = [
            ['_id' => 1, 'x' => 11],
            ['_id' => 2, 'x' => 22],
            ['_id' => 3, 'x' => 33],
        ];
124

125
        $this->assertSameDocuments($expected, $this->collection->find());
126 127 128 129
    }

    public function testFindOneAndUpdateWithUpsertWhenNoDocumentsMatchReturningDocumentBeforeModification()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
130 131 132 133 134
        $filter = ['_id' => 4];
        $update = ['$inc' => ['x' => 1]];
        $options = [
            'projection' => ['x' => 1, '_id' => 0],
            'sort' => ['x' => 1],
135
            'upsert' => true,
Jeremy Mikola's avatar
Jeremy Mikola committed
136
        ];
137 138 139 140

        $document = $this->collection->findOneAndUpdate($filter, $update, $options);
        $this->assertNull($document);

Jeremy Mikola's avatar
Jeremy Mikola committed
141 142 143 144 145 146
        $expected = [
            ['_id' => 1, 'x' => 11],
            ['_id' => 2, 'x' => 22],
            ['_id' => 3, 'x' => 33],
            ['_id' => 4, 'x' => 1],
        ];
147

148
        $this->assertSameDocuments($expected, $this->collection->find());
149 150 151 152
    }

    public function testFindOneAndUpdateWhenNoDocumentsMatchReturningDocumentAfterModification()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
153 154 155 156 157
        $filter = ['_id' => 4];
        $update = ['$inc' => ['x' => 1]];
        $options = [
            'projection' => ['x' => 1, '_id' => 0],
            'sort' => ['x' => 1],
158
            'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
Jeremy Mikola's avatar
Jeremy Mikola committed
159
        ];
160 161 162 163

        $document = $this->collection->findOneAndUpdate($filter, $update, $options);
        $this->assertNull($document);

Jeremy Mikola's avatar
Jeremy Mikola committed
164 165 166 167 168
        $expected = [
            ['_id' => 1, 'x' => 11],
            ['_id' => 2, 'x' => 22],
            ['_id' => 3, 'x' => 33],
        ];
169

170
        $this->assertSameDocuments($expected, $this->collection->find());
171 172 173 174
    }

    public function testFindOneAndUpdateWithUpsertWhenNoDocumentsMatchReturningDocumentAfterModification()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
175 176 177 178 179
        $filter = ['_id' => 4];
        $update = ['$inc' => ['x' => 1]];
        $options = [
            'projection' => ['x' => 1, '_id' => 0],
            'sort' => ['x' => 1],
180
            'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
181
            'upsert' => true,
Jeremy Mikola's avatar
Jeremy Mikola committed
182
        ];
183 184

        $document = $this->collection->findOneAndUpdate($filter, $update, $options);
Jeremy Mikola's avatar
Jeremy Mikola committed
185 186 187 188 189 190 191 192
        $this->assertSameDocument(['x' => 1], $document);

        $expected = [
            ['_id' => 1, 'x' => 11],
            ['_id' => 2, 'x' => 22],
            ['_id' => 3, 'x' => 33],
            ['_id' => 4, 'x' => 1],
        ];
193

194
        $this->assertSameDocuments($expected, $this->collection->find());
195 196
    }
}