Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

## 1.1.1 under development

- no changes in this release.
- Enh #24: Add `SimpleEventDispatcher::getEventClasses()` that return classes of dispatched events (vjik)

## 1.1.0 February 22, 2021

Expand Down
17 changes: 16 additions & 1 deletion src/EventDispatcher/SimpleEventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ final class SimpleEventDispatcher implements EventDispatcherInterface
/** @var array<int, Closure> */
private array $listeners;

/** @var object[] */
/**
* @var object[]
* @psalm-var list<object>
*/
private array $events = [];

/**
Expand All @@ -38,11 +41,23 @@ public function dispatch(object $event): object
return $event;
}

/**
* @return object[]
* @psalm-return list<object>
*/
public function getEvents(): array
{
return $this->events;
}

/**
* @psalm-return list<class-string>
*/
public function getEventClasses(): array
{
return array_map('\get_class', $this->events);
}

public function clearEvents(): void
{
$this->events = [];
Expand Down
16 changes: 16 additions & 0 deletions tests/EventDispatcher/SimpleEventDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use stdClass;
use Yiisoft\Test\Support\EventDispatcher\SimpleEventDispatcher;
use Yiisoft\Test\Support\Tests\EventDispatcher\Stub\StoppableEvent;

Expand Down Expand Up @@ -225,6 +226,21 @@ public function testGetEvents(): void
$this->assertSame([$event1, $event2, $event3], $dispatcher->getEvents());
}

public function testGetEventClasses(): void
{
$dispatcher = $this->prepareDispatcher();

$dispatcher->dispatch(new stdClass());
$dispatcher->dispatch(new DateTimeImmutable());
$dispatcher->dispatch(new DateTime());

self::assertSame([
stdClass::class,
DateTimeImmutable::class,
DateTime::class,
], $dispatcher->getEventClasses());
}

public function testGetEmptyEvents(): void
{
$dispatcher = $this->prepareDispatcher();
Expand Down