1
|
|
|
<?php |
2
|
|
|
namespace Yii\EventDispatcher\Tests\Provider; |
3
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
5
|
|
|
use Yii\EventDispatcher\Provider\Provider; |
6
|
|
|
use Yii\EventDispatcher\Tests\EventListener; |
7
|
|
|
use Yii\EventDispatcher\Tests\InvokableEventListener; |
8
|
|
|
use Yii\EventDispatcher\Tests\CustomEvent; |
9
|
|
|
use Yii\EventDispatcher\Tests\StaticEventListener; |
10
|
|
|
|
11
|
|
|
class ProviderTest extends TestCase |
12
|
|
|
{ |
13
|
|
View Code Duplication |
public function testAttachCallableArray() |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
$provider = new Provider(); |
16
|
|
|
$provider->attach([StaticEventListener::class, 'handle']); |
17
|
|
|
|
18
|
|
|
$listeners = $provider->getListenersForEvent(new CustomEvent()); |
|
|
|
|
19
|
|
|
$this->assertCount(1, $listeners); |
20
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
View Code Duplication |
public function testAttachCallableFunction() |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$provider = new Provider(); |
26
|
|
|
$provider->attach('Yii\EventDispatcher\Tests\Provider\handle'); |
27
|
|
|
|
28
|
|
|
$listeners = $provider->getListenersForEvent(new CustomEvent()); |
|
|
|
|
29
|
|
|
$this->assertCount(1, $listeners); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
View Code Duplication |
public function testAttachClosure() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$provider = new Provider(); |
35
|
|
|
$provider->attach(function (CustomEvent $event) {}); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$listeners = $provider->getListenersForEvent(new CustomEvent()); |
|
|
|
|
38
|
|
|
$this->assertCount(1, $listeners); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function testAttachCallableObject() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$provider = new Provider(); |
44
|
|
|
$provider->attach([new EventListener(), 'handle']); |
45
|
|
|
|
46
|
|
|
$listeners = $provider->getListenersForEvent(new CustomEvent()); |
|
|
|
|
47
|
|
|
$this->assertCount(1, $listeners); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
public function testInvokable() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$provider = new Provider(); |
53
|
|
|
$provider->attach(new InvokableEventListener()); |
54
|
|
|
|
55
|
|
|
$listeners = $provider->getListenersForEvent(new CustomEvent()); |
|
|
|
|
56
|
|
|
$this->assertCount(1, $listeners); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
function handle(CustomEvent $event) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
// do nothing |
63
|
|
|
} |
64
|
|
|
|
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.