Skip to content

Commit 20dca4a

Browse files
Only emit Test\AssertionSucceeded and Test\AssertionFailed events when they have subscribers
1 parent d86db43 commit 20dca4a

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

src/Event/Dispatcher/DeferringDispatcher.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ public function registerSubscriber(Subscriber $subscriber): void
3434
$this->dispatcher->registerSubscriber($subscriber);
3535
}
3636

37+
/**
38+
* @psalm-param class-string $className
39+
*/
40+
public function hasSubscriberFor(string $className): bool
41+
{
42+
return $this->dispatcher->hasSubscriberFor($className);
43+
}
44+
3745
public function dispatch(Event $event): void
3846
{
3947
if ($this->recording) {

src/Event/Dispatcher/DirectDispatcher.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ public function registerSubscriber(Subscriber $subscriber): void
6666
$this->subscribers[$eventClassName][] = $subscriber;
6767
}
6868

69+
/**
70+
* @psalm-param class-string $className
71+
*/
72+
public function hasSubscriberFor(string $className): bool
73+
{
74+
if ($this->tracers !== []) {
75+
return true;
76+
}
77+
78+
if (isset($this->subscribers[$className])) {
79+
return true;
80+
}
81+
82+
return false;
83+
}
84+
6985
/**
7086
* @throws UnknownEventTypeException
7187
*/

src/Event/Dispatcher/SubscribableDispatcher.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ interface SubscribableDispatcher extends Dispatcher
2020
public function registerSubscriber(Subscriber $subscriber): void;
2121

2222
public function registerTracer(Tracer\Tracer $tracer): void;
23+
24+
/**
25+
* @psalm-param class-string $className
26+
*/
27+
public function hasSubscriberFor(string $className): bool;
2328
}

src/Event/Emitter/DispatchingEmitter.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ public function testRegisteredComparator(string $className): void
400400
*/
401401
public function testAssertionSucceeded(mixed $value, Constraint\Constraint $constraint, string $message): void
402402
{
403+
if (!$this->hasSubscriberFor(Test\AssertionSucceeded::class)) {
404+
return;
405+
}
406+
403407
$this->dispatcher->dispatch(
404408
new Test\AssertionSucceeded(
405409
$this->telemetryInfo(),
@@ -417,6 +421,10 @@ public function testAssertionSucceeded(mixed $value, Constraint\Constraint $cons
417421
*/
418422
public function testAssertionFailed(mixed $value, Constraint\Constraint $constraint, string $message): void
419423
{
424+
if (!$this->hasSubscriberFor(Test\AssertionFailed::class)) {
425+
return;
426+
}
427+
420428
$this->dispatcher->dispatch(
421429
new Test\AssertionFailed(
422430
$this->telemetryInfo(),
@@ -1048,4 +1056,13 @@ private function telemetryInfo(): Telemetry\Info
10481056

10491057
return $info;
10501058
}
1059+
1060+
private function hasSubscriberFor(string $className): bool
1061+
{
1062+
if (!$this->dispatcher instanceof SubscribableDispatcher) {
1063+
return true;
1064+
}
1065+
1066+
return $this->dispatcher->hasSubscriberFor($className);
1067+
}
10511068
}

0 commit comments

Comments
 (0)