ClientTest.php 6.41 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
        $uriOptions = [
            'readConcernLevel' => ReadConcern::LOCAL,
53 54 55 56
            'readPreference' => 'secondaryPreferred',
            'w' => WriteConcern::MAJORITY,
        ];

57 58 59 60 61
        $driverOptions = [
            'typeMap' => ['root' => 'array'],
        ];

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

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

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

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

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

98 99 100 101 102 103 104 105 106 107 108 109 110
    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());
    }

111
    public function testSelectDatabaseInheritsOptions()
112
    {
113 114
        $uriOptions = [
            'readConcernLevel' => ReadConcern::LOCAL,
115 116 117 118
            'readPreference' => 'secondaryPreferred',
            'w' => WriteConcern::MAJORITY,
        ];

119 120 121 122 123
        $driverOptions = [
            'typeMap' => ['root' => 'array'],
        ];

        $client = new Client($this->getUri(), $uriOptions, $driverOptions);
124 125 126
        $database = $client->selectDatabase($this->getDatabaseName());
        $debug = $database->__debugInfo();

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

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

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

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