Skip to content
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

Add seeEvent/dontSeeEvent event assertions #173

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Changes from all commits
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
117 changes: 91 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.
* Both regular and orphan events are checked.
*
* ```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,35 @@ public function seeOrphanEvent(array|object|string $expected): void
$this->assertEventTriggered($data, $expected);
}

/**
* Verifies that one or more events were dispatched during the test.
* Both regular and orphan events are checked.
*
* If you need to verify that expected event is not orphan,
* add `dontSeeOrphanEvent` call.
*
* ```php
* <?php
* $I->seeEvent('App\MyEvent');
* $I->seeEvent(new App\Events\MyEvent());
* $I->seeEvent(['App\MyEvent', 'App\MyOtherEvent']);
* ```
*
* @param array|object|string $expected
*/
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 +239,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);
$actualEventsCollection = 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";

$eventTriggered = false;

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

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

Expand Down Expand Up @@ -246,13 +313,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
Loading