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