1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\EventDispatcher\Tests\Provider; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Yiisoft\EventDispatcher\Provider\ConcreteProvider; |
7
|
|
|
use Yiisoft\EventDispatcher\Tests\Event\ClassInterface; |
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
|
|
|
|
13
|
|
|
use function array_slice; |
14
|
|
|
|
15
|
|
|
final class ConcreteProviderTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function testListenerForEventIsReturned(): void |
18
|
|
|
{ |
19
|
|
|
$provider = $this->createConcreteProvider(); |
20
|
|
|
|
21
|
|
|
$listener = fn () => null; |
22
|
|
|
|
23
|
|
|
$provider->attach(Event::class, $listener); |
24
|
|
|
|
25
|
|
|
$listeners = $provider->getListenersForEvent(new Event()); |
26
|
|
|
|
27
|
|
|
$listeners = \iterator_to_array($listeners, false); |
28
|
|
|
$this->assertCount(1, $listeners); |
29
|
|
|
$this->assertContains($listener, $listeners); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testDetachListenersForEventAreDetached(): void |
33
|
|
|
{ |
34
|
|
|
$provider = $this->createConcreteProvider(); |
35
|
|
|
|
36
|
|
|
$provider->attach(Event::class, fn () => null); |
37
|
|
|
$provider->detach(Event::class); |
38
|
|
|
|
39
|
|
|
$listeners = $provider->getListenersForEvent(new Event()); |
40
|
|
|
|
41
|
|
|
$this->assertCount(0, $listeners); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testListenersForClassHierarchyAreReturned(): void |
45
|
|
|
{ |
46
|
|
|
$provider = $this->createConcreteProvider(); |
47
|
|
|
|
48
|
|
|
$provider->attach(ParentInterface::class, function (ParentInterface $parentInterface) { |
49
|
|
|
$parentInterface->register('parent interface'); |
50
|
|
|
}); |
51
|
|
|
$provider->attach(ParentClass::class, function (ParentClass $parentClass) { |
52
|
|
|
$parentClass->register('parent class'); |
53
|
|
|
}); |
54
|
|
|
$provider->attach(ClassInterface::class, function (ClassInterface $classInterface) { |
55
|
|
|
$classInterface->register('class interface'); |
56
|
|
|
}); |
57
|
|
|
$provider->attach(ClassItself::class, function (ClassItself $classItself) { |
58
|
|
|
$classItself->register('class itself'); |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
$event = new ClassItself(); |
62
|
|
|
foreach ($provider->getListenersForEvent($event) as $listener) { |
63
|
|
|
$listener($event); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$classHierarchyHandlers = array_slice($event->registered(), 0, 2); |
67
|
|
|
$interfaceHandlers = array_slice($event->registered(), 2); |
68
|
|
|
|
69
|
|
|
$this->assertEquals( |
70
|
|
|
[ |
71
|
|
|
'class itself', |
72
|
|
|
'parent class', |
73
|
|
|
], |
74
|
|
|
$classHierarchyHandlers |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$this->assertContains('class interface', $interfaceHandlers); |
78
|
|
|
$this->assertContains('parent interface', $interfaceHandlers); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function createConcreteProvider(): ConcreteProvider |
82
|
|
|
{ |
83
|
|
|
return new ConcreteProvider(); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|