4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Tests\ParameterBag;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
17 class EnvPlaceholderParameterBagTest extends TestCase
20 * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
22 public function testGetThrowsInvalidArgumentExceptionIfEnvNameContainsNonWordCharacters()
24 $bag = new EnvPlaceholderParameterBag();
25 $bag->get('env(%foo%)');
28 public function testMergeWillNotDuplicateIdenticalParameters()
30 $envVariableName = 'DB_HOST';
31 $parameter = sprintf('env(%s)', $envVariableName);
32 $firstBag = new EnvPlaceholderParameterBag();
34 // initialize placeholders
35 $firstBag->get($parameter);
36 $secondBag = clone $firstBag;
38 $firstBag->mergeEnvPlaceholders($secondBag);
39 $mergedPlaceholders = $firstBag->getEnvPlaceholders();
41 $placeholderForVariable = $mergedPlaceholders[$envVariableName];
42 $placeholder = array_values($placeholderForVariable)[0];
44 $this->assertCount(1, $placeholderForVariable);
45 $this->assertInternalType('string', $placeholder);
46 $this->assertContains($envVariableName, $placeholder);
49 public function testMergeWhereFirstBagIsEmptyWillWork()
51 $envVariableName = 'DB_HOST';
52 $parameter = sprintf('env(%s)', $envVariableName);
53 $firstBag = new EnvPlaceholderParameterBag();
54 $secondBag = new EnvPlaceholderParameterBag();
56 // initialize placeholder only in second bag
57 $secondBag->get($parameter);
59 $this->assertEmpty($firstBag->getEnvPlaceholders());
61 $firstBag->mergeEnvPlaceholders($secondBag);
62 $mergedPlaceholders = $firstBag->getEnvPlaceholders();
64 $placeholderForVariable = $mergedPlaceholders[$envVariableName];
65 $placeholder = array_values($placeholderForVariable)[0];
67 $this->assertCount(1, $placeholderForVariable);
68 $this->assertInternalType('string', $placeholder);
69 $this->assertContains($envVariableName, $placeholder);
72 public function testMergeWherePlaceholderOnlyExistsInSecond()
74 $uniqueEnvName = 'DB_HOST';
75 $commonEnvName = 'DB_USER';
77 $uniqueParamName = sprintf('env(%s)', $uniqueEnvName);
78 $commonParamName = sprintf('env(%s)', $commonEnvName);
80 $firstBag = new EnvPlaceholderParameterBag();
81 // initialize common placeholder
82 $firstBag->get($commonParamName);
83 $secondBag = clone $firstBag;
85 // initialize unique placeholder
86 $secondBag->get($uniqueParamName);
88 $firstBag->mergeEnvPlaceholders($secondBag);
89 $merged = $firstBag->getEnvPlaceholders();
91 $this->assertCount(1, $merged[$uniqueEnvName]);
92 // second bag has same placeholder for commonEnvName
93 $this->assertCount(1, $merged[$commonEnvName]);
96 public function testMergeWithDifferentIdentifiersForPlaceholders()
99 $paramName = sprintf('env(%s)', $envName);
101 $firstBag = new EnvPlaceholderParameterBag();
102 $secondBag = new EnvPlaceholderParameterBag();
103 // initialize placeholders
104 $firstPlaceholder = $firstBag->get($paramName);
105 $secondPlaceholder = $secondBag->get($paramName);
107 $firstBag->mergeEnvPlaceholders($secondBag);
108 $merged = $firstBag->getEnvPlaceholders();
110 $this->assertNotEquals($firstPlaceholder, $secondPlaceholder);
111 $this->assertCount(2, $merged[$envName]);
114 public function testResolveEnvCastsIntToString()
116 $bag = new EnvPlaceholderParameterBag();
117 $bag->get('env(INT_VAR)');
118 $bag->set('env(INT_VAR)', 2);
120 $this->assertSame('2', $bag->all()['env(INT_VAR)']);
123 public function testResolveEnvAllowsNull()
125 $bag = new EnvPlaceholderParameterBag();
126 $bag->get('env(NULL_VAR)');
127 $bag->set('env(NULL_VAR)', null);
129 $this->assertNull($bag->all()['env(NULL_VAR)']);
133 * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
134 * @expectedExceptionMessage The default value of env parameter "ARRAY_VAR" must be scalar or null, array given.
136 public function testResolveThrowsOnBadDefaultValue()
138 $bag = new EnvPlaceholderParameterBag();
139 $bag->get('env(ARRAY_VAR)');
140 $bag->set('env(ARRAY_VAR)', array());
144 public function testGetEnvAllowsNull()
146 $bag = new EnvPlaceholderParameterBag();
147 $bag->set('env(NULL_VAR)', null);
148 $bag->get('env(NULL_VAR)');
151 $this->assertNull($bag->all()['env(NULL_VAR)']);
155 * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
156 * @expectedExceptionMessage The default value of an env() parameter must be scalar or null, but "array" given to "env(ARRAY_VAR)".
158 public function testGetThrowsOnBadDefaultValue()
160 $bag = new EnvPlaceholderParameterBag();
161 $bag->set('env(ARRAY_VAR)', array());
162 $bag->get('env(ARRAY_VAR)');