File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # CHANGELOG
2+
3+
4+ ## 1.18.0 (unreleased)
5+
6+ * Add ` addSubscriber ` and ` removeSubscriber ` methods to the ` Client ` class to ease dependency injection configuration
7+
Original file line number Diff line number Diff line change 2323use MongoDB \Driver \Exception \InvalidArgumentException as DriverInvalidArgumentException ;
2424use MongoDB \Driver \Exception \RuntimeException as DriverRuntimeException ;
2525use MongoDB \Driver \Manager ;
26+ use MongoDB \Driver \Monitoring \Subscriber ;
2627use MongoDB \Driver \ReadConcern ;
2728use MongoDB \Driver \ReadPreference ;
2829use MongoDB \Driver \Session ;
@@ -163,6 +164,16 @@ public function __toString()
163164 return $ this ->uri ;
164165 }
165166
167+ /**
168+ * Registers a monitoring event subscriber with this Client's Manager
169+ *
170+ * @see Manager::addSubscriber()
171+ */
172+ final public function addSubscriber (Subscriber $ subscriber ): void
173+ {
174+ $ this ->manager ->addSubscriber ($ subscriber );
175+ }
176+
166177 /**
167178 * Returns a ClientEncryption instance for explicit encryption and decryption
168179 *
@@ -296,6 +307,16 @@ public function listDatabases(array $options = [])
296307 return $ operation ->execute ($ server );
297308 }
298309
310+ /**
311+ * Unregisters a monitoring event subscriber with this Client's Manager
312+ *
313+ * @see Manager::removeSubscriber()
314+ */
315+ final public function removeSubscriber (Subscriber $ subscriber ): void
316+ {
317+ $ this ->manager ->removeSubscriber ($ subscriber );
318+ }
319+
299320 /**
300321 * Select a collection.
301322 *
Original file line number Diff line number Diff line change 44
55use MongoDB \Client ;
66use MongoDB \Driver \BulkWrite ;
7+ use MongoDB \Driver \Command ;
78use MongoDB \Driver \Manager ;
9+ use MongoDB \Driver \Monitoring \CommandSubscriber ;
810use MongoDB \Driver \Session ;
911use MongoDB \Model \DatabaseInfo ;
1012use MongoDB \Model \DatabaseInfoIterator ;
@@ -119,4 +121,20 @@ public function testStartSession(): void
119121 {
120122 $ this ->assertInstanceOf (Session::class, $ this ->client ->startSession ());
121123 }
124+
125+ public function testAddAndRemoveSubscriber (): void
126+ {
127+ $ client = new Client (static ::getUri ());
128+
129+ $ addedSubscriber = $ this ->createMock (CommandSubscriber::class);
130+ $ addedSubscriber ->expects ($ this ->once ())->method ('commandStarted ' );
131+ $ client ->addSubscriber ($ addedSubscriber );
132+
133+ $ removedSubscriber = $ this ->createMock (CommandSubscriber::class);
134+ $ removedSubscriber ->expects ($ this ->never ())->method ('commandStarted ' );
135+ $ client ->addSubscriber ($ removedSubscriber );
136+ $ client ->removeSubscriber ($ removedSubscriber );
137+
138+ $ client ->getManager ()->executeCommand ('admin ' , new Command (['ping ' => 1 ]));
139+ }
122140}
You can’t perform that action at this time.
0 commit comments