Skip to content

Commit 0bb7dd5

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

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CHANGELOG
2+
=========
3+
4+
1.18.0 (unreleased)
5+
-------------------
6+
7+
* Add `addSubscriber` and `removeSubscriber` methods to the `Client` class to ease dependency injection configuration
8+

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/ClientFunctionalTest.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\BulkWrite;
7+
use MongoDB\Driver\Command;
78
use MongoDB\Driver\Manager;
9+
use MongoDB\Driver\Monitoring\CommandSubscriber;
810
use MongoDB\Driver\Session;
911
use MongoDB\Model\DatabaseInfo;
1012
use 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
}

0 commit comments

Comments
 (0)