-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathNotificationCenterTest.php
760 lines (649 loc) · 31.2 KB
/
NotificationCenterTest.php
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
<?php
/**
* Copyright 2017-2018, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Optimizely\Tests;
use Monolog\Logger;
use Optimizely\Event\Builder\EventBuilder;
use Optimizely\ErrorHandler\NoOpErrorHandler;
use Optimizely\Logger\DefaultLogger;
use Optimizely\Logger\NoOpLogger;
use Optimizely\Notification\NotificationCenter;
use Optimizely\Notification\NotificationType;
use Optimizely\Exceptions\InvalidCallbackArgumentCountException;
use Optimizely\Exceptions\InvalidNotificationTypeException;
class NotificationCenterTest extends \PHPUnit_Framework_TestCase
{
private $notificationCenterObj;
private $loggerMock;
public function setUp()
{
$this->loggerMock = $this->getMockBuilder(NoOpLogger::class)
->setMethods(array('log'))
->getMock();
$this->errorHandlerMock = $this->getMockBuilder(NoOpErrorHandler::class)
->setMethods(array('handleError'))
->getMock();
$this->notificationCenterObj = new NotificationCenter($this->loggerMock, $this->errorHandlerMock);
}
public function testAddNotificationWithInvalidParams()
{
// should log, throw an exception and return null if invalid notification type given
$invalid_type = "HelloWorld";
$this->errorHandlerMock->expects($this->at(0))
->method('handleError')
->with(new InvalidNotificationTypeException('Invalid notification type.'));
$this->loggerMock->expects($this->at(0))
->method('log')
->with(Logger::ERROR, "Invalid notification type.");
$this->assertNull($this->notificationCenterObj->addNotificationListener($invalid_type, function () {
}));
// should log and return null if invalid callable given
$invalid_callable = "HelloWorld";
$this->loggerMock->expects($this->at(0))
->method('log')
->with(Logger::ERROR, "Invalid notification callback.");
$this->assertNull($this->notificationCenterObj->addNotificationListener(NotificationType::ACTIVATE, $invalid_callable));
}
public function testAddNotificationWithValidTypeAndCallback()
{
$notificationType = NotificationType::ACTIVATE;
$this->notificationCenterObj->clearAllNotificationListeners();
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// === should add, log and return notification ID when a plain function is passed as an argument === //
////////////////////////////////////////////////////////////////////////////////////////////////////////////
$simple_method = function () {
};
$this->loggerMock->expects($this->at(0))
->method('log')
->with(Logger::INFO, "Callback added for notification type '{$notificationType}'.");
$this->assertSame(
1,
$this->notificationCenterObj->addNotificationListener($notificationType, $simple_method)
);
// verify that notifications length has incremented by 1
$this->assertSame(
1,
sizeof($this->notificationCenterObj->getNotifications()[$notificationType])
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// === should add, log and return notification ID when an anonymous function is passed as an argument === //
////////////////////////////////////////////////////////////////////////////////////////////////////////////
$this->loggerMock->expects($this->at(0))
->method('log')
->with(Logger::INFO, "Callback added for notification type '{$notificationType}'.");
$this->assertSame(
2,
$this->notificationCenterObj->addNotificationListener(
$notificationType,
function () {
}
)
);
// verify that notifications length has incremented by 1
$this->assertSame(
2,
sizeof($this->notificationCenterObj->getNotifications()[$notificationType])
);
///////////////////////////////////////////////////////////////////////////////////////////////////////
// === should add, log and return notification ID when an object method is passed as an argument === //
///////////////////////////////////////////////////////////////////////////////////////////////////////
$eBuilder = new EventBuilder(new NoOpLogger());
$callbackInput = array($eBuilder, 'createImpressionEvent');
$this->loggerMock->expects($this->at(0))
->method('log')
->with(Logger::INFO, "Callback added for notification type '{$notificationType}'.");
$this->assertSame(
3,
$this->notificationCenterObj->addNotificationListener($notificationType, $callbackInput)
);
// verify that notifications length has incremented by 1
$this->assertSame(
3,
sizeof($this->notificationCenterObj->getNotifications()[$notificationType])
);
}
public function testAddNotificationForMultipleNotificationTypes()
{
$this->notificationCenterObj->clearAllNotificationListeners();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// === should add, log and return notification ID when a valid callback is added for each notification type === //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$this->loggerMock->expects($this->at(0))
->method('log')
->with(Logger::INFO, sprintf("Callback added for notification type '%s'.", NotificationType::ACTIVATE));
$this->assertSame(
1,
$this->notificationCenterObj->addNotificationListener(
NotificationType::ACTIVATE,
function () {
}
)
);
// verify that notifications length for NotificationType::ACTIVATE has incremented by 1
$this->assertSame(
1,
sizeof($this->notificationCenterObj->getNotifications()[NotificationType::ACTIVATE])
);
$this->loggerMock->expects($this->at(0))
->method('log')
->with(Logger::INFO, sprintf("Callback added for notification type '%s'.", NotificationType::TRACK));
$this->assertSame(
2,
$this->notificationCenterObj->addNotificationListener(
NotificationType::TRACK,
function () {
}
)
);
// verify that notifications length for NotificationType::TRACK has incremented by 1
$this->assertSame(
1,
sizeof($this->notificationCenterObj->getNotifications()[NotificationType::TRACK])
);
}
public function testAddNotificationForMultipleCallbacksForASingleNotificationType()
{
$this->notificationCenterObj->clearAllNotificationListeners();
///////////////////////////////////////////////////////////////////////////////////////
// === should add, log and return notification ID when multiple valid callbacks
// are added for a single notification type === //
///////////////////////////////////////////////////////////////////////////////////////
$this->loggerMock->expects($this->at(0))
->method('log')
->with(Logger::INFO, sprintf("Callback added for notification type '%s'.", NotificationType::ACTIVATE));
$this->assertSame(
1,
$this->notificationCenterObj->addNotificationListener(
NotificationType::ACTIVATE,
function () {
}
)
);
// verify that notifications length for NotificationType::ACTIVATE has incremented by 1
$this->assertSame(
1,
sizeof($this->notificationCenterObj->getNotifications()[NotificationType::ACTIVATE])
);
$this->assertSame(
2,
$this->notificationCenterObj->addNotificationListener(
NotificationType::ACTIVATE,
function () {
echo "HelloWorld";
}
)
);
// verify that notifications length for NotificationType::ACTIVATE has incremented by 1
$this->assertSame(
2,
sizeof($this->notificationCenterObj->getNotifications()[NotificationType::ACTIVATE])
);
$this->assertSame(
3,
$this->notificationCenterObj->addNotificationListener(
NotificationType::ACTIVATE,
function () {
$a = 1;
}
)
);
// verify that notifications length for NotificationType::ACTIVATE has incremented by 1
$this->assertSame(
3,
sizeof($this->notificationCenterObj->getNotifications()[NotificationType::ACTIVATE])
);
}
public function testAddNotificationThatAlreadyAddedCallbackIsNotReAdded()
{
// Note: anonymous methods sent with the same body will be re-added.
// Only variable and object methods can be checked for duplication
$functionToSend = function () {
};
$this->notificationCenterObj->clearAllNotificationListeners();
///////////////////////////////////////////////////////////////////////////
// ===== verify that a variable method with same body isn't re-added ===== //
///////////////////////////////////////////////////////////////////////////
$this->loggerMock->expects($this->at(0))
->method('log')
->with(Logger::INFO, sprintf("Callback added for notification type '%s'.", NotificationType::ACTIVATE));
// verify that notification ID 1 is returned
$this->assertSame(
1,
$this->notificationCenterObj->addNotificationListener(NotificationType::ACTIVATE, $functionToSend)
);
$this->loggerMock->expects($this->at(0))
->method('log')
->with(Logger::DEBUG, sprintf("Callback already added for notification type '%s'.", NotificationType::ACTIVATE));
// verify that -1 is returned when adding the same callback
$this->assertSame(
-1,
$this->notificationCenterObj->addNotificationListener(NotificationType::ACTIVATE, $functionToSend)
);
// verify that same method is added for a different notification type
$this->loggerMock->expects($this->at(0))
->method('log')
->with(Logger::INFO, sprintf("Callback added for notification type '%s'.", NotificationType::TRACK));
$this->assertSame(
2,
$this->notificationCenterObj->addNotificationListener(NotificationType::TRACK, $functionToSend)
);
/////////////////////////////////////////////////////////////////////////
// ===== verify that an object method with same body isn't re-added ===== //
/////////////////////////////////////////////////////////////////////////
$eBuilder = new EventBuilder(new NoOpLogger());
$callbackInput = array($eBuilder, 'createImpressionEvent');
$this->loggerMock->expects($this->at(0))
->method('log')
->with(Logger::INFO, sprintf("Callback added for notification type '%s'.", NotificationType::ACTIVATE));
$this->assertSame(
3,
$this->notificationCenterObj->addNotificationListener(NotificationType::ACTIVATE, $callbackInput)
);
$this->loggerMock->expects($this->at(0))
->method('log')
->with(Logger::DEBUG, sprintf("Callback already added for notification type '%s'.", NotificationType::ACTIVATE));
// verify that -1 is returned when adding the same callback
$this->assertSame(
-1,
$this->notificationCenterObj->addNotificationListener(NotificationType::ACTIVATE, $callbackInput)
);
// verify that same method is added for a different notification type
$this->loggerMock->expects($this->at(0))
->method('log')
->with(Logger::INFO, sprintf("Callback added for notification type '%s'.", NotificationType::TRACK));
$this->assertSame(
4,
$this->notificationCenterObj->addNotificationListener(NotificationType::TRACK, $callbackInput)
);
}
public function testRemoveNotification()
{
$this->notificationCenterObj->clearAllNotificationListeners();
// add a callback for multiple notification types
$this->assertSame(
1,
$this->notificationCenterObj->addNotificationListener(
NotificationType::ACTIVATE,
function () {
}
)
);
$this->assertSame(
2,
$this->notificationCenterObj->addNotificationListener(
NotificationType::TRACK,
function () {
}
)
);
// add another callback for NotificationType::ACTIVATE
$this->assertSame(
3,
$this->notificationCenterObj->addNotificationListener(
NotificationType::ACTIVATE,
function () {
//doSomething
}
)
);
// Verify that notifications length for NotificationType::ACTIVATE is 2
$this->assertSame(
2,
sizeof($this->notificationCenterObj->getNotifications()[NotificationType::ACTIVATE])
);
// Verify that notifications length for NotificationType::TRACK is 1
$this->assertSame(
1,
sizeof($this->notificationCenterObj->getNotifications()[NotificationType::TRACK])
);
///////////////////////////////////////////////////////////////////////////////
// === Verify that no callback is removed for an invalid notification ID === //
///////////////////////////////////////////////////////////////////////////////
$invalid_id = 4;
$this->loggerMock->expects($this->at(0))
->method('log')
->with(Logger::DEBUG, sprintf("No Callback found with notification ID '%s'.", $invalid_id));
$this->assertFalse($this->notificationCenterObj->removeNotificationListener($invalid_id));
/////////////////////////////////////////////////////////////////////
// === Verify that callback is removed for a valid notification ID //
/////////////////////////////////////////////////////////////////////
$valid_id = 3;
$this->loggerMock->expects($this->at(0))
->method('log')
->with(Logger::INFO, sprintf("Callback with notification ID '%s' has been removed.", $valid_id));
$this->assertTrue($this->notificationCenterObj->removeNotificationListener($valid_id));
// verify that notifications length for NotificationType::ACTIVATE is now 1
$this->assertSame(
1,
sizeof($this->notificationCenterObj->getNotifications()[NotificationType::ACTIVATE])
);
//verify that notifications length for NotificationType::TRACK remains same
$this->assertSame(
1,
sizeof($this->notificationCenterObj->getNotifications()[NotificationType::TRACK])
);
/////////////////////////////////////////////////////////////////////////////////////////////////
// === Verify that no callback is removed once a callback has been already removed against a notification ID === //
/////////////////////////////////////////////////////////////////////////////////////////////////
$valid_id = 3;
$this->loggerMock->expects($this->at(0))
->method('log')
->with(Logger::DEBUG, sprintf("No Callback found with notification ID '%s'.", $valid_id));
$this->assertFalse($this->notificationCenterObj->removeNotificationListener($valid_id));
//verify that notifications lengths for NotificationType::ACTIVATE and NotificationType::TRACK remain same
$this->assertSame(
1,
sizeof($this->notificationCenterObj->getNotifications()[NotificationType::ACTIVATE])
);
$this->assertSame(
1,
sizeof($this->notificationCenterObj->getNotifications()[NotificationType::TRACK])
);
}
public function testclearNotificationsAndVerifyThatclearNotificationListenersWithArgsIsCalled()
{
# Mock NotificationCenter
$this->notificationCenterMock = $this->getMockBuilder(NotificationCenter::class)
->setConstructorArgs(array($this->loggerMock, $this->errorHandlerMock))
->setMethods(array('clearNotificationListeners'))
->getMock();
# Log deprecation message
$this->loggerMock->expects($this->at(0))
->method('log')
->with(
Logger::WARNING,
sprintf("'clearNotifications' is deprecated. Call 'clearNotificationListeners' instead.")
);
$this->notificationCenterMock->expects($this->once())
->method('clearNotificationListeners')
->with(
NotificationType::ACTIVATE
);
$this->notificationCenterMock->clearNotifications(NotificationType::ACTIVATE);
}
public function testclearNotificationListeners()
{
// ensure that notifications length is zero for each notification type
$this->notificationCenterObj->clearAllNotificationListeners();
// add a callback for multiple notification types
$this->notificationCenterObj->addNotificationListener(
NotificationType::ACTIVATE,
function () {
}
);
$this->notificationCenterObj->addNotificationListener(
NotificationType::TRACK,
function () {
}
);
// add another callback for NotificationType::ACTIVATE
$this->notificationCenterObj->addNotificationListener(
NotificationType::ACTIVATE,
function () {
}
);
// Verify that notifications length for NotificationType::ACTIVATE is 2
$this->assertSame(
2,
sizeof($this->notificationCenterObj->getNotifications()[NotificationType::ACTIVATE])
);
// Verify that notifications length for NotificationType::TRACK is 1
$this->assertSame(
1,
sizeof($this->notificationCenterObj->getNotifications()[NotificationType::TRACK])
);
/////////////////////////////////////////////////////////////////////////////////////////
// === Verify that no notifications are removed given an invalid notification type === //
/////////////////////////////////////////////////////////////////////////////////////////
$invalid_type = "HelloWorld";
$this->loggerMock->expects($this->at(0))
->method('log')
->with(Logger::ERROR, "Invalid notification type.");
$this->errorHandlerMock->expects($this->at(0))
->method('handleError')
->with(new InvalidNotificationTypeException('Invalid notification type.'));
$this->assertNull($this->notificationCenterObj->clearNotificationListeners($invalid_type));
// Verify that notifications length for NotificationType::ACTIVATE is still 2
$this->assertSame(
2,
sizeof($this->notificationCenterObj->getNotifications()[NotificationType::ACTIVATE])
);
// Verify that notifications length for NotificationType::TRACK is still 1
$this->assertSame(
1,
sizeof($this->notificationCenterObj->getNotifications()[NotificationType::TRACK])
);
///////////////////////////////////////////////////////////////////////////////////////
// === Verify that all notifications are removed given a valid notification type === //
///////////////////////////////////////////////////////////////////////////////////////
$this->loggerMock->expects($this->at(0))
->method('log')
->with(
Logger::INFO,
sprintf("All callbacks for notification type '%s' have been removed.", NotificationType::ACTIVATE)
);
$this->notificationCenterObj->clearNotificationListeners(NotificationType::ACTIVATE);
// Verify that notifications length for NotificationType::ACTIVATE is now 0
$this->assertSame(
0,
sizeof($this->notificationCenterObj->getNotifications()[NotificationType::ACTIVATE])
);
// Verify that notifications length for NotificationType::TRACK is still 1
$this->assertSame(
1,
sizeof($this->notificationCenterObj->getNotifications()[NotificationType::TRACK])
);
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// == Verify that no error is thrown when clearNotification is called again for the same notification type === //
///////////////////////////////////////////////////////////////////////////////////////////////////////////
$this->notificationCenterObj->clearNotificationListeners(NotificationType::ACTIVATE);
}
public function testclearAllNotificationListeners()
{
// using a new notification center object to avoid using the method being tested,
// to reset notifications list
$notificationCenterA = new NotificationCenter($this->loggerMock, $this->errorHandlerMock);
// verify that for each of the notification types, the notifications length is zero
$this->assertSame(
0,
sizeof($notificationCenterA->getNotifications()[NotificationType::ACTIVATE])
);
$this->assertSame(
0,
sizeof($notificationCenterA->getNotifications()[NotificationType::TRACK])
);
// add a callback for multiple notification types
$notificationCenterA->addNotificationListener(
NotificationType::ACTIVATE,
function () {
}
);
$notificationCenterA->addNotificationListener(
NotificationType::ACTIVATE,
function () {
}
);
$notificationCenterA->addNotificationListener(
NotificationType::ACTIVATE,
function () {
}
);
$notificationCenterA->addNotificationListener(
NotificationType::TRACK,
function () {
}
);
$notificationCenterA->addNotificationListener(
NotificationType::TRACK,
function () {
}
);
// verify that notifications length for each type reflects the just added callbacks
$this->assertSame(
3,
sizeof($notificationCenterA->getNotifications()[NotificationType::ACTIVATE])
);
$this->assertSame(
2,
sizeof($notificationCenterA->getNotifications()[NotificationType::TRACK])
);
////////////////////////////////////////////////////////////////////////////////////////////////////
// === verify that clearAllNotificationListeners removes all notifications for each notification type === //
////////////////////////////////////////////////////////////////////////////////////////////////////
$notificationCenterA->clearAllNotificationListeners();
// verify that notifications length for each type is now set to 0
$this->assertSame(
0,
sizeof($notificationCenterA->getNotifications()[NotificationType::ACTIVATE])
);
$this->assertSame(
0,
sizeof($notificationCenterA->getNotifications()[NotificationType::TRACK])
);
///////////////////////////////////////////////////////////////////////////////////////
//=== verify that clearAllNotificationListeners doesn't throw an error when called again === //
///////////////////////////////////////////////////////////////////////////////////////
$notificationCenterA->clearAllNotificationListeners();
}
public function testcleanAllNotificationsAndVerifyThatclearAllNotificationListenersIsCalled()
{
# Mock NotificationCenter
$this->notificationCenterMock = $this->getMockBuilder(NotificationCenter::class)
->setConstructorArgs(array($this->loggerMock, $this->errorHandlerMock))
->setMethods(array('clearAllNotificationListeners'))
->getMock();
# Log deprecation message
$this->loggerMock->expects($this->at(0))
->method('log')
->with(
Logger::WARNING,
sprintf("'cleanAllNotifications' is deprecated. Call 'clearAllNotificationListeners' instead.")
);
$this->notificationCenterMock->expects($this->once())
->method('clearAllNotificationListeners');
$this->notificationCenterMock->cleanAllNotifications();
}
public function testsendNotificationsGivenLessThanExpectedNumberOfArguments()
{
$clientObj = new FireNotificationTester;
$this->notificationCenterObj->clearAllNotificationListeners();
// add a notification callback with arguments
$this->notificationCenterObj->addNotificationListener(
NotificationType::ACTIVATE,
array($clientObj, 'decision_callback_with_args')
);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// === Verify that an exception is thrown and message logged when less number of args passed than expected === //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$this->errorHandlerMock->expects($this->at(0))
->method('handleError')
->with(new InvalidCallbackArgumentCountException('Registered callback expects more number of arguments than the actual number'));
$this->loggerMock->expects($this->at(0))
->method('log')
->with(Logger::ERROR, "Problem calling notify callback.");
$this->notificationCenterObj->sendNotifications(NotificationType::ACTIVATE, array("HelloWorld"));
}
public function testsendNotificationsAndVerifyThatAllCallbacksWithoutArgsAreCalled()
{
$clientMock = $this->getMockBuilder(FireNotificationTester::class)
->setMethods(array('decision_callback_no_args', 'decision_callback_no_args_2', 'track_callback_no_args'))
->getMock();
$this->notificationCenterObj->clearAllNotificationListeners();
//add notification callbacks
$this->notificationCenterObj->addNotificationListener(
NotificationType::ACTIVATE,
array($clientMock, 'decision_callback_no_args')
);
$this->notificationCenterObj->addNotificationListener(
NotificationType::ACTIVATE,
array($clientMock, 'decision_callback_no_args_2')
);
$this->notificationCenterObj->addNotificationListener(
NotificationType::TRACK,
array($clientMock, 'track_callback_no_args')
);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// === Verify that all callbacks for NotificationType::ACTIVATE are called and no other callbacks are called === //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$clientMock->expects($this->exactly(1))
->method('decision_callback_no_args');
$clientMock->expects($this->exactly(1))
->method('decision_callback_no_args_2');
$clientMock->expects($this->never())
->method('track_callback_no_args');
$this->notificationCenterObj->sendNotifications(NotificationType::ACTIVATE);
////////////////////////////////////////////////////////////////////////////////////////////
// === Verify that none of the callbacks are called given an invalid NotificationType === //
////////////////////////////////////////////////////////////////////////////////////////////
$clientMock->expects($this->never())
->method('decision_callback_no_args');
$clientMock->expects($this->never())
->method('decision_callback_no_args_2');
$clientMock->expects($this->never())
->method('track_callback_no_args');
$this->notificationCenterObj->sendNotifications("abacada");
}
public function testsendNotificationsAndVerifyThatAllCallbacksWithArgsAreCalled()
{
$clientMock = $this->getMockBuilder(FireNotificationTester::class)
->setMethods(array('decision_callback_with_args', 'decision_callback_with_args_2', 'track_callback_no_args'))
->getMock();
$this->notificationCenterObj->clearAllNotificationListeners();
//add notification callbacks with args
$this->notificationCenterObj->addNotificationListener(
NotificationType::ACTIVATE,
array($clientMock, 'decision_callback_with_args')
);
$this->notificationCenterObj->addNotificationListener(
NotificationType::ACTIVATE,
array($clientMock, 'decision_callback_with_args_2')
);
$this->notificationCenterObj->addNotificationListener(
NotificationType::TRACK,
array($clientMock, 'track_callback_no_args')
);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// === Verify that all callbacks for NotificationType::ACTIVATE are called and no other callbacks are called === //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$clientMock->expects($this->exactly(1))
->method('decision_callback_with_args')
->with(
5,
5.5,
'string',
array(5,6),
function () {
}
);
$clientMock->expects($this->exactly(1))
->method('decision_callback_with_args_2')
->with(
5,
5.5,
'string',
array(5,6),
function () {
}
);
$clientMock->expects($this->never())
->method('track_callback_no_args');
$this->notificationCenterObj->sendNotifications(
NotificationType::ACTIVATE,
array(5, 5.5, 'string', array(5,6), function () {
})
);
}
}