FindOneAndReplaceFunctionalTest.php 6.66 KB
Newer Older
1 2 3 4 5
<?php

namespace MongoDB\Tests\Collection\CrudSpec;

use MongoDB\Collection;
6
use MongoDB\Operation\FindOneAndReplace;
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

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

        $this->createFixtures(3);
    }

    public function testFindOneAndReplaceWhenManyDocumentsMatchReturningDocumentBeforeModification()
    {
        $filter = array('_id' => array('$gt' => 1));
        $replacement = array('x' => 32);
        $options = array(
            'projection' => array('x' => 1, '_id' => 0),
            'sort' => array('x' => 1),
        );

        $document = $this->collection->findOneAndReplace($filter, $replacement, $options);
32
        $this->assertSameDocument(array('x' => 22), $document);
33 34 35 36 37 38 39

        $expected = array(
            array('_id' => 1, 'x' => 11),
            array('_id' => 2, 'x' => 32),
            array('_id' => 3, 'x' => 33),
        );

40
        $this->assertSameDocuments($expected, $this->collection->find());
41 42 43 44 45 46 47 48 49
    }

    public function testFindOneAndReplaceWhenManyDocumentsMatchReturningDocumentAfterModification()
    {
        $filter = array('_id' => array('$gt' => 1));
        $replacement = array('x' => 32);
        $options = array(
            'projection' => array('x' => 1, '_id' => 0),
            'sort' => array('x' => 1),
50
            'returnDocument' => FindOneAndReplace::RETURN_DOCUMENT_AFTER,
51 52 53
        );

        $document = $this->collection->findOneAndReplace($filter, $replacement, $options);
54
        $this->assertSameDocument(array('x' => 32), $document);
55 56 57 58 59 60 61

        $expected = array(
            array('_id' => 1, 'x' => 11),
            array('_id' => 2, 'x' => 32),
            array('_id' => 3, 'x' => 33),
        );

62
        $this->assertSameDocuments($expected, $this->collection->find());
63 64 65 66 67 68 69 70 71 72 73 74
    }

    public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentBeforeModification()
    {
        $filter = array('_id' => 2);
        $replacement = array('x' => 32);
        $options = array(
            'projection' => array('x' => 1, '_id' => 0),
            'sort' => array('x' => 1),
        );

        $document = $this->collection->findOneAndReplace($filter, $replacement, $options);
75
        $this->assertSameDocument(array('x' => 22), $document);
76 77 78 79 80 81 82

        $expected = array(
            array('_id' => 1, 'x' => 11),
            array('_id' => 2, 'x' => 32),
            array('_id' => 3, 'x' => 33),
        );

83
        $this->assertSameDocuments($expected, $this->collection->find());
84 85 86 87 88 89 90 91 92
    }

    public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentAfterModification()
    {
        $filter = array('_id' => 2);
        $replacement = array('x' => 32);
        $options = array(
            'projection' => array('x' => 1, '_id' => 0),
            'sort' => array('x' => 1),
93
            'returnDocument' => FindOneAndReplace::RETURN_DOCUMENT_AFTER,
94 95 96
        );

        $document = $this->collection->findOneAndReplace($filter, $replacement, $options);
97
        $this->assertSameDocument(array('x' => 32), $document);
98 99 100 101 102 103 104

        $expected = array(
            array('_id' => 1, 'x' => 11),
            array('_id' => 2, 'x' => 32),
            array('_id' => 3, 'x' => 33),
        );

105
        $this->assertSameDocuments($expected, $this->collection->find());
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    }

    public function testFindOneAndReplaceWhenNoDocumentsMatchReturningDocumentBeforeModification()
    {
        $filter = array('_id' => 4);
        $replacement = array('x' => 44);
        $options = array(
            'projection' => array('x' => 1, '_id' => 0),
            'sort' => array('x' => 1),
        );

        $document = $this->collection->findOneAndReplace($filter, $replacement, $options);
        $this->assertNull($document);

        $expected = array(
            array('_id' => 1, 'x' => 11),
            array('_id' => 2, 'x' => 22),
            array('_id' => 3, 'x' => 33),
        );

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

    public function testFindOneAndReplaceWithUpsertWhenNoDocumentsMatchReturningDocumentBeforeModification()
    {
        $filter = array('_id' => 4);
132 133
        // Server 2.4 and earlier requires any custom ID to also be in the replacement document
        $replacement = array('_id' => 4, 'x' => 44);
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        $options = array(
            'projection' => array('x' => 1, '_id' => 0),
            'sort' => array('x' => 1),
            'upsert' => true,
        );

        $document = $this->collection->findOneAndReplace($filter, $replacement, $options);
        $this->assertNull($document);

        $expected = array(
            array('_id' => 1, 'x' => 11),
            array('_id' => 2, 'x' => 22),
            array('_id' => 3, 'x' => 33),
            array('_id' => 4, 'x' => 44),
        );

150
        $this->assertSameDocuments($expected, $this->collection->find());
151 152 153 154 155 156 157 158 159
    }

    public function testFindOneAndReplaceWhenNoDocumentsMatchReturningDocumentAfterModification()
    {
        $filter = array('_id' => 4);
        $replacement = array('x' => 44);
        $options = array(
            'projection' => array('x' => 1, '_id' => 0),
            'sort' => array('x' => 1),
160
            'returnDocument' => FindOneAndReplace::RETURN_DOCUMENT_AFTER,
161 162 163 164 165 166 167 168 169 170 171
        );

        $document = $this->collection->findOneAndReplace($filter, $replacement, $options);
        $this->assertNull($document);

        $expected = array(
            array('_id' => 1, 'x' => 11),
            array('_id' => 2, 'x' => 22),
            array('_id' => 3, 'x' => 33),
        );

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

    public function testFindOneAndReplaceWithUpsertWhenNoDocumentsMatchReturningDocumentAfterModification()
    {
        $filter = array('_id' => 4);
178 179
        // Server 2.4 and earlier requires any custom ID to also be in the replacement document
        $replacement = array('_id' => 4, 'x' => 44);
180 181 182
        $options = array(
            'projection' => array('x' => 1, '_id' => 0),
            'sort' => array('x' => 1),
183
            'returnDocument' => FindOneAndReplace::RETURN_DOCUMENT_AFTER,
184 185 186 187
            'upsert' => true,
        );

        $document = $this->collection->findOneAndReplace($filter, $replacement, $options);
188
        $this->assertSameDocument(array('x' => 44), $document);
189 190 191 192 193 194 195 196

        $expected = array(
            array('_id' => 1, 'x' => 11),
            array('_id' => 2, 'x' => 22),
            array('_id' => 3, 'x' => 33),
            array('_id' => 4, 'x' => 44),
        );

197
        $this->assertSameDocuments($expected, $this->collection->find());
198 199
    }
}