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

namespace MongoDB\Tests\Collection\CrudSpec;

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

/**
 * 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
24 25 26 27 28 29
        $filter = ['_id' => ['$gt' => 1]];
        $update = ['$inc' => ['x' => 1]];
        $options = [
            'projection' => ['x' => 1, '_id' => 0],
            'sort' => ['x' => 1],
        ];
30 31

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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