-
Notifications
You must be signed in to change notification settings - Fork 30
feat: Add init with sdk key #189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
26dd1a0
4844558
f56015a
1b8d6ed
829a1d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/** | ||
* Copyright 2020, Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
namespace Optimizely; | ||
|
||
use Optimizely\Optimizely; | ||
|
||
/** | ||
* Class OptimizelyFactory | ||
* | ||
* @package Optimizely | ||
*/ | ||
class OptimizelyFactory | ||
{ | ||
public static function createDefaultInstance($sdkKey, $fallbackDatafile = null) | ||
{ | ||
return new Optimizely( | ||
$fallbackDatafile, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
$sdkKey | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* Copyright 2020, Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
namespace Optimizely\Tests; | ||
|
||
use Exception; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Handler\MockHandler; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Psr7\Response; | ||
use Optimizely\OptimizelyFactory; | ||
use Optimizely\ProjectConfigManager\HTTPProjectConfigManager; | ||
|
||
class OptimizelyFactoryTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function setUp() | ||
{ | ||
$this->datafile = DATAFILE; | ||
$this->typedAudiencesDataFile = DATAFILE_WITH_TYPED_AUDIENCES; | ||
} | ||
|
||
public function testDefaultInstance() | ||
{ | ||
$optimizelyClient = OptimizelyFactory::createDefaultInstance("some-sdk-key", $this->datafile); | ||
|
||
// client hasn't been mocked yet. Hence, config manager should return config of hardcoded | ||
// datafile. | ||
$this->assertEquals('15', $optimizelyClient->configManager->getConfig()->getRevision()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should mock it anyway, to prevent it from ever making a real HTTP request. Even if it won't because of hard coded datafile. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given the way PHP Unit allows to use doubles, I don't think we can mock the first http call that a config manager makes when it's initialized inside Optly client. I can only mock and set a double, after the Optimizely client has been created and in that case, it has already made a real HTTP request. |
||
|
||
// Mock http client to return a valid datafile | ||
$mock = new MockHandler([ | ||
new Response(200, [], $this->typedAudiencesDataFile) | ||
]); | ||
|
||
$handler = HandlerStack::create($mock); | ||
|
||
$client = new Client(['handler' => $handler]); | ||
$httpClient = new \ReflectionProperty(HTTPProjectConfigManager::class, 'httpClient'); | ||
$httpClient->setAccessible(true); | ||
$httpClient->setValue($optimizelyClient->configManager, $client); | ||
|
||
/// Fetch datafile | ||
$optimizelyClient->configManager->fetch(); | ||
|
||
$this->assertEquals('3', $optimizelyClient->configManager->getConfig()->getRevision()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this have to be public now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HTTPProjectConfigManager exposes a public method fetch, which the user can use to get the latest datafile from cdn. Previously, we were expecting the user to create it's own config manager and pass. Now, we will be creating httpconfigmanager given the sdk key so the user must be able to access it to fetch the latest datafile.