FindTest.php 4.94 KB
Newer Older
1 2 3 4
<?php

namespace MongoDB\Tests\Operation;

5
use MongoDB\Exception\InvalidArgumentException;
6 7 8 9 10 11 12 13 14
use MongoDB\Operation\Find;

class FindTest extends TestCase
{
    /**
     * @dataProvider provideInvalidDocumentValues
     */
    public function testConstructorFilterArgumentTypeCheck($filter)
    {
15
        $this->expectException(InvalidArgumentException::class);
16 17 18 19 20 21 22 23
        new Find($this->getDatabaseName(), $this->getCollectionName(), $filter);
    }

    /**
     * @dataProvider provideInvalidConstructorOptions
     */
    public function testConstructorOptionTypeChecks(array $options)
    {
24
        $this->expectException(InvalidArgumentException::class);
Jeremy Mikola's avatar
Jeremy Mikola committed
25
        new Find($this->getDatabaseName(), $this->getCollectionName(), [], $options);
26 27 28 29
    }

    public function provideInvalidConstructorOptions()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
30
        $options = [];
31 32

        foreach ($this->getInvalidBooleanValues() as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
33
            $options[][] = ['allowPartialResults' => $value];
34 35 36
        }

        foreach ($this->getInvalidIntegerValues() as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
37
            $options[][] = ['batchSize' => $value];
38 39
        }

40 41 42 43
        foreach ($this->getInvalidDocumentValues() as $value) {
            $options[][] = ['collation' => $value];
        }

44
        foreach ($this->getInvalidStringValues() as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
45
            $options[][] = ['comment' => $value];
46 47 48
        }

        foreach ($this->getInvalidIntegerValues() as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
49
            $options[][] = ['cursorType' => $value];
50 51
        }

52 53 54 55
        foreach ($this->getInvalidHintValues() as $value) {
            $options[][] = ['hint' => $value];
        }

56
        foreach ($this->getInvalidIntegerValues() as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
57
            $options[][] = ['limit' => $value];
58 59
        }

60 61 62 63
        foreach ($this->getInvalidDocumentValues() as $value) {
            $options[][] = ['max' => $value];
        }

Katherine Walker's avatar
Katherine Walker committed
64
        foreach ($this->getInvalidIntegerValues() as $value) {
65 66 67
            $options[][] = ['maxAwaitTimeMS' => $value];
        }

68 69 70 71
        foreach ($this->getInvalidIntegerValues() as $value) {
            $options[][] = ['maxScan' => $value];
        }

72
        foreach ($this->getInvalidIntegerValues() as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
73
            $options[][] = ['maxTimeMS' => $value];
74 75
        }

76 77 78 79
        foreach ($this->getInvalidDocumentValues() as $value) {
            $options[][] = ['min' => $value];
        }

80
        foreach ($this->getInvalidDocumentValues() as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
81
            $options[][] = ['modifiers' => $value];
82 83 84
        }

        foreach ($this->getInvalidBooleanValues() as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
85
            $options[][] = ['oplogReplay' => $value];
86 87 88
        }

        foreach ($this->getInvalidDocumentValues() as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
89
            $options[][] = ['projection' => $value];
90 91
        }

92 93 94 95
        foreach ($this->getInvalidReadConcernValues() as $value) {
            $options[][] = ['readConcern' => $value];
        }

96
        foreach ($this->getInvalidReadPreferenceValues() as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
97
            $options[][] = ['readPreference' => $value];
98 99
        }

100 101 102 103
        foreach ($this->getInvalidBooleanValues() as $value) {
            $options[][] = ['returnKey' => $value];
        }

104 105 106 107
        foreach ($this->getInvalidSessionValues() as $value) {
            $options[][] = ['session' => $value];
        }

108 109 110 111
        foreach ($this->getInvalidBooleanValues() as $value) {
            $options[][] = ['showRecordId' => $value];
        }

112
        foreach ($this->getInvalidIntegerValues() as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
113
            $options[][] = ['skip' => $value];
114 115
        }

116 117 118 119
        foreach ($this->getInvalidBooleanValues() as $value) {
            $options[][] = ['snapshot' => $value];
        }

120
        foreach ($this->getInvalidDocumentValues() as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
121
            $options[][] = ['sort' => $value];
122 123
        }

124 125 126 127
        foreach ($this->getInvalidArrayValues() as $value) {
            $options[][] = ['typeMap' => $value];
        }

128 129 130
        return $options;
    }

131 132
    public function testSnapshotOptionIsDeprecated()
    {
133
        $this->assertDeprecated(function () {
134 135 136
            new Find($this->getDatabaseName(), $this->getCollectionName(), [], ['snapshot' => true]);
        });

137
        $this->assertDeprecated(function () {
138 139 140 141
            new Find($this->getDatabaseName(), $this->getCollectionName(), [], ['snapshot' => false]);
        });
    }

142 143
    public function testMaxScanOptionIsDeprecated()
    {
144
        $this->assertDeprecated(function () {
145 146 147 148
            new Find($this->getDatabaseName(), $this->getCollectionName(), [], ['maxScan' => 1]);
        });
    }

149 150 151 152 153
    private function getInvalidHintValues()
    {
        return [123, 3.14, true];
    }

154 155 156 157 158
    /**
     * @dataProvider provideInvalidConstructorCursorTypeOptions
     */
    public function testConstructorCursorTypeOption($cursorType)
    {
159
        $this->expectException(InvalidArgumentException::class);
Jeremy Mikola's avatar
Jeremy Mikola committed
160
        new Find($this->getDatabaseName(), $this->getCollectionName(), [], ['cursorType' => $cursorType]);
161 162 163 164
    }

    public function provideInvalidConstructorCursorTypeOptions()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
165
        return $this->wrapValuesForDataProvider([-1, 0, 4]);
166 167
    }
}