CreateIndexesTest.php 2.06 KB
Newer Older
1 2 3 4
<?php

namespace MongoDB\Tests\Operation;

5
use MongoDB\Exception\InvalidArgumentException;
6 7 8 9
use MongoDB\Operation\CreateIndexes;

class CreateIndexesTest extends TestCase
{
10
    public function testConstructorIndexesArgumentMustBeAList()
11
    {
12 13
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('$indexes is not a list (unexpected index: "1")');
14
        new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), [1 => ['key' => ['x' => 1]]]);
15 16
    }

17 18 19 20 21
    /**
     * @dataProvider provideInvalidConstructorOptions
     */
    public function testConstructorOptionTypeChecks(array $options)
    {
22
        $this->expectException(InvalidArgumentException::class);
23 24 25 26 27 28 29
        new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), [['key' => ['x' => 1]]], $options);
    }

    public function provideInvalidConstructorOptions()
    {
        $options = [];

30 31 32 33
        foreach ($this->getInvalidIntegerValues() as $value) {
            $options[][] = ['maxTimeMS' => $value];
        }

34 35 36 37
        foreach ($this->getInvalidSessionValues() as $value) {
            $options[][] = ['session' => $value];
        }

38 39 40 41 42 43 44
        foreach ($this->getInvalidWriteConcernValues() as $value) {
            $options[][] = ['writeConcern' => $value];
        }

        return $options;
    }

45
    public function testConstructorRequiresAtLeastOneIndex()
46
    {
47 48
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('$indexes is empty');
49
        new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), []);
50 51 52
    }

    /**
53 54
     * @dataProvider provideInvalidIndexSpecificationTypes
     */
55
    public function testConstructorRequiresIndexSpecificationsToBeAnArray($index)
56
    {
57
        $this->expectException(InvalidArgumentException::class);
58 59 60 61 62 63 64 65
        new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), [$index]);
    }

    public function provideInvalidIndexSpecificationTypes()
    {
        return $this->wrapValuesForDataProvider($this->getInvalidArrayValues());
    }
}