|
23 | 23 | use Optimizely\ErrorHandler\NoOpErrorHandler;
|
24 | 24 | use Optimizely\Event\LogEvent;
|
25 | 25 | use Optimizely\Exceptions\InvalidAttributeException;
|
| 26 | +use Optimizely\Exceptions\InvalidEventTagException; |
26 | 27 | use Optimizely\Logger\NoOpLogger;
|
27 | 28 | use Optimizely\Notification\NotificationCenter;
|
28 | 29 | use Optimizely\Notification\NotificationType;
|
@@ -674,10 +675,90 @@ public function testTrackInvalidAttributes()
|
674 | 675 | $this->notificationCenterMock->expects($this->never())
|
675 | 676 | ->method('sendNotifications');
|
676 | 677 |
|
677 |
| - // Call activate |
| 678 | + // Call track |
678 | 679 | $this->assertNull($optlyObject->track('purchase', 'test_user', 42));
|
679 | 680 | }
|
680 | 681 |
|
| 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 | + |
681 | 762 | public function testTrackNoAttributesNoEventValue()
|
682 | 763 | {
|
683 | 764 | $this->eventBuilderMock->expects($this->once())
|
@@ -2477,6 +2558,34 @@ public function testGetFeatureVariableString()
|
2477 | 2558 | );
|
2478 | 2559 | }
|
2479 | 2560 |
|
| 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 | + |
2480 | 2589 | public function testSendImpressionEventWithNoAttributes(){
|
2481 | 2590 | $optlyObject = new OptimizelyTester($this->datafile, new ValidEventDispatcher(), $this->loggerMock);
|
2482 | 2591 |
|
@@ -2531,7 +2640,29 @@ public function testSendImpressionEventWithNoAttributes(){
|
2531 | 2640 | $optlyObject->sendImpressionEvent('group_experiment_1', 'group_exp_1_var_2', 'user_1', null);
|
2532 | 2641 | }
|
2533 | 2642 |
|
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 | + |
2535 | 2666 | public function testSendImpressionEventWithAttributes(){
|
2536 | 2667 | $optlyObject = new OptimizelyTester($this->datafile, new ValidEventDispatcher(), $this->loggerMock);
|
2537 | 2668 |
|
|
0 commit comments