1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\EventDispatcher\Tests\Provider; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Yiisoft\EventDispatcher\Provider\Provider; |
8
|
|
|
use Yiisoft\EventDispatcher\Tests\Event\ClassItself; |
9
|
|
|
use Yiisoft\EventDispatcher\Tests\Event\Event; |
10
|
|
|
use Yiisoft\EventDispatcher\Tests\Event\ParentClass; |
11
|
|
|
use Yiisoft\EventDispatcher\Tests\Event\ParentInterface; |
12
|
|
|
use Yiisoft\EventDispatcher\Tests\Event\ClassInterface; |
13
|
|
|
use Yiisoft\EventDispatcher\Tests\Listener\Invokable; |
14
|
|
|
use Yiisoft\EventDispatcher\Tests\Listener\NonStatic; |
15
|
|
|
use Yiisoft\EventDispatcher\Tests\Listener\WithStaticMethod; |
16
|
|
|
|
17
|
|
|
class ProviderTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Provider |
21
|
|
|
*/ |
22
|
|
|
private Provider $provider; |
23
|
|
|
|
24
|
|
|
protected function setUp(): void |
25
|
|
|
{ |
26
|
|
|
parent::setUp(); |
27
|
|
|
|
28
|
|
|
$this->provider = new Provider(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testAttachCallableArray() |
32
|
|
|
{ |
33
|
|
|
$this->provider->attach([WithStaticMethod::class, 'handle']); |
34
|
|
|
|
35
|
|
|
$listeners = $this->provider->getListenersForEvent(new Event()); |
36
|
|
|
$this->assertCount(1, $listeners); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testAttachCallableFunction() |
40
|
|
|
{ |
41
|
|
|
$this->provider->attach('Yiisoft\EventDispatcher\Tests\Provider\handle'); |
42
|
|
|
|
43
|
|
|
$listeners = $this->provider->getListenersForEvent(new Event()); |
44
|
|
|
$this->assertCount(1, $listeners); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testAttachClosure() |
48
|
|
|
{ |
49
|
|
|
$this->provider->attach(function (Event $event) { |
|
|
|
|
50
|
|
|
// do nothing |
51
|
|
|
}); |
52
|
|
|
|
53
|
|
|
$listeners = $this->provider->getListenersForEvent(new Event()); |
54
|
|
|
$this->assertCount(1, $listeners); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testAttachCallableObject() |
58
|
|
|
{ |
59
|
|
|
$this->provider->attach([new NonStatic(), 'handle']); |
60
|
|
|
|
61
|
|
|
$listeners = $this->provider->getListenersForEvent(new Event()); |
62
|
|
|
$this->assertCount(1, $listeners); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testInvokable() |
66
|
|
|
{ |
67
|
|
|
$this->provider->attach(new Invokable()); |
68
|
|
|
|
69
|
|
|
$listeners = $this->provider->getListenersForEvent(new Event()); |
70
|
|
|
$this->assertCount(1, $listeners); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testListenersForClassHierarchyAreReturned() |
74
|
|
|
{ |
75
|
|
|
$this->provider->attach(function (ParentInterface $parentInterface) { |
76
|
|
|
$parentInterface->register('parent interface'); |
77
|
|
|
}); |
78
|
|
|
$this->provider->attach(function (ParentClass $parentClass) { |
79
|
|
|
$parentClass->register('parent class'); |
80
|
|
|
}); |
81
|
|
|
$this->provider->attach(function (ClassInterface $classInterface) { |
82
|
|
|
$classInterface->register('class interface'); |
83
|
|
|
}); |
84
|
|
|
$this->provider->attach(function (ClassItself $classItself) { |
85
|
|
|
$classItself->register('class itself'); |
86
|
|
|
}); |
87
|
|
|
|
88
|
|
|
$event = new ClassItself(); |
89
|
|
|
foreach ($this->provider->getListenersForEvent($event) as $listener) { |
90
|
|
|
$listener($event); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$classHierarchyHandlers = array_slice($event->registered(), 0, 2); |
94
|
|
|
$interfaceHandlers = array_slice($event->registered(), 2); |
95
|
|
|
|
96
|
|
|
$this->assertEquals( |
97
|
|
|
[ |
98
|
|
|
'class itself', |
99
|
|
|
'parent class' |
100
|
|
|
], |
101
|
|
|
$classHierarchyHandlers |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
$this->assertContains('class interface', $interfaceHandlers); |
105
|
|
|
$this->assertContains('parent interface', $interfaceHandlers); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testDetachListenersForEventAreDetached(): void |
109
|
|
|
{ |
110
|
|
|
$this->provider->attach(fn (Event $event) => null); |
|
|
|
|
111
|
|
|
$this->provider->detach(Event::class); |
112
|
|
|
|
113
|
|
|
$listeners = $this->provider->getListenersForEvent(new Event()); |
114
|
|
|
|
115
|
|
|
$this->assertCount(0, $listeners); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testListenerWithNoParameterThrowsException(): void |
119
|
|
|
{ |
120
|
|
|
$this->expectException(InvalidArgumentException::class); |
121
|
|
|
$this->expectExceptionMessage('Listeners must declare an object type they can accept.'); |
122
|
|
|
|
123
|
|
|
$this->provider->attach(fn () => null); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
function handle(Event $event) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
// do nothing |
130
|
|
|
} |
131
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.