InsertManyFunctionalTest.php 5.29 KB
Newer Older
1 2
<?php

3
namespace MongoDB\Tests\Operation;
4

5
use MongoDB\BSON\ObjectId;
6
use MongoDB\Collection;
7
use MongoDB\Driver\WriteConcern;
8
use MongoDB\Exception\BadMethodCallException;
9
use MongoDB\InsertManyResult;
10
use MongoDB\Model\BSONDocument;
11
use MongoDB\Operation\InsertMany;
12
use MongoDB\Tests\CommandObserver;
13
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
14
use function version_compare;
15 16 17

class InsertManyFunctionalTest extends FunctionalTestCase
{
18 19
    use SetUpTearDownTrait;

20
    /** @var Collection */
21 22
    private $collection;

23
    private function doSetUp()
24 25 26 27 28 29
    {
        parent::setUp();

        $this->collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
    }

30 31 32 33 34
    public function testInsertMany()
    {
        $documents = [
            ['_id' => 'foo', 'x' => 11],
            ['x' => 22],
35 36
            (object) ['_id' => 'bar', 'x' => 33],
            new BSONDocument(['_id' => 'baz', 'x' => 44]),
37 38 39 40 41
        ];

        $operation = new InsertMany($this->getDatabaseName(), $this->getCollectionName(), $documents);
        $result = $operation->execute($this->getPrimaryServer());

42
        $this->assertInstanceOf(InsertManyResult::class, $result);
43
        $this->assertSame(4, $result->getInsertedCount());
44 45 46

        $insertedIds = $result->getInsertedIds();
        $this->assertSame('foo', $insertedIds[0]);
47
        $this->assertInstanceOf(ObjectId::class, $insertedIds[1]);
48
        $this->assertSame('bar', $insertedIds[2]);
49
        $this->assertSame('baz', $insertedIds[3]);
50 51 52 53

        $expected = [
            ['_id' => 'foo', 'x' => 11],
            ['_id' => $insertedIds[1], 'x' => 22],
54 55
            ['_id' => 'bar', 'x' => 33],
            ['_id' => 'baz', 'x' => 44],
56 57 58 59
        ];

        $this->assertSameDocuments($expected, $this->collection->find());
    }
60

61 62 63 64 65 66
    public function testSessionOption()
    {
        if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
            $this->markTestSkipped('Sessions are not supported');
        }

67 68
        (new CommandObserver())->observe(
            function () {
69 70 71 72 73 74 75 76 77
                $operation = new InsertMany(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    [['_id' => 1], ['_id' => 2]],
                    ['session' => $this->createSession()]
                );

                $operation->execute($this->getPrimaryServer());
            },
78
            function (array $event) {
79
                $this->assertObjectHasAttribute('lsid', $event['started']->getCommand());
80 81 82 83
            }
        );
    }

84 85
    public function testBypassDocumentValidationSetWhenTrue()
    {
86 87 88 89
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
            $this->markTestSkipped('bypassDocumentValidation is not supported');
        }

90 91
        (new CommandObserver())->observe(
            function () {
92 93 94 95 96 97 98 99 100
                $operation = new InsertMany(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    [['_id' => 1], ['_id' => 2]],
                    ['bypassDocumentValidation' => true]
                );

                $operation->execute($this->getPrimaryServer());
            },
101
            function (array $event) {
102 103 104 105 106 107 108 109
                $this->assertObjectHasAttribute('bypassDocumentValidation', $event['started']->getCommand());
                $this->assertEquals(true, $event['started']->getCommand()->bypassDocumentValidation);
            }
        );
    }

    public function testBypassDocumentValidationUnsetWhenFalse()
    {
110 111 112 113
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
            $this->markTestSkipped('bypassDocumentValidation is not supported');
        }

114 115
        (new CommandObserver())->observe(
            function () {
116 117 118 119 120 121 122 123 124
                $operation = new InsertMany(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    [['_id' => 1], ['_id' => 2]],
                    ['bypassDocumentValidation' => false]
                );

                $operation->execute($this->getPrimaryServer());
            },
125
            function (array $event) {
126 127 128 129 130
                $this->assertObjectNotHasAttribute('bypassDocumentValidation', $event['started']->getCommand());
            }
        );
    }

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    public function testUnacknowledgedWriteConcern()
    {
        $documents = [['x' => 11]];
        $options = ['writeConcern' => new WriteConcern(0)];

        $operation = new InsertMany($this->getDatabaseName(), $this->getCollectionName(), $documents, $options);
        $result = $operation->execute($this->getPrimaryServer());

        $this->assertFalse($result->isAcknowledged());

        return $result;
    }

    /**
     * @depends testUnacknowledgedWriteConcern
     */
    public function testUnacknowledgedWriteConcernAccessesInsertedCount(InsertManyResult $result)
    {
149 150
        $this->expectException(BadMethodCallException::class);
        $this->expectExceptionMessageRegExp('/[\w:\\\\]+ should not be called for an unacknowledged write result/');
151 152 153 154 155 156 157 158
        $result->getInsertedCount();
    }

    /**
     * @depends testUnacknowledgedWriteConcern
     */
    public function testUnacknowledgedWriteConcernAccessesInsertedId(InsertManyResult $result)
    {
159
        $this->assertInstanceOf(ObjectId::class, $result->getInsertedIds()[0]);
160
    }
161
}