Skip to content

Commit bffeba0

Browse files
committed
resetting context for integration tests
1 parent 61ed071 commit bffeba0

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

src/SplitIO/Component/Common/Context.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace SplitIO\Component\Common;
33

44
use SplitIO\Component\Log\Logger;
5+
use SplitIO\Exception\Exception;
56

67
/**
78
* Class Context
@@ -77,10 +78,10 @@ public function __wakeup()
7778
public static function trackFactory($apiKey)
7879
{
7980
$current = self::getInstance()->factoryTracker[$apiKey] ?? 0;
80-
if ($current == 0) {
81-
self::getInstance()->getLogger()->warning(self::MULTIPLE_INSTANCES);
82-
} else {
81+
if ($current > 0) {
8382
self::getInstance()->getLogger()->warning(sprintf(self::SAME_APIKEY, $current));
83+
} elseif (count(self::getInstance()->factoryTracker) > 0) {
84+
self::getInstance()->getLogger()->warning(self::MULTIPLE_INSTANCES);
8485
}
8586
$current += 1;
8687
self::getInstance()->factoryTracker[$apiKey] = $current;

src/SplitIO/Sdk.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ public static function factory($apiKey = 'localhost', array $options = array())
3131
//Adding API Key into args array.
3232
$options['apiKey'] = $apiKey;
3333

34-
//Tracking Factory Instantiation
35-
Context::trackFactory($apiKey);
36-
3734
//Register Logger
3835
self::registerLogger((isset($options['log'])) ? $options['log'] : array());
3936

37+
//Tracking Factory Instantiation
38+
Context::trackFactory($apiKey);
39+
4040
if ($apiKey == 'localhost') {
4141
return new LocalhostSplitFactory($options);
4242
} else {

tests/Suite/Redis/ReflectiveTools.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,14 @@ public static function overrideTracker()
8282
$property->setAccessible(true);
8383
$property->setValue($di, array());
8484
}
85+
86+
public static function resetContext()
87+
{
88+
$context = Context::getInstance();
89+
$reflection = new \ReflectionClass($context);
90+
$instance = $reflection->getProperty('instance');
91+
$instance->setAccessible(true);
92+
$instance->setValue(null, null);
93+
$instance->setAccessible(false);
94+
}
8595
}

tests/Suite/Sdk/SdkClientTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace SplitIO\Test\Suite\Sdk;
33

44
use \stdClass;
5+
use ReflectionClass;
56
use SplitIO\Test\Suite\Redis\ReflectiveTools;
67
use SplitIO\Component\Cache\ImpressionCache;
78
use SplitIO\Component\Cache\EventsCache;
@@ -19,6 +20,7 @@ class SdkClientTest extends \PHPUnit\Framework\TestCase
1920
{
2021
public function testLocalClient()
2122
{
23+
ReflectiveTools::resetContext();
2224
$options['splitFile'] = dirname(dirname(__DIR__)).'/files/.splits';
2325
$splitFactory = \SplitIO\Sdk::factory('localhost', $options);
2426
$splitSdk = $splitFactory->client();
@@ -35,6 +37,7 @@ public function testLocalClient()
3537

3638
public function testLocalClientYAML()
3739
{
40+
ReflectiveTools::resetContext();
3841
$options['splitFile'] = dirname(dirname(__DIR__)).'/files/splits.yml';
3942
$splitFactory = \SplitIO\Sdk::factory('localhost', $options);
4043
$splitSdk = $splitFactory->client();
@@ -207,6 +210,7 @@ private function validateLastImpression(
207210

208211
public function testClient()
209212
{
213+
ReflectiveTools::resetContext();
210214
//Testing version string
211215
$this->assertTrue(is_string(\SplitIO\version()));
212216

@@ -437,6 +441,7 @@ public function testClient()
437441
*/
438442
public function testCustomLog()
439443
{
444+
ReflectiveTools::resetContext();
440445
// create a log channel
441446
$log = $this
442447
->getMockBuilder('Psr\Log\LoggerInterface')
@@ -472,6 +477,7 @@ public function testCustomLog()
472477

473478
public function testInvalidCacheAdapter()
474479
{
480+
ReflectiveTools::resetContext();
475481
$this->expectException('\SplitIO\Exception\Exception');
476482

477483
$sdkConfig = array(
@@ -485,6 +491,7 @@ public function testInvalidCacheAdapter()
485491

486492
public function testCacheExceptionReturnsControl()
487493
{
494+
ReflectiveTools::resetContext();
488495
$log = $this
489496
->getMockBuilder('Psr\Log\LoggerInterface')
490497
->disableOriginalConstructor()
@@ -523,6 +530,7 @@ public function testCacheExceptionReturnsControl()
523530

524531
public function testGetTreatmentsWithDistinctFeatures()
525532
{
533+
ReflectiveTools::resetContext();
526534
//Testing version string
527535
$this->assertTrue(is_string(\SplitIO\version()));
528536

@@ -559,13 +567,13 @@ public function testGetTreatmentsWithDistinctFeatures()
559567

560568
public function testGetTreatmentsWithRepeteadedFeatures()
561569
{
570+
ReflectiveTools::resetContext();
562571
//Testing version string
563572
$this->assertTrue(is_string(\SplitIO\version()));
564573

565574
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
566575
$options = array('prefix' => TEST_PREFIX);
567576

568-
ReflectiveTools::resetIPAddress();
569577
$sdkConfig = array(
570578
'log' => array('adapter' => 'stdout'),
571579
'cache' => array('adapter' => 'predis', 'parameters' => $parameters, 'options' => $options),
@@ -597,13 +605,13 @@ public function testGetTreatmentsWithRepeteadedFeatures()
597605

598606
public function testGetTreatmentsWithRepeteadedAndNullFeatures()
599607
{
608+
ReflectiveTools::resetIPAddress();
600609
//Testing version string
601610
$this->assertTrue(is_string(\SplitIO\version()));
602611

603612
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
604613
$options = array('prefix' => TEST_PREFIX);
605614

606-
ReflectiveTools::resetIPAddress();
607615
$sdkConfig = array(
608616
'log' => array('adapter' => 'stdout'),
609617
'cache' => array('adapter' => 'predis', 'parameters' => $parameters, 'options' => $options),
@@ -635,6 +643,7 @@ public function testGetTreatmentsWithRepeteadedAndNullFeatures()
635643

636644
public function testGetTreatmentsFetchesSplitsInOneCall()
637645
{
646+
ReflectiveTools::resetIPAddress();
638647
// Set redis-library client mock
639648
$predisMock = $this
640649
->getMockBuilder('\Predis\Client')
@@ -682,13 +691,13 @@ public function testGetTreatmentsFetchesSplitsInOneCall()
682691

683692
public function testMultipleInstantiationNotOverrideIP()
684693
{
694+
ReflectiveTools::resetIPAddress();
685695
//Testing version string
686696
$this->assertTrue(is_string(\SplitIO\version()));
687697

688698
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
689699
$options = array('prefix' => TEST_PREFIX);
690700

691-
ReflectiveTools::resetIPAddress();
692701
$sdkConfig = array(
693702
'log' => array('adapter' => 'stdout'),
694703
'cache' => array('adapter' => 'predis', 'parameters' => $parameters, 'options' => $options),

0 commit comments

Comments
 (0)