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