ClientTest.php 6.46 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
use MongoDB\Exception\InvalidArgumentException;
10 11 12 13 14 15

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

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

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

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

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

        return $options;
    }

43 44
    public function testToString()
    {
45
        $client = new Client(static::getUri());
46

47
        $this->assertSame(static::getUri(), (string) $client);
48
    }
49

50
    public function testSelectCollectionInheritsOptions()
51
    {
52 53
        $uriOptions = [
            'readConcernLevel' => ReadConcern::LOCAL,
54 55 56 57
            'readPreference' => 'secondaryPreferred',
            'w' => WriteConcern::MAJORITY,
        ];

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

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

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

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

85
        $client = new Client(static::getUri());
86 87 88
        $collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName(), $collectionOptions);
        $debug = $collection->__debugInfo();

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

99 100 101 102
    public function testGetSelectsDatabaseAndInheritsOptions()
    {
        $uriOptions = ['w' => WriteConcern::MAJORITY];

103
        $client = new Client(static::getUri(), $uriOptions);
104 105 106 107 108 109 110 111
        $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());
    }

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

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

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

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

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

147
        $client = new Client(static::getUri());
148 149 150
        $database = $client->selectDatabase($this->getDatabaseName(), $databaseOptions);
        $debug = $database->__debugInfo();

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