Passed
Pull Request — master (#14)
by Alex
02:11
created

testImplementsListenerCollectionInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\EventDispatcher\Listener;
6
7
use Arp\EventDispatcher\Listener\AddListenerAwareInterface;
8
use Arp\EventDispatcher\Listener\ListenerCollection;
9
use Arp\EventDispatcher\Listener\ListenerCollectionInterface;
10
use PHPUnit\Framework\TestCase;
11
12
/**
13
 * @author  Alex Patterson <[email protected]>
14
 * @package ArpTest\EventDispatcher\Listener
15
 */
16
final class ListenerCollectionTest extends TestCase
17
{
18
    /**
19
     * Assert that the listener collection implements ListenerCollectionInterface.
20
     *
21
     * @covers \Arp\EventDispatcher\Listener\ListenerCollection
22
     */
23
    public function testImplementsListenerCollectionInterface(): void
24
    {
25
        $collection = new ListenerCollection();
26
27
        $this->assertInstanceOf(ListenerCollectionInterface::class, $collection);
28
    }
29
30
    /**
31
     * Assert that getIterator() will return a clone of the listener priority queue.
32
     *
33
     * @covers \Arp\EventDispatcher\Listener\ListenerCollection::getIterator
34
     */
35
    public function testGetIteratorWillReturnCloneOfListenerQueue(): void
36
    {
37
        $collection = new ListenerCollection();
38
39
        $listeners = [
40
            'Foo' => static function () {
41
                return 'Foo';
42
            },
43
            'Bar' => static function () {
44
                return 'Bar';
45
            },
46
        ];
47
48
        $collection->addListeners($listeners, 1);
49
50
        $results = [];
51
        foreach ($collection as $index => $listener) {
52
            $results[] = $listener();
53
        }
54
55
        $this->assertSame(['Foo', 'Bar'], $results);
56
    }
57
58
    /**
59
     * Assert that the count() method will return an integer matching the number of listeners added to the collection.
60
     *
61
     * @covers \Arp\EventDispatcher\Listener\ListenerCollection::addListeners
62
     */
63
    public function testCountWillReturnIntegerMatchingTheNumberOfEventListeners(): void
64
    {
65
        $collection = new ListenerCollection();
66
67
        /** @var callable[] $listeners */
68
        $listeners = [
69
            static function () {
70
            },
71
            static function () {
72
            },
73
            static function () {
74
            },
75
            static function () {
76
            },
77
        ];
78
79
        $collection->addListeners($listeners);
80
81
        $this->assertSame(count($listeners), $collection->count());
82
    }
83
84
    /**
85
     * Assert that we can add a collection of event listeners via the __construct.
86
     *
87
     * @covers \Arp\EventDispatcher\Listener\ListenerCollection
88
     */
89
    public function testEventListenersCanBeAddedViaConstructor(): void
90
    {
91
        $expected = [
92
            'foo',
93
            'bar',
94
            'baz',
95
        ];
96
97
        $listeners = [
98
            static function () {
99
                return 'foo';
100
            },
101
            static function () {
102
                return 'bar';
103
            },
104
            static function () {
105
                return 'baz';
106
            },
107
        ];
108
109
        $collection = new ListenerCollection($listeners);
110
111
        $results = [];
112
        foreach ($collection as $index => $listener) {
113
            $results[] = $listener();
114
        }
115
116
        $this->assertSame($expected, $results);
117
        $this->assertSame(count($listeners), $collection->count());
118
    }
119
120
    /**
121
     * Assert that the listeners priorities are respected, regardless of when the listener is registered with the
122
     * collection.
123
     *
124
     * @covers \Arp\EventDispatcher\Listener\ListenerCollection::addListener
125
     * @covers \Arp\EventDispatcher\Listener\ListenerCollection::addListeners
126
     */
127
    public function testListenerPriorities(): void
128
    {
129
        $listeners = [
130
            static function () {
131
                return 5;
132
            },
133
            static function () {
134
                return 1;
135
            },
136
            static function () {
137
                return 3;
138
            },
139
            static function () {
140
                return 7;
141
            },
142
            static function () {
143
                return 4;
144
            },
145
            static function () {
146
                return 8;
147
            },
148
            static function () {
149
                return 6;
150
            },
151
            static function () {
152
                return 2;
153
            },
154
        ];
155
156
        $collection = new ListenerCollection();
157
158
        $collection->addListener($listeners[0], 300); // 5
159
        $collection->addListener($listeners[1], 700); // 1
160
        $collection->addListener($listeners[2], 500); // 3
161
        $collection->addListener($listeners[3], 101); // 7
162
        $collection->addListener($listeners[4], 400); // 4
163
        $collection->addListener($listeners[5], 100); // 8
164
        $collection->addListener($listeners[6], 200); // 6
165
        $collection->addListener($listeners[7], 600); // 2
166
167
        $results = [];
168
        foreach ($collection as $item) {
169
            $results[] = $item();
170
        }
171
172
        $this->assertSame([1, 2, 3, 4, 5, 6, 7, 8], $results);
173
    }
174
175
    /**
176
     * Assert that the listeners natural order is respected when provided with event listeners with the same
177
     * priorities. This means that the collection operates on a first in first out basis.
178
     *
179
     * @covers \Arp\EventDispatcher\Listener\ListenerCollection::addListener
180
     * @covers \Arp\EventDispatcher\Listener\ListenerCollection::addListeners
181
     */
182
    public function testListenerPrioritiesRespectNaturalOrderWhenPrioritiesAreTheSame(): void
183
    {
184
        $listeners = [
185
            static function () {
186
                return 5;
187
            },
188
            static function () {
189
                return 1;
190
            },
191
            static function () {
192
                return 3;
193
            },
194
            static function () {
195
                return 7;
196
            },
197
            static function () {
198
                return 4;
199
            },
200
            static function () {
201
                return 8;
202
            },
203
            static function () {
204
                return 6;
205
            },
206
            static function () {
207
                return 2;
208
            },
209
        ];
210
211
        $collection = new ListenerCollection();
212
213
        $collection->addListener($listeners[0], 1); // 5
214
        $collection->addListener($listeners[1], 1); // 1
215
        $collection->addListener($listeners[2], 1); // 3
216
        $collection->addListener($listeners[3], 1); // 7
217
        $collection->addListener($listeners[4], 1); // 4
218
        $collection->addListener($listeners[5], 1); // 8
219
        $collection->addListener($listeners[6], 1); // 6
220
        $collection->addListener($listeners[7], 1); // 2
221
222
        $results = [];
223
        foreach ($collection as $item) {
224
            $results[] = $item();
225
        }
226
227
        $this->assertSame([5, 1, 3, 7, 4, 8, 6, 2], $results);
228
    }
229
}
230