Skip to content

Commit ba02914

Browse files
committed
PHPLIB-1309 Add addSubscriber/removeSubscriber to Client class to ease configuration
1 parent 9f8eac1 commit ba02914

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/Client.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use MongoDB\Driver\Exception\InvalidArgumentException as DriverInvalidArgumentException;
2424
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
2525
use MongoDB\Driver\Manager;
26+
use MongoDB\Driver\Monitoring\Subscriber;
2627
use MongoDB\Driver\ReadConcern;
2728
use MongoDB\Driver\ReadPreference;
2829
use 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
*

tests/ClientTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use MongoDB\Client;
66
use MongoDB\Driver\ClientEncryption;
7+
use MongoDB\Driver\Command;
78
use MongoDB\Driver\Exception\InvalidArgumentException as DriverInvalidArgumentException;
9+
use MongoDB\Driver\Monitoring\CommandSubscriber;
810
use MongoDB\Driver\ReadConcern;
911
use MongoDB\Driver\ReadPreference;
1012
use MongoDB\Driver\WriteConcern;
@@ -237,4 +239,20 @@ public function testCreateClientEncryptionWithInvalidKeyVaultClient(): void
237239

238240
$client->createClientEncryption(['keyVaultClient' => 'foo']);
239241
}
242+
243+
public function testAddAndRemoveSubscriber(): void
244+
{
245+
$client = new Client(static::getUri());
246+
247+
$addedSubscriber = $this->createMock(CommandSubscriber::class);
248+
$addedSubscriber->expects($this->once())->method('commandStarted');
249+
$client->addSubscriber($addedSubscriber);
250+
251+
$removedSubscriber = $this->createMock(CommandSubscriber::class);
252+
$removedSubscriber->expects($this->never())->method('commandStarted');
253+
$client->addSubscriber($removedSubscriber);
254+
$client->removeSubscriber($removedSubscriber);
255+
256+
$client->getManager()->executeCommand('admin', new Command(['ping' => 1]));
257+
}
240258
}

0 commit comments

Comments
 (0)