|
17 | 17 | namespace Optimizely\Tests; |
18 | 18 |
|
19 | 19 | use Exception; |
| 20 | +use GuzzleHttp\Client; |
| 21 | +use GuzzleHttp\Handler\MockHandler; |
| 22 | +use GuzzleHttp\HandlerStack; |
| 23 | +use GuzzleHttp\Middleware; |
| 24 | +use GuzzleHttp\Psr7\Response; |
20 | 25 | use Monolog\Logger; |
21 | 26 | use Optimizely\Config\DatafileProjectConfig; |
22 | 27 | use Optimizely\DecisionService\DecisionService; |
@@ -63,9 +68,7 @@ private function setOptimizelyConfigObject($optimizely, $config, $configManager) |
63 | 68 | $projConfig->setAccessible(true); |
64 | 69 | $projConfig->setValue($configManager, $config); |
65 | 70 |
|
66 | | - $projConfigManager = new \ReflectionProperty(Optimizely::class, '_projectConfigManager'); |
67 | | - $projConfigManager->setAccessible(true); |
68 | | - $projConfigManager->setValue($optimizely, $configManager); |
| 71 | + $optimizely->configManager = $configManager; |
69 | 72 | } |
70 | 73 |
|
71 | 74 | public function setUp() |
@@ -221,6 +224,92 @@ public function testInitDatafileInvalidFormat() |
221 | 224 | $this->expectOutputRegex('/Provided datafile is in an invalid format./'); |
222 | 225 | } |
223 | 226 |
|
| 227 | + public function testInitWithSdkKey() |
| 228 | + { |
| 229 | + $sdkKey = "some-sdk-key"; |
| 230 | + $optimizelyClient = new Optimizely( |
| 231 | + null, |
| 232 | + null, |
| 233 | + null, |
| 234 | + null, |
| 235 | + null, |
| 236 | + null, |
| 237 | + null, |
| 238 | + null, |
| 239 | + $sdkKey |
| 240 | + ); |
| 241 | + |
| 242 | + // client hasn't been mocked yet. Hence, activate should return null. |
| 243 | + $actualVariation = $optimizelyClient->activate('test_experiment_integer_feature', 'test_user'); |
| 244 | + $this->assertNull($actualVariation); |
| 245 | + |
| 246 | + // Mock http client to return a valid datafile |
| 247 | + $mock = new MockHandler([ |
| 248 | + new Response(200, [], $this->datafile) |
| 249 | + ]); |
| 250 | + |
| 251 | + $container = []; |
| 252 | + $history = Middleware::history($container); |
| 253 | + $handler = HandlerStack::create($mock); |
| 254 | + $handler->push($history); |
| 255 | + |
| 256 | + $client = new Client(['handler' => $handler]); |
| 257 | + $httpClient = new \ReflectionProperty(HTTPProjectConfigManager::class, 'httpClient'); |
| 258 | + $httpClient->setAccessible(true); |
| 259 | + $httpClient->setValue($optimizelyClient->configManager, $client); |
| 260 | + |
| 261 | + // Fetch datafile |
| 262 | + $optimizelyClient->configManager->fetch(); |
| 263 | + |
| 264 | + // activate should return expected variation. |
| 265 | + $actualVariation = $optimizelyClient->activate('test_experiment_integer_feature', 'test_user'); |
| 266 | + $this->assertEquals('variation', $actualVariation); |
| 267 | + |
| 268 | + // assert that https call is made to mock as expected. |
| 269 | + $transaction = $container[0]; |
| 270 | + $this->assertEquals( |
| 271 | + 'https://cdn.optimizely.com/datafiles/some-sdk-key.json', |
| 272 | + $transaction['request']->getUri() |
| 273 | + ); |
| 274 | + } |
| 275 | + |
| 276 | + public function testInitWithBothSdkKeyAndDatafile() |
| 277 | + { |
| 278 | + $sdkKey = "some-sdk-key"; |
| 279 | + $optimizelyClient = new Optimizely( |
| 280 | + DATAFILE, |
| 281 | + null, |
| 282 | + null, |
| 283 | + null, |
| 284 | + null, |
| 285 | + null, |
| 286 | + null, |
| 287 | + null, |
| 288 | + $sdkKey |
| 289 | + ); |
| 290 | + |
| 291 | + // client hasn't been mocked yet. Hence, config manager should return config of hardcoded |
| 292 | + // datafile. |
| 293 | + $this->assertEquals('15', $optimizelyClient->configManager->getConfig()->getRevision()); |
| 294 | + |
| 295 | + |
| 296 | + // Mock http client to return a valid datafile |
| 297 | + $mock = new MockHandler([ |
| 298 | + new Response(200, [], $this->typedAudiencesDataFile) |
| 299 | + ]); |
| 300 | + |
| 301 | + $handler = HandlerStack::create($mock); |
| 302 | + $client = new Client(['handler' => $handler]); |
| 303 | + $httpClient = new \ReflectionProperty(HTTPProjectConfigManager::class, 'httpClient'); |
| 304 | + $httpClient->setAccessible(true); |
| 305 | + $httpClient->setValue($optimizelyClient->configManager, $client); |
| 306 | + |
| 307 | + // Fetch datafile |
| 308 | + $optimizelyClient->configManager->fetch(); |
| 309 | + |
| 310 | + $this->assertEquals('3', $optimizelyClient->configManager->getConfig()->getRevision()); |
| 311 | + } |
| 312 | + |
224 | 313 | public function testActivateInvalidOptimizelyObject() |
225 | 314 | { |
226 | 315 | $optimizelyMock = $this->getMockBuilder(Optimizely::class) |
@@ -4471,9 +4560,7 @@ public function testGetConfigReturnsDatafileProjectConfigInstance() |
4471 | 4560 | ->setMethods(array('getConfig')) |
4472 | 4561 | ->getMock(); |
4473 | 4562 |
|
4474 | | - $projectConfigManager = new \ReflectionProperty(Optimizely::class, '_projectConfigManager'); |
4475 | | - $projectConfigManager->setAccessible(true); |
4476 | | - $projectConfigManager->setValue($optlyObject, $projectConfigManagerMock); |
| 4563 | + $optlyObject->configManager = $projectConfigManagerMock; |
4477 | 4564 |
|
4478 | 4565 | $expectedProjectConfig = new DatafileProjectConfig($this->datafile, $this->loggerMock, new NoOpErrorHandler()); |
4479 | 4566 |
|
|
0 commit comments