Skip to content

Commit 5fd6321

Browse files
committed
TDP-1887 Don't init a SplitIO factory twice, even if the original was NULL. Pass a splitFile path instead of overriding system functionsi for testing.
1 parent 95467e7 commit 5fd6321

6 files changed

+28
-13
lines changed

README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,19 @@ You need to set the toggle "key" somewhere. This is usually something like a us
8181
];
8282

8383
## Testing
84+
### This Project
85+
composer test
8486

85-
composer test
87+
### Your Project
88+
The SplitIO SDK throws errors when it can't find a configuration file in localhost mode. To work around that, create a dummy local `split.yaml` file and ask the factory to load it. For example, you can set SPLITIO_SPLIT_FILE in your testing environment to point to your file:
89+
90+
# .env.testing
91+
SPLITIO_SPLIT_FILE=tests/__data__/split.yaml
92+
93+
This assumes a file like this one at `tests/__data__/split.yaml`:
94+
95+
- hello_world:
96+
treatment: off
97+
- hello_world:
98+
treatment: on
99+

config/feature-toggle.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
]
2323
],
2424

25-
'log' => ['adapter' => 'stdout', 'level' => 'error']
26-
]
27-
]
25+
'log' => ['adapter' => 'stdout', 'level' => 'error'],
26+
27+
'splitFile' => env('SPLITIO_SPLIT_FILE'),
28+
],
29+
],
2830
];

phpunit.xml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<server name="MAIL_DRIVER" value="array"/>
2121
<server name="QUEUE_CONNECTION" value="sync"/>
2222
<server name="SESSION_DRIVER" value="array"/>
23+
<server name="SPLITIO_SPLIT_FILE" value="tests/__data__/split.yaml"/>
2324
</php>
2425
<logging>
2526
<log type="junit" target="./junit.xml"/>

src/FeatureToggleClientProvider.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,13 @@ public function boot()
4747
{
4848
$this->publishes([
4949
__DIR__.'/../config/feature-toggle.php' => config_path('feature-toggle.php'),
50+
'config'
5051
]);
5152
}
5253

54+
private static $splitIOFactoryInitialized = false;
5355
private static $splitIOFactory;
56+
5457
/**
5558
* Return the singleton factory instance. This is only needed for testing, since
5659
* SplitIO enforces a singleton factory by returning an error the second time one
@@ -62,12 +65,13 @@ public function boot()
6265
*
6366
* @return SplitFactoryInterface
6467
*/
65-
private static function getSplitIOFactory(): SplitFactoryInterface {
66-
if (!static::$splitIOFactory) {
68+
private static function getSplitIOFactory(): ?SplitFactoryInterface {
69+
if (!static::$splitIOFactoryInitialized) {
6770
static::$splitIOFactory = Sdk::factory(
6871
config('feature-toggle.splitio.api_key'),
6972
config('feature-toggle.splitio.factory')
7073
);
74+
static::$splitIOFactoryInitialized = true;
7175
}
7276

7377
return static::$splitIOFactory;

src/Lib/SplitIOFeatureToggleClient.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class SplitIOFeatureToggleClient implements FeatureToggleClient {
1414

1515
function __construct(SplitFactoryInterface $factory) {
1616
$this->splitFactory = $factory;
17-
$this->splitClient = $factory->client();
17+
$this->splitClient = optional($factory)->client();
1818
}
1919

2020
public function setAttributes($attributes) {

tests/SplitIOTest.php

-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
<?php
22

3-
namespace SplitIO\Sdk;
4-
function posix_getpwuid() {
5-
return ['dir' => __DIR__ . '/__data__'];
6-
}
7-
83
namespace PartehGSS\Laravel\FreatureToggle\Tests;
94

105
use PartechGSS\Laravel\FeatureToggle\Contracts\FeatureToggleClient;
@@ -16,7 +11,6 @@ protected function getPackageProviders($app)
1611
return [FeatureToggleClientProvider::class];
1712
}
1813

19-
2014
/**
2115
* Test that the control treatment is returned for a non-existant flag.
2216
* @test

0 commit comments

Comments
 (0)