TestEventListener   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 9
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 2
c 1
b 0
f 0
dl 0
loc 9
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A on() 0 2 1
A supports() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 *  This file is part of the Micro framework package.
7
 *
8
 *  (c) Stanislau Komar <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Micro\Plugin\EventEmitter\Test\Unit\Business\Provider;
15
16
use Micro\Component\DependencyInjection\Autowire\AutowireHelperInterface;
17
use Micro\Component\EventEmitter\EventInterface;
18
use Micro\Component\EventEmitter\EventListenerInterface;
19
use Micro\Plugin\EventEmitter\Business\Provider\ApplicationListenerProvider;
20
use PHPUnit\Framework\MockObject\MockObject;
21
use PHPUnit\Framework\TestCase;
22
23
class ApplicationListenerProviderTest extends TestCase
24
{
25
    private ApplicationListenerProvider $listener;
26
27
    protected function setUp(): void
28
    {
29
        $autowireHelper = $this->createMock(AutowireHelperInterface::class);
30
        $autowireHelper
31
            ->method('autowire')
32
            ->with(TestEventListener::class)
33
            ->willReturn(fn () => new TestEventListener());
34
35
        $listenersClasses = new \ArrayObject([
36
            TestEventListener::class,
37
        ]);
38
39
        $this->listener = new ApplicationListenerProvider(
40
            $autowireHelper,
41
            $listenersClasses,
42
        );
43
    }
44
45
    public function testGetName()
46
    {
47
        $this->assertIsString($this->listener->getName());
48
    }
49
50
    public function testGetListenersForEvent()
51
    {
52
        $evt = $this->createMock(EventInterface::class);
53
54
        foreach ($this->listener->getListenersForEvent($evt) as $listener) {
55
            $this->assertInstanceOf(TestEventListener::class, $listener);
56
        }
57
    }
58
59
    public function testGetListenersEmptyForEvent()
60
    {
61
        $evt = new class() implements EventInterface {};
62
        $empty = [];
63
        foreach ($this->listener->getListenersForEvent($evt) as $evt) {
64
            $empty[] = $evt;
65
        }
66
67
        $this->assertEmpty($empty);
68
    }
69
70
    public function testToString()
71
    {
72
        $name = $this->listener->getName();
73
        $this->assertEquals($name, (string) $this->listener);
74
    }
75
76
    public function testGetEventListeners()
77
    {
78
        $events = $this->listener->getEventListeners();
79
        foreach ($events as $class) {
80
            $this->assertEquals(TestEventListener::class, $class);
81
        }
82
    }
83
}
84
85
class TestEventListener implements EventListenerInterface
86
{
87
    public function on(EventInterface $event): void
88
    {
89
    }
90
91
    public static function supports(EventInterface $event): bool
92
    {
93
        return $event instanceof MockObject;
94
    }
95
}
96