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

Skip to content

Commit 8d311e8

Browse files
[DI] Add tests for key env var resolver
1 parent 310b2eb commit 8d311e8

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function testSimpleProcessor()
3838
'float' => array('float'),
3939
'int' => array('int'),
4040
'json' => array('array'),
41+
'key' => array('string'),
4142
'resolve' => array('string'),
4243
'string' => array('string'),
4344
);

src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,4 +314,94 @@ public function testGetEnvUnknown()
314314
return 'foo';
315315
});
316316
}
317+
318+
/**
319+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
320+
* @expectedExceptionMessage Invalid configuration: env var "key:foo" does not contain a key specifier.
321+
*/
322+
public function testGetEnvKeyInvalidKey()
323+
{
324+
$processor = new EnvVarProcessor(new Container());
325+
326+
$processor->getEnv('key', 'foo', function ($name) {
327+
$this->fail('Should not get here');
328+
});
329+
}
330+
331+
/**
332+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
333+
* @expectedExceptionMessage Resolved value of "foo" did not result in an array value.
334+
* @dataProvider noArrayValues
335+
*/
336+
public function testGetEnvKeyNoArrayResult($value)
337+
{
338+
$processor = new EnvVarProcessor(new Container());
339+
340+
$processor->getEnv('key', 'index:foo', function ($name) use ($value) {
341+
$this->assertSame('foo', $name);
342+
343+
return $value;
344+
});
345+
}
346+
347+
public function noArrayValues()
348+
{
349+
return array(
350+
array(null),
351+
array('string'),
352+
array(1),
353+
array(true),
354+
);
355+
}
356+
357+
/**
358+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
359+
* @expectedExceptionMessage Key "index" not found in
360+
* @dataProvider invalidArrayValues
361+
*/
362+
public function testGetEnvKeyArrayKeyNotFound($value)
363+
{
364+
$processor = new EnvVarProcessor(new Container());
365+
366+
$processor->getEnv('key', 'index:foo', function ($name) use ($value) {
367+
$this->assertSame('foo', $name);
368+
369+
return $value;
370+
});
371+
}
372+
373+
public function invalidArrayValues()
374+
{
375+
return array(
376+
array(array()),
377+
array(array('index2' => 'value')),
378+
array(array('index', 'index2')),
379+
);
380+
}
381+
382+
public function testGetEnvKey()
383+
{
384+
$processor = new EnvVarProcessor(new Container());
385+
386+
$this->assertSame('password', $processor->getEnv('key', 'index:foo', function ($name) {
387+
$this->assertSame('foo', $name);
388+
389+
return array(
390+
'index' => 'password',
391+
);
392+
}));
393+
}
394+
395+
public function testGetEnvKeyChained()
396+
{
397+
$processor = new EnvVarProcessor(new Container());
398+
399+
$this->assertSame('password', $processor->getEnv('key', 'index:file:foo', function ($name) {
400+
$this->assertSame('file:foo', $name);
401+
402+
return array(
403+
'index' => 'password',
404+
);
405+
}));
406+
}
317407
}

0 commit comments

Comments
 (0)