Skip to content

Add seeEvent/dontSeeEvent event assertions #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 89 additions & 26 deletions src/Codeception/Module/Symfony/EventsAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,40 @@ public function dontSeeOrphanEvent(array|object|string $expected = null): void
if ($expected === null) {
$this->assertSame(0, $data->count());
} else {
$this->assertEventNotTriggered($data, $expected);
$this->assertEventTriggered($data, $expected, true);
}
}

/**
* Verifies that there were no events during the test.
* Orphan events are included. Use `dontSeeOrphanEvent` to exclude them.
*
* ```php
* <?php
* $I->dontSeeEvent();
* $I->dontSeeEvent('App\MyEvent');
* $I->dontSeeEvent(new App\Events\MyEvent());
* $I->dontSeeEvent(['App\MyEvent', 'App\MyOtherEvent']);
* ```
*
* @param array|object|string|null $expected
*/
public function dontSeeEvent(array|object|string $expected = null): void
{
$eventCollector = $this->grabEventCollector(__FUNCTION__);

$data = [
$eventCollector->getOrphanedEvents(),
$eventCollector->getCalledListeners(),
];
$expected = is_array($expected) ? $expected : [$expected];

if ($expected === null) {
foreach ($data as $dataItem) {
$this->assertSame(0, $dataItem->count());
}
} else {
$this->assertEventTriggered($data, $expected, true);
}
}

Expand Down Expand Up @@ -123,6 +156,33 @@ public function seeOrphanEvent(array|object|string $expected): void
$this->assertEventTriggered($data, $expected);
}

/**
* Verifies that one or more events were dispatched during the test.
* Orphan events are included. Use `seeOrphanEvent` to exclude them.
*
* ```php
* <?php
* $I->seeEvent('App\MyEvent');
* $I->seeEvent(new App\Events\MyEvent());
* $I->seeEvent(['App\MyEvent', 'App\MyOtherEvent']);
* ```
*
* @param array|object|string $expected
* @see seeOrphanEvent
*/
public function seeEvent(array|object|string $expected): void
{
$eventCollector = $this->grabEventCollector(__FUNCTION__);

$data = [
$eventCollector->getOrphanedEvents(),
$eventCollector->getCalledListeners(),
];
$expected = is_array($expected) ? $expected : [$expected];

$this->assertEventTriggered($data, $expected);
}

/**
* Verifies that one or more event listeners were called during the test.
*
Expand Down Expand Up @@ -177,33 +237,38 @@ public function seeEventListenerIsCalled(
$this->assertListenerCalled($data, $expected, $withEvents);
}

protected function assertEventNotTriggered(Data $data, array $expected): void
{
$actual = $data->getValue(true);

foreach ($expected as $expectedEvent) {
$expectedEvent = is_object($expectedEvent) ? $expectedEvent::class : $expectedEvent;
$this->assertFalse(
$this->eventWasTriggered($actual, (string)$expectedEvent),
"The '{$expectedEvent}' event triggered"
);
}
}
protected function assertEventTriggered(
array|Data $data,
array $expected,
bool $invertAssertion = false
): void {
$assertTrue = !$invertAssertion;
$data = is_array($data) ? $data : [$data];
$totalEvents = array_sum(array_map('count', $data));

protected function assertEventTriggered(Data $data, array $expected): void
{
if ($data->count() === 0) {
if ($assertTrue && $totalEvents === 0) {
$this->fail('No event was triggered');
}

$actual = $data->getValue(true);
$actual = array_map(static fn (Data $data) => $data->getValue(true), $data);

foreach ($expected as $expectedEvent) {
$expectedEvent = is_object($expectedEvent) ? $expectedEvent::class : $expectedEvent;
$this->assertTrue(
$this->eventWasTriggered($actual, (string)$expectedEvent),
"The '{$expectedEvent}' event did not trigger"
);
$message = $assertTrue
? "The '{$expectedEvent}' event did not trigger"
: "The '{$expectedEvent}' event triggered";

$condition = false;

foreach ($actual as $actualEvents) {
$condition = $condition || $this->eventWasTriggered($actualEvents, (string)$expectedEvent);
}

if ($assertTrue) {
$this->assertTrue($condition, $message);
} else {
$this->assertFalse($condition, $message);
}
}
}

Expand Down Expand Up @@ -246,13 +311,11 @@ protected function eventWasTriggered(array $actual, string $expectedEvent): bool

foreach ($actual as $actualEvent) {
if (is_array($actualEvent)) { // Called Listeners
if (str_starts_with($actualEvent['pretty'], $expectedEvent)) {
$triggered = true;
}
} else { // Orphan Events
if ($actualEvent === $expectedEvent) {
if ($actualEvent['event'] === $expectedEvent) {
$triggered = true;
}
} elseif ($actualEvent === $expectedEvent) { // Orphan Events
$triggered = true;
}
}

Expand Down