getLazyListenerWillCreateAndDispatchEventData()
last analyzed

Size

Total Lines 47
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 47
nc 1
nop 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A LazyListenerTest.php$2 ➔ create() 0 3 1
A LazyListenerTest.php$1 ➔ doSomething() 0 3 1
A LazyListenerTest.php$2 ➔ __construct() 0 3 1
A LazyListenerTest.php$0 ➔ create() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\EventDispatcher\Listener;
6
7
use Arp\EventDispatcher\Listener\Exception\EventListenerException;
8
use Arp\EventDispatcher\Listener\LazyListener;
0 ignored issues
show
Bug introduced by
The type Arp\EventDispatcher\Listener\LazyListener was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use PHPUnit\Framework\ExpectationFailedException;
10
use PHPUnit\Framework\TestCase;
11
12
/**
13
 * @covers \Arp\EventDispatcher\Listener\LazyListener
14
 */
15
final class LazyListenerTest extends TestCase
16
{
17
    public function testIsCallable(): void
18
    {
19
        $listener = new LazyListener(
20
            static function () {
21
            }
22
        );
23
24
        $this->assertIsCallable($listener);
25
    }
26
27
    /**
28
     * @throws EventListenerException
29
     */
30
    public function testInvokeWillThrowEventListenerExceptionIfTheFactoryMethodIsNotCallable(): void
31
    {
32
        $event = new \stdClass();
33
        $factory = new \stdClass();
34
35
        $lazyListener = new LazyListener($factory);
36
37
        $this->expectException(EventListenerException::class);
38
        $this->expectExceptionMessage(
39
            sprintf(
40
                'The method \'%s\' is not callable for lazy load factory \'%s\'',
41
                '__invoke',
42
                'array'
43
            )
44
        );
45
46
        $lazyListener($event);
47
    }
48
49
    /**
50
     * @throws EventListenerException
51
     */
52
    public function testInvokeWillThrowEventListenerExceptionIfTheListenerMethodIsNotCallable(): void
53
    {
54
        $event = new \stdClass();
55
        $listener = new \stdClass();
56
        $factory = static fn () => $listener;
57
58
        $lazyListener = new LazyListener($factory);
59
60
        $this->expectException(EventListenerException::class);
61
        $this->expectExceptionMessage(
62
            sprintf(
63
                'The method \'%s\' is not callable for lazy load event listener \'%s\'',
64
                '__invoke',
65
                'array'
66
            )
67
        );
68
69
        $lazyListener($event);
70
    }
71
72
    /**
73
     * @throws EventListenerException
74
     * @throws ExpectationFailedException
75
     *
76
     * @dataProvider getLazyListenerWillCreateAndDispatchEventData
77
     */
78
    public function testLazyListenerWillCreateAndDispatchEvent(
79
        mixed $expected,
80
        mixed $factory,
81
        ?string $factoryMethod = null,
82
        ?string $listenerMethod = null
83
    ): void {
84
        $event = new \stdClass();
85
        $lazyListener = new LazyListener($factory, $factoryMethod, $listenerMethod);
86
87
        $this->assertSame($expected, $lazyListener($event));
88
    }
89
90
    /**
91
     * @return array<mixed>
92
     */
93
    public function getLazyListenerWillCreateAndDispatchEventData(): array
94
    {
95
        $factory1 = new class () {
96
            public function create(): callable
97
            {
98
                return static fn () => 'hello123';
99
            }
100
        };
101
102
        $listener1 = new class () {
103
            public function doSomething(object $event): string
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

103
            public function doSomething(/** @scrutinizer ignore-unused */ object $event): string

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...
104
            {
105
                return 'test123';
106
            }
107
        };
108
109
        $factory2 = new class ($listener1) {
110
            private object $listener;
111
112
            public function __construct(object $listener)
113
            {
114
                $this->listener = $listener;
115
            }
116
117
            public function create(): object
118
            {
119
                return $this->listener;
120
            }
121
        };
122
123
        return [
124
            [
125
                'hello123',
126
                static fn () => static fn () => 'hello123',
127
            ],
128
129
            [
130
                'hello123',
131
                $factory1,
132
                'create'
133
            ],
134
135
            [
136
                'test123',
137
                $factory2,
138
                'create',
139
                'doSomething',
140
            ]
141
        ];
142
    }
143
}
144