Passed
Pull Request — master (#15)
by Alexander
01:58 queued 41s
created

testListenerWithNoParameterThrowsException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Yiisoft\EventDispatcher\Tests\Provider;
4
5
use InvalidArgumentException;
6
use PHPUnit\Framework\TestCase;
7
use Yiisoft\EventDispatcher\Provider\Provider;
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
use Yiisoft\EventDispatcher\Tests\Event\ClassInterface;
13
use Yiisoft\EventDispatcher\Tests\Listener\Invokable;
14
use Yiisoft\EventDispatcher\Tests\Listener\NonStatic;
15
use Yiisoft\EventDispatcher\Tests\Listener\WithStaticMethod;
16
17
final class ProviderTest extends TestCase
18
{
19
    public function testAttachCallableArray(): void
20
    {
21
        $this->provider->attach([WithStaticMethod::class, 'handle']);
0 ignored issues
show
Bug Best Practice introduced by
The property provider does not exist on Yiisoft\EventDispatcher\...s\Provider\ProviderTest. Did you maybe forget to declare it?
Loading history...
22
23
        $listeners = $this->provider->getListenersForEvent(new Event());
24
        $this->assertCount(1, $listeners);
25
    }
26
27
    public function testAttachCallableFunction(): void
28
    {
29
        $this->provider->attach('Yiisoft\EventDispatcher\Tests\Provider\handle');
0 ignored issues
show
Bug Best Practice introduced by
The property provider does not exist on Yiisoft\EventDispatcher\...s\Provider\ProviderTest. Did you maybe forget to declare it?
Loading history...
30
31
        $listeners = $this->provider->getListenersForEvent(new Event());
32
        $this->assertCount(1, $listeners);
33
    }
34
35
    public function testAttachClosure(): void
36
    {
37
        $this->provider->attach(function (Event $event) {
0 ignored issues
show
Bug Best Practice introduced by
The property provider does not exist on Yiisoft\EventDispatcher\...s\Provider\ProviderTest. Did you maybe forget to declare it?
Loading history...
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

37
        $this->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...
38
            // do nothing
39
        });
40
41
        $listeners = $this->provider->getListenersForEvent(new Event());
42
        $this->assertCount(1, $listeners);
43
    }
44
45
    public function testAttachCallableObject(): void
46
    {
47
        $this->provider->attach([new NonStatic(), 'handle']);
0 ignored issues
show
Bug Best Practice introduced by
The property provider does not exist on Yiisoft\EventDispatcher\...s\Provider\ProviderTest. Did you maybe forget to declare it?
Loading history...
48
49
        $listeners = $this->provider->getListenersForEvent(new Event());
50
        $this->assertCount(1, $listeners);
51
    }
52
53
    public function testInvokable(): void
54
    {
55
        $this->provider->attach(new Invokable());
0 ignored issues
show
Bug Best Practice introduced by
The property provider does not exist on Yiisoft\EventDispatcher\...s\Provider\ProviderTest. Did you maybe forget to declare it?
Loading history...
56
57
        $listeners = $this->provider->getListenersForEvent(new Event());
58
        $this->assertCount(1, $listeners);
59
    }
60
61
    public function testListenersForClassHierarchyAreReturned(): void
62
    {
63
        $this->provider->attach(function (ParentInterface $parentInterface) {
0 ignored issues
show
Bug Best Practice introduced by
The property provider does not exist on Yiisoft\EventDispatcher\...s\Provider\ProviderTest. Did you maybe forget to declare it?
Loading history...
64
            $parentInterface->register('parent interface');
65
        });
66
        $this->provider->attach(function (ParentClass $parentClass) {
67
            $parentClass->register('parent class');
68
        });
69
        $this->provider->attach(function (ClassInterface $classInterface) {
70
            $classInterface->register('class interface');
71
        });
72
        $this->provider->attach(function (ClassItself $classItself) {
73
            $classItself->register('class itself');
74
        });
75
76
        $event = new ClassItself();
77
        foreach ($this->provider->getListenersForEvent($event) as $listener) {
78
            $listener($event);
79
        }
80
81
        $classHierarchyHandlers = array_slice($event->registered(), 0, 2);
82
        $interfaceHandlers = array_slice($event->registered(), 2);
83
84
        $this->assertEquals(
85
            [
86
                'class itself',
87
                'parent class'
88
            ],
89
            $classHierarchyHandlers
90
        );
91
92
        $this->assertContains('class interface', $interfaceHandlers);
93
        $this->assertContains('parent interface', $interfaceHandlers);
94
    }
95
96
    public function testDetachListenersForEventAreDetached(): void
97
    {
98
        $this->provider->attach(fn (Event $event) => null);
0 ignored issues
show
Bug Best Practice introduced by
The property provider does not exist on Yiisoft\EventDispatcher\...s\Provider\ProviderTest. Did you maybe forget to declare it?
Loading history...
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

98
        $this->provider->attach(fn (/** @scrutinizer ignore-unused */ Event $event) => null);

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...
99
        $this->provider->detach(Event::class);
100
101
        $listeners = $this->provider->getListenersForEvent(new Event());
102
103
        $this->assertCount(0, $listeners);
104
    }
105
106
    public function testListenerWithNoParameterThrowsException(): void
107
    {
108
        $this->expectException(InvalidArgumentException::class);
109
        $this->expectExceptionMessage('Listeners must declare an object type they can accept.');
110
111
        $this->provider->attach(fn () => null);
0 ignored issues
show
Bug Best Practice introduced by
The property provider does not exist on Yiisoft\EventDispatcher\...s\Provider\ProviderTest. Did you maybe forget to declare it?
Loading history...
112
    }
113
}
114
115
function handle(Event $event): void
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

115
function handle(/** @scrutinizer ignore-unused */ Event $event): void

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...
116
{
117
    // do nothing
118
}
119