1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\EventDispatcher\Tests\Dispatcher; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Psr\EventDispatcher\ListenerProviderInterface; |
7
|
|
|
use Yiisoft\EventDispatcher\Dispatcher\Dispatcher; |
8
|
|
|
use Yiisoft\EventDispatcher\Tests\Event\Event; |
9
|
|
|
use Yiisoft\EventDispatcher\Tests\Event\StoppableEvent; |
10
|
|
|
|
11
|
|
|
class DispatcherTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
public function testCallsAllListeners(): void |
14
|
|
|
{ |
15
|
|
|
$event = new Event(); |
16
|
|
|
|
17
|
|
|
$provider = new class() implements ListenerProviderInterface { |
18
|
|
|
public function getListenersForEvent(object $event): iterable |
19
|
|
|
{ |
20
|
|
|
yield function (Event $event) { |
21
|
|
|
$event->register(1); |
22
|
|
|
}; |
23
|
|
|
yield function (Event $event) { |
24
|
|
|
$event->register(2); |
25
|
|
|
}; |
26
|
|
|
yield function (Event $event) { |
27
|
|
|
$event->register(3); |
28
|
|
|
}; |
29
|
|
|
} |
30
|
|
|
}; |
31
|
|
|
|
32
|
|
|
$dispatcher = new Dispatcher($provider); |
33
|
|
|
$dispatcher->dispatch($event); |
34
|
|
|
|
35
|
|
|
$this->assertEquals([1, 2, 3], $event->registered()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testPropagationStops(): void |
39
|
|
|
{ |
40
|
|
|
$event = new StoppableEvent(); |
41
|
|
|
|
42
|
|
|
$provider = new class() implements ListenerProviderInterface { |
43
|
|
|
public function getListenersForEvent(object $event): iterable |
44
|
|
|
{ |
45
|
|
|
yield function (StoppableEvent $event) { |
46
|
|
|
$event->register(1); |
47
|
|
|
$event->stopPropagation(); |
48
|
|
|
}; |
49
|
|
|
yield function (StoppableEvent $event) { |
50
|
|
|
$event->register(2); |
51
|
|
|
}; |
52
|
|
|
yield function (StoppableEvent $event) { |
53
|
|
|
$event->register(3); |
54
|
|
|
}; |
55
|
|
|
} |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
$dispatcher = new Dispatcher($provider); |
59
|
|
|
$dispatcher->dispatch($event); |
60
|
|
|
|
61
|
|
|
$this->assertEquals([1], $event->registered()); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|