Skip to content

Commit a2e8bee

Browse files
authored
Improve Code Coverage
1 parent 146a886 commit a2e8bee

File tree

7 files changed

+155
-21
lines changed

7 files changed

+155
-21
lines changed

src/Optimizely/Notification/NotificationCenter.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ public function __construct(LoggerInterface $logger, ErrorHandlerInterface $erro
5050
$this->_errorHandler = $errorHandler;
5151
}
5252

53-
public function getNotificationId()
54-
{
55-
return $this->_notificationId;
56-
}
57-
5853
public function getNotifications()
5954
{
6055
return $this->_notifications;
@@ -179,8 +174,6 @@ public function sendNotifications($notification_type, array $args = [])
179174
call_user_func_array($callback, $args);
180175
} catch (ArgumentCountError $e) {
181176
$this->reportArgumentCountError();
182-
} catch (Throwable $e) {
183-
$this->_logger->log(Logger::ERROR, "Problem calling notify callback.");
184177
} catch (Exception $e) {
185178
$this->_logger->log(Logger::ERROR, "Problem calling notify callback.");
186179
}

src/Optimizely/Optimizely.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ private function validateUserInputs($attributes, $eventTags = null) {
177177
$this->_errorHandler->handleError(
178178
new InvalidEventTagException('Provided event tags are in an invalid format.')
179179
);
180+
return false;
180181
}
181182
}
182183

src/Optimizely/ProjectConfig.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -514,19 +514,8 @@ public function getForcedVariation($experimentKey, $userId)
514514
}
515515

516516
$variationId = $experimentToVariationMap[$experimentId];
517-
// check for null and empty string variation ID
518-
if (strlen($variationId) == 0) {
519-
$this->_logger->log(Logger::DEBUG, sprintf('No variation mapped to experiment "%s" in the forced variation map.', $experimentKey));
520-
return null;
521-
}
522-
523517
$variation = $this->getVariationFromId($experimentKey, $variationId);
524518
$variationKey = $variation->getKey();
525-
// check if the variation exists in the datafile (a new variation is returned if it is not in the datafile)
526-
if (strlen($variationKey) == 0) {
527-
// this case is logged in getVariationFromId
528-
return null;
529-
}
530519

531520
$this->_logger->log(Logger::DEBUG, sprintf('Variation "%s" is mapped to experiment "%s" and user "%s" in the forced variation map', $variationKey, $experimentKey, $userId));
532521

tests/NotificationTests/NotificationCenterTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,20 @@ public function testsendNotificationsAndVerifyThatAllCallbacksWithoutArgsAreCall
606606
->method('track_callback_no_args');
607607

608608
$this->notificationCenterObj->sendNotifications(NotificationType::ACTIVATE);
609+
610+
////////////////////////////////////////////////////////////////////////////////////////////
611+
// === Verify that none of the callbacks are called given an invalid NotificationType === //
612+
////////////////////////////////////////////////////////////////////////////////////////////
613+
614+
$clientMock->expects($this->never())
615+
->method('decision_callback_no_args');
616+
$clientMock->expects($this->never())
617+
->method('decision_callback_no_args_2');
618+
619+
$clientMock->expects($this->never())
620+
->method('track_callback_no_args');
621+
622+
$this->notificationCenterObj->sendNotifications("abacada");
609623
}
610624

611625
public function testsendNotificationsAndVerifyThatAllCallbacksWithArgsAreCalled()

tests/OptimizelyTest.php

Lines changed: 133 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Optimizely\ErrorHandler\NoOpErrorHandler;
2424
use Optimizely\Event\LogEvent;
2525
use Optimizely\Exceptions\InvalidAttributeException;
26+
use Optimizely\Exceptions\InvalidEventTagException;
2627
use Optimizely\Logger\NoOpLogger;
2728
use Optimizely\Notification\NotificationCenter;
2829
use Optimizely\Notification\NotificationType;
@@ -674,10 +675,90 @@ public function testTrackInvalidAttributes()
674675
$this->notificationCenterMock->expects($this->never())
675676
->method('sendNotifications');
676677

677-
// Call activate
678+
// Call track
678679
$this->assertNull($optlyObject->track('purchase', 'test_user', 42));
679680
}
680681

682+
public function testTrackInvalidEventTags()
683+
{
684+
$this->loggerMock->expects($this->once())
685+
->method('log')
686+
->with(Logger::ERROR, 'Provided event tags are in an invalid format.');
687+
688+
$errorHandlerMock = $this->getMockBuilder(NoOpErrorHandler::class)
689+
->setMethods(array('handleError'))
690+
->getMock();
691+
$errorHandlerMock->expects($this->once())
692+
->method('handleError')
693+
->with(new InvalidEventTagException('Provided event tags are in an invalid format.'));
694+
695+
$optlyObject = new Optimizely(
696+
$this->datafile, null, $this->loggerMock, $errorHandlerMock
697+
);
698+
699+
$optlyObject->track('purchase', 'test_user', [], [1=>2]);
700+
}
701+
702+
public function testTrackUnknownEventKey()
703+
{
704+
$this->loggerMock->expects($this->at(0))
705+
->method('log')
706+
->with(Logger::ERROR, 'Event key "unknown_key" is not in datafile.');
707+
708+
709+
$this->loggerMock->expects($this->at(1))
710+
->method('log')
711+
->with(Logger::ERROR, 'Not tracking user "test_user" for event "unknown_key".');
712+
713+
$this->optimizelyObject->track('unknown_key', 'test_user');
714+
}
715+
716+
public function testActivateGivenEventKeyWithNoExperiments()
717+
{
718+
$this->loggerMock->expects($this->once())
719+
->method('log')
720+
->with(Logger::INFO, 'There are no valid experiments for event "unlinked_event" to track.');
721+
722+
$this->optimizelyObject->track('unlinked_event', 'test_user');
723+
}
724+
725+
public function testTrackEventDispatchFailure(){
726+
727+
$eventDispatcherMock = $this->getMockBuilder(DefaultEventDispatcher::class)
728+
->setMethods(array('dispatchEvent'))
729+
->getMock();
730+
731+
$this->eventBuilderMock->expects($this->once())
732+
->method('createConversionEvent')
733+
->with(
734+
$this->projectConfig,
735+
'purchase',
736+
["7718750065" => "7725250007"],
737+
'test_user',
738+
null,
739+
null
740+
)
741+
->willReturn(new LogEvent('logx.optimizely.com/track', ['param1' => 'val1'], 'POST', []));
742+
743+
$eventDispatcher = new \ReflectionProperty(Optimizely::class, '_eventDispatcher');
744+
$eventDispatcher->setAccessible(true);
745+
$eventDispatcher->setValue($this->optimizelyObject, $eventDispatcherMock);
746+
747+
$eventBuilder = new \ReflectionProperty(Optimizely::class, '_eventBuilder');
748+
$eventBuilder->setAccessible(true);
749+
$eventBuilder->setValue($this->optimizelyObject, $this->eventBuilderMock);
750+
751+
$eventDispatcherMock->expects($this->once())
752+
->method('dispatchEvent')
753+
->will($this->throwException(new Exception));
754+
755+
$this->loggerMock->expects($this->at(16))
756+
->method('log')
757+
->with(Logger::ERROR, 'Unable to dispatch conversion event. Error ');
758+
759+
$this->optimizelyObject->track('purchase', 'test_user');
760+
}
761+
681762
public function testTrackNoAttributesNoEventValue()
682763
{
683764
$this->eventBuilderMock->expects($this->once())
@@ -2477,6 +2558,34 @@ public function testGetFeatureVariableString()
24772558
);
24782559
}
24792560

2561+
public function testGetFeatureVariableMethodsReturnNullWhenGetVariableValueForTypeReturnsNull(){
2562+
$optimizelyMock = $this->getMockBuilder(Optimizely::class)
2563+
->setConstructorArgs(array($this->datafile, null, $this->loggerMock))
2564+
->setMethods(array('getFeatureVariableValueForType'))
2565+
->getMock();
2566+
$optimizelyMock->expects($this->exactly(4))
2567+
->method('getFeatureVariableValueForType')
2568+
->willReturn(null);
2569+
2570+
$this->assertNull(
2571+
$optimizelyMock->getFeatureVariableBoolean(
2572+
'boolean_single_variable_feature', 'boolean_variable', 'user_id', [])
2573+
);
2574+
$this->assertNull(
2575+
$optimizelyMock->getFeatureVariableString(
2576+
'string_single_variable_feature', 'string_variable', 'user_id', [])
2577+
);
2578+
$this->assertNull(
2579+
$optimizelyMock->getFeatureVariableDouble(
2580+
'double_single_variable_feature', 'double_variable', 'user_id', [])
2581+
);
2582+
$this->assertNull(
2583+
$optimizelyMock->getFeatureVariableInteger(
2584+
'integer_single_variable_feature', 'integer_variable', 'user_id', [])
2585+
);
2586+
2587+
}
2588+
24802589
public function testSendImpressionEventWithNoAttributes(){
24812590
$optlyObject = new OptimizelyTester($this->datafile, new ValidEventDispatcher(), $this->loggerMock);
24822591

@@ -2531,7 +2640,29 @@ public function testSendImpressionEventWithNoAttributes(){
25312640
$optlyObject->sendImpressionEvent('group_experiment_1', 'group_exp_1_var_2', 'user_1', null);
25322641
}
25332642

2534-
2643+
public function testSendImpressionEventDispatchFailure()
2644+
{
2645+
$optlyObject = new OptimizelyTester($this->datafile, new ValidEventDispatcher(), $this->loggerMock);
2646+
2647+
$eventDispatcherMock = $this->getMockBuilder(DefaultEventDispatcher::class)
2648+
->setMethods(array('dispatchEvent'))
2649+
->getMock();
2650+
2651+
$eventDispatcher = new \ReflectionProperty(Optimizely::class, '_eventDispatcher');
2652+
$eventDispatcher->setAccessible(true);
2653+
$eventDispatcher->setValue($optlyObject, $eventDispatcherMock);
2654+
2655+
$eventDispatcherMock->expects($this->once())
2656+
->method('dispatchEvent')
2657+
->will($this->throwException(new Exception));
2658+
2659+
$this->loggerMock->expects($this->at(2))
2660+
->method('log')
2661+
->with(Logger::ERROR, 'Unable to dispatch impression event. Error ');
2662+
2663+
$optlyObject->sendImpressionEvent('test_experiment', 'control', 'test_user', []);
2664+
}
2665+
25352666
public function testSendImpressionEventWithAttributes(){
25362667
$optlyObject = new OptimizelyTester($this->datafile, new ValidEventDispatcher(), $this->loggerMock);
25372668

tests/ProjectConfigTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ public function testInit()
122122
$eventKeyMap = new \ReflectionProperty(ProjectConfig::class, '_eventKeyMap');
123123
$eventKeyMap->setAccessible(true);
124124
$this->assertEquals([
125-
'purchase' => $this->config->getEvent('purchase')
125+
'purchase' => $this->config->getEvent('purchase'),
126+
'unlinked_event' => $this->config->getEvent('unlinked_event')
126127
], $eventKeyMap->getValue($this->config));
127128

128129
// Check attribute key map

tests/TestData.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,11 @@
458458
],
459459
"id": "7718020063",
460460
"key": "purchase"
461+
},
462+
{
463+
"experimentIds": [],
464+
"id": "7718020064",
465+
"key": "unlinked_event"
461466
}
462467
],
463468
"anonymizeIP": false,

0 commit comments

Comments
 (0)