File tree Expand file tree Collapse file tree 4 files changed +61
-0
lines changed Expand file tree Collapse file tree 4 files changed +61
-0
lines changed Original file line number Diff line number Diff line change @@ -73,6 +73,23 @@ public function __debugInfo()
73
73
];
74
74
}
75
75
76
+ /**
77
+ * Select a database.
78
+ *
79
+ * Note: collections whose names contain special characters (e.g. "-") may
80
+ * be selected with complex syntax (e.g. $client->{"that-database"}) or
81
+ * {@link selectDatabase()}.
82
+ *
83
+ * @see http://php.net/oop5.overloading#object.get
84
+ * @see http://php.net/types.string#language.types.string.parsing.complex
85
+ * @param string $databaseName Name of the database to select
86
+ * @return Database
87
+ */
88
+ public function __get ($ databaseName )
89
+ {
90
+ return $ this ->selectDatabase ($ databaseName );
91
+ }
92
+
76
93
/**
77
94
* Return the connection string (i.e. URI).
78
95
*
Original file line number Diff line number Diff line change @@ -102,6 +102,23 @@ public function __debugInfo()
102
102
];
103
103
}
104
104
105
+ /**
106
+ * Select a collection within this database.
107
+ *
108
+ * Note: collections whose names contain special characters (e.g. ".") may
109
+ * be selected with complex syntax (e.g. $database->{"system.profile"}) or
110
+ * {@link selectCollection()}.
111
+ *
112
+ * @see http://php.net/oop5.overloading#object.get
113
+ * @see http://php.net/types.string#language.types.string.parsing.complex
114
+ * @param string $collectionName Name of the collection to select
115
+ * @return Collection
116
+ */
117
+ public function __get ($ collectionName )
118
+ {
119
+ return $ this ->selectCollection ($ collectionName );
120
+ }
121
+
105
122
/**
106
123
* Return the database name.
107
124
*
Original file line number Diff line number Diff line change @@ -97,6 +97,19 @@ public function testSelectCollectionPassesOptions()
97
97
$ this ->assertSame (WriteConcern::MAJORITY , $ debug ['writeConcern ' ]->getW ());
98
98
}
99
99
100
+ public function testGetSelectsDatabaseAndInheritsOptions ()
101
+ {
102
+ $ uriOptions = ['w ' => WriteConcern::MAJORITY ];
103
+
104
+ $ client = new Client ($ this ->getUri (), $ uriOptions );
105
+ $ database = $ client ->{$ this ->getDatabaseName ()};
106
+ $ debug = $ database ->__debugInfo ();
107
+
108
+ $ this ->assertSame ($ this ->getDatabaseName (), $ debug ['databaseName ' ]);
109
+ $ this ->assertInstanceOf ('MongoDB\Driver\WriteConcern ' , $ debug ['writeConcern ' ]);
110
+ $ this ->assertSame (WriteConcern::MAJORITY , $ debug ['writeConcern ' ]->getW ());
111
+ }
112
+
100
113
public function testSelectDatabaseInheritsOptions ()
101
114
{
102
115
$ this ->markTestSkipped ('Depends on https://jira.mongodb.org/browse/PHPC-523 ' );
Original file line number Diff line number Diff line change @@ -131,6 +131,20 @@ public function testDrop()
131
131
$ this ->assertCollectionCount ($ this ->getNamespace (), 0 );
132
132
}
133
133
134
+ public function testGetSelectsCollectionAndInheritsOptions ()
135
+ {
136
+ $ databaseOptions = ['writeConcern ' => new WriteConcern (WriteConcern::MAJORITY )];
137
+
138
+ $ database = new Database ($ this ->manager , $ this ->getDatabaseName (), $ databaseOptions );
139
+ $ collection = $ database ->{$ this ->getCollectionName ()};
140
+ $ debug = $ collection ->__debugInfo ();
141
+
142
+ $ this ->assertSame ($ this ->getCollectionName (), $ debug ['collectionName ' ]);
143
+ $ this ->assertSame ($ this ->getDatabaseName (), $ debug ['databaseName ' ]);
144
+ $ this ->assertInstanceOf ('MongoDB\Driver\WriteConcern ' , $ debug ['writeConcern ' ]);
145
+ $ this ->assertSame (WriteConcern::MAJORITY , $ debug ['writeConcern ' ]->getW ());
146
+ }
147
+
134
148
public function testSelectCollectionInheritsOptions ()
135
149
{
136
150
$ databaseOptions = [
You can’t perform that action at this time.
0 commit comments