Skip to content

Commit 1b8d6ed

Browse files
committed
feat: Add defaultinstance in OptimizelyFactory
1 parent f56015a commit 1b8d6ed

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

Diff for: src/Optimizely/OptimizelyFactory.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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;
18+
19+
use Optimizely\Optimizely;
20+
21+
/**
22+
* Class OptimizelyFactory
23+
*
24+
* @package Optimizely
25+
*/
26+
class OptimizelyFactory
27+
{
28+
public static function createDefaultInstance($sdkKey, $fallbackDatafile = null)
29+
{
30+
return new Optimizely(
31+
$fallbackDatafile,
32+
null,
33+
null,
34+
null,
35+
null,
36+
null,
37+
null,
38+
null,
39+
$sdkKey
40+
);
41+
}
42+
}

Diff for: tests/OptimizelyFactoryTest.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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

Comments
 (0)