Skip to content

Commit 0f421be

Browse files
committed
PHPC-2289: Prohibit passing LogSubscriber to Manager::addSubscriber()
1 parent 22f96fd commit 0f421be

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/MongoDB/Manager.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ static PHP_METHOD(MongoDB_Driver_Manager, addSubscriber)
296296
Z_PARAM_OBJECT_OF_CLASS(subscriber, php_phongo_subscriber_ce)
297297
PHONGO_PARSE_PARAMETERS_END();
298298

299+
if (instanceof_function(Z_OBJCE_P(subscriber), php_phongo_logsubscriber_ce)) {
300+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "LogSubscriber instances cannot be registered with a Manager");
301+
}
302+
299303
intern = Z_MANAGER_OBJ_P(getThis());
300304

301305
/* Lazily initialize the subscriber HashTable */
@@ -304,8 +308,6 @@ static PHP_METHOD(MongoDB_Driver_Manager, addSubscriber)
304308
zend_hash_init(intern->subscribers, 0, NULL, ZVAL_PTR_DTOR, 0);
305309
}
306310

307-
// TODO: Consider throwing if subscriber is unsupported (see: PHPC-2289)
308-
309311
phongo_apm_add_subscriber(intern->subscribers, subscriber);
310312
}
311313

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::addSubscriber() does not support LogSubscriber instances
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/basic.inc';
7+
8+
use MongoDB\Driver\Monitoring\CommandSubscriber;
9+
use MongoDB\Driver\Monitoring\LogSubscriber;
10+
11+
class MyLogger implements LogSubscriber
12+
{
13+
public function log($level, $domain, $message): void {}
14+
}
15+
16+
class MySubscriber implements CommandSubscriber, LogSubscriber
17+
{
18+
public function commandStarted($event): void {}
19+
20+
public function commandSucceeded($event): void {}
21+
22+
public function commandFailed($event): void {}
23+
24+
public function log($level, $domain, $message): void {}
25+
}
26+
27+
$manager = create_test_manager();
28+
29+
echo throws(function () use ($manager) {
30+
$manager->addSubscriber(new MyLogger);
31+
}, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n";
32+
33+
echo throws(function () use ($manager) {
34+
$manager->addSubscriber(new MySubscriber);
35+
}, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n";
36+
37+
?>
38+
===DONE===
39+
--EXPECT--
40+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
41+
LogSubscriber instances cannot be registered with a Manager
42+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
43+
LogSubscriber instances cannot be registered with a Manager
44+
===DONE===

0 commit comments

Comments
 (0)