DatabaseFunctionalTest.php 13.2 KB
Newer Older
1 2 3 4
<?php

namespace MongoDB\Tests\Database;

5
use MongoDB\Database;
6
use MongoDB\Driver\BulkWrite;
7
use MongoDB\Driver\ReadConcern;
8 9
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
10
use MongoDB\Exception\InvalidArgumentException;
11
use MongoDB\Operation\CreateIndexes;
12

13 14 15 16 17
/**
 * Functional tests for the Database class.
 */
class DatabaseFunctionalTest extends FunctionalTestCase
{
18
    /**
19
     * @dataProvider provideInvalidDatabaseNames
20 21 22
     */
    public function testConstructorDatabaseNameArgument($databaseName)
    {
23
        $this->expectException(InvalidArgumentException::class);
24 25 26 27
        // TODO: Move to unit test once ManagerInterface can be mocked (PHPC-378)
        new Database($this->manager, $databaseName);
    }

28
    public function provideInvalidDatabaseNames()
29
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
30 31 32 33
        return [
            [null],
            [''],
        ];
34 35
    }

36 37 38 39 40
    /**
     * @dataProvider provideInvalidConstructorOptions
     */
    public function testConstructorOptionTypeChecks(array $options)
    {
41
        $this->expectException(InvalidArgumentException::class);
42 43 44 45 46 47 48
        new Database($this->manager, $this->getDatabaseName(), $options);
    }

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

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

53 54 55 56
        foreach ($this->getInvalidReadPreferenceValues() as $value) {
            $options[][] = ['readPreference' => $value];
        }

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

61 62 63 64 65 66 67
        foreach ($this->getInvalidWriteConcernValues() as $value) {
            $options[][] = ['writeConcern' => $value];
        }

        return $options;
    }

68 69 70 71 72
    public function testGetManager()
    {
        $this->assertSame($this->manager, $this->database->getManager());
    }

73 74 75 76 77 78 79 80 81 82
    public function testToString()
    {
        $this->assertEquals($this->getDatabaseName(), (string) $this->database);
    }

    public function getGetDatabaseName()
    {
        $this->assertEquals($this->getDatabaseName(), $this->database->getDatabaseName());
    }

83 84 85
    public function testCommand()
    {
        $command = ['isMaster' => 1];
86 87 88 89 90
        $options = [
            'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY),
        ];

        $cursor = $this->database->command($command, $options);
91 92 93 94 95

        $this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
        $commandResult = current($cursor->toArray());

        $this->assertCommandSucceeded($commandResult);
96
        $this->assertObjectHasAttribute('ismaster', $commandResult);
97 98 99
        $this->assertTrue($commandResult->ismaster);
    }

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    public function testCommandAppliesTypeMapToCursor()
    {
        $command = ['isMaster' => 1];
        $options = [
            'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY),
            'typeMap' => ['root' => 'array'],
        ];

        $cursor = $this->database->command($command, $options);

        $this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
        $commandResult = current($cursor->toArray());

        $this->assertCommandSucceeded($commandResult);
        $this->assertInternalType('array', $commandResult);
115
        $this->assertArrayHasKey('ismaster', $commandResult);
116 117 118
        $this->assertTrue($commandResult['ismaster']);
    }

119 120 121 122 123
    /**
     * @dataProvider provideInvalidDocumentValues
     */
    public function testCommandCommandArgumentTypeCheck($command)
    {
124
        $this->expectException(InvalidArgumentException::class);
125 126 127
        $this->database->command($command);
    }

128 129
    public function testDrop()
    {
130 131 132 133
        $bulkWrite = new BulkWrite();
        $bulkWrite->insert(['x' => 1]);

        $writeResult = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
134 135 136 137 138 139
        $this->assertEquals(1, $writeResult->getInsertedCount());

        $commandResult = $this->database->drop();
        $this->assertCommandSucceeded($commandResult);
        $this->assertCollectionCount($this->getNamespace(), 0);
    }
140

141 142 143 144 145 146 147 148
    public function testGetSelectsCollectionAndInheritsOptions()
    {
        $databaseOptions = ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)];

        $database = new Database($this->manager, $this->getDatabaseName(), $databaseOptions);
        $collection = $database->{$this->getCollectionName()};
        $debug = $collection->__debugInfo();

149
        $this->assertSame($this->manager, $debug['manager']);
150
        $this->assertSame($this->getDatabaseName(), $debug['databaseName']);
151
        $this->assertSame($this->getCollectionName(), $debug['collectionName']);
152 153 154 155
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    public function testModifyCollection()
    {
        $this->database->createCollection($this->getCollectionName());

        $indexes = [['key' => ['lastAccess' => 1], 'expireAfterSeconds' => 3]];
        $createIndexes = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
        $createIndexes->execute($this->getPrimaryServer());

        $commandResult = $this->database->modifyCollection($this->getCollectionName(), ['index' => ['keyPattern' => ['lastAccess' => 1], 'expireAfterSeconds' => 1000]]);

        $this->assertCommandSucceeded($commandResult);
        $this->assertSame(3, $commandResult['expireAfterSeconds_old']);
        $this->assertSame(1000, $commandResult['expireAfterSeconds_new']);
    }


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

        $database = new Database($this->manager, $this->getDatabaseName(), $databaseOptions);
        $collection = $database->selectCollection($this->getCollectionName());
        $debug = $collection->__debugInfo();

185 186 187
        $this->assertSame($this->manager, $debug['manager']);
        $this->assertSame($this->getDatabaseName(), $debug['databaseName']);
        $this->assertSame($this->getCollectionName(), $debug['collectionName']);
188 189
        $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
190 191
        $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
192 193
        $this->assertInternalType('array', $debug['typeMap']);
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
194 195 196 197
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

198
    public function testSelectCollectionPassesOptions()
199 200
    {
        $collectionOptions = [
201
            'readConcern' => new ReadConcern(ReadConcern::LOCAL),
202
            'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
203
            'typeMap' => ['root' => 'array'],
204 205 206 207 208 209
            'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
        ];

        $collection = $this->database->selectCollection($this->getCollectionName(), $collectionOptions);
        $debug = $collection->__debugInfo();

210 211
        $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
212 213
        $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
214 215
        $this->assertInternalType('array', $debug['typeMap']);
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
216 217 218
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }
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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    public function testSelectGridFSBucketInheritsOptions()
    {
        $databaseOptions = [
            'readConcern' => new ReadConcern(ReadConcern::LOCAL),
            'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
            'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
        ];

        $database = new Database($this->manager, $this->getDatabaseName(), $databaseOptions);
        $bucket = $database->selectGridFSBucket();
        $debug = $bucket->__debugInfo();

        $this->assertSame($this->manager, $debug['manager']);
        $this->assertSame($this->getDatabaseName(), $debug['databaseName']);
        $this->assertSame('fs', $debug['bucketName']);
        $this->assertSame(261120, $debug['chunkSizeBytes']);
        $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
        $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

    public function testSelectGridFSBucketPassesOptions()
    {
        $bucketOptions = [
            'bucketName' => 'custom_fs',
            'chunkSizeBytes' => 8192,
            'readConcern' => new ReadConcern(ReadConcern::LOCAL),
            'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
            'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
        ];

        $database = new Database($this->manager, $this->getDatabaseName());
        $bucket = $database->selectGridFSBucket($bucketOptions);
        $debug = $bucket->__debugInfo();

        $this->assertSame($this->getDatabaseName(), $debug['databaseName']);
        $this->assertSame('custom_fs', $debug['bucketName']);
        $this->assertSame(8192, $debug['chunkSizeBytes']);
        $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
        $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

269
    public function testWithOptionsInheritsOptions()
270 271
    {
        $databaseOptions = [
272
            'readConcern' => new ReadConcern(ReadConcern::LOCAL),
273
            'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
274
            'typeMap' => ['root' => 'array'],
275 276 277 278 279 280 281
            'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
        ];

        $database = new Database($this->manager, $this->getDatabaseName(), $databaseOptions);
        $clone = $database->withOptions();
        $debug = $clone->__debugInfo();

282 283
        $this->assertSame($this->manager, $debug['manager']);
        $this->assertSame($this->getDatabaseName(), $debug['databaseName']);
284 285
        $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
286 287
        $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
288 289
        $this->assertInternalType('array', $debug['typeMap']);
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
290 291 292 293
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

294
    public function testWithOptionsPassesOptions()
295 296
    {
        $databaseOptions = [
297
            'readConcern' => new ReadConcern(ReadConcern::LOCAL),
298
            'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
299
            'typeMap' => ['root' => 'array'],
300 301 302 303 304 305
            'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
        ];

        $clone = $this->database->withOptions($databaseOptions);
        $debug = $clone->__debugInfo();

306 307
        $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
308 309
        $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
310 311
        $this->assertInternalType('array', $debug['typeMap']);
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
312 313 314
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }
315
}