Skip to content

Commit 2ea94f6

Browse files
committed
Add tests for the event dispatcher.
1 parent e764f96 commit 2ea94f6

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the aether/aether.
5+
*
6+
* Copyright (C) 2024 Dominik Szamburski
7+
*
8+
* This software may be modified and distributed under the terms
9+
* of the MIT license. See the LICENSE file for details.
10+
*/
11+
12+
namespace Aether\Tests\Events;
13+
14+
use Aether\Events\Event;
15+
use Aether\Events\EventDispatcher;
16+
use Aether\Events\EventDispatcherInterface;
17+
use PHPUnit\Framework\Attributes\CoversClass;
18+
use PHPUnit\Framework\TestCase;
19+
20+
#[CoversClass(EventDispatcher::class)]
21+
class EventDispatcherTest extends TestCase
22+
{
23+
private EventDispatcherInterface $dispatcher;
24+
25+
protected function setUp(): void
26+
{
27+
$this->dispatcher = new EventDispatcher();
28+
}
29+
30+
public function testDispatchesWithoutListeners(): void
31+
{
32+
$listeners = $this->dispatcher->getListeners('event');
33+
34+
self::assertEmpty($listeners);
35+
self::assertCount(0, $listeners);
36+
}
37+
38+
public function testDispatchShouldInvokeListener(): void
39+
{
40+
$invoked = false;
41+
42+
$this->dispatcher->listen('test.event', function () use (&$invoked) {
43+
$invoked = true;
44+
});
45+
46+
$this->dispatcher->dispatch(new class () {}, 'test.event');
47+
48+
self::assertTrue($invoked, 'Listener should be invoked on event dispatch.');
49+
}
50+
51+
public function testDispatchShouldNotInvokeListenerAfterPropagationStopped(): void
52+
{
53+
$invokedAfterStop = false;
54+
55+
$this->dispatcher->listen('test.event', function (Event $event) {
56+
$event->setPropagationStopped();
57+
});
58+
59+
$this->dispatcher->listen('test.event', function () use (&$invokedAfterStop) {
60+
$invokedAfterStop = true;
61+
});
62+
63+
$this->dispatcher->dispatch(new Event(), 'test.event');
64+
65+
self::assertFalse($invokedAfterStop, 'Listener should not be invoked after propagation is stopped');
66+
}
67+
}

0 commit comments

Comments
 (0)