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\Compiler;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\Compiler\RegisterEnvVarProcessorsPass;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
17 use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;
19 class RegisterEnvVarProcessorsPassTest extends TestCase
21 public function testSimpleProcessor()
23 $container = new ContainerBuilder();
24 $container->register('foo', SimpleProcessor::class)->addTag('container.env_var_processor');
26 (new RegisterEnvVarProcessorsPass())->process($container);
28 $this->assertTrue($container->has('container.env_var_processors_locator'));
29 $this->assertInstanceOf(SimpleProcessor::class, $container->get('container.env_var_processors_locator')->get('foo'));
32 'foo' => array('string'),
33 'base64' => array('string'),
34 'bool' => array('bool'),
35 'const' => array('bool', 'int', 'float', 'string', 'array'),
36 'file' => array('string'),
37 'float' => array('float'),
38 'int' => array('int'),
39 'json' => array('array'),
40 'resolve' => array('string'),
41 'string' => array('string'),
44 $this->assertSame($expected, $container->getParameterBag()->getProvidedTypes());
47 public function testNoProcessor()
49 $container = new ContainerBuilder();
51 (new RegisterEnvVarProcessorsPass())->process($container);
53 $this->assertFalse($container->has('container.env_var_processors_locator'));
57 * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
58 * @expectedExceptionMessage Invalid type "foo" returned by "Symfony\Component\DependencyInjection\Tests\Compiler\BadProcessor::getProvidedTypes()", expected one of "array", "bool", "float", "int", "string".
60 public function testBadProcessor()
62 $container = new ContainerBuilder();
63 $container->register('foo', BadProcessor::class)->addTag('container.env_var_processor');
65 (new RegisterEnvVarProcessorsPass())->process($container);
69 class SimpleProcessor implements EnvVarProcessorInterface
71 public function getEnv($prefix, $name, \Closure $getEnv)
73 return $getEnv($name);
76 public static function getProvidedTypes()
78 return array('foo' => 'string');
82 class BadProcessor extends SimpleProcessor
84 public static function getProvidedTypes()
86 return array('foo' => 'string|foo');