aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/lib/classes/NotificationCenterTest.php
blob: 8a6ba9a68db69694900c7ceb80f29e31398aca09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
/*
 * notification_center_test.php - unit tests for the NotificationCenter class
 *
 * Copyright (c) 2009  Elmar Ludwig
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 */

interface Observer
{
    public function update($event, $object, $user_data);
}

# sample predicate (implemented as a Callable class)
# using #soundex for comparisons
# @see http://php.net/language.oop5.magic
class NotificationCenterTestSoundexPredicate
{
    private $goldStandard;

    public function __construct($goldStandard)
    {
        $this->goldStandard = $goldStandard;
    }

    public function __invoke($arg)
    {
        return soundex($this->goldStandard) === soundex($arg);
    }
}

class NotificationCenterTest extends \Codeception\Test\Unit
{
    private $observer;
    private $subject;

    public function setUp(): void
    {
        $this->observer = $this->createMock("Observer");
        $this->subject = new stdClass();

        // Register observers for simple events
        NotificationCenter::addObserver($this->observer, 'update', NULL);
        NotificationCenter::addObserver($this->observer, 'update', NULL, $this->subject);
        NotificationCenter::addObserver($this->observer, 'update', 'foo');
        NotificationCenter::addObserver($this->observer, 'update', 'foo', $this->subject);

        // Register observers for SORM events
        NotificationCenter::addObserver($this->observer, 'update', 'SORMDidCreate');
        NotificationCenter::addObserver($this->observer, 'update', 'AnotherSORMDidCreate');
        NotificationCenter::addObserver($this->observer, 'update', 'Namespaced\\SormDidUpdate', $this->subject);
    }

    public function tearDown(): void
    {
        NotificationCenter::removeObserver($this->observer);
    }

    public function testAddObserver1()
    {
        $this->observer->expects($this->exactly(4))->method('update');

        NotificationCenter::postNotification('foo', $this->subject);
    }

    public function testAddObserver2()
    {
        $this->observer->expects($this->exactly(2))->method('update');

        NotificationCenter::postNotification('bar', $this->subject);
    }

    public function testAddObserver3()
    {
        $this->observer->expects($this->exactly(2))->method('update');

        NotificationCenter::postNotification('foo', 'other');
    }

    public function testAddObserver4()
    {
        $this->observer->expects($this->once())->method('update');

        NotificationCenter::postNotification('bar', 'other');
    }

    public function testPostNotification()
    {

        $user_data = [42];
        $this->observer->expects($this->exactly(4))
            ->method('update')
            ->with("foo", $this->subject, $user_data);

        NotificationCenter::postNotification('foo', $this->subject, $user_data);
    }

    public function testRemoveOtherObserver()
    {
        $this->observer->expects($this->exactly(4))->method('update');

        $observer = $this->createMock("Observer");
        NotificationCenter::removeObserver($observer);
        NotificationCenter::postNotification('foo', $this->subject);
    }

    public function testRemoveObserver1()
    {
        $this->observer->expects($this->never())->method('update');

        NotificationCenter::removeObserver($this->observer);
        NotificationCenter::postNotification('foo', $this->subject);
        NotificationCenter::postNotification('bar', $this->subject);
        NotificationCenter::postNotification('foo', 'other');
        NotificationCenter::postNotification('bar', 'other');
    }

    public function testRemoveObserver2a()
    {
        $this->observer->expects($this->exactly(2))->method('update');

        NotificationCenter::removeObserver($this->observer, 'foo');
        NotificationCenter::postNotification('foo', $this->subject);
    }

    public function testRemoveObserver2b()
    {
        $this->observer->expects($this->exactly(2))->method('update');

        NotificationCenter::removeObserver($this->observer, 'foo');
        NotificationCenter::postNotification('bar', $this->subject);
    }

    public function testRemoveObserver2c()
    {
        $this->observer->expects($this->once())->method('update');

        NotificationCenter::removeObserver($this->observer, 'foo');
        NotificationCenter::postNotification('foo', 'other');
    }

    public function testRemoveObserver2d()
    {
        $this->observer->expects($this->once())->method('update');

        NotificationCenter::removeObserver($this->observer, 'foo');
        NotificationCenter::postNotification('bar', 'other');
    }

    public function testRemoveObserver3a()
    {
        $this->observer->expects($this->exactly(2))->method('update');

        NotificationCenter::removeObserver($this->observer, NULL, $this->subject);
        NotificationCenter::postNotification('foo', $this->subject);
    }

    public function testRemoveObserver3b()
    {
        $this->observer->expects($this->once())->method('update');

        NotificationCenter::removeObserver($this->observer, NULL, $this->subject);
        NotificationCenter::postNotification('bar', $this->subject);
    }

    public function testRemoveObserver3c()
    {
        $this->observer->expects($this->exactly(2))->method('update');

        NotificationCenter::removeObserver($this->observer, NULL, $this->subject);
        NotificationCenter::postNotification('foo', 'other');
    }

    public function testRemoveObserver3d()
    {
        $this->observer->expects($this->once())->method('update');

        NotificationCenter::removeObserver($this->observer, NULL, $this->subject);
        NotificationCenter::postNotification('bar', 'other');
    }

    public function testRemoveObserver4a()
    {
        $this->observer->expects($this->exactly(3))->method('update');

        NotificationCenter::removeObserver($this->observer, 'foo', $this->subject);
        NotificationCenter::postNotification('foo', $this->subject);
    }

    public function testRemoveObserver4b()
    {
        $this->observer->expects($this->exactly(2))->method('update');

        NotificationCenter::removeObserver($this->observer, 'foo', $this->subject);
        NotificationCenter::postNotification('bar', $this->subject);
    }

    public function testRemoveObserver4c()
    {
        $this->observer->expects($this->exactly(2))->method('update');

        NotificationCenter::removeObserver($this->observer, 'foo', $this->subject);
        NotificationCenter::postNotification('foo', 'other');
    }

    public function testRemoveObserver4d()
    {
        $this->observer->expects($this->once())->method('update');

        NotificationCenter::removeObserver($this->observer, 'foo', $this->subject);
        NotificationCenter::postNotification('bar', 'other');
    }


    public function testWildCardObserver()
    {
        // prepare fixtures
        $user_data = [42];
        $subject = new stdClass();

        // register observer
        $wildcard = $this->createMock("Observer");
        $wildcard->expects($this->once())->method('update')->with('foo', $subject, $user_data);

        NotificationCenter::addObserver($wildcard, 'update', NULL);


        // expect notication
        NotificationCenter::postNotification('foo', $subject, $user_data);

        // remove observer
        NotificationCenter::removeObserver($wildcard);
    }

    public function testLikeObserver()
    {
        // prepare fixtures
        $user_data = [42];
        $subject = new stdClass();

        // register observer
        $wildcard = $this->createMock("Observer");
        $wildcard->expects($this->once())->method('update')->with('foobar', $subject, $user_data);

        NotificationCenter::addObserver($wildcard, 'update', 'foo*');


        // expect notication
        NotificationCenter::postNotification('foobar', $subject, $user_data);

        NotificationCenter::postNotification('tafkap', $subject, $user_data);

        // remove observer
        NotificationCenter::removeObserver($wildcard);
    }

    function assertMatchingNotification($matcher, $subject)
    {
        // register observer
        $observer = $this->createMock("Observer");
        NotificationCenter::addObserver($observer, 'update',
                                        'SomeNotification', $matcher);

        // expect notication
        $observer->expects($this->once())->method('update')->with('SomeNotification', $subject);

        // fire!
        NotificationCenter::postNotification('SomeNotification', $subject);

        // remove observer
        NotificationCenter::removeObserver($observer);
    }

    public function testCustomPredicateWithAnonFunc()
    {
        $matcher = function ($subject) {
            return preg_match('@^/road/to@', $subject);
        };
        $subject = '/road/to/nowhere';

        $this->assertMatchingNotification($matcher, $subject);
    }

    public function testCustomPredicateWithCallable()
    {
        $matcher = new NotificationCenterTestSoundexPredicate("leopard");
        $subject = "lab hurt";

        $this->assertMatchingNotification($matcher, $subject);
    }

    public function testStaticOnOffMethods()
    {
        $observer = $this->createMock('Observer');
        $observer->expects($this->once())->method('update');

        NotificationCenter::on('OnOff', [$observer, 'update']);

        NotificationCenter::postNotification('OnOff', 'on');

        NotificationCenter::off('OnOff', [$observer, 'update']);

        NotificationCenter::postNotification('OnOff', 'on');
    }

    public function testSORMOberservers()
    {
        $observer = $this->createMock('Observer');

        // Register observer for SORM event
        NotificationCenter::addObserver($observer, 'update', 'SORMDidCreate');

        $observer->expects($this->once())->method('update');
        NotificationCenter::postNotification('SORMDidCreate', 'foo');
        NotificationCenter::postNotification('AnotherSORMDidCreate', 'foo');
    }

    public function testNamespacedSORMOberservers()
    {
        $observer = $this->createMock('Observer');

        // Register observer for namespaced SORM event
        NotificationCenter::addObserver($observer, 'update', 'Namespaced\\SORMDidCreate');

        $observer->expects($this->once())->method('update');
        NotificationCenter::postNotification('SORMDidCreate', 'foo');
        NotificationCenter::postNotification('Namespaced\\SORMDidCreate', 'foo');
    }
}