Skip to content

Commit 21096b0

Browse files
committed
chore: Add more test coverage for strategy context and evaluation
1 parent 39ec2e9 commit 21096b0

2 files changed

Lines changed: 145 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenFeature\Test\unit;
6+
7+
use InvalidArgumentException;
8+
use OpenFeature\implementation\flags\EvaluationContext;
9+
use OpenFeature\implementation\multiprovider\strategy\StrategyEvaluationContext;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class StrategyEvaluationContextTest extends TestCase
13+
{
14+
public function testValidBooleanFlagType(): void
15+
{
16+
$context = new StrategyEvaluationContext('flag-key', 'boolean', true, new EvaluationContext());
17+
$this->assertEquals('flag-key', $context->getFlagKey());
18+
$this->assertEquals('boolean', $context->getFlagType());
19+
$this->assertTrue($context->getDefaultValue());
20+
$this->assertInstanceOf(EvaluationContext::class, $context->getEvaluationContext());
21+
}
22+
23+
public function testValidStringFlagType(): void
24+
{
25+
$context = new StrategyEvaluationContext('flag-key', 'string', 'default', new EvaluationContext());
26+
$this->assertEquals('string', $context->getFlagType());
27+
$this->assertEquals('default', $context->getDefaultValue());
28+
}
29+
30+
public function testValidIntegerFlagType(): void
31+
{
32+
$context = new StrategyEvaluationContext('flag-key', 'integer', 42, new EvaluationContext());
33+
$this->assertEquals('integer', $context->getFlagType());
34+
$this->assertEquals(42, $context->getDefaultValue());
35+
}
36+
37+
public function testValidFloatFlagType(): void
38+
{
39+
$context = new StrategyEvaluationContext('flag-key', 'float', 3.14, new EvaluationContext());
40+
$this->assertEquals('float', $context->getFlagType());
41+
$this->assertEquals(3.14, $context->getDefaultValue());
42+
}
43+
44+
public function testValidObjectFlagType(): void
45+
{
46+
$context = new StrategyEvaluationContext('flag-key', 'object', ['key' => 'value'], new EvaluationContext());
47+
$this->assertEquals('object', $context->getFlagType());
48+
$this->assertEquals(['key' => 'value'], $context->getDefaultValue());
49+
}
50+
51+
public function testInvalidBooleanDefaultValueThrows(): void
52+
{
53+
$this->expectException(InvalidArgumentException::class);
54+
new StrategyEvaluationContext('flag-key', 'boolean', 'not-a-bool', new EvaluationContext());
55+
}
56+
57+
public function testInvalidStringDefaultValueThrows(): void
58+
{
59+
$this->expectException(InvalidArgumentException::class);
60+
new StrategyEvaluationContext('flag-key', 'string', 123, new EvaluationContext());
61+
}
62+
63+
public function testInvalidIntegerDefaultValueThrows(): void
64+
{
65+
$this->expectException(InvalidArgumentException::class);
66+
new StrategyEvaluationContext('flag-key', 'integer', 'not-an-int', new EvaluationContext());
67+
}
68+
69+
public function testInvalidFloatDefaultValueThrows(): void
70+
{
71+
$this->expectException(InvalidArgumentException::class);
72+
new StrategyEvaluationContext('flag-key', 'float', 'not-a-float', new EvaluationContext());
73+
}
74+
75+
public function testInvalidObjectDefaultValueThrows(): void
76+
{
77+
$this->expectException(InvalidArgumentException::class);
78+
new StrategyEvaluationContext('flag-key', 'object', 'not-an-array', new EvaluationContext());
79+
}
80+
81+
public function testUnknownFlagTypeThrows(): void
82+
{
83+
$this->expectException(InvalidArgumentException::class);
84+
new StrategyEvaluationContext('flag-key', 'unknown-type', 'value', new EvaluationContext());
85+
}
86+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenFeature\Test\unit;
6+
7+
use Mockery;
8+
use Mockery\MockInterface;
9+
use OpenFeature\implementation\flags\EvaluationContext;
10+
use OpenFeature\implementation\multiprovider\strategy\StrategyEvaluationContext;
11+
use OpenFeature\implementation\multiprovider\strategy\StrategyPerProviderContext;
12+
use OpenFeature\interfaces\provider\Provider;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class StrategyPerProviderContextTest extends TestCase
16+
{
17+
/** @var Provider&MockInterface */
18+
private Provider $mockProvider;
19+
20+
protected function setUp(): void
21+
{
22+
parent::setUp();
23+
$this->mockProvider = Mockery::mock(Provider::class);
24+
$this->mockProvider->shouldReceive('getMetadata->getName')->andReturn('TestProvider');
25+
}
26+
27+
public function testProviderContextGetters(): void
28+
{
29+
$baseContext = new StrategyEvaluationContext(
30+
'flag-key',
31+
'string',
32+
'default-value',
33+
new EvaluationContext(),
34+
);
35+
$providerName = 'TestProviderName';
36+
$context = new StrategyPerProviderContext($baseContext, $providerName, $this->mockProvider);
37+
38+
$this->assertEquals('flag-key', $context->getFlagKey());
39+
$this->assertEquals('string', $context->getFlagType());
40+
$this->assertEquals('default-value', $context->getDefaultValue());
41+
$this->assertInstanceOf(EvaluationContext::class, $context->getEvaluationContext());
42+
$this->assertEquals($providerName, $context->getProviderName());
43+
$this->assertSame($this->mockProvider, $context->getProvider());
44+
}
45+
46+
public function testProviderContextWithDifferentProviderName(): void
47+
{
48+
$baseContext = new StrategyEvaluationContext(
49+
'flag-key',
50+
'boolean',
51+
true,
52+
new EvaluationContext(),
53+
);
54+
$providerName = 'AnotherProvider';
55+
$context = new StrategyPerProviderContext($baseContext, $providerName, $this->mockProvider);
56+
57+
$this->assertEquals($providerName, $context->getProviderName());
58+
}
59+
}

0 commit comments

Comments
 (0)