EnvPlaceholderParameterBag example

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Loader\Configurator\EnvConfigurator;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
use Symfony\Component\DependencyInjection\Tests\Fixtures\StringBackedEnum;

class EnvPlaceholderParameterBagTest extends TestCase
{
    public function testEnumEnvVarProcessorPassesRegex()
    {
        $bag = new EnvPlaceholderParameterBag();
        $name = trim((new EnvConfigurator('FOO'))->enum(StringBackedEnum::class), '%');
        $this->assertIsString($bag->get($name));
    }

    public function testGetThrowsInvalidArgumentExceptionIfEnvNameContainsNonWordCharacters()
    {
        $bag = new EnvPlaceholderParameterBag();
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Invalid env(%foo%) name: only "word" characters are allowed.');
        $bag->get('env(%foo%)');
    }

    
$this->assertFalse($container->getDefinition('webhook.transport')->hasErrors());
        $this->assertTrue($container->getDefinition('webhook.body_configurator.json')->hasErrors());
        $this->assertSame(
            ['You cannot use the "webhook transport" service since the Serializer component is not enabled. Try setting "framework.serializer.enabled" to true.'],
            $container->getDefinition('webhook.body_configurator.json')->getErrors()
        );
    }

    protected function createContainer(array $data = [])
    {
        return new ContainerBuilder(new EnvPlaceholderParameterBag(array_merge([
            'kernel.bundles' => ['FrameworkBundle' => FrameworkBundle::class],
            'kernel.bundles_metadata' => ['FrameworkBundle' => ['namespace' => 'Symfony\\Bundle\\FrameworkBundle', 'path' => __DIR__.'/../..']],
            'kernel.cache_dir' => __DIR__,
            'kernel.build_dir' => __DIR__,
            'kernel.project_dir' => __DIR__,
            'kernel.debug' => false,
            'kernel.environment' => 'test',
            'kernel.name' => 'kernel',
            'kernel.container_class' => 'testContainer',
            'container.build_hash' => 'Abc1234',
            'container.build_id' => hash('crc32', 'Abc123423456789'),
            
protected $resolving = [];
    protected $syntheticIds = [];

    private array $envCache = [];
    private bool $compiled = false;
    private \Closure $getEnv;

    private static $make;

    public function __construct(ParameterBagInterface $parameterBag = null)
    {
        $this->parameterBag = $parameterBag ?? new EnvPlaceholderParameterBag();
    }

    /** * Compiles the container. * * This method does two things: * * * Parameter values are resolved; * * The parameter bag is frozen. * * @return void */
->setPublic(true)
            ->setProperty('foo', new Expression('env("BAR_FOO")'));
        $container->compile(true);

        $_ENV['BAR_FOO'] = 'Foo value';

        $this->assertEquals('Foo value', $container->get('bar')->foo);
    }

    public function testGetEnvCountersWithEnum()
    {
        $bag = new EnvPlaceholderParameterBag();
        $config = new ContainerBuilder($bag);
        $config->resolveEnvPlaceholders([
            $bag->get('env(enum:'.StringBackedEnum::class.':foo)'),
            $bag->get('env(Bar)'),
        ]);

        $expected = [
            'enum:Symfony\Component\DependencyInjection\Tests\Fixtures\StringBackedEnum:foo' => 1,
            'Bar' => 1,
        ];

        
protected $resolving = [];
    protected $syntheticIds = [];

    private array $envCache = [];
    private bool $compiled = false;
    private \Closure $getEnv;

    private static \Closure $make;

    public function __construct(ParameterBagInterface $parameterBag = null)
    {
        $this->parameterBag = $parameterBag ?? new EnvPlaceholderParameterBag();
    }

    /** * Compiles the container. * * This method does two things: * * * Parameter values are resolved; * * The parameter bag is frozen. * * @return void */
$buildContainer = \Closure::bind(function D): ContainerBuilder {
                $this->initializeBundles();

                return $this->buildContainer();
            }$kernel$kernel::class);
            $container = $buildContainer();
        } else {
            if (!$kernelContainer instanceof Container) {
                throw new RuntimeException(sprintf('This command does not support the application container: "%s" does not extend "%s".', get_debug_type($kernelContainer), Container::class));
            }

            (new XmlFileLoader($container = new ContainerBuilder($parameterBag = new EnvPlaceholderParameterBag())new FileLocator()))->load($kernelContainer->getParameter('debug.container.dump'));

            $refl = new \ReflectionProperty($parameterBag, 'resolved');
            $refl->setValue($parameterBag, true);

            $container->getCompilerPassConfig()->setBeforeOptimizationPasses([]);
            $container->getCompilerPassConfig()->setOptimizationPasses([new ResolveFactoryClassPass()]);
            $container->getCompilerPassConfig()->setBeforeRemovingPasses([]);
        }

        $container->setParameter('container.build_hash', 'lint_container');
        $container->setParameter('container.build_id', 'lint_container');

        
new CheckTypeDeclarationsPass(true))->process($container);

        $this->addToAssertionCount(1);
    }

    public function testProcessHandleMixedEnvPlaceholder()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Invalid definition for service "foobar": argument 1 of "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\BarMethodCall::setArray()" accepts "array", "string" passed.');

        $container = new ContainerBuilder(new EnvPlaceholderParameterBag([
            'ccc' => '%env(FOO)%',
        ]));

        $container
            ->register('foobar', BarMethodCall::class)
            ->addMethodCall('setArray', ['foo%ccc%']);

        (new CheckTypeDeclarationsPass(true))->process($container);
    }

    public function testProcessHandleMultipleEnvPlaceholder()
    {
Home | Imprint | This part of the site doesn't use cookies.