Skip to content

Commit e432821

Browse files
committed
Add traits to implement Command and SDAM subscriber methods
1 parent 89fb97a commit e432821

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

src/Monitoring/CommandEvents.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace MongoDB\Monitoring;
4+
5+
use MongoDB\Driver\Monitoring\CommandFailedEvent;
6+
use MongoDB\Driver\Monitoring\CommandStartedEvent;
7+
use MongoDB\Driver\Monitoring\CommandSubscriber;
8+
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
9+
10+
/** @see CommandSubscriber */
11+
trait CommandEvents
12+
{
13+
public function commandFailed(CommandFailedEvent $event): void
14+
{
15+
}
16+
17+
public function commandStarted(CommandStartedEvent $event): void
18+
{
19+
}
20+
21+
public function commandSucceeded(CommandSucceededEvent $event): void
22+
{
23+
}
24+
}

src/Monitoring/SDAMEvents.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace MongoDB\Monitoring;
4+
5+
use MongoDB\Driver\Monitoring\SDAMSubscriber;
6+
use MongoDB\Driver\Monitoring\ServerChangedEvent;
7+
use MongoDB\Driver\Monitoring\ServerClosedEvent;
8+
use MongoDB\Driver\Monitoring\ServerHeartbeatFailedEvent;
9+
use MongoDB\Driver\Monitoring\ServerHeartbeatStartedEvent;
10+
use MongoDB\Driver\Monitoring\ServerHeartbeatSucceededEvent;
11+
use MongoDB\Driver\Monitoring\ServerOpeningEvent;
12+
use MongoDB\Driver\Monitoring\TopologyChangedEvent;
13+
use MongoDB\Driver\Monitoring\TopologyClosedEvent;
14+
use MongoDB\Driver\Monitoring\TopologyOpeningEvent;
15+
16+
/** @see SDAMSubscriber */
17+
trait SDAMEvents
18+
{
19+
public function serverChanged(ServerChangedEvent $event): void
20+
{
21+
}
22+
23+
public function serverClosed(ServerClosedEvent $event): void
24+
{
25+
}
26+
27+
public function serverOpening(ServerOpeningEvent $event): void
28+
{
29+
}
30+
31+
public function serverHeartbeatFailed(ServerHeartbeatFailedEvent $event): void
32+
{
33+
}
34+
35+
public function serverHeartbeatStarted(ServerHeartbeatStartedEvent $event): void
36+
{
37+
}
38+
39+
public function serverHeartbeatSucceeded(ServerHeartbeatSucceededEvent $event): void
40+
{
41+
}
42+
43+
public function topologyChanged(TopologyChangedEvent $event): void
44+
{
45+
}
46+
47+
public function topologyClosed(TopologyClosedEvent $event): void
48+
{
49+
}
50+
51+
public function topologyOpening(TopologyOpeningEvent $event): void
52+
{
53+
}
54+
}

tests/MonitoringTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace MongoDB\Tests;
4+
5+
use MongoDB\Driver\Monitoring\CommandSubscriber;
6+
use MongoDB\Driver\Monitoring\SDAMSubscriber;
7+
use MongoDB\Driver\Monitoring\Subscriber;
8+
use MongoDB\Monitoring\CommandEvents;
9+
use MongoDB\Monitoring\SDAMEvents;
10+
11+
use function MongoDB\Driver\Monitoring\addSubscriber;
12+
use function MongoDB\Driver\Monitoring\removeSubscriber;
13+
14+
class MonitoringTest extends TestCase
15+
{
16+
/**
17+
* @doesNotPerformAssertions
18+
*
19+
* @dataProvider provideSubscribers
20+
*/
21+
public function testSubscriber(Subscriber $subscriber): void
22+
{
23+
try {
24+
// Fatal error if the trait does not implement all methods required by the interface
25+
addSubscriber($subscriber);
26+
} finally {
27+
removeSubscriber($subscriber);
28+
}
29+
}
30+
31+
public static function provideSubscribers(): iterable
32+
{
33+
yield 'Command' => [
34+
new class implements CommandSubscriber {
35+
use CommandEvents;
36+
},
37+
];
38+
39+
yield 'SDAM' => [
40+
new class implements SDAMSubscriber {
41+
use SDAMEvents;
42+
},
43+
];
44+
}
45+
}

0 commit comments

Comments
 (0)