1
|
|
|
<?php |
2
|
|
|
namespace Yiisoft\EventDispatcher\Tests\Provider; |
3
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
5
|
|
|
use Yiisoft\EventDispatcher\Provider\Provider; |
6
|
|
|
use Yiisoft\EventDispatcher\Tests\Event\ClassItself; |
7
|
|
|
use Yiisoft\EventDispatcher\Tests\Event\Event; |
8
|
|
|
use Yiisoft\EventDispatcher\Tests\Event\ParentClass; |
9
|
|
|
use Yiisoft\EventDispatcher\Tests\Event\ParentInterface; |
10
|
|
|
use Yiisoft\EventDispatcher\Tests\Event\ClassInterface; |
11
|
|
|
use Yiisoft\EventDispatcher\Tests\Listener\Invokable; |
12
|
|
|
use Yiisoft\EventDispatcher\Tests\Listener\NonStatic; |
13
|
|
|
use Yiisoft\EventDispatcher\Tests\Listener\WithStaticMethod; |
14
|
|
|
|
15
|
|
|
class ProviderTest extends TestCase |
16
|
|
|
{ |
17
|
|
View Code Duplication |
public function testAttachCallableArray() |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$provider = new Provider(); |
20
|
|
|
$provider->attach([WithStaticMethod::class, 'handle']); |
21
|
|
|
|
22
|
|
|
$listeners = $provider->getListenersForEvent(new Event()); |
23
|
|
|
$this->assertCount(1, $listeners); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
View Code Duplication |
public function testAttachCallableFunction() |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$provider = new Provider(); |
29
|
|
|
$provider->attach('Yiisoft\EventDispatcher\Tests\Provider\handle'); |
30
|
|
|
|
31
|
|
|
$listeners = $provider->getListenersForEvent(new Event()); |
32
|
|
|
$this->assertCount(1, $listeners); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
View Code Duplication |
public function testAttachClosure() |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$provider = new Provider(); |
38
|
|
|
$provider->attach(function (Event $event) { |
|
|
|
|
39
|
|
|
// do nothing |
40
|
|
|
}); |
41
|
|
|
|
42
|
|
|
$listeners = $provider->getListenersForEvent(new Event()); |
43
|
|
|
$this->assertCount(1, $listeners); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
View Code Duplication |
public function testAttachCallableObject() |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$provider = new Provider(); |
49
|
|
|
$provider->attach([new NonStatic(), 'handle']); |
50
|
|
|
|
51
|
|
|
$listeners = $provider->getListenersForEvent(new Event()); |
52
|
|
|
$this->assertCount(1, $listeners); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
public function testInvokable() |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$provider = new Provider(); |
58
|
|
|
$provider->attach(new Invokable()); |
59
|
|
|
|
60
|
|
|
$listeners = $provider->getListenersForEvent(new Event()); |
61
|
|
|
$this->assertCount(1, $listeners); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testListenersForClassHierarchyAreReturned() |
65
|
|
|
{ |
66
|
|
|
$provider = new Provider(); |
67
|
|
|
|
68
|
|
|
$provider->attach(function (ParentInterface $parentInterface) { |
69
|
|
|
$parentInterface->register('parent interface'); |
70
|
|
|
}); |
71
|
|
|
$provider->attach(function (ParentClass $parentClass) { |
72
|
|
|
$parentClass->register('parent class'); |
73
|
|
|
}); |
74
|
|
|
$provider->attach(function (ClassInterface $classInterface) { |
75
|
|
|
$classInterface->register('class interface'); |
76
|
|
|
}); |
77
|
|
|
$provider->attach(function (ClassItself $classItself) { |
78
|
|
|
$classItself->register('class itself'); |
79
|
|
|
}); |
80
|
|
|
|
81
|
|
|
$event = new ClassItself(); |
82
|
|
|
foreach ($provider->getListenersForEvent($event) as $listener) { |
83
|
|
|
$listener($event); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$classHierarchyHandlers = array_slice($event->registered(), 0, 2); |
87
|
|
|
$interfaceHandlers = array_slice($event->registered(), 2); |
88
|
|
|
|
89
|
|
|
$this->assertEquals( |
90
|
|
|
[ |
91
|
|
|
'class itself', |
92
|
|
|
'parent class' |
93
|
|
|
], |
94
|
|
|
$classHierarchyHandlers |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$this->assertContains('class interface', $interfaceHandlers); |
98
|
|
|
$this->assertContains('parent interface', $interfaceHandlers); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
function handle(Event $event) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
// do nothing |
105
|
|
|
} |
106
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.