Skip to content

Commit de92ec6

Browse files
committed
Renames notification-center methods
1 parent a9156d0 commit de92ec6

File tree

2 files changed

+94
-21
lines changed

2 files changed

+94
-21
lines changed

Diff for: src/Optimizely/Notification/NotificationCenter.php

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2017, Optimizely Inc and Contributors
3+
* Copyright 2017-2018, Optimizely Inc and Contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -117,12 +117,25 @@ public function removeNotificationListener($notification_id)
117117
return false;
118118
}
119119

120+
/**
121+
* Logs deprecation message
122+
*
123+
* @deprecated Use 'clearNotificationListeners' instead.
124+
*/
125+
public function clearNotifications($notification_type){
126+
$this->_logger->log(
127+
Logger::WARNING,
128+
"'clearNotifications' is deprecated. Call 'clearNotificationListeners' instead."
129+
);
130+
$this->clearNotificationListeners($notification_type);
131+
}
132+
120133
/**
121134
* Removes all notification callbacks for the given notification type
122135
*
123136
* @param string $notification_type One of the constants defined in NotificationType
124137
*/
125-
public function clearNotifications($notification_type)
138+
public function clearNotificationListeners($notification_type)
126139
{
127140
if (!NotificationType::isNotificationTypeValid($notification_type)) {
128141
$this->_logger->log(Logger::ERROR, "Invalid notification type.");
@@ -134,11 +147,24 @@ public function clearNotifications($notification_type)
134147
$this->_logger->log(Logger::INFO, "All callbacks for notification type '{$notification_type}' have been removed.");
135148
}
136149

150+
/**
151+
* Logs deprecation message
152+
*
153+
* @deprecated Use 'clearAllNotificationListeners' instead.
154+
*/
155+
public function cleanAllNotifications(){
156+
$this->_logger->log(
157+
Logger::WARNING,
158+
"'cleanAllNotifications' is deprecated. Call 'clearAllNotificationListeners' instead."
159+
);
160+
$this->clearAllNotificationListeners();
161+
}
162+
137163
/**
138164
* Removes all notifications for all notification types
139165
* from the notification center
140166
*/
141-
public function cleanAllNotifications()
167+
public function clearAllNotificationListeners()
142168
{
143169
foreach (array_values(NotificationType::getAll()) as $type) {
144170
$this->_notifications[$type] = [];

Diff for: tests/NotificationTests/NotificationCenterTest.php

+65-18
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function testAddNotificationWithInvalidParams()
7373
public function testAddNotificationWithValidTypeAndCallback()
7474
{
7575
$notificationType = NotificationType::ACTIVATE;
76-
$this->notificationCenterObj->cleanAllNotifications();
76+
$this->notificationCenterObj->clearAllNotificationListeners();
7777

7878
////////////////////////////////////////////////////////////////////////////////////////////////////////////
7979
// === should add, log and return notification ID when a plain function is passed as an argument === //
@@ -135,7 +135,7 @@ function () {
135135

136136
public function testAddNotificationForMultipleNotificationTypes()
137137
{
138-
$this->notificationCenterObj->cleanAllNotifications();
138+
$this->notificationCenterObj->clearAllNotificationListeners();
139139

140140
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
141141
// === should add, log and return notification ID when a valid callback is added for each notification type === //
@@ -179,7 +179,7 @@ function () {
179179

180180
public function testAddNotificationForMultipleCallbacksForASingleNotificationType()
181181
{
182-
$this->notificationCenterObj->cleanAllNotifications();
182+
$this->notificationCenterObj->clearAllNotificationListeners();
183183

184184
///////////////////////////////////////////////////////////////////////////////////////
185185
// === should add, log and return notification ID when multiple valid callbacks
@@ -243,7 +243,7 @@ public function testAddNotificationThatAlreadyAddedCallbackIsNotReAdded()
243243

244244
$functionToSend = function () {
245245
};
246-
$this->notificationCenterObj->cleanAllNotifications();
246+
$this->notificationCenterObj->clearAllNotificationListeners();
247247

248248
///////////////////////////////////////////////////////////////////////////
249249
// ===== verify that a variable method with same body isn't re-added ===== //
@@ -313,7 +313,7 @@ public function testAddNotificationThatAlreadyAddedCallbackIsNotReAdded()
313313

314314
public function testRemoveNotification()
315315
{
316-
$this->notificationCenterObj->cleanAllNotifications();
316+
$this->notificationCenterObj->clearAllNotificationListeners();
317317

318318
// add a callback for multiple notification types
319319
$this->assertSame(
@@ -407,10 +407,35 @@ function () {
407407
);
408408
}
409409

410-
public function testClearNotifications()
410+
public function testclearNotificationsAndVerifyThatclearNotificationListenersWithArgsIsCalled()
411+
{
412+
# Mock NotificationCenter
413+
$this->notificationCenterMock = $this->getMockBuilder(NotificationCenter::class)
414+
->setConstructorArgs(array($this->loggerMock, $this->errorHandlerMock))
415+
->setMethods(array('clearNotificationListeners'))
416+
->getMock();
417+
418+
# Log deprecation message
419+
$this->loggerMock->expects($this->at(0))
420+
->method('log')
421+
->with(
422+
Logger::WARNING,
423+
sprintf("'clearNotifications' is deprecated. Call 'clearNotificationListeners' instead.")
424+
);
425+
426+
$this->notificationCenterMock->expects($this->once())
427+
->method('clearNotificationListeners')
428+
->with(
429+
NotificationType::ACTIVATE
430+
);
431+
432+
$this->notificationCenterMock->clearNotifications(NotificationType::ACTIVATE);
433+
}
434+
435+
public function testclearNotificationListeners()
411436
{
412437
// ensure that notifications length is zero for each notification type
413-
$this->notificationCenterObj->cleanAllNotifications();
438+
$this->notificationCenterObj->clearAllNotificationListeners();
414439

415440
// add a callback for multiple notification types
416441
$this->notificationCenterObj->addNotificationListener(
@@ -457,7 +482,7 @@ function () {
457482
->method('handleError')
458483
->with(new InvalidNotificationTypeException('Invalid notification type.'));
459484

460-
$this->assertNull($this->notificationCenterObj->clearNotifications($invalid_type));
485+
$this->assertNull($this->notificationCenterObj->clearNotificationListeners($invalid_type));
461486

462487
// Verify that notifications length for NotificationType::ACTIVATE is still 2
463488
$this->assertSame(
@@ -482,7 +507,7 @@ function () {
482507
sprintf("All callbacks for notification type '%s' have been removed.", NotificationType::ACTIVATE)
483508
);
484509

485-
$this->notificationCenterObj->clearNotifications(NotificationType::ACTIVATE);
510+
$this->notificationCenterObj->clearNotificationListeners(NotificationType::ACTIVATE);
486511

487512
// Verify that notifications length for NotificationType::ACTIVATE is now 0
488513
$this->assertSame(
@@ -499,11 +524,11 @@ function () {
499524
///////////////////////////////////////////////////////////////////////////////////////////////////////////
500525
// == Verify that no error is thrown when clearNotification is called again for the same notification type === //
501526
///////////////////////////////////////////////////////////////////////////////////////////////////////////
502-
$this->notificationCenterObj->clearNotifications(NotificationType::ACTIVATE);
527+
$this->notificationCenterObj->clearNotificationListeners(NotificationType::ACTIVATE);
503528
}
504529

505530

506-
public function testCleanAllNotifications()
531+
public function testclearAllNotificationListeners()
507532
{
508533
// using a new notification center object to avoid using the method being tested,
509534
// to reset notifications list
@@ -558,10 +583,10 @@ function () {
558583
);
559584

560585
////////////////////////////////////////////////////////////////////////////////////////////////////
561-
// === verify that cleanAllNotifications removes all notifications for each notification type === //
586+
// === verify that clearAllNotificationListeners removes all notifications for each notification type === //
562587
////////////////////////////////////////////////////////////////////////////////////////////////////
563588

564-
$notificationCenterA->cleanAllNotifications();
589+
$notificationCenterA->clearAllNotificationListeners();
565590

566591
// verify that notifications length for each type is now set to 0
567592
$this->assertSame(
@@ -574,15 +599,37 @@ function () {
574599
);
575600

576601
///////////////////////////////////////////////////////////////////////////////////////
577-
//=== verify that cleanAllNotifications doesn't throw an error when called again === //
602+
//=== verify that clearAllNotificationListeners doesn't throw an error when called again === //
578603
///////////////////////////////////////////////////////////////////////////////////////
579-
$notificationCenterA->cleanAllNotifications();
604+
$notificationCenterA->clearAllNotificationListeners();
605+
}
606+
607+
public function testcleanAllNotificationsAndVerifyThatclearAllNotificationListenersIsCalled()
608+
{
609+
# Mock NotificationCenter
610+
$this->notificationCenterMock = $this->getMockBuilder(NotificationCenter::class)
611+
->setConstructorArgs(array($this->loggerMock, $this->errorHandlerMock))
612+
->setMethods(array('clearAllNotificationListeners'))
613+
->getMock();
614+
615+
# Log deprecation message
616+
$this->loggerMock->expects($this->at(0))
617+
->method('log')
618+
->with(
619+
Logger::WARNING,
620+
sprintf("'cleanAllNotifications' is deprecated. Call 'clearAllNotificationListeners' instead.")
621+
);
622+
623+
$this->notificationCenterMock->expects($this->once())
624+
->method('clearAllNotificationListeners');
625+
626+
$this->notificationCenterMock->cleanAllNotifications();
580627
}
581628

582629
public function testsendNotificationsGivenLessThanExpectedNumberOfArguments()
583630
{
584631
$clientObj = new FireNotificationTester;
585-
$this->notificationCenterObj->cleanAllNotifications();
632+
$this->notificationCenterObj->clearAllNotificationListeners();
586633

587634
// add a notification callback with arguments
588635
$this->notificationCenterObj->addNotificationListener(
@@ -610,7 +657,7 @@ public function testsendNotificationsAndVerifyThatAllCallbacksWithoutArgsAreCall
610657
->setMethods(array('decision_callback_no_args', 'decision_callback_no_args_2', 'track_callback_no_args'))
611658
->getMock();
612659

613-
$this->notificationCenterObj->cleanAllNotifications();
660+
$this->notificationCenterObj->clearAllNotificationListeners();
614661

615662
//add notification callbacks
616663
$this->notificationCenterObj->addNotificationListener(
@@ -661,7 +708,7 @@ public function testsendNotificationsAndVerifyThatAllCallbacksWithArgsAreCalled(
661708
->setMethods(array('decision_callback_with_args', 'decision_callback_with_args_2', 'track_callback_no_args'))
662709
->getMock();
663710

664-
$this->notificationCenterObj->cleanAllNotifications();
711+
$this->notificationCenterObj->clearAllNotificationListeners();
665712

666713
//add notification callbacks with args
667714
$this->notificationCenterObj->addNotificationListener(

0 commit comments

Comments
 (0)