Skip to content

Commit 42dda3c

Browse files
authored
Better explain orphan events (#116)
1 parent 897ef17 commit 42dda3c

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

src/Codeception/Module/Symfony/EventsAssertionsTrait.php

+31-20
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,35 @@
1414
trait EventsAssertionsTrait
1515
{
1616
/**
17-
* Verifies that one or more orphan events were not dispatched during the test.
17+
* Verifies that there were no orphan events during the test.
1818
*
19-
* An orphan event is an event that is triggered by manually executing the
19+
* An orphan event is an event that was triggered by manually executing the
2020
* [`dispatch()`](https://symfony.com/doc/current/components/event_dispatcher.html#dispatch-the-event) method
21-
* of the EventDispatcher, in other words, it is an event that is not handled by any listener.
21+
* of the EventDispatcher but was not handled by any listener after it was dispatched.
2222
*
2323
* ```php
2424
* <?php
25-
* $I->dontSeeOrphanEventTriggered('App\MyEvent');
26-
* $I->dontSeeOrphanEventTriggered(new App\Events\MyEvent());
27-
* $I->dontSeeOrphanEventTriggered(['App\MyEvent', 'App\MyOtherEvent']);
25+
* $I->dontSeeOrphanEvent();
26+
* $I->dontSeeOrphanEvent('App\MyEvent');
27+
* $I->dontSeeOrphanEvent(new App\Events\MyEvent());
28+
* $I->dontSeeOrphanEvent(['App\MyEvent', 'App\MyOtherEvent']);
2829
* ```
2930
*
3031
* @param string|object|string[] $expected
3132
*/
32-
public function dontSeeOrphanEventTriggered($expected): void
33+
public function dontSeeOrphanEvent($expected = null): void
3334
{
3435
$eventCollector = $this->grabEventCollector(__FUNCTION__);
3536

3637
/** @var Data $data */
3738
$data = $eventCollector->getOrphanedEvents();
3839
$expected = is_array($expected) ? $expected : [$expected];
3940

40-
$this->assertEventNotTriggered($data, $expected);
41+
if ($expected === null) {
42+
$this->assertSame(0, $data->count());
43+
} else {
44+
$this->assertEventNotTriggered($data, $expected);
45+
}
4146
}
4247

4348
/**
@@ -66,20 +71,20 @@ public function dontSeeEventTriggered($expected): void
6671
/**
6772
* Verifies that one or more orphan events were dispatched during the test.
6873
*
69-
* An orphan event is an event that is triggered by manually executing the
74+
* An orphan event is an event that was triggered by manually executing the
7075
* [`dispatch()`](https://symfony.com/doc/current/components/event_dispatcher.html#dispatch-the-event) method
71-
* of the EventDispatcher, in other words, it is an event that is not handled by any listener.
76+
* of the EventDispatcher but was not handled by any listener after it was dispatched.
7277
*
7378
* ```php
7479
* <?php
75-
* $I->seeOrphanEventTriggered('App\MyEvent');
76-
* $I->seeOrphanEventTriggered(new App\Events\MyEvent());
77-
* $I->seeOrphanEventTriggered(['App\MyEvent', 'App\MyOtherEvent']);
80+
* $I->seeOrphanEvent('App\MyEvent');
81+
* $I->seeOrphanEvent(new App\Events\MyEvent());
82+
* $I->seeOrphanEvent(['App\MyEvent', 'App\MyOtherEvent']);
7883
* ```
7984
*
8085
* @param string|object|string[] $expected
8186
*/
82-
public function seeOrphanEventTriggered($expected): void
87+
public function seeOrphanEvent($expected): void
8388
{
8489
$eventCollector = $this->grabEventCollector(__FUNCTION__);
8590

@@ -115,10 +120,6 @@ public function seeEventTriggered($expected): void
115120

116121
protected function assertEventNotTriggered(Data $data, array $expected): void
117122
{
118-
if ($data->count() === 0) {
119-
$this->fail('No event was triggered');
120-
}
121-
122123
$actual = $data->getValue(true);
123124

124125
foreach ($expected as $expectedEvent) {
@@ -132,6 +133,10 @@ protected function assertEventNotTriggered(Data $data, array $expected): void
132133

133134
protected function assertEventTriggered(Data $data, array $expected): void
134135
{
136+
if ($data->count() === 0) {
137+
$this->fail('No event was triggered');
138+
}
139+
135140
$actual = $data->getValue(true);
136141

137142
foreach ($expected as $expectedEvent) {
@@ -148,8 +153,14 @@ protected function eventWasTriggered(array $actual, string $expectedEvent): bool
148153
$triggered = false;
149154

150155
foreach ($actual as $actualEvent) {
151-
if (strpos($actualEvent['pretty'], $expectedEvent) === 0) {
152-
$triggered = true;
156+
if (is_array($actualEvent)) { // Called Listeners
157+
if (strpos($actualEvent['pretty'], $expectedEvent) === 0) {
158+
$triggered = true;
159+
}
160+
} else { // Orphan Events
161+
if ($actualEvent === $expectedEvent) {
162+
$triggered = true;
163+
}
153164
}
154165
}
155166
return $triggered;

0 commit comments

Comments
 (0)