ClientTest.php 5.53 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://localhost:27017', (string) $client);
20 21
    }

22 23 24 25 26 27
    public function testToString()
    {
        $client = new Client($this->getUri());

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

29
    public function testSelectCollectionInheritsOptions()
30
    {
31 32 33 34
        $this->markTestSkipped('Depends on https://jira.mongodb.org/browse/PHPC-523');

        $uriOptions = [
            'readConcernLevel' => ReadConcern::LOCAL,
35 36 37 38
            'readPreference' => 'secondaryPreferred',
            'w' => WriteConcern::MAJORITY,
        ];

39 40 41 42 43
        $driverOptions = [
            'typeMap' => ['root' => 'array'],
        ];

        $client = new Client($this->getUri(), $uriOptions, $driverOptions);
44 45 46
        $collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
        $debug = $collection->__debugInfo();

47 48
        $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
49 50
        $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
51 52
        $this->assertInternalType('array', $debug['typeMap']);
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
53 54 55 56
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

57
    public function testSelectCollectionPassesOptions()
58 59
    {
        $collectionOptions = [
60
            'readConcern' => new ReadConcern(ReadConcern::LOCAL),
61
            'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
62
            'typeMap' => ['root' => 'array'],
63 64 65 66 67 68 69
            'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
        ];

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

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

80
    public function testSelectDatabaseInheritsOptions()
81
    {
82 83 84 85
        $this->markTestSkipped('Depends on https://jira.mongodb.org/browse/PHPC-523');

        $uriOptions = [
            'readConcernLevel' => ReadConcern::LOCAL,
86 87 88 89
            'readPreference' => 'secondaryPreferred',
            'w' => WriteConcern::MAJORITY,
        ];

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

        $client = new Client($this->getUri(), $uriOptions, $driverOptions);
95 96 97
        $database = $client->selectDatabase($this->getDatabaseName());
        $debug = $database->__debugInfo();

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

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

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

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