Skip to content
Merged
Changes from all commits
Commits
File filter

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