7
7
use MongoDB \Collection ;
8
8
use MongoDB \Database ;
9
9
use MongoDB \Driver \Manager ;
10
- use MongoDB \Driver \ReadConcern ;
11
10
use MongoDB \Driver \ReadPreference ;
12
11
use MongoDB \Driver \Server ;
13
- use MongoDB \Driver \WriteConcern ;
14
12
use stdClass ;
15
- use function array_diff_key ;
16
- use function array_fill_keys ;
17
13
use function array_key_exists ;
18
- use function array_keys ;
19
- use function assertContains ;
14
+ use function assertArrayHasKey ;
20
15
use function assertCount ;
21
- use function assertEmpty ;
22
16
use function assertInstanceOf ;
23
17
use function assertInternalType ;
18
+ use function assertNotEmpty ;
24
19
use function assertNotFalse ;
25
20
use function assertStringStartsWith ;
26
- use function assertThat ;
27
21
use function count ;
28
22
use function current ;
29
23
use function explode ;
30
24
use function implode ;
31
- use function isType ;
32
25
use function key ;
33
- use function logicalOr ;
34
26
use function parse_url ;
35
27
use function strlen ;
36
28
use function strpos ;
@@ -102,76 +94,33 @@ public function createEntities(array $entities)
102
94
}
103
95
}
104
96
105
- public static function createReadConcern ( stdClass $ o ) : ReadConcern
97
+ public function getEntityMap ( ) : EntityMap
106
98
{
107
- self ::assertHasOnlyKeys ($ o , ['level ' ]);
108
-
109
- $ level = $ o ->level ?? null ;
110
- assertInternalType ('string ' , $ level );
111
-
112
- return new ReadConcern ($ level );
99
+ return $ this ->entityMap ;
113
100
}
114
101
115
- public static function createReadPreference ( stdClass $ o ) : ReadPreference
102
+ public function getInternalClient ( ) : Client
116
103
{
117
- self ::assertHasOnlyKeys ($ o , ['mode ' , 'tagSets ' , 'maxStalenessSeconds ' , 'hedge ' ]);
118
-
119
- $ mode = $ o ->mode ?? null ;
120
- $ tagSets = $ o ->tagSets ?? null ;
121
- $ maxStalenessSeconds = $ o ->maxStalenessSeconds ?? null ;
122
- $ hedge = $ o ->hedge ?? null ;
123
-
124
- assertInternalType ('string ' , $ mode );
125
-
126
- if (isset ($ tagSets )) {
127
- assertInternalType ('array ' , $ tagSets );
128
- assertContains ('object ' , $ tagSets );
129
- }
130
-
131
- $ options = [];
132
-
133
- if (isset ($ maxStalenessSeconds )) {
134
- assertInternalType ('int ' , $ maxStalenessSeconds );
135
- $ options ['maxStalenessSeconds ' ] = $ maxStalenessSeconds ;
136
- }
137
-
138
- if (isset ($ hedge )) {
139
- assertInternalType ('object ' , $ hedge );
140
- $ options ['hedge ' ] = $ hedge ;
141
- }
142
-
143
- return new ReadPreference ($ mode , $ tagSets , $ options );
104
+ return $ this ->internalClient ;
144
105
}
145
106
146
- public static function createWriteConcern ( stdClass $ o ) : WriteConcern
107
+ public function assertExpectedEventsForClients ( array $ expectedEventsForClients )
147
108
{
148
- self :: assertHasOnlyKeys ( $ o , [ ' w ' , ' wtimeoutMS ' , ' journal ' ] );
109
+ assertNotEmpty ( $ expectedEventsForClients );
149
110
150
- $ w = $ o -> w ?? - 2 ; /* MONGOC_WRITE_CONCERN_W_DEFAULT */
151
- $ wtimeoutMS = $ o -> wtimeoutMS ?? 0 ;
152
- $ journal = $ o -> journal ?? null ;
111
+ foreach ( $ expectedEventsForClients as $ expectedEventsForClient ) {
112
+ assertInternalType ( ' object ' , $ expectedEventsForClient ) ;
113
+ Util:: assertHasOnlyKeys ( $ expectedEventsForClient , [ ' client ' , ' events ' ]) ;
153
114
154
- assertThat ( $ w , logicalOr ( isType ( ' int ' ), isType ( ' string ' ))) ;
155
- assertInternalType ( ' int ' , $ wtimeoutMS ) ;
115
+ $ client = $ expectedEventsForClient -> client ?? null ;
116
+ $ expectedEvents = $ expectedEventsForClient -> events ?? null ;
156
117
157
- $ args = [$ w , $ wtimeoutMS ];
118
+ assertInternalType ('string ' , $ client );
119
+ assertArrayHasKey ($ client , $ this ->eventObserversByClient );
120
+ assertInternalType ('array ' , $ expectedEvents );
158
121
159
- if (isset ($ journal )) {
160
- assertInternalType ('bool ' , $ journal );
161
- $ args [] = $ journal ;
122
+ $ this ->eventObserversByClient [$ client ]->assert ($ expectedEvents );
162
123
}
163
-
164
- return new WriteConcern (...$ args );
165
- }
166
-
167
- public function getEntityMap () : EntityMap
168
- {
169
- return $ this ->entityMap ;
170
- }
171
-
172
- public function getInternalClient () : Client
173
- {
174
- return $ this ->internalClient ;
175
124
}
176
125
177
126
public function startEventObservers ()
@@ -188,16 +137,16 @@ public function stopEventObservers()
188
137
}
189
138
}
190
139
191
- private static function assertHasOnlyKeys ( $ arrayOrObject , array $ keys )
140
+ public function getEventObserverForClient ( string $ id ) : EventObserver
192
141
{
193
- assertThat ( $ arrayOrObject , logicalOr ( isType ( ' array ' ), isType ( ' object ' )) );
194
- $ diff = array_diff_key (( array ) $ arrayOrObject , array_fill_keys ( $ keys , 1 ));
195
- assertEmpty ( $ diff , ' Unsupported keys: ' . implode ( ' , ' , array_keys ( $ diff ))) ;
142
+ assertArrayHasKey ( $ id , $ this -> eventObserversByClient );
143
+
144
+ return $ this -> eventObserversByClient [ $ id ] ;
196
145
}
197
146
198
147
private function createClient (stdClass $ o ) : Client
199
148
{
200
- self ::assertHasOnlyKeys ($ o , ['id ' , 'uriOptions ' , 'useMultipleMongoses ' , 'observeEvents ' , 'ignoreCommandMonitoringEvents ' ]);
149
+ Util ::assertHasOnlyKeys ($ o , ['id ' , 'uriOptions ' , 'useMultipleMongoses ' , 'observeEvents ' , 'ignoreCommandMonitoringEvents ' ]);
201
150
202
151
$ useMultipleMongoses = $ o ->useMultipleMongoses ?? null ;
203
152
$ observeEvents = $ o ->observeEvents ?? null ;
@@ -229,15 +178,15 @@ private function createClient(stdClass $o) : Client
229
178
assertInternalType ('array ' , $ observeEvents );
230
179
assertInternalType ('array ' , $ ignoreCommandMonitoringEvents );
231
180
232
- $ this ->eventObserversByClient [$ o ->id ] = new EventObserver ($ observeEvents , $ ignoreCommandMonitoringEvents );
181
+ $ this ->eventObserversByClient [$ o ->id ] = new EventObserver ($ observeEvents , $ ignoreCommandMonitoringEvents, $ o -> id , $ this -> entityMap );
233
182
}
234
183
235
184
return new Client ($ uri , $ uriOptions );
236
185
}
237
186
238
187
private function createCollection (stdClass $ o ) : Collection
239
188
{
240
- self ::assertHasOnlyKeys ($ o , ['id ' , 'database ' , 'collectionName ' , 'collectionOptions ' ]);
189
+ Util ::assertHasOnlyKeys ($ o , ['id ' , 'database ' , 'collectionName ' , 'collectionOptions ' ]);
241
190
242
191
$ collectionName = $ o ->collectionName ?? null ;
243
192
$ database = $ o ->database ?? null ;
@@ -260,7 +209,7 @@ private function createCollection(stdClass $o) : Collection
260
209
261
210
private function createDatabase (stdClass $ o ) : Database
262
211
{
263
- self ::assertHasOnlyKeys ($ o , ['id ' , 'client ' , 'databaseName ' , 'databaseOptions ' ]);
212
+ Util ::assertHasOnlyKeys ($ o , ['id ' , 'client ' , 'databaseName ' , 'databaseOptions ' ]);
264
213
265
214
$ databaseName = $ o ->databaseName ?? null ;
266
215
$ client = $ o ->client ?? null ;
@@ -283,21 +232,21 @@ private function createDatabase(stdClass $o) : Database
283
232
284
233
private static function prepareCollectionOrDatabaseOptions (array $ options ) : array
285
234
{
286
- self ::assertHasOnlyKeys ($ options , ['readConcern ' , 'readPreference ' , 'writeConcern ' ]);
235
+ Util ::assertHasOnlyKeys ($ options , ['readConcern ' , 'readPreference ' , 'writeConcern ' ]);
287
236
288
237
if (array_key_exists ('readConcern ' , $ options )) {
289
238
assertInternalType ('object ' , $ options ['readConcern ' ]);
290
- $ options ['readConcern ' ] = self ::createReadConcern ($ options ['readConcern ' ]);
239
+ $ options ['readConcern ' ] = Util ::createReadConcern ($ options ['readConcern ' ]);
291
240
}
292
241
293
242
if (array_key_exists ('readPreference ' , $ options )) {
294
243
assertInternalType ('object ' , $ options ['readPreference ' ]);
295
- $ options ['readPreference ' ] = self ::createReadPreference ($ options ['readPreference ' ]);
244
+ $ options ['readPreference ' ] = Util ::createReadPreference ($ options ['readPreference ' ]);
296
245
}
297
246
298
247
if (array_key_exists ('writeConcern ' , $ options )) {
299
248
assertInternalType ('object ' , $ options ['writeConcern ' ]);
300
- $ options ['writeConcern ' ] = self ::createWriteConcern ($ options ['writeConcern ' ]);
249
+ $ options ['writeConcern ' ] = Util ::createWriteConcern ($ options ['writeConcern ' ]);
301
250
}
302
251
303
252
return $ options ;
0 commit comments