Repository: symfony/symfony
File: src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php (PHP)

Code before:
        $getEnv = $processor->getEnv(...);

        $result = $processor->getEnv('resolve', 'foo', static fn ($name) => 'foo' === $name ? '%env(BAR)%' : $getEnv('string', $name, static function () {}));

        $this->assertSame('BAR in environment', $result);

        unset($_ENV['BAR']);
    }

    #[DataProvider('validCsv')]
    public function testGetEnvCsv($value, $processed)
    {
        $processor = new EnvVarProcessor(new Container());

        $result = $processor->getEnv('csv', 'foo', function ($name) use ($value) {
            $this->assertSame('foo', $name);

            return $value;
        });

        $this->assertSame($processed, $result);
    }

    public function testGetEnvShuffle()
    {
        srand(2); // to set seed for `shuffle`'s seed

        $this->assertSame(
            ['bar', 'foo'],
            (new EnvVarProcessor(new Container()))->getEnv('shuffle', '', static fn () => ['foo', 'bar']),
        );
    }

    public function testGetEnvShuffleInvalid()
    {
        $this->expectException(RuntimeException::class);
        $this->expectExceptionMessage('Env var "foo" cannot be shuffled, expected array, got "string".');
        (new EnvVarProcessor(new Container()))->getEnv('shuffle', 'foo', static fn () => 'bar');
    }

    public static function validCsv()
    {
        $complex = <<<'CSV'
            ,"""","foo""","\""",\,foo\
            CSV;

        return [
            ['', []],
            [',', ['', '']],
            ['1', ['1']],
            ['1,2," 3 "', ['1', '2', ' 3 ']],

Review comment (suggestion):
```suggestion
        srand(2); // to set seed for `shuffle`
```

Code after:
        $getEnv = $processor->getEnv(...);

        $result = $processor->getEnv('resolve', 'foo', static fn ($name) => 'foo' === $name ? '%env(BAR)%' : $getEnv('string', $name, static function () {}));

        $this->assertSame('BAR in environment', $result);

        unset($_ENV['BAR']);
    }

    #[DataProvider('validCsv')]
    public function testGetEnvCsv($value, $processed)
    {
        $processor = new EnvVarProcessor(new Container());

        $result = $processor->getEnv('csv', 'foo', function ($name) use ($value) {
            $this->assertSame('foo', $name);

            return $value;
        });

        $this->assertSame($processed, $result);
    }

    public function testGetEnvShuffle()
    {
        srand(2); // to set seed for `shuffle`

        $this->assertSame(
            ['bar', 'foo'],
            (new EnvVarProcessor(new Container()))->getEnv('shuffle', '', static fn () => ['foo', 'bar']),
        );
    }

    public function testGetEnvShuffleInvalid()
    {
        $this->expectException(RuntimeException::class);
        $this->expectExceptionMessage('Env var "foo" cannot be shuffled, expected array, got "string".');
        (new EnvVarProcessor(new Container()))->getEnv('shuffle', 'foo', static fn () => 'bar');
    }

    public static function validCsv()
    {
        $complex = <<<'CSV'
            ,"""","foo""","\""",\,foo\
            CSV;

        return [
            ['', []],
            [',', ['', '']],
            ['1', ['1']],
            ['1,2," 3 "', ['1', '2', ' 3 ']],