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