Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2992bb3

Browse files
mcfedrnicolas-grekas
authored andcommitted
[DI] Add tests for EnvVarProcessor
1 parent ae16d77 commit 2992bb3

File tree

1 file changed

+304
-0
lines changed

1 file changed

+304
-0
lines changed
Lines changed: 304 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\DependencyInjection\Container;
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
8+
use Symfony\Component\DependencyInjection\EnvVarProcessor;
9+
10+
class EnvVarProcessorTest extends TestCase
11+
{
12+
const TEST_CONST = 'test';
13+
14+
/**
15+
* @dataProvider validStrings
16+
*/
17+
public function testGetEnvString($value, $processed)
18+
{
19+
$container = new ContainerBuilder();
20+
$container->setParameter('env(foo)', $value);
21+
$container->compile();
22+
23+
$processor = new EnvVarProcessor($container);
24+
25+
$result = $processor->getEnv('string', 'foo', function () {
26+
$this->fail('Should not be called');
27+
});
28+
29+
$this->assertSame($processed, $result);
30+
}
31+
32+
public function validStrings()
33+
{
34+
return array(
35+
array('hello', 'hello'),
36+
array('true', 'true'),
37+
array('false', 'false'),
38+
array('null', 'null'),
39+
array('1', '1'),
40+
array('0', '0'),
41+
array('1.1', '1.1'),
42+
array('1e1', '1e1'),
43+
);
44+
}
45+
46+
/**
47+
* @dataProvider validBools
48+
*/
49+
public function testGetEnvBool($value, $processed)
50+
{
51+
$processor = new EnvVarProcessor(new Container());
52+
53+
$result = $processor->getEnv('bool', 'foo', function ($name) use ($value) {
54+
$this->assertSame('foo', $name);
55+
56+
return $value;
57+
});
58+
59+
$this->assertSame($processed, $result);
60+
}
61+
62+
public function validBools()
63+
{
64+
return array(
65+
array('true', true),
66+
array('false', false),
67+
array('null', false),
68+
array('1', true),
69+
array('0', false),
70+
array('1.1', true),
71+
array('1e1', true),
72+
);
73+
}
74+
75+
/**
76+
* @dataProvider validInts
77+
*/
78+
public function testGetEnvInt($value, $processed)
79+
{
80+
$processor = new EnvVarProcessor(new Container());
81+
82+
$result = $processor->getEnv('int', 'foo', function ($name) use ($value) {
83+
$this->assertSame('foo', $name);
84+
85+
return $value;
86+
});
87+
88+
$this->assertSame($processed, $result);
89+
}
90+
91+
public function validInts()
92+
{
93+
return array(
94+
array('1', 1),
95+
array('1.1', 1),
96+
array('1e1', 10),
97+
);
98+
}
99+
100+
/**
101+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
102+
* @expectedExceptionMessage Non-numeric env var
103+
* @dataProvider invalidInts
104+
*/
105+
public function testGetEnvIntInvalid($value)
106+
{
107+
$processor = new EnvVarProcessor(new Container());
108+
109+
$processor->getEnv('int', 'foo', function ($name) use ($value) {
110+
$this->assertSame('foo', $name);
111+
112+
return $value;
113+
});
114+
}
115+
116+
public function invalidInts()
117+
{
118+
return array(
119+
array('foo'),
120+
array('true'),
121+
array('null'),
122+
);
123+
}
124+
125+
/**
126+
* @dataProvider validFloats
127+
*/
128+
public function testGetEnvFloat($value, $processed)
129+
{
130+
$processor = new EnvVarProcessor(new Container());
131+
132+
$result = $processor->getEnv('float', 'foo', function ($name) use ($value) {
133+
$this->assertSame('foo', $name);
134+
135+
return $value;
136+
});
137+
138+
$this->assertSame($processed, $result);
139+
}
140+
141+
public function validFloats()
142+
{
143+
return array(
144+
array('1', 1.0),
145+
array('1.1', 1.1),
146+
array('1e1', 10.0),
147+
);
148+
}
149+
150+
/**
151+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
152+
* @expectedExceptionMessage Non-numeric env var
153+
* @dataProvider invalidFloats
154+
*/
155+
public function testGetEnvFloatInvalid($value)
156+
{
157+
$processor = new EnvVarProcessor(new Container());
158+
159+
$processor->getEnv('float', 'foo', function ($name) use ($value) {
160+
$this->assertSame('foo', $name);
161+
162+
return $value;
163+
});
164+
}
165+
166+
public function invalidFloats()
167+
{
168+
return array(
169+
array('foo'),
170+
array('true'),
171+
array('null'),
172+
);
173+
}
174+
175+
/**
176+
* @dataProvider validConsts
177+
*/
178+
public function testGetEnvConst($value, $processed)
179+
{
180+
$processor = new EnvVarProcessor(new Container());
181+
182+
$result = $processor->getEnv('const', 'foo', function ($name) use ($value) {
183+
$this->assertSame('foo', $name);
184+
185+
return $value;
186+
});
187+
188+
$this->assertSame($processed, $result);
189+
}
190+
191+
public function validConsts()
192+
{
193+
return array(
194+
array('Symfony\Component\DependencyInjection\Tests\EnvVarProcessorTest::TEST_CONST', self::TEST_CONST),
195+
array('E_ERROR', E_ERROR),
196+
);
197+
}
198+
199+
/**
200+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
201+
* @expectedExceptionMessage undefined constant
202+
* @dataProvider invalidConsts
203+
*/
204+
public function testGetEnvConstInvalid($value)
205+
{
206+
$processor = new EnvVarProcessor(new Container());
207+
208+
$processor->getEnv('const', 'foo', function ($name) use ($value) {
209+
$this->assertSame('foo', $name);
210+
211+
return $value;
212+
});
213+
}
214+
215+
public function invalidConsts()
216+
{
217+
return array(
218+
array('Symfony\Component\DependencyInjection\Tests\EnvVarProcessorTest::UNDEFINED_CONST'),
219+
array('UNDEFINED_CONST'),
220+
);
221+
}
222+
223+
public function testGetEnvBase64()
224+
{
225+
$processor = new EnvVarProcessor(new Container());
226+
227+
$result = $processor->getEnv('base64', 'foo', function ($name) {
228+
$this->assertSame('foo', $name);
229+
230+
return base64_encode('hello');
231+
});
232+
233+
$this->assertSame('hello', $result);
234+
}
235+
236+
public function testGetEnvJson()
237+
{
238+
$processor = new EnvVarProcessor(new Container());
239+
240+
$result = $processor->getEnv('json', 'foo', function ($name) {
241+
$this->assertSame('foo', $name);
242+
243+
return json_encode(array(1));
244+
});
245+
246+
$this->assertSame(array(1), $result);
247+
}
248+
249+
/**
250+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
251+
* @expectedExceptionMessage Syntax error
252+
*/
253+
public function testGetEnvInvalidJson()
254+
{
255+
$processor = new EnvVarProcessor(new Container());
256+
257+
$processor->getEnv('json', 'foo', function ($name) {
258+
$this->assertSame('foo', $name);
259+
260+
return 'invalid_json';
261+
});
262+
}
263+
264+
/**
265+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
266+
* @expectedExceptionMessage Invalid JSON env var
267+
* @dataProvider otherJsonValues
268+
*/
269+
public function testGetEnvJsonOther($value)
270+
{
271+
$processor = new EnvVarProcessor(new Container());
272+
273+
$processor->getEnv('json', 'foo', function ($name) use ($value) {
274+
$this->assertSame('foo', $name);
275+
276+
return json_encode($value);
277+
});
278+
}
279+
280+
public function otherJsonValues()
281+
{
282+
return array(
283+
array(1),
284+
array(1.1),
285+
array(true),
286+
array(false),
287+
);
288+
}
289+
290+
/**
291+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
292+
* @expectedExceptionMessage Unsupported env var prefix
293+
*/
294+
public function testGetEnvUnknown()
295+
{
296+
$processor = new EnvVarProcessor(new Container());
297+
298+
$processor->getEnv('unknown', 'foo', function ($name) {
299+
$this->assertSame('foo', $name);
300+
301+
return 'foo';
302+
});
303+
}
304+
}

0 commit comments

Comments
 (0)