|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2020, Optimizely |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +namespace Optimizely\Tests; |
| 18 | + |
| 19 | +use Exception; |
| 20 | +use GuzzleHttp\Client; |
| 21 | +use GuzzleHttp\Handler\MockHandler; |
| 22 | +use GuzzleHttp\HandlerStack; |
| 23 | +use GuzzleHttp\Psr7\Response; |
| 24 | +use Optimizely\OptimizelyFactory; |
| 25 | +use Optimizely\ProjectConfigManager\HTTPProjectConfigManager; |
| 26 | + |
| 27 | +class OptimizelyFactoryTest extends \PHPUnit_Framework_TestCase |
| 28 | +{ |
| 29 | + public function setUp() |
| 30 | + { |
| 31 | + $this->datafile = DATAFILE; |
| 32 | + $this->typedAudiencesDataFile = DATAFILE_WITH_TYPED_AUDIENCES; |
| 33 | + } |
| 34 | + |
| 35 | + public function testDefaultInstance() |
| 36 | + { |
| 37 | + $optimizelyClient = OptimizelyFactory::createDefaultInstance("some-sdk-key", $this->datafile); |
| 38 | + |
| 39 | + // client hasn't been mocked yet. Hence, config manager should return config of hardcoded |
| 40 | + // datafile. |
| 41 | + $this->assertEquals('15', $optimizelyClient->configManager->getConfig()->getRevision()); |
| 42 | + |
| 43 | + // Mock http client to return a valid datafile |
| 44 | + $mock = new MockHandler([ |
| 45 | + new Response(200, [], $this->typedAudiencesDataFile) |
| 46 | + ]); |
| 47 | + |
| 48 | + $handler = HandlerStack::create($mock); |
| 49 | + |
| 50 | + $client = new Client(['handler' => $handler]); |
| 51 | + $httpClient = new \ReflectionProperty(HTTPProjectConfigManager::class, 'httpClient'); |
| 52 | + $httpClient->setAccessible(true); |
| 53 | + $httpClient->setValue($optimizelyClient->configManager, $client); |
| 54 | + |
| 55 | + /// Fetch datafile |
| 56 | + $optimizelyClient->configManager->fetch(); |
| 57 | + |
| 58 | + $this->assertEquals('3', $optimizelyClient->configManager->getConfig()->getRevision()); |
| 59 | + } |
| 60 | +} |
0 commit comments