3
3
namespace MongoDB \Tests ;
4
4
5
5
use MongoDB \Client ;
6
+ use MongoDB \Driver \ReadPreference ;
7
+ use MongoDB \Driver \WriteConcern ;
6
8
7
9
/**
8
10
* Unit tests for the Client class.
@@ -22,4 +24,72 @@ public function testToString()
22
24
23
25
$ this ->assertSame ($ this ->getUri (), (string ) $ client );
24
26
}
27
+
28
+ public function testSelectCollectionInheritsReadPreferenceAndWriteConcern ()
29
+ {
30
+ $ clientOptions = [
31
+ 'readPreference ' => 'secondaryPreferred ' ,
32
+ 'w ' => WriteConcern::MAJORITY ,
33
+ ];
34
+
35
+ $ client = new Client ($ this ->getUri (), $ clientOptions );
36
+ $ collection = $ client ->selectCollection ($ this ->getDatabaseName (), $ this ->getCollectionName ());
37
+ $ debug = $ collection ->__debugInfo ();
38
+
39
+ $ this ->assertInstanceOf ('MongoDB\Driver\ReadPreference ' , $ debug ['readPreference ' ]);
40
+ $ this ->assertSame (ReadPreference::RP_SECONDARY_PREFERRED , $ debug ['readPreference ' ]->getMode ());
41
+ $ this ->assertInstanceOf ('MongoDB\Driver\WriteConcern ' , $ debug ['writeConcern ' ]);
42
+ $ this ->assertSame (WriteConcern::MAJORITY , $ debug ['writeConcern ' ]->getW ());
43
+ }
44
+
45
+ public function testSelectCollectionPassesReadPreferenceAndWriteConcern ()
46
+ {
47
+ $ collectionOptions = [
48
+ 'readPreference ' => new ReadPreference (ReadPreference::RP_SECONDARY_PREFERRED ),
49
+ 'writeConcern ' => new WriteConcern (WriteConcern::MAJORITY ),
50
+ ];
51
+
52
+ $ client = new Client ($ this ->getUri ());
53
+ $ collection = $ client ->selectCollection ($ this ->getDatabaseName (), $ this ->getCollectionName (), $ collectionOptions );
54
+ $ debug = $ collection ->__debugInfo ();
55
+
56
+ $ this ->assertInstanceOf ('MongoDB\Driver\ReadPreference ' , $ debug ['readPreference ' ]);
57
+ $ this ->assertSame (ReadPreference::RP_SECONDARY_PREFERRED , $ debug ['readPreference ' ]->getMode ());
58
+ $ this ->assertInstanceOf ('MongoDB\Driver\WriteConcern ' , $ debug ['writeConcern ' ]);
59
+ $ this ->assertSame (WriteConcern::MAJORITY , $ debug ['writeConcern ' ]->getW ());
60
+ }
61
+
62
+ public function testSelectDatabaseInheritsReadPreferenceAndWriteConcern ()
63
+ {
64
+ $ clientOptions = [
65
+ 'readPreference ' => 'secondaryPreferred ' ,
66
+ 'w ' => WriteConcern::MAJORITY ,
67
+ ];
68
+
69
+ $ client = new Client ($ this ->getUri (), $ clientOptions );
70
+ $ database = $ client ->selectDatabase ($ this ->getDatabaseName ());
71
+ $ debug = $ database ->__debugInfo ();
72
+
73
+ $ this ->assertInstanceOf ('MongoDB\Driver\ReadPreference ' , $ debug ['readPreference ' ]);
74
+ $ this ->assertSame (ReadPreference::RP_SECONDARY_PREFERRED , $ debug ['readPreference ' ]->getMode ());
75
+ $ this ->assertInstanceOf ('MongoDB\Driver\WriteConcern ' , $ debug ['writeConcern ' ]);
76
+ $ this ->assertSame (WriteConcern::MAJORITY , $ debug ['writeConcern ' ]->getW ());
77
+ }
78
+
79
+ public function testSelectDatabasePassesReadPreferenceAndWriteConcern ()
80
+ {
81
+ $ databaseOptions = [
82
+ 'readPreference ' => new ReadPreference (ReadPreference::RP_SECONDARY_PREFERRED ),
83
+ 'writeConcern ' => new WriteConcern (WriteConcern::MAJORITY ),
84
+ ];
85
+
86
+ $ client = new Client ($ this ->getUri ());
87
+ $ database = $ client ->selectDatabase ($ this ->getDatabaseName (), $ databaseOptions );
88
+ $ debug = $ database ->__debugInfo ();
89
+
90
+ $ this ->assertInstanceOf ('MongoDB\Driver\ReadPreference ' , $ debug ['readPreference ' ]);
91
+ $ this ->assertSame (ReadPreference::RP_SECONDARY_PREFERRED , $ debug ['readPreference ' ]->getMode ());
92
+ $ this ->assertInstanceOf ('MongoDB\Driver\WriteConcern ' , $ debug ['writeConcern ' ]);
93
+ $ this ->assertSame (WriteConcern::MAJORITY , $ debug ['writeConcern ' ]->getW ());
94
+ }
25
95
}
0 commit comments