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

namespace MongoDB\Tests;

use MongoDB\Client;
6
use MongoDB\Driver\ClientEncryption;
7
use MongoDB\Driver\Exception\InvalidArgumentException as DriverInvalidArgumentException;
8
use MongoDB\Driver\ReadConcern;
9 10
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
11
use MongoDB\Exception\InvalidArgumentException;
12 13 14 15 16 17

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

22
        $this->assertEquals('mongodb://127.0.0.1/', (string) $client);
23 24
    }

25 26 27 28 29 30 31 32 33 34 35 36 37 38
    /**
     * @doesNotPerformAssertions
     */
    public function testConstructorAutoEncryptionOpts()
    {
        $autoEncryptionOpts = [
            'keyVaultClient' => new Client(static::getUri()),
            'keyVaultNamespace' => 'default.keys',
            'kmsProviders' => ['aws' => ['accessKeyId' => 'abc', 'secretAccessKey' => 'def']],
        ];

        new Client(static::getUri(), [], ['autoEncryption' => $autoEncryptionOpts]);
    }

39 40 41
    /**
     * @dataProvider provideInvalidConstructorDriverOptions
     */
42
    public function testConstructorDriverOptionTypeChecks(array $driverOptions, string $exception = InvalidArgumentException::class)
43
    {
44
        $this->expectException($exception);
45
        new Client(static::getUri(), [], $driverOptions);
46 47 48 49 50 51
    }

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

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

56 57
        $options[][] = ['autoEncryption' => ['keyVaultClient' => 'foo']];

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        foreach ($this->getInvalidStringValues() as $value) {
            $options[][] = ['driver' => ['name' => $value]];
        }

        foreach ($this->getInvalidStringValues() as $value) {
            $options[][] = ['driver' => ['version' => $value]];
        }

        foreach ($this->getInvalidStringValues() as $value) {
            $options[] = [
                'driverOptions' => ['driver' => ['platform' => $value]],
                'exception' => DriverInvalidArgumentException::class,
            ];
        }

73 74 75
        return $options;
    }

76 77
    public function testToString()
    {
78
        $client = new Client(static::getUri());
79

80
        $this->assertSame(static::getUri(), (string) $client);
81
    }
82

83
    public function testSelectCollectionInheritsOptions()
84
    {
85 86
        $uriOptions = [
            'readConcernLevel' => ReadConcern::LOCAL,
87 88 89 90
            'readPreference' => 'secondaryPreferred',
            'w' => WriteConcern::MAJORITY,
        ];

91 92 93 94
        $driverOptions = [
            'typeMap' => ['root' => 'array'],
        ];

95
        $client = new Client(static::getUri(), $uriOptions, $driverOptions);
96 97 98
        $collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
        $debug = $collection->__debugInfo();

99
        $this->assertInstanceOf(ReadConcern::class, $debug['readConcern']);
100
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
101
        $this->assertInstanceOf(ReadPreference::class, $debug['readPreference']);
102
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
103
        $this->assertIsArray($debug['typeMap']);
104
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
105
        $this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
106 107 108
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

109
    public function testSelectCollectionPassesOptions()
110 111
    {
        $collectionOptions = [
112
            'readConcern' => new ReadConcern(ReadConcern::LOCAL),
113
            'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
114
            'typeMap' => ['root' => 'array'],
115 116 117
            'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
        ];

118
        $client = new Client(static::getUri());
119 120 121
        $collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName(), $collectionOptions);
        $debug = $collection->__debugInfo();

122
        $this->assertInstanceOf(ReadConcern::class, $debug['readConcern']);
123
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
124
        $this->assertInstanceOf(ReadPreference::class, $debug['readPreference']);
125
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
126
        $this->assertIsArray($debug['typeMap']);
127
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
128
        $this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
129 130 131
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

132 133 134 135
    public function testGetSelectsDatabaseAndInheritsOptions()
    {
        $uriOptions = ['w' => WriteConcern::MAJORITY];

136
        $client = new Client(static::getUri(), $uriOptions);
137 138 139 140
        $database = $client->{$this->getDatabaseName()};
        $debug = $database->__debugInfo();

        $this->assertSame($this->getDatabaseName(), $debug['databaseName']);
141
        $this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
142 143 144
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

145
    public function testSelectDatabaseInheritsOptions()
146
    {
147 148
        $uriOptions = [
            'readConcernLevel' => ReadConcern::LOCAL,
149 150 151 152
            'readPreference' => 'secondaryPreferred',
            'w' => WriteConcern::MAJORITY,
        ];

153 154 155 156
        $driverOptions = [
            'typeMap' => ['root' => 'array'],
        ];

157
        $client = new Client(static::getUri(), $uriOptions, $driverOptions);
158 159 160
        $database = $client->selectDatabase($this->getDatabaseName());
        $debug = $database->__debugInfo();

161
        $this->assertInstanceOf(ReadConcern::class, $debug['readConcern']);
162
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
163
        $this->assertInstanceOf(ReadPreference::class, $debug['readPreference']);
164
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
165
        $this->assertIsArray($debug['typeMap']);
166
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
167
        $this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
168 169 170
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

171
    public function testSelectDatabasePassesOptions()
172 173
    {
        $databaseOptions = [
174
            'readConcern' => new ReadConcern(ReadConcern::LOCAL),
175
            'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
176
            'typeMap' => ['root' => 'array'],
177 178 179
            'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
        ];

180
        $client = new Client(static::getUri());
181 182 183
        $database = $client->selectDatabase($this->getDatabaseName(), $databaseOptions);
        $debug = $database->__debugInfo();

184
        $this->assertInstanceOf(ReadConcern::class, $debug['readConcern']);
185
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
186
        $this->assertInstanceOf(ReadPreference::class, $debug['readPreference']);
187
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
188
        $this->assertIsArray($debug['typeMap']);
189
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
190
        $this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
191 192
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

    public function testCreateClientEncryption()
    {
        $client = new Client(static::getUri());

        $options = [
            'keyVaultNamespace' => 'default.keys',
            'kmsProviders' => ['aws' => ['accessKeyId' => 'abc', 'secretAccessKey' => 'def']],
        ];

        $clientEncryption = $client->createClientEncryption($options);
        $this->assertInstanceOf(ClientEncryption::class, $clientEncryption);
    }

    public function testCreateClientEncryptionWithKeyVaultClient()
    {
        $client = new Client(static::getUri());

        $options = [
            'keyVaultClient' => $client,
            'keyVaultNamespace' => 'default.keys',
            'kmsProviders' => ['aws' => ['accessKeyId' => 'abc', 'secretAccessKey' => 'def']],
        ];

        $clientEncryption = $client->createClientEncryption($options);
        $this->assertInstanceOf(ClientEncryption::class, $clientEncryption);
    }

    public function testCreateClientEncryptionWithManager()
    {
        $client = new Client(static::getUri());

        $options = [
            'keyVaultClient' => $client->getManager(),
            'keyVaultNamespace' => 'default.keys',
            'kmsProviders' => ['aws' => ['accessKeyId' => 'abc', 'secretAccessKey' => 'def']],
        ];

        $clientEncryption = $client->createClientEncryption($options);
        $this->assertInstanceOf(ClientEncryption::class, $clientEncryption);
    }

    public function testCreateClientEncryptionWithInvalidKeyVaultClient()
    {
        $client = new Client(static::getUri());

        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Expected "keyVaultClient" option to have type "MongoDB\Client" or "MongoDB\Driver\Manager" but found "string"');

        $client->createClientEncryption(['keyVaultClient' => 'foo']);
    }
244
}