InsertManyTest.php 2.13 KB
Newer Older
1 2 3 4
<?php

namespace MongoDB\Tests\Operation;

5
use MongoDB\Exception\InvalidArgumentException;
6 7 8 9 10 11
use MongoDB\Operation\InsertMany;

class InsertManyTest extends TestCase
{
    public function testConstructorDocumentsMustNotBeEmpty()
    {
12 13
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('$documents is empty');
Jeremy Mikola's avatar
Jeremy Mikola committed
14
        new InsertMany($this->getDatabaseName(), $this->getCollectionName(), []);
15 16 17 18
    }

    public function testConstructorDocumentsMustBeAList()
    {
19 20
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('$documents is not a list (unexpected index: "1")');
Jeremy Mikola's avatar
Jeremy Mikola committed
21
        new InsertMany($this->getDatabaseName(), $this->getCollectionName(), [1 => ['x' => 1]]);
22 23 24
    }

    /**
25
     * @dataProvider provideInvalidDocumentValues
26
     */
27
    public function testConstructorDocumentsArgumentElementTypeChecks($document)
28
    {
29 30
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessageRegExp('/Expected \$documents[0\] to have type "array or object" but found "[\w ]+"/');
Jeremy Mikola's avatar
Jeremy Mikola committed
31
        new InsertMany($this->getDatabaseName(), $this->getCollectionName(), [$document]);
32 33 34
    }

    /**
35
     * @dataProvider provideInvalidConstructorOptions
36
     */
37
    public function testConstructorOptionTypeChecks(array $options)
38
    {
39
        $this->expectException(InvalidArgumentException::class);
Jeremy Mikola's avatar
Jeremy Mikola committed
40
        new InsertMany($this->getDatabaseName(), $this->getCollectionName(), [['x' => 1]], $options);
41 42
    }

43
    public function provideInvalidConstructorOptions()
44
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
45
        $options = [];
46

47 48 49 50
        foreach ($this->getInvalidBooleanValues() as $value) {
            $options[][] = ['bypassDocumentValidation' => $value];
        }

51
        foreach ($this->getInvalidBooleanValues(true) as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
52
            $options[][] = ['ordered' => $value];
53 54
        }

55 56 57 58
        foreach ($this->getInvalidSessionValues() as $value) {
            $options[][] = ['session' => $value];
        }

59
        foreach ($this->getInvalidWriteConcernValues() as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
60
            $options[][] = ['writeConcern' => $value];
61 62 63
        }

        return $options;
64 65
    }
}