Skip to content

Commit 2c4dba8

Browse files
authored
test: Write test for OpenfeatureAPI to return No-op client when OpenfeatureClient is unresolvable (open-feature#157)
### Overview This pull request refactors the client creation logic in `OpenFeatureAPI` to improve testability and resilience. - Introduces a dedicated `resolveClient` method, which encapsulates client instantiation logic. - Ensures that failures during client resolution are handled gracefully by returning a `NoOpClient`. - Adds a testing dependency to allow mocking of final classes, enabling comprehensive test coverage without altering production code. --------- Signed-off-by: tmakinde <[email protected]>
1 parent 17e0510 commit 2c4dba8

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"behat/behat": "^3.11",
2424
"captainhook/captainhook": "^5.10",
2525
"captainhook/plugin-composer": "^5.3",
26+
"dg/bypass-finals": "^1.9",
2627
"ergebnis/composer-normalize": "^2.25",
2728
"hamcrest/hamcrest-php": "^2.0",
2829
"mdwheele/zalgo": "^0.3.1",

src/OpenFeatureAPI.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public function getProvider(): Provider
6666
return $this->provider;
6767
}
6868

69+
protected function resolveClient(string $name, string $version): OpenFeatureClient
70+
{
71+
return new OpenFeatureClient($this, $name, $version);
72+
}
73+
6974
/**
7075
* -----------------
7176
* Requirement 1.1.2
@@ -103,7 +108,7 @@ public function getClient(?string $name = null, ?string $version = null): Client
103108
$version = $version ?? 'OpenFeature';
104109

105110
try {
106-
$client = new OpenFeatureClient($this, $name, $version);
111+
$client = $this->resolveClient($name, $version);
107112
$client->setLogger($this->getLogger());
108113

109114
return $client;

tests/unit/OpenFeatureAPITest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
namespace OpenFeature\Test\unit;
66

7+
use DG\BypassFinals;
8+
use Exception;
79
use Mockery\MockInterface;
810
use OpenFeature\OpenFeatureAPI;
911
use OpenFeature\Test\APITestHelper;
1012
use OpenFeature\Test\TestCase;
1113
use OpenFeature\Test\TestHook;
1214
use OpenFeature\Test\TestProvider;
1315
use OpenFeature\implementation\flags\EvaluationContext;
16+
use OpenFeature\implementation\flags\NoOpClient;
1417
use OpenFeature\implementation\provider\NoOpProvider;
1518
use OpenFeature\interfaces\flags\API;
1619
use OpenFeature\interfaces\hooks\Hook;
@@ -220,4 +223,23 @@ public function testApiWillCreateClientWithProvider(): void
220223
$this->assertEquals($name, $client->getMetadata()->getName());
221224
$this->assertInstanceOf(TestProvider::class, $api->getProvider());
222225
}
226+
227+
/**
228+
* @runInSeparateProcess
229+
* @preserveGlobalState disabled
230+
*/
231+
public function testReturnsNoOpClientWhenClientResolutionFails(): void
232+
{
233+
BypassFinals::enable();
234+
$name = 'test-name';
235+
$version = 'test-version';
236+
/** @var (MockInterface&OpenFeatureAPI) $api @phpstan-ignore-line */
237+
$api = $this->mockery(OpenFeatureAPI::class)->shouldAllowMockingProtectedMethods()->makePartial(); // @phpstan-ignore-line
238+
239+
$api->shouldReceive('resolveClient')->once()
240+
->with($name, $version)
241+
->andThrow(new Exception('Simulated client resolution failure'));
242+
$client = $api->getClient($name, $version);
243+
$this->assertInstanceOf(NoOpClient::class, $client);
244+
}
223245
}

0 commit comments

Comments
 (0)