AggregateTest.php 3.35 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\Aggregate;

class AggregateTest extends TestCase
{
    public function testConstructorPipelineArgumentMustBeAList()
    {
12 13
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('$pipeline is not a list (unexpected index: "1")');
Jeremy Mikola's avatar
Jeremy Mikola committed
14
        new Aggregate($this->getDatabaseName(), $this->getCollectionName(), [1 => ['$match' => ['x' => 1]]]);
15 16 17 18 19 20 21
    }

    /**
     * @dataProvider provideInvalidConstructorOptions
     */
    public function testConstructorOptionTypeChecks(array $options)
    {
22
        $this->expectException(InvalidArgumentException::class);
Jeremy Mikola's avatar
Jeremy Mikola committed
23
        new Aggregate($this->getDatabaseName(), $this->getCollectionName(), [['$match' => ['x' => 1]]], $options);
24 25 26 27
    }

    public function provideInvalidConstructorOptions()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
28
        $options = [];
29

30
        foreach ($this->getInvalidBooleanValues(true) as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
31
            $options[][] = ['allowDiskUse' => $value];
32 33 34
        }

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

38 39 40 41
        foreach ($this->getInvalidBooleanValues() as $value) {
            $options[][] = ['bypassDocumentValidation' => $value];
        }

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

46 47 48 49
        foreach ($this->getInvalidStringValues() as $value) {
            $options[][] = ['comment' => $value];
        }

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

54 55 56 57
        foreach ($this->getInvalidBooleanValues() as $value) {
            $options[][] = ['explain' => $value];
        }

58 59 60 61
        foreach ($this->getInvalidIntegerValues() as $value) {
            $options[][] = ['maxAwaitTimeMS' => $value];
        }

62
        foreach ($this->getInvalidIntegerValues() as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
63
            $options[][] = ['maxTimeMS' => $value];
64 65
        }

66 67 68 69
        foreach ($this->getInvalidReadConcernValues() as $value) {
            $options[][] = ['readConcern' => $value];
        }

70
        foreach ($this->getInvalidReadPreferenceValues() as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
71
            $options[][] = ['readPreference' => $value];
72 73
        }

74 75 76 77
        foreach ($this->getInvalidSessionValues() as $value) {
            $options[][] = ['session' => $value];
        }

78 79 80 81
        foreach ($this->getInvalidArrayValues() as $value) {
            $options[][] = ['typeMap' => $value];
        }

82
        foreach ($this->getInvalidBooleanValues(true) as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
83
            $options[][] = ['useCursor' => $value];
84 85
        }

86 87 88 89
        foreach ($this->getInvalidWriteConcernValues() as $value) {
            $options[][] = ['writeConcern' => $value];
        }

90 91 92 93 94
        return $options;
    }

    public function testConstructorBatchSizeOptionRequiresUseCursor()
    {
95 96
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('"batchSize" option should not be used if "useCursor" is false');
97 98 99
        new Aggregate(
            $this->getDatabaseName(),
            $this->getCollectionName(),
Jeremy Mikola's avatar
Jeremy Mikola committed
100 101
            [['$match' => ['x' => 1]]],
            ['batchSize' => 100, 'useCursor' => false]
102 103
        );
    }
104

105 106 107 108
    private function getInvalidHintValues()
    {
        return [123, 3.14, true];
    }
109
}