FindAndModifyFunctionalTest.php 6.21 KB
Newer Older
1 2 3 4 5
<?php

namespace MongoDB\Tests\Operation;

use MongoDB\Driver\BulkWrite;
6 7
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference;
8 9
use MongoDB\Model\BSONDocument;
use MongoDB\Operation\FindAndModify;
10
use MongoDB\Tests\CommandObserver;
11
use function version_compare;
12 13 14

class FindAndModifyFunctionalTest extends FunctionalTestCase
{
15 16 17 18 19
    /**
     * @see https://jira.mongodb.org/browse/PHPLIB-344
     */
    public function testManagerReadConcernIsOmitted()
    {
20
        $manager = new Manager(static::getUri(), ['readConcernLevel' => 'majority']);
21 22
        $server = $manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));

23 24
        (new CommandObserver())->observe(
            function () use ($server) {
25 26 27 28 29 30 31 32
                $operation = new FindAndModify(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    ['remove' => true]
                );

                $operation->execute($server);
            },
33
            function (array $event) {
34
                $this->assertObjectNotHasAttribute('readConcern', $event['started']->getCommand());
35 36 37 38
            }
        );
    }

39 40
    public function testDefaultWriteConcernIsOmitted()
    {
41 42
        (new CommandObserver())->observe(
            function () {
43 44 45 46 47 48 49 50
                $operation = new FindAndModify(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    ['remove' => true, 'writeConcern' => $this->createDefaultWriteConcern()]
                );

                $operation->execute($this->getPrimaryServer());
            },
51
            function (array $event) {
52
                $this->assertObjectNotHasAttribute('writeConcern', $event['started']->getCommand());
53 54 55 56
            }
        );
    }

57 58 59 60 61 62
    public function testSessionOption()
    {
        if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
            $this->markTestSkipped('Sessions are not supported');
        }

63 64
        (new CommandObserver())->observe(
            function () {
65 66 67 68 69 70 71 72
                $operation = new FindAndModify(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    ['remove' => true, 'session' => $this->createSession()]
                );

                $operation->execute($this->getPrimaryServer());
            },
73
            function (array $event) {
74
                $this->assertObjectHasAttribute('lsid', $event['started']->getCommand());
75 76 77 78
            }
        );
    }

79 80
    public function testBypassDocumentValidationSetWhenTrue()
    {
81 82 83 84
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
            $this->markTestSkipped('bypassDocumentValidation is not supported');
        }

85 86
        (new CommandObserver())->observe(
            function () {
87 88 89 90 91 92 93 94
                $operation = new FindAndModify(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    ['remove' => true, 'bypassDocumentValidation' => true]
                );

                $operation->execute($this->getPrimaryServer());
            },
95
            function (array $event) {
96 97 98 99 100 101 102 103
                $this->assertObjectHasAttribute('bypassDocumentValidation', $event['started']->getCommand());
                $this->assertEquals(true, $event['started']->getCommand()->bypassDocumentValidation);
            }
        );
    }

    public function testBypassDocumentValidationUnsetWhenFalse()
    {
104 105 106 107
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
            $this->markTestSkipped('bypassDocumentValidation is not supported');
        }

108 109
        (new CommandObserver())->observe(
            function () {
110 111 112 113 114 115 116 117
                $operation = new FindAndModify(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    ['remove' => true, 'bypassDocumentValidation' => false]
                );

                $operation->execute($this->getPrimaryServer());
            },
118
            function (array $event) {
119 120 121 122 123
                $this->assertObjectNotHasAttribute('bypassDocumentValidation', $event['started']->getCommand());
            }
        );
    }

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    /**
     * @dataProvider provideTypeMapOptionsAndExpectedDocument
     */
    public function testTypeMapOption(array $typeMap = null, $expectedDocument)
    {
        $this->createFixtures(1);

        $operation = new FindAndModify(
            $this->getDatabaseName(),
            $this->getCollectionName(),
            [
                'remove' => true,
                'typeMap' => $typeMap,
            ]
        );
        $document = $operation->execute($this->getPrimaryServer());

        $this->assertEquals($expectedDocument, $document);
    }

    public function provideTypeMapOptionsAndExpectedDocument()
    {
        return [
            [
                null,
                (object) ['_id' => 1, 'x' => (object) ['foo' => 'bar']],
            ],
            [
                ['root' => 'array', 'document' => 'array'],
                ['_id' => 1, 'x' => ['foo' => 'bar']],
            ],
            [
                ['root' => 'object', 'document' => 'array'],
                (object) ['_id' => 1, 'x' => ['foo' => 'bar']],
            ],
            [
                ['root' => 'array', 'document' => 'stdClass'],
                ['_id' => 1, 'x' => (object) ['foo' => 'bar']],
            ],
            [
164
                ['root' => BSONDocument::class, 'document' => 'object'],
165 166
                new BSONDocument(['_id' => 1, 'x' => (object) ['foo' => 'bar']]),
            ],
167 168 169 170
            [
                ['root' => 'array', 'document' => 'stdClass', 'fieldPaths' => ['x' => 'array']],
                ['_id' => 1, 'x' => ['foo' => 'bar']],
            ],
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
        ];
    }

    /**
     * Create data fixtures.
     *
     * @param integer $n
     */
    private function createFixtures($n)
    {
        $bulkWrite = new BulkWrite(['ordered' => true]);

        for ($i = 1; $i <= $n; $i++) {
            $bulkWrite->insert([
                '_id' => $i,
                'x' => (object) ['foo' => 'bar'],
            ]);
        }

        $result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);

        $this->assertEquals($n, $result->getInsertedCount());
    }
}