|
| 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 | +} |
0 commit comments