Skip to content

Commit 0c4aec4

Browse files
committed
add some tests
1 parent b8c5c14 commit 0c4aec4

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

src/Symfony/Component/FeatureFlag/FeatureChecker.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\FeatureFlag;
1313

14+
use Symfony\Component\FeatureFlag\Exception\FeatureNotFoundException;
15+
1416
final class FeatureChecker implements FeatureCheckerInterface
1517
{
1618
private array $cache = [];
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\FeatureFlag\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\FeatureFlag\FeatureChecker;
16+
use Symfony\Component\FeatureFlag\FeatureRegistry;
17+
18+
class FeatureCheckerTest extends TestCase
19+
{
20+
public function testGetValue()
21+
{
22+
$featureChecker = new FeatureChecker(new FeatureRegistry([
23+
'feature_integer' => fn () => 42,
24+
'feature_random' => fn () => random_int(1, 42),
25+
]));
26+
27+
$this->assertSame(42, $featureChecker->getValue('feature_integer'));
28+
$this->assertIsInt($value = $featureChecker->getValue('feature_random'));
29+
$this->assertSame($value, $featureChecker->getValue('feature_random'));
30+
}
31+
32+
public function testGetDefaultValue()
33+
{
34+
$featureRegistry = new FeatureRegistry([
35+
'existing_feature' => fn () => 1,
36+
]);
37+
38+
$this->assertSame(1, (new FeatureChecker($featureRegistry))->getValue('existing_feature'));
39+
$this->assertSame(1, (new FeatureChecker($featureRegistry, 42))->getValue('existing_feature'));
40+
41+
$this->assertSame(false, (new FeatureChecker($featureRegistry))->getValue('unknown_feature'));
42+
$this->assertSame(42, (new FeatureChecker($featureRegistry, 42))->getValue('unknown_feature'));
43+
}
44+
45+
public function testIsEnabled()
46+
{
47+
$featureChecker = new FeatureChecker(new FeatureRegistry([
48+
'feature_true' => fn () => true,
49+
'feature_false' => fn () => false,
50+
'feature_integer' => fn () => 1,
51+
]));
52+
53+
$this->assertTrue($featureChecker->isEnabled('feature_true'));
54+
$this->assertFalse($featureChecker->isEnabled('feature_false'));
55+
$this->assertFalse($featureChecker->isEnabled('feature_integer'));
56+
$this->assertFalse($featureChecker->isEnabled('unknown_feature'));
57+
}
58+
59+
/**
60+
* @dataProvider provideIsEnabledWithExpectedValue
61+
*/
62+
public function testIsEnabledWithExpectedValue(string $featureName, mixed $expectedFeatureValue, bool $expectedResult)
63+
{
64+
$featureChecker = new FeatureChecker(new FeatureRegistry([
65+
'feature_true' => fn () => true,
66+
'feature_integer' => fn () => 1,
67+
]));
68+
69+
$this->assertSame($expectedResult, $featureChecker->isEnabled($featureName, $expectedFeatureValue));
70+
}
71+
72+
public static function provideIsEnabledWithExpectedValue()
73+
{
74+
yield 'with the same boolean' => ['feature_true', true, true];
75+
yield 'with the same integer' => ['feature_integer', 1, true];
76+
yield 'with a different boolean' => ['feature_true', false, false];
77+
yield 'with different types' => ['feature_integer', true, false];
78+
}
79+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\FeatureFlag\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\FeatureFlag\Exception\FeatureNotFoundException;
16+
use Symfony\Component\FeatureFlag\FeatureRegistry;
17+
18+
class FeatureRegistryTest extends TestCase
19+
{
20+
private FeatureRegistry $featureRegistry;
21+
22+
protected function setUp(): void
23+
{
24+
$this->featureRegistry = new FeatureRegistry([
25+
'first_feature' => fn () => true,
26+
'second_feature' => fn () => false,
27+
]);
28+
}
29+
30+
public function testGet()
31+
{
32+
$this->assertIsCallable($this->featureRegistry->get('first_feature'));
33+
}
34+
35+
public function testGetNotFound()
36+
{
37+
$this->expectException(FeatureNotFoundException::class);
38+
$this->expectExceptionMessage('Feature "unknown_feature" not found.');
39+
40+
$this->featureRegistry->get('unknown_feature');
41+
}
42+
43+
public function testGetNames()
44+
{
45+
$this->assertSame(['first_feature', 'second_feature'], $this->featureRegistry->getNames());
46+
}
47+
}

0 commit comments

Comments
 (0)