@@ -179,6 +179,29 @@ public function testActivateInvalidOptimizelyObject()
179
179
$ this ->expectOutputRegex ('/Datafile has invalid format. Failing "activate"./ ' );
180
180
}
181
181
182
+ public function testActivateCallsValidateInputs ()
183
+ {
184
+ $ optimizelyMock = $ this ->getMockBuilder (Optimizely::class)
185
+ ->setConstructorArgs (array ($ this ->datafile ))
186
+ ->setMethods (array ('validateInputs ' ))
187
+ ->getMock ();
188
+
189
+ $ experimentKey = 'test_experiment ' ;
190
+ $ userId = 'test_user ' ;
191
+ $ inputArray = [
192
+ Optimizely::EXPERIMENT_KEY => $ experimentKey ,
193
+ Optimizely::USER_ID => $ userId
194
+ ];
195
+
196
+ // assert that validateInputs gets called with exactly same keys
197
+ $ optimizelyMock ->expects ($ this ->once ())
198
+ ->method ('validateInputs ' )
199
+ ->with ($ inputArray )
200
+ ->willReturn (false );
201
+
202
+ $ this ->assertNull ($ optimizelyMock ->activate ($ experimentKey , $ userId ));
203
+ }
204
+
182
205
public function testActivateInvalidAttributes ()
183
206
{
184
207
$ this ->loggerMock ->expects ($ this ->exactly (2 ))
@@ -460,6 +483,29 @@ public function testGetVariationInvalidOptimizelyObject()
460
483
$ this ->expectOutputRegex ('/Datafile has invalid format. Failing "getVariation"./ ' );
461
484
}
462
485
486
+ public function testGetVariationCallsValidateInputs ()
487
+ {
488
+ $ optimizelyMock = $ this ->getMockBuilder (Optimizely::class)
489
+ ->setConstructorArgs (array ($ this ->datafile ))
490
+ ->setMethods (array ('validateInputs ' ))
491
+ ->getMock ();
492
+
493
+ $ experimentKey = 'test_experiment ' ;
494
+ $ userId = 'test_user ' ;
495
+ $ inputArray = [
496
+ Optimizely::EXPERIMENT_KEY => $ experimentKey ,
497
+ Optimizely::USER_ID => $ userId
498
+ ];
499
+
500
+ // assert that validateInputs gets called with exactly same keys
501
+ $ optimizelyMock ->expects ($ this ->once ())
502
+ ->method ('validateInputs ' )
503
+ ->with ($ inputArray )
504
+ ->willReturn (false );
505
+
506
+ $ this ->assertNull ($ optimizelyMock ->getVariation ($ experimentKey , $ userId ));
507
+ }
508
+
463
509
public function testGetVariationInvalidAttributes ()
464
510
{
465
511
$ this ->loggerMock ->expects ($ this ->once ())
@@ -679,6 +725,29 @@ public function testTrackInvalidOptimizelyObject()
679
725
$ this ->expectOutputRegex ('/Datafile has invalid format. Failing "track"./ ' );
680
726
}
681
727
728
+ public function testTrackCallsValidateInputs ()
729
+ {
730
+ $ optimizelyMock = $ this ->getMockBuilder (Optimizely::class)
731
+ ->setConstructorArgs (array ($ this ->datafile ))
732
+ ->setMethods (array ('validateInputs ' ))
733
+ ->getMock ();
734
+
735
+ $ eventKey = 'test_event ' ;
736
+ $ userId = 'test_user ' ;
737
+ $ inputArray = [
738
+ Optimizely::EVENT_KEY => $ eventKey ,
739
+ Optimizely::USER_ID => $ userId
740
+ ];
741
+
742
+ // assert that validateInputs gets called with exactly same keys
743
+ $ optimizelyMock ->expects ($ this ->once ())
744
+ ->method ('validateInputs ' )
745
+ ->with ($ inputArray )
746
+ ->willReturn (false );
747
+
748
+ $ this ->assertNull ($ optimizelyMock ->track ($ eventKey , $ userId ));
749
+ }
750
+
682
751
public function testTrackInvalidAttributes ()
683
752
{
684
753
$ this ->loggerMock ->expects ($ this ->once ())
@@ -1632,14 +1701,17 @@ public function testSetForcedVariationWithInvalidUserIDs()
1632
1701
'location ' => 'San Francisco '
1633
1702
];
1634
1703
1635
- // null user ID --> set should fail, normal bucketing [TODO (Alda): getVariation on a null userID should return null]
1704
+ // null user ID should fail setForcedVariation. getVariation on a null userID should return null
1636
1705
$ this ->assertFalse ($ optlyObject ->setForcedVariation ('test_experiment ' , null , 'bad_variation ' ), 'Set variation for null user ID should have failed. ' );
1706
+
1637
1707
$ variationKey = $ optlyObject ->getVariation ('test_experiment ' , null , $ userAttributes );
1638
- $ this ->assertEquals ('variation ' , $ variationKey , sprintf ('Invalid variation "%s" for null user ID. ' , $ variationKey ));
1639
- // empty string user ID --> set should fail, normal bucketing [TODO (Alda): getVariation on an empty userID should return null]
1708
+ $ this ->assertNull ($ variationKey );
1709
+
1710
+ // empty string user ID should fail setForcedVariation. getVariation on an empty userID should return null
1640
1711
$ this ->assertFalse ($ optlyObject ->setForcedVariation ('test_experiment ' , '' , 'bad_variation ' ), 'Set variation for empty string user ID should have failed. ' );
1712
+
1641
1713
$ variationKey = $ optlyObject ->getVariation ('test_experiment ' , '' , $ userAttributes );
1642
- $ this ->assertEquals ( ' variation ' , $ variationKey, sprintf ( ' Invalid variation "%s" for empty string user ID. ' , $ variationKey ) );
1714
+ $ this ->assertNull ( $ variationKey );
1643
1715
}
1644
1716
1645
1717
public function testSetForcedVariationWithInvalidExperimentKeys ()
@@ -2850,4 +2922,66 @@ public function testSendImpressionEventWithAttributes()
2850
2922
2851
2923
$ optlyObject ->sendImpressionEvent ('test_experiment ' , 'control ' , 'test_user ' , $ userAttributes );
2852
2924
}
2925
+
2926
+ /*
2927
+ * test ValidateInputs method validates and logs for different and multiple keys
2928
+ */
2929
+ public function testValidateInputs ()
2930
+ {
2931
+ $ optlyObject = new OptimizelyTester ($ this ->datafile , null , $ this ->loggerMock );
2932
+
2933
+ $ INVALID_USER_ID_LOG = 'Provided User ID is in an invalid format. ' ;
2934
+ $ INVALID_EVENT_KEY_LOG = 'Provided Event Key is in an invalid format. ' ;
2935
+ $ INVALID_RANDOM_KEY_LOG = 'Provided ABCD is in an invalid format. ' ;
2936
+
2937
+ $ this ->loggerMock ->expects ($ this ->at (0 ))
2938
+ ->method ('log ' )
2939
+ ->with (Logger::ERROR , $ INVALID_USER_ID_LOG );
2940
+ $ this ->assertFalse ($ optlyObject ->validateInputs ([Optimizely::USER_ID => null ]));
2941
+
2942
+ $ this ->loggerMock ->expects ($ this ->at (0 ))
2943
+ ->method ('log ' )
2944
+ ->with (Logger::ERROR , $ INVALID_RANDOM_KEY_LOG );
2945
+ $ this ->assertFalse ($ optlyObject ->validateInputs (['ABCD ' => null ]));
2946
+
2947
+ // Verify that multiple messages are logged.
2948
+ $ this ->loggerMock ->expects ($ this ->at (0 ))
2949
+ ->method ('log ' )
2950
+ ->with (Logger::ERROR , $ INVALID_EVENT_KEY_LOG );
2951
+
2952
+ $ this ->loggerMock ->expects ($ this ->at (1 ))
2953
+ ->method ('log ' )
2954
+ ->with (Logger::ERROR , $ INVALID_USER_ID_LOG );
2955
+ $ this ->assertFalse ($ optlyObject ->validateInputs ([Optimizely::EVENT_KEY => null , Optimizely::USER_ID => null ]));
2956
+
2957
+ // Verify that logger level is taken into account
2958
+ $ this ->loggerMock ->expects ($ this ->at (0 ))
2959
+ ->method ('log ' )
2960
+ ->with (Logger::INFO , $ INVALID_RANDOM_KEY_LOG );
2961
+ $ this ->assertFalse ($ optlyObject ->validateInputs (['ABCD ' => null ], Logger::INFO ));
2962
+
2963
+ // Verify that it returns true and nothing is logged if valid values given
2964
+ $ this ->loggerMock ->expects ($ this ->never ())
2965
+ ->method ('log ' );
2966
+ $ this ->assertTrue ($ optlyObject ->validateInputs ([Optimizely::EVENT_KEY => 'test_event ' , Optimizely::USER_ID => 'test_user ' ]));
2967
+ }
2968
+
2969
+ /*
2970
+ * test ValidateInputs method only returns true for non-empty string
2971
+ */
2972
+ public function testValidateInputsWithDifferentValues ()
2973
+ {
2974
+ $ optlyObject = new OptimizelyTester ($ this ->datafile );
2975
+
2976
+ $ this ->assertTrue ($ optlyObject ->validateInputs (['key ' => '0 ' ]));
2977
+ $ this ->assertTrue ($ optlyObject ->validateInputs (['key ' => 'test_user ' ]));
2978
+
2979
+ $ this ->assertFalse ($ optlyObject ->validateInputs (['key ' => '' ]));
2980
+ $ this ->assertFalse ($ optlyObject ->validateInputs (['key ' => null ]));
2981
+ $ this ->assertFalse ($ optlyObject ->validateInputs (['key ' => false ]));
2982
+ $ this ->assertFalse ($ optlyObject ->validateInputs (['key ' => true ]));
2983
+ $ this ->assertFalse ($ optlyObject ->validateInputs (['key ' => 2 ]));
2984
+ $ this ->assertFalse ($ optlyObject ->validateInputs (['key ' => 2.0 ]));
2985
+ $ this ->assertFalse ($ optlyObject ->validateInputs (['key ' => array ()]));
2986
+ }
2853
2987
}
0 commit comments