Skip to content

Added orphan events assertions #111

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 1 commit into from
Feb 3, 2021
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
54 changes: 54 additions & 0 deletions src/Codeception/Module/Symfony/EventsAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,33 @@

trait EventsAssertionsTrait
{
/**
* Verifies that one or more orphan events were not dispatched during the test.
*
* An orphan event is an event that is triggered by manually executing the
* [`dispatch()`](https://symfony.com/doc/current/components/event_dispatcher.html#dispatch-the-event) method
* of the EventDispatcher, in other words, it is an event that is not handled by any listener.
*
* ```php
* <?php
* $I->dontSeeOrphanEventTriggered('App\MyEvent');
* $I->dontSeeOrphanEventTriggered(new App\Events\MyEvent());
* $I->dontSeeOrphanEventTriggered(['App\MyEvent', 'App\MyOtherEvent']);
* ```
*
* @param string|object|string[] $expected
*/
public function dontSeeOrphanEventTriggered($expected): void
{
$eventCollector = $this->grabEventCollector(__FUNCTION__);

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

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

/**
* Verifies that one or more event listeners were not called during the test.
*
Expand All @@ -36,6 +63,33 @@ public function dontSeeEventTriggered($expected): void
$this->assertEventNotTriggered($data, $expected);
}

/**
* Verifies that one or more orphan events were dispatched during the test.
*
* An orphan event is an event that is triggered by manually executing the
* [`dispatch()`](https://symfony.com/doc/current/components/event_dispatcher.html#dispatch-the-event) method
* of the EventDispatcher, in other words, it is an event that is not handled by any listener.
*
* ```php
* <?php
* $I->seeOrphanEventTriggered('App\MyEvent');
* $I->seeOrphanEventTriggered(new App\Events\MyEvent());
* $I->seeOrphanEventTriggered(['App\MyEvent', 'App\MyOtherEvent']);
* ```
*
* @param string|object|string[] $expected
*/
public function seeOrphanEventTriggered($expected): void
{
$eventCollector = $this->grabEventCollector(__FUNCTION__);

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

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

/**
* Verifies that one or more event listeners were called during the test.
*
Expand Down