|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Unleash\Client\Bundle\Test\DependencyInjection\Dsn; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 7 | +use ReflectionException; |
| 8 | +use ReflectionObject; |
| 9 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 10 | +use Symfony\Component\DependencyInjection\EnvVarProcessor; |
| 11 | +use Symfony\Component\DependencyInjection\EnvVarProcessorInterface; |
| 12 | +use Unleash\Client\Bundle\DependencyInjection\Dsn\LateBoundDsnParameter; |
| 13 | +use Unleash\Client\Bundle\Test\TestKernel; |
| 14 | + |
| 15 | +final class LateBoundDsnParameterTest extends KernelTestCase |
| 16 | +{ |
| 17 | + protected function tearDown(): void |
| 18 | + { |
| 19 | + while (true) { |
| 20 | + $previousHandler = set_exception_handler(static fn () => null); |
| 21 | + restore_exception_handler(); |
| 22 | + |
| 23 | + if ($previousHandler === null) { |
| 24 | + break; |
| 25 | + } |
| 26 | + restore_exception_handler(); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @param string $envValue The raw value of the string that will be assigned to the raw env name |
| 32 | + * @param string $envName The name of the environment variable including all preprocessors |
| 33 | + * @param mixed $expected If it is a callable, it's called with the result as a parameter to do complex assertions |
| 34 | + * otherwise a strict comparison with the result is done. |
| 35 | + * |
| 36 | + * @throws ReflectionException |
| 37 | + */ |
| 38 | + #[DataProvider('preprocessorsData')] |
| 39 | + public function testPreprocessors(string $envValue, string $envName, mixed $expected) |
| 40 | + { |
| 41 | + $preprocessors = [new EnvVarProcessor(self::getContainer(), null), $this->customEnvProcessor()]; |
| 42 | + |
| 43 | + $rawEnv = array_reverse(explode(':', $envName))[0]; |
| 44 | + $_ENV[$rawEnv] = $envValue; |
| 45 | + |
| 46 | + $instance = new LateBoundDsnParameter($envName, '', $preprocessors); |
| 47 | + $getEnvValue = (new ReflectionObject($instance))->getMethod('getEnvValue'); |
| 48 | + $result = $getEnvValue->invoke($instance, $envName); |
| 49 | + |
| 50 | + if (is_callable($expected)) { |
| 51 | + $expected($result); |
| 52 | + } else { |
| 53 | + self::assertSame($expected, $result); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public static function preprocessorsData(): iterable |
| 58 | + { |
| 59 | + yield ['test', 'TEST_ENV', 'test']; |
| 60 | + yield ['1', 'string:int:TEST_ENV', '1']; |
| 61 | + yield ['1', 'bool:TEST_ENV', true]; |
| 62 | + yield ['1', 'not:bool:TEST_ENV', false]; |
| 63 | + yield ['55', 'int:TEST_ENV', 55]; |
| 64 | + yield ['55.5', 'float:TEST_ENV', 55.5]; |
| 65 | + yield ['JSON_THROW_ON_ERROR', 'const:TEST_ENV', JSON_THROW_ON_ERROR]; |
| 66 | + yield [base64_encode('test'), 'base64:TEST_ENV', 'test']; |
| 67 | + yield [json_encode(['a' => 1, 'b' => 'c']), 'json:TEST_ENV', ['a' => 1, 'b' => 'c']]; |
| 68 | + yield ['test_%some_param%', 'resolve:TEST_ENV', 'test_test']; |
| 69 | + yield ['a,b,c,d', 'csv:TEST_ENV', ['a', 'b', 'c', 'd']]; |
| 70 | + yield ['a,b,c,d', 'shuffle:csv:TEST_ENV', function (array $result) { |
| 71 | + self::assertTrue(in_array('a', $result)); |
| 72 | + self::assertTrue(in_array('b', $result)); |
| 73 | + self::assertTrue(in_array('c', $result)); |
| 74 | + self::assertTrue(in_array('d', $result)); |
| 75 | + }]; |
| 76 | + yield [__DIR__ . '/../../data/file.txt', 'file:TEST_ENV', "hello\n"]; |
| 77 | + yield [__DIR__ . '/../../data/file.php', 'require:TEST_ENV', 'test']; |
| 78 | + yield [__DIR__ . '/../../data/file.txt', 'trim:file:TEST_ENV', 'hello']; |
| 79 | + yield [json_encode(['a' => 'test']), 'key:a:json:TEST_ENV', 'test']; |
| 80 | + yield ['https://testUser:testPwd@test-domain.org:8000/test-path?testQuery=testValue#testFragment', 'url:TEST_ENV', function (array $result) { |
| 81 | + self::assertSame('https', $result['scheme']); |
| 82 | + self::assertSame('test-domain.org', $result['host']); |
| 83 | + self::assertSame('testUser', $result['user']); |
| 84 | + self::assertSame('testPwd', $result['pass']); |
| 85 | + self::assertSame('test-path', $result['path']); |
| 86 | + self::assertSame('testQuery=testValue', $result['query']); |
| 87 | + self::assertSame('testFragment', $result['fragment']); |
| 88 | + self::assertSame(8000, $result['port']); |
| 89 | + }]; |
| 90 | + yield ['https://testUser:testPwd@test-domain.org:8000/test-path?testQuery=testValue#testFragment', 'key:testQuery:query_string:key:query:url:TEST_ENV', 'testValue']; |
| 91 | + yield ['whatever', 'defined:TEST_ENV', true]; |
| 92 | + yield ['whatever', 'test:TEST_ENV', 'test']; |
| 93 | + } |
| 94 | + |
| 95 | + public function testDefault() |
| 96 | + { |
| 97 | + $envName = 'default:some_param:NONEXISTENT_ENV'; // some_param is from kernel container |
| 98 | + $preprocessors = [new EnvVarProcessor(self::getContainer(), null)]; |
| 99 | + $instance = new LateBoundDsnParameter($envName, '', $preprocessors); |
| 100 | + $getEnvValue = (new ReflectionObject($instance))->getMethod('getEnvValue'); |
| 101 | + $result = $getEnvValue->invoke($instance, $envName); |
| 102 | + self::assertSame('test', $result); |
| 103 | + } |
| 104 | + |
| 105 | + protected static function getKernelClass(): string |
| 106 | + { |
| 107 | + return TestKernel::class; |
| 108 | + } |
| 109 | + |
| 110 | + private function customEnvProcessor(): EnvVarProcessorInterface |
| 111 | + { |
| 112 | + return new class implements EnvVarProcessorInterface { |
| 113 | + public function getEnv(string $prefix, string $name, Closure $getEnv): mixed |
| 114 | + { |
| 115 | + return 'test'; |
| 116 | + } |
| 117 | + |
| 118 | + public static function getProvidedTypes(): array |
| 119 | + { |
| 120 | + return ['test' => 'string']; |
| 121 | + } |
| 122 | + }; |
| 123 | + } |
| 124 | +} |
0 commit comments