AggregateTest.php 3.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php

namespace MongoDB\Tests\Operation;

use MongoDB\Operation\Aggregate;

class AggregateTest extends TestCase
{
    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @expectedExceptionMessage $pipeline is empty
     */
    public function testConstructorPipelineArgumentMustNotBeEmpty()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
15
        new Aggregate($this->getDatabaseName(), $this->getCollectionName(), []);
16 17 18 19 20 21 22 23
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @expectedExceptionMessage $pipeline is not a list (unexpected index: "1")
     */
    public function testConstructorPipelineArgumentMustBeAList()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
24
        new Aggregate($this->getDatabaseName(), $this->getCollectionName(), [1 => ['$match' => ['x' => 1]]]);
25 26 27
    }

    /**
28
     * @expectedException MongoDB\Exception\InvalidArgumentException
29 30 31 32
     * @dataProvider provideInvalidConstructorOptions
     */
    public function testConstructorOptionTypeChecks(array $options)
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
33
        new Aggregate($this->getDatabaseName(), $this->getCollectionName(), [['$match' => ['x' => 1]]], $options);
34 35 36 37
    }

    public function provideInvalidConstructorOptions()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
38
        $options = [];
39 40

        foreach ($this->getInvalidBooleanValues() as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
41
            $options[][] = ['allowDiskUse' => $value];
42 43 44
        }

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

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

52
        foreach ($this->getInvalidIntegerValues() as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
53
            $options[][] = ['maxTimeMS' => $value];
54 55
        }

56 57 58 59
        foreach ($this->getInvalidReadConcernValues() as $value) {
            $options[][] = ['readConcern' => $value];
        }

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

64 65 66 67
        foreach ($this->getInvalidArrayValues() as $value) {
            $options[][] = ['typeMap' => $value];
        }

68
        foreach ($this->getInvalidBooleanValues() as $value) {
Jeremy Mikola's avatar
Jeremy Mikola committed
69
            $options[][] = ['useCursor' => $value];
70 71 72 73 74 75 76 77 78 79 80 81 82 83
        }

        return $options;
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @expectedExceptionMessage "batchSize" option should not be used if "useCursor" is false
     */
    public function testConstructorBatchSizeOptionRequiresUseCursor()
    {
        new Aggregate(
            $this->getDatabaseName(),
            $this->getCollectionName(),
Jeremy Mikola's avatar
Jeremy Mikola committed
84 85
            [['$match' => ['x' => 1]]],
            ['batchSize' => 100, 'useCursor' => false]
86 87
        );
    }
88 89 90 91 92 93 94 95 96 97 98 99 100 101

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @expectedExceptionMessage "typeMap" option should not be used if "useCursor" is false
     */
    public function testConstructorTypeMapOptionRequiresUseCursor()
    {
        new Aggregate(
            $this->getDatabaseName(),
            $this->getCollectionName(),
            [['$match' => ['x' => 1]]],
            ['typeMap' => ['root' => 'array'], 'useCursor' => false]
        );
    }
102
}