ClientTest.php 6.58 KB
Newer Older
1 2 3 4 5
<?php

namespace MongoDB\Tests;

use MongoDB\Client;
6
use MongoDB\Driver\ReadConcern;
7 8
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
9 10 11 12 13 14

/**
 * Unit tests for the Client class.
 */
class ClientTest extends TestCase
{
15 16 17 18
    public function testConstructorDefaultUri()
    {
        $client = new Client();

19
        $this->assertEquals('mongodb://127.0.0.1/', (string) $client);
20 21
    }

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @dataProvider provideInvalidConstructorDriverOptions
     */
    public function testConstructorDriverOptionTypeChecks(array $driverOptions)
    {
        new Client($this->getUri(), [], $driverOptions);
    }

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

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

        return $options;
    }

42 43 44 45 46 47
    public function testToString()
    {
        $client = new Client($this->getUri());

        $this->assertSame($this->getUri(), (string) $client);
    }
48

49
    public function testSelectCollectionInheritsOptions()
50
    {
51 52 53 54
        $this->markTestSkipped('Depends on https://jira.mongodb.org/browse/PHPC-523');

        $uriOptions = [
            'readConcernLevel' => ReadConcern::LOCAL,
55 56 57 58
            'readPreference' => 'secondaryPreferred',
            'w' => WriteConcern::MAJORITY,
        ];

59 60 61 62 63
        $driverOptions = [
            'typeMap' => ['root' => 'array'],
        ];

        $client = new Client($this->getUri(), $uriOptions, $driverOptions);
64 65 66
        $collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
        $debug = $collection->__debugInfo();

67 68
        $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
69 70
        $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
71 72
        $this->assertInternalType('array', $debug['typeMap']);
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
73 74 75 76
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

77
    public function testSelectCollectionPassesOptions()
78 79
    {
        $collectionOptions = [
80
            'readConcern' => new ReadConcern(ReadConcern::LOCAL),
81
            'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
82
            'typeMap' => ['root' => 'array'],
83 84 85 86 87 88 89
            'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
        ];

        $client = new Client($this->getUri());
        $collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName(), $collectionOptions);
        $debug = $collection->__debugInfo();

90 91
        $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
92 93
        $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
94 95
        $this->assertInternalType('array', $debug['typeMap']);
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
96 97 98 99
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

100 101 102 103 104 105 106 107 108 109 110 111 112
    public function testGetSelectsDatabaseAndInheritsOptions()
    {
        $uriOptions = ['w' => WriteConcern::MAJORITY];

        $client = new Client($this->getUri(), $uriOptions);
        $database = $client->{$this->getDatabaseName()};
        $debug = $database->__debugInfo();

        $this->assertSame($this->getDatabaseName(), $debug['databaseName']);
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

113
    public function testSelectDatabaseInheritsOptions()
114
    {
115 116 117 118
        $this->markTestSkipped('Depends on https://jira.mongodb.org/browse/PHPC-523');

        $uriOptions = [
            'readConcernLevel' => ReadConcern::LOCAL,
119 120 121 122
            'readPreference' => 'secondaryPreferred',
            'w' => WriteConcern::MAJORITY,
        ];

123 124 125 126 127
        $driverOptions = [
            'typeMap' => ['root' => 'array'],
        ];

        $client = new Client($this->getUri(), $uriOptions, $driverOptions);
128 129 130
        $database = $client->selectDatabase($this->getDatabaseName());
        $debug = $database->__debugInfo();

131 132
        $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
133 134
        $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
135 136
        $this->assertInternalType('array', $debug['typeMap']);
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
137 138 139 140
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

141
    public function testSelectDatabasePassesOptions()
142 143
    {
        $databaseOptions = [
144
            'readConcern' => new ReadConcern(ReadConcern::LOCAL),
145
            'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
146
            'typeMap' => ['root' => 'array'],
147 148 149 150 151 152 153
            'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
        ];

        $client = new Client($this->getUri());
        $database = $client->selectDatabase($this->getDatabaseName(), $databaseOptions);
        $debug = $database->__debugInfo();

154 155
        $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
156 157
        $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
158 159
        $this->assertInternalType('array', $debug['typeMap']);
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
160 161 162
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }
163
}