WatchTest.php 3 KB
Newer Older
1 2 3 4
<?php

namespace MongoDB\Tests\Operation;

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

/**
 * Although these are unit tests, we extend FunctionalTestCase because Watch is
 * constructed with a Manager instance.
 */
class WatchTest extends FunctionalTestCase
{
15 16 17 18 19 20 21 22
    public function testConstructorCollectionNameShouldBeNullIfDatabaseNameIsNull()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('$collectionName should also be null if $databaseName is null');

        new Watch($this->manager, null, 'foo', []);
    }

23 24
    public function testConstructorPipelineArgumentMustBeAList()
    {
25 26 27
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('$pipeline is not a list (unexpected index: "foo")');

28 29 30 31 32 33 34 35 36 37 38
        /* Note: Watch uses array_unshift() to prepend the $changeStream stage
         * to the pipeline. Since array_unshift() reindexes numeric keys, we'll
         * use a string key to test for this exception. */
        new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), ['foo' => ['$match' => ['x' => 1]]]);
    }

    /**
     * @dataProvider provideInvalidConstructorOptions
     */
    public function testConstructorOptionTypeChecks(array $options)
    {
39
        $this->expectException(InvalidArgumentException::class);
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
        new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], $options);
    }

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

        foreach ($this->getInvalidIntegerValues() as $value) {
            $options[][] = ['batchSize' => $value];
        }

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

55
        foreach ($this->getInvalidStringValues(true) as $value) {
56 57 58 59 60 61 62 63 64 65 66
            $options[][] = ['fullDocument' => $value];
        }

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

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

67
        foreach ($this->getInvalidReadPreferenceValues(true) as $value) {
68 69 70 71 72 73 74
            $options[][] = ['readPreference' => $value];
        }

        foreach ($this->getInvalidDocumentValues() as $value) {
            $options[][] = ['resumeAfter' => $value];
        }

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

79 80 81 82
        foreach ($this->getInvalidTimestampValues() as $value) {
            $options[][] = ['startAtOperationTime' => $value];
        }

83 84 85 86
        foreach ($this->getInvalidArrayValues() as $value) {
            $options[][] = ['typeMap' => $value];
        }

87 88
        return $options;
    }
89 90 91

    private function getInvalidTimestampValues()
    {
92
        return [123, 3.14, 'foo', true, [], new stdClass()];
93
    }
94
}