Completed
Push — master ( d65720...005aea )
by Alexander
13:47
created

handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
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
    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
    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
    public function testAttachClosure()
36
    {
37
        $provider = new Provider();
38
        $provider->attach(function (Event $event) {
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

38
        $provider->attach(function (/** @scrutinizer ignore-unused */ Event $event) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
            // do nothing
40
        });
41
42
        $listeners = $provider->getListenersForEvent(new Event());
43
        $this->assertCount(1, $listeners);
44
    }
45
46
    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
    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)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

102
function handle(/** @scrutinizer ignore-unused */ Event $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
{
104
    // do nothing
105
}
106