1
|
|
|
<?php |
2
|
|
|
namespace Yii\EventDispatcher\Tests; |
3
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
5
|
|
|
use Psr\EventDispatcher\ListenerProviderInterface; |
6
|
|
|
use Psr\EventDispatcher\StoppableEventInterface; |
7
|
|
|
use Yii\EventDispatcher\Dispatcher; |
8
|
|
|
|
9
|
|
|
class DispatcherTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
public function testCallsAllListeners() |
12
|
|
|
{ |
13
|
|
|
$event = new class { |
14
|
|
|
public $listeners = []; |
15
|
|
|
}; |
16
|
|
|
|
17
|
|
|
$provider = new class implements ListenerProviderInterface { |
18
|
|
|
public function getListenersForEvent(object $event): iterable |
19
|
|
|
{ |
20
|
|
|
yield function ($event) { $event->listeners[] = 1; }; |
21
|
|
|
yield function ($event) { $event->listeners[] = 2; }; |
22
|
|
|
yield function ($event) { $event->listeners[] = 3; }; |
23
|
|
|
} |
24
|
|
|
}; |
25
|
|
|
|
26
|
|
|
$dispatcher = new Dispatcher($provider); |
27
|
|
|
$dispatcher->dispatch($event); |
|
|
|
|
28
|
|
|
|
29
|
|
|
$this->assertEquals([1, 2, 3], $event->listeners); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testPropagationStops() |
33
|
|
|
{ |
34
|
|
|
$event = new class implements StoppableEventInterface { |
35
|
|
|
public $listeners = []; |
36
|
|
|
|
37
|
|
|
private $isPropagationStopped = false; |
38
|
|
|
|
39
|
|
|
public function isPropagationStopped(): bool |
40
|
|
|
{ |
41
|
|
|
return $this->isPropagationStopped; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function stopPropagation(): void |
45
|
|
|
{ |
46
|
|
|
$this->isPropagationStopped = true; |
47
|
|
|
} |
48
|
|
|
}; |
49
|
|
|
|
50
|
|
|
$provider = new class implements ListenerProviderInterface { |
51
|
|
|
public function getListenersForEvent(object $event): iterable |
52
|
|
|
{ |
53
|
|
|
yield function ($event) { |
54
|
|
|
$event->listeners[] = 1; |
55
|
|
|
$event->stopPropagation(); |
56
|
|
|
}; |
57
|
|
|
yield function ($event) { $event->listeners[] = 2; }; |
58
|
|
|
yield function ($event) { $event->listeners[] = 3; }; |
59
|
|
|
} |
60
|
|
|
}; |
61
|
|
|
|
62
|
|
|
$dispatcher = new Dispatcher($provider); |
63
|
|
|
$dispatcher->dispatch($event); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$this->assertEquals([1], $event->listeners); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: