Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / dependency-injection / Tests / ParameterBag / EnvPlaceholderParameterBagTest.php
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\DependencyInjection\Tests\ParameterBag;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
16
17 class EnvPlaceholderParameterBagTest extends TestCase
18 {
19     /**
20      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
21      */
22     public function testGetThrowsInvalidArgumentExceptionIfEnvNameContainsNonWordCharacters()
23     {
24         $bag = new EnvPlaceholderParameterBag();
25         $bag->get('env(%foo%)');
26     }
27
28     public function testMergeWillNotDuplicateIdenticalParameters()
29     {
30         $envVariableName = 'DB_HOST';
31         $parameter = sprintf('env(%s)', $envVariableName);
32         $firstBag = new EnvPlaceholderParameterBag();
33
34         // initialize placeholders
35         $firstBag->get($parameter);
36         $secondBag = clone $firstBag;
37
38         $firstBag->mergeEnvPlaceholders($secondBag);
39         $mergedPlaceholders = $firstBag->getEnvPlaceholders();
40
41         $placeholderForVariable = $mergedPlaceholders[$envVariableName];
42         $placeholder = array_values($placeholderForVariable)[0];
43
44         $this->assertCount(1, $placeholderForVariable);
45         $this->assertInternalType('string', $placeholder);
46         $this->assertContains($envVariableName, $placeholder);
47     }
48
49     public function testMergeWhereFirstBagIsEmptyWillWork()
50     {
51         $envVariableName = 'DB_HOST';
52         $parameter = sprintf('env(%s)', $envVariableName);
53         $firstBag = new EnvPlaceholderParameterBag();
54         $secondBag = new EnvPlaceholderParameterBag();
55
56         // initialize placeholder only in second bag
57         $secondBag->get($parameter);
58
59         $this->assertEmpty($firstBag->getEnvPlaceholders());
60
61         $firstBag->mergeEnvPlaceholders($secondBag);
62         $mergedPlaceholders = $firstBag->getEnvPlaceholders();
63
64         $placeholderForVariable = $mergedPlaceholders[$envVariableName];
65         $placeholder = array_values($placeholderForVariable)[0];
66
67         $this->assertCount(1, $placeholderForVariable);
68         $this->assertInternalType('string', $placeholder);
69         $this->assertContains($envVariableName, $placeholder);
70     }
71
72     public function testMergeWherePlaceholderOnlyExistsInSecond()
73     {
74         $uniqueEnvName = 'DB_HOST';
75         $commonEnvName = 'DB_USER';
76
77         $uniqueParamName = sprintf('env(%s)', $uniqueEnvName);
78         $commonParamName = sprintf('env(%s)', $commonEnvName);
79
80         $firstBag = new EnvPlaceholderParameterBag();
81         // initialize common placeholder
82         $firstBag->get($commonParamName);
83         $secondBag = clone $firstBag;
84
85         // initialize unique placeholder
86         $secondBag->get($uniqueParamName);
87
88         $firstBag->mergeEnvPlaceholders($secondBag);
89         $merged = $firstBag->getEnvPlaceholders();
90
91         $this->assertCount(1, $merged[$uniqueEnvName]);
92         // second bag has same placeholder for commonEnvName
93         $this->assertCount(1, $merged[$commonEnvName]);
94     }
95
96     public function testMergeWithDifferentIdentifiersForPlaceholders()
97     {
98         $envName = 'DB_USER';
99         $paramName = sprintf('env(%s)', $envName);
100
101         $firstBag = new EnvPlaceholderParameterBag();
102         $secondBag = new EnvPlaceholderParameterBag();
103         // initialize placeholders
104         $firstPlaceholder = $firstBag->get($paramName);
105         $secondPlaceholder = $secondBag->get($paramName);
106
107         $firstBag->mergeEnvPlaceholders($secondBag);
108         $merged = $firstBag->getEnvPlaceholders();
109
110         $this->assertNotEquals($firstPlaceholder, $secondPlaceholder);
111         $this->assertCount(2, $merged[$envName]);
112     }
113
114     public function testResolveEnvCastsIntToString()
115     {
116         $bag = new EnvPlaceholderParameterBag();
117         $bag->get('env(INT_VAR)');
118         $bag->set('env(INT_VAR)', 2);
119         $bag->resolve();
120         $this->assertSame('2', $bag->all()['env(INT_VAR)']);
121     }
122
123     public function testResolveEnvAllowsNull()
124     {
125         $bag = new EnvPlaceholderParameterBag();
126         $bag->get('env(NULL_VAR)');
127         $bag->set('env(NULL_VAR)', null);
128         $bag->resolve();
129         $this->assertNull($bag->all()['env(NULL_VAR)']);
130     }
131
132     /**
133      * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
134      * @expectedExceptionMessage The default value of env parameter "ARRAY_VAR" must be scalar or null, array given.
135      */
136     public function testResolveThrowsOnBadDefaultValue()
137     {
138         $bag = new EnvPlaceholderParameterBag();
139         $bag->get('env(ARRAY_VAR)');
140         $bag->set('env(ARRAY_VAR)', array());
141         $bag->resolve();
142     }
143
144     public function testGetEnvAllowsNull()
145     {
146         $bag = new EnvPlaceholderParameterBag();
147         $bag->set('env(NULL_VAR)', null);
148         $bag->get('env(NULL_VAR)');
149         $bag->resolve();
150
151         $this->assertNull($bag->all()['env(NULL_VAR)']);
152     }
153
154     /**
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)".
157      */
158     public function testGetThrowsOnBadDefaultValue()
159     {
160         $bag = new EnvPlaceholderParameterBag();
161         $bag->set('env(ARRAY_VAR)', array());
162         $bag->get('env(ARRAY_VAR)');
163         $bag->resolve();
164     }
165 }