├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml.dist ├── src ├── DependencyInjection │ ├── Compiler │ │ └── ParameterReplacementPass.php │ ├── Configuration.php │ └── IncenteevDynamicParametersExtension.php ├── ExpressionLanguage │ └── FunctionProvider.php ├── IncenteevDynamicParametersBundle.php ├── ParameterRetriever.php └── Resources │ └── config │ └── services.xml └── tests ├── DependencyInjection ├── Compiler │ └── ParameterReplacementPassTest.php ├── ConfigurationTest.php └── IncenteevDynamicParametersExtensionTest.php └── ParameterRetrieverTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | /composer.lock 2 | /phpunit.xml 3 | /vendor 4 | /build 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3 5 | - 5.4 6 | - 5.5 7 | - 5.6 8 | - hhvm 9 | 10 | sudo: false 11 | 12 | install: composer install 13 | 14 | script: phpunit --coverage-text --coverage-clover=coverage.clover 15 | 16 | after_script: 17 | - sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi' 18 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to DynamicParameterBundle 2 | 3 | Contributions should be submitted through a Github pull request. 4 | 5 | ## Running the testsuite 6 | 7 | Running the testsuite requires having PHPUnit installed on the system. 8 | 9 | Before running the testsuite, you need to install the dependencies: 10 | 11 | ```bash 12 | $ composer update 13 | ``` 14 | 15 | You can then run the testsuite: 16 | 17 | ```bash 18 | $ phpunit --coverage-text 19 | ``` 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Incenteev 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DynamicParametersBundle 2 | 3 | This bundle provides a way to read parameters from environment variables at runtime. 4 | The value defined in the container parameter is used as fallback when the environment variable is not available. 5 | 6 | [![Build Status](https://travis-ci.org/Incenteev/DynamicParametersBundle.svg?branch=master)](https://travis-ci.org/Incenteev/DynamicParametersBundle) 7 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Incenteev/DynamicParametersBundle/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Incenteev/DynamicParametersBundle/?branch=master) 8 | [![Code Coverage](https://scrutinizer-ci.com/g/Incenteev/DynamicParametersBundle/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/Incenteev/DynamicParametersBundle/?branch=master) 9 | [![SensioLabsInsight](https://insight.sensiolabs.com/projects/2e97bd6b-7ae8-41d1-b0a7-a3106f21c50d/mini.png)](https://insight.sensiolabs.com/projects/2e97bd6b-7ae8-41d1-b0a7-a3106f21c50d) 10 | [![Latest Stable Version](https://poser.pugx.org/incenteev/dynamic-parameters-bundle/v/stable.svg)](https://packagist.org/packages/incenteev/dynamic-parameters-bundle) 11 | [![Latest Unstable Version](https://poser.pugx.org/incenteev/dynamic-parameters-bundle/v/unstable.svg)](https://packagist.org/packages/incenteev/dynamic-parameters-bundle) 12 | [![License](https://poser.pugx.org/incenteev/dynamic-parameters-bundle/license.svg)](https://packagist.org/packages/incenteev/dynamic-parameters-bundle) 13 | 14 | ## Installation 15 | 16 | Installation is a quick (I promise!) 2 step process: 17 | 18 | 1. Download IncenteevDynamicParametersBundle 19 | 2. Enable the bundle 20 | 21 | ### Step 1: Install IncenteevDynamicParametersBundle with composer 22 | 23 | Run the following composer require command: 24 | 25 | ```bash 26 | $ composer require incenteev/dynamic-parameters-bundle 27 | ``` 28 | 29 | ### Step 2: Enable the bundle 30 | 31 | Finally, enable the bundle in the kernel: 32 | 33 | ```php 34 | // app/AppKernel.php 35 | 36 | public function registerBundles() 37 | { 38 | $bundles = array( 39 | // ... 40 | new Incenteev\DynamicParametersBundle\IncenteevDynamicParametersBundle(), 41 | ); 42 | } 43 | ``` 44 | 45 | ## Usage 46 | 47 | Define the map of parameter names with the environment variable used to configure them. 48 | 49 | ```yaml 50 | # app/config/config.yml 51 | incenteev_dynamic_parameters: 52 | parameters: 53 | database_host: DATABASE_HOST 54 | "database.name": DATABASE_NAME 55 | ``` 56 | 57 | Environment variables are always strings. To be able to set parameters of other types, the bundle supports parsing the environment variable as inline Yaml: 58 | 59 | ```yaml 60 | # app/config/config.yml 61 | incenteev_dynamic_parameters: 62 | parameters: 63 | use_ssl: 64 | variable: HAS_SSL 65 | yaml: true 66 | ``` 67 | 68 | ### ParameterHandler integration 69 | 70 | If you are using the [env-map feature of the Incenteev ParameterHandler](https://github.com/Incenteev/ParameterHandler/#using-environment-variables-to-set-the-parameters), 71 | you can import the whole env-map very easily: 72 | 73 | ```yaml 74 | # app/config/config.yml 75 | incenteev_dynamic_parameters: 76 | import_parameter_handler_map: true 77 | parameters: 78 | something_else: NOT_IN_THE_COMPOSER_JSON 79 | ``` 80 | 81 | The ParameterHandler parses the environment variables as inline Yaml, so the Yaml parsing is automatically enabled for these variables when importing the map. 82 | 83 | > Note: Any parameter defined explicitly will win over the imported map. 84 | 85 | By default, the bundle will look for the composer.json file in ``%kernel.root_dir%/../composer.json``. If you use a non-standard location for your kernel, you can change the path to your composer.json file to read the env-map: 86 | 87 | ```yaml 88 | # app/config/config.yml 89 | incenteev_dynamic_parameters: 90 | import_parameter_handler_map: true 91 | composer_file: path/to/composer.json 92 | ``` 93 | 94 | ### Retrieving parameters at runtime 95 | 96 | The bundle takes care of service arguments, but changing the behavior of ``$container->getParameter()`` is not possible. However, it exposes a service to get parameters taking the environment variables into account. 97 | 98 | ```php 99 | $this->get('incenteev_dynamic_parameters.retriever')->get('use_ssl'); 100 | ``` 101 | 102 | ## Limitations 103 | 104 | - Getting a parameter from the container directly at runtime will not use the environment variable 105 | - Parameters or arguments built by concatenating other parameters together will not rely on the environment variables (yet) 106 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "incenteev/dynamic-parameters-bundle", 3 | "description": "Runtime retrieval of parameters from environment variables for Symfony", 4 | "keywords": ["symfony parameters", "environment variable"], 5 | "homepage": "https://github.com/Incenteev/DynamicParametersBundle", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Christophe Coevoet", 10 | "email": "stof@notk.org" 11 | } 12 | ], 13 | "require": { 14 | "symfony/dependency-injection": "^2.6.2@dev", 15 | "symfony/expression-language": "~2.6", 16 | "symfony/http-kernel": "~2.3", 17 | "symfony/config": "~2.3", 18 | "symfony/yaml": "~2.0" 19 | }, 20 | "autoload": { 21 | "psr-4": { "Incenteev\\DynamicParametersBundle\\": "src" } 22 | }, 23 | "autoload-dev": { 24 | "psr-4": { "Incenteev\\DynamicParametersBundle\\Tests\\": "tests" } 25 | }, 26 | "extra": { 27 | "branch-alias": { 28 | "dev-master": "1.0.x-dev" 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | ./tests 8 | 9 | 10 | 11 | 12 | 13 | src 14 | 15 | src/Resources 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/DependencyInjection/Compiler/ParameterReplacementPass.php: -------------------------------------------------------------------------------- 1 | hasParameter('incenteev_dynamic_parameters.parameters')) { 22 | return; 23 | } 24 | 25 | foreach ($container->getParameter('incenteev_dynamic_parameters.parameters') as $name => $paramConfig) { 26 | $function = $paramConfig['yaml'] ? 'dynamic_yaml_parameter' : 'dynamic_parameter'; 27 | $this->parameterExpressions[$name] = sprintf('%s(%s, %s)', $function, var_export($name, true), var_export($paramConfig['variable'], true)); 28 | } 29 | 30 | $this->visitedDefinitions = new \SplObjectStorage(); 31 | 32 | foreach ($container->getDefinitions() as $definition) { 33 | $this->updateDefinitionArguments($definition); 34 | } 35 | 36 | // Release memory 37 | $this->visitedDefinitions = null; 38 | $this->parameterExpressions = array(); 39 | } 40 | 41 | private function updateDefinitionArguments(Definition $definition) 42 | { 43 | if ($this->visitedDefinitions->contains($definition)) { 44 | return; 45 | } 46 | 47 | $this->visitedDefinitions->attach($definition); 48 | 49 | $definition->setProperties($this->updateArguments($definition->getProperties())); 50 | $definition->setArguments($this->updateArguments($definition->getArguments())); 51 | 52 | $methodsCalls = array(); 53 | 54 | foreach ($definition->getMethodCalls() as $index => $call) { 55 | $methodsCalls[$index] = array($call[0], $this->updateArguments($call[1])); 56 | } 57 | 58 | $definition->setMethodCalls($methodsCalls); 59 | } 60 | 61 | private function updateArguments(array $values) 62 | { 63 | foreach ($values as $key => $value) { 64 | if ($value instanceof Definition) { 65 | $this->updateDefinitionArguments($value); 66 | 67 | continue; 68 | } 69 | 70 | if ($value instanceof Parameter && isset($this->parameterExpressions[(string) $value])) { 71 | $values[$key] = new Expression($this->parameterExpressions[(string) $value]); 72 | 73 | continue; 74 | } 75 | 76 | if (is_array($value)) { 77 | $values[$key] = $this->updateArguments($value); 78 | 79 | continue; 80 | } 81 | 82 | if (!is_string($value)) { 83 | continue; 84 | } 85 | 86 | // Parameter-only argument 87 | if (preg_match('/^%([^%\s]++)%$/', $value, $match)) { 88 | $parameter = strtolower($match[1]); 89 | 90 | if (isset($this->parameterExpressions[$parameter])) { 91 | $values[$key] = new Expression($this->parameterExpressions[$parameter]); 92 | } 93 | 94 | continue; 95 | } 96 | } 97 | 98 | return $values; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/DependencyInjection/Configuration.php: -------------------------------------------------------------------------------- 1 | root('incenteev_dynamic_parameters') 14 | ->fixXmlConfig('parameter') 15 | ->children() 16 | ->booleanNode('import_parameter_handler_map')->defaultFalse()->end() 17 | ->scalarNode('composer_file') 18 | ->defaultValue('%kernel.root_dir%/../composer.json') 19 | ->cannotBeEmpty() 20 | ->info('The path to the composer.json file to load the ParameterHandler env-map') 21 | ->end() 22 | ->arrayNode('parameters') 23 | ->useAttributeAsKey('name') 24 | ->prototype('array') 25 | ->beforeNormalization() 26 | ->ifString() 27 | ->then(function ($v) { 28 | return array('variable' => $v); 29 | }) 30 | ->end() 31 | ->children() 32 | ->scalarNode('variable')->isRequired()->cannotBeEmpty()->end() 33 | ->booleanNode('yaml')->defaultFalse()->end() 34 | ->end() 35 | ->end() 36 | ->end() 37 | ->end(); 38 | 39 | return $treeBuilder; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/DependencyInjection/IncenteevDynamicParametersExtension.php: -------------------------------------------------------------------------------- 1 | load('services.xml'); 17 | 18 | $parameters = $config['parameters']; 19 | 20 | if ($config['import_parameter_handler_map']) { 21 | $composerFile = $container->getParameterBag()->resolveValue($config['composer_file']); 22 | 23 | $container->addResource(new FileResource($composerFile)); 24 | 25 | $parameters = array_replace($this->loadHandlerEnvMap($composerFile), $parameters); 26 | } 27 | 28 | $container->setParameter('incenteev_dynamic_parameters.parameters', $parameters); 29 | } 30 | 31 | private function loadHandlerEnvMap($composerFile) 32 | { 33 | $settings = json_decode(file_get_contents($composerFile), true); 34 | 35 | if (empty($settings['extra']['incenteev-parameters'])) { 36 | return array(); 37 | } 38 | 39 | $handlerConfigs = $settings['extra']['incenteev-parameters']; 40 | 41 | // Normalize to the multiple-file syntax 42 | if (array_keys($handlerConfigs) !== range(0, count($handlerConfigs) - 1)) { 43 | $handlerConfigs = array($handlerConfigs); 44 | } 45 | 46 | $parameters = array(); 47 | foreach ($handlerConfigs as $config) { 48 | if (!empty($config['env-map'])) { 49 | $envMap = array_map(function ($var) { 50 | return array('variable' => $var, 'yaml' => true); 51 | }, $config['env-map']); 52 | 53 | $parameters = array_replace($parameters, $envMap); 54 | } 55 | } 56 | 57 | return $parameters; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/ExpressionLanguage/FunctionProvider.php: -------------------------------------------------------------------------------- 1 | getParameter(%s) : getenv(%s))', $envVar, $paramName, $envVar); 16 | }, function (array $variables, $paramName, $envVar) { 17 | $envParam = getenv($envVar); 18 | 19 | if (false !== $envParam) { 20 | return $envParam; 21 | } 22 | 23 | return $variables['container']->getParameter($paramName); 24 | }), 25 | new ExpressionFunction('dynamic_yaml_parameter', function ($paramName, $envVar) { 26 | return sprintf('(false === getenv(%s) ? $this->getParameter(%s) : \Symfony\Component\Yaml\Inline::parse(getenv(%s)))', $envVar, $paramName, $envVar); 27 | }, function (array $variables, $paramName, $envVar) { 28 | $envParam = getenv($envVar); 29 | 30 | if (false !== $envParam) { 31 | return Inline::parse($envParam); 32 | } 33 | 34 | return $variables['container']->getParameter($paramName); 35 | }), 36 | ); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/IncenteevDynamicParametersBundle.php: -------------------------------------------------------------------------------- 1 | addCompilerPass(new ParameterReplacementPass()); 15 | 16 | $container->addExpressionLanguageProvider(new FunctionProvider()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/ParameterRetriever.php: -------------------------------------------------------------------------------- 1 | container = $container; 16 | $this->parameterMap = $parameterMap; 17 | } 18 | 19 | /** 20 | * @param string $name 21 | * 22 | * @return array|string|bool|int|float|null 23 | */ 24 | public function getParameter($name) 25 | { 26 | if (!isset($this->parameterMap[$name])) { 27 | return $this->container->getParameter($name); 28 | } 29 | 30 | $varName = $this->parameterMap[$name]['variable']; 31 | 32 | $var = getenv($varName); 33 | 34 | if (false === $var) { 35 | return $this->container->getParameter($name); 36 | } 37 | 38 | if ($this->parameterMap[$name]['yaml']) { 39 | return Inline::parse($var); 40 | } 41 | 42 | return $var; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Resources/config/services.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 | 9 | 10 | %incenteev_dynamic_parameters.parameters% 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /tests/DependencyInjection/Compiler/ParameterReplacementPassTest.php: -------------------------------------------------------------------------------- 1 | setParameter('foo', 'bar'); 16 | $container->setParameter('bar', 'baz'); 17 | $container->setParameter('incenteev_dynamic_parameters.parameters', array('foo' => array('variable' => 'SYMFONY_FOO', 'yaml' => false))); 18 | 19 | $container->register('srv.foo', 'stClass') 20 | ->setProperty('test', '%foo%') 21 | ->setProperty('test2', '%bar%'); 22 | 23 | $container->register('srv.bar', 'ArrayObject') 24 | ->addMethodCall('append', array('%foo%')) 25 | ->addMethodCall('append', array(new Parameter('foo'))); 26 | 27 | $pass = new ParameterReplacementPass(); 28 | $pass->process($container); 29 | 30 | $def = $container->getDefinition('srv.foo'); 31 | $props = $def->getProperties(); 32 | 33 | $this->assertInstanceOf('Symfony\Component\ExpressionLanguage\Expression', $props['test'], 'Parameters are replaced in properties'); 34 | $this->assertEquals('dynamic_parameter(\'foo\', \'SYMFONY_FOO\')', (string) $props['test']); 35 | $this->assertEquals('%bar%', $props['test2'], 'Other parameters are not replaced'); 36 | 37 | $def = $container->getDefinition('srv.bar'); 38 | $calls = $def->getMethodCalls(); 39 | 40 | $this->assertInstanceOf('Symfony\Component\ExpressionLanguage\Expression', $calls[0][1][0], 'Parameters are replaced in arguments'); 41 | $this->assertEquals('dynamic_parameter(\'foo\', \'SYMFONY_FOO\')', (string) $calls[0][1][0]); 42 | 43 | $this->assertInstanceOf('Symfony\Component\ExpressionLanguage\Expression', $calls[1][1][0], 'Parameter instances are replaced in arguments'); 44 | $this->assertEquals('dynamic_parameter(\'foo\', \'SYMFONY_FOO\')', (string) $calls[1][1][0]); 45 | } 46 | 47 | public function testReplaceYamlParameter() 48 | { 49 | $container = new ContainerBuilder(); 50 | 51 | $container->setParameter('foo', 'bar'); 52 | $container->setParameter('incenteev_dynamic_parameters.parameters', array('foo' => array('variable' => 'SYMFONY_FOO', 'yaml' => true))); 53 | 54 | $container->register('srv.foo', 'stClass') 55 | ->setProperty('test', '%foo%'); 56 | 57 | $pass = new ParameterReplacementPass(); 58 | $pass->process($container); 59 | 60 | $def = $container->getDefinition('srv.foo'); 61 | $props = $def->getProperties(); 62 | 63 | $this->assertInstanceOf('Symfony\Component\ExpressionLanguage\Expression', $props['test'], 'Parameters are replaced in properties'); 64 | $this->assertEquals('dynamic_yaml_parameter(\'foo\', \'SYMFONY_FOO\')', (string) $props['test']); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /tests/DependencyInjection/ConfigurationTest.php: -------------------------------------------------------------------------------- 1 | true, 15 | 'parameters' => array( 16 | 'foo' => 'FOO', 17 | 'bar' => array('variable' => 'SYMFONY__BAR', 'yaml' => true), 18 | ), 19 | ), 20 | array( 21 | 'parameters' => array( 22 | 'baz' => 'SYMFONY__BAZ', 23 | 'foo' => array('variable'=> 'OVERWRITE_FOO'), 24 | ), 25 | ), 26 | ); 27 | 28 | $expected = array( 29 | 'import_parameter_handler_map' => true, 30 | 'composer_file' => '%kernel.root_dir%/../composer.json', 31 | 'parameters' => array( 32 | 'foo' => array('variable' => 'OVERWRITE_FOO', 'yaml' => false), 33 | 'bar' => array('variable' => 'SYMFONY__BAR', 'yaml' => true), 34 | 'baz' => array('variable' => 'SYMFONY__BAZ', 'yaml' => false), 35 | ), 36 | ); 37 | 38 | $this->assertEquals($expected, $this->processConfiguration($configs)); 39 | } 40 | 41 | /** 42 | * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException 43 | */ 44 | public function testInvalidComposerFile() 45 | { 46 | $this->processConfiguration(array(array('composer_file' => null))); 47 | } 48 | 49 | /** 50 | * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException 51 | */ 52 | public function testInvalidVariableName() 53 | { 54 | $this->processConfiguration(array(array('parameters' => array('foo' => '')))); 55 | } 56 | 57 | private function processConfiguration(array $configs) 58 | { 59 | $processor = new Processor(); 60 | $configuration = new Configuration(); 61 | 62 | return $processor->processConfiguration($configuration, $configs); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /tests/DependencyInjection/IncenteevDynamicParametersExtensionTest.php: -------------------------------------------------------------------------------- 1 | load(array(), $container); 16 | 17 | $this->assertTrue($container->hasParameter('incenteev_dynamic_parameters.parameters')); 18 | $this->assertSame(array(), $container->getParameter('incenteev_dynamic_parameters.parameters')); 19 | 20 | $this->assertTrue($container->hasDefinition('incenteev_dynamic_parameters.retriever')); 21 | } 22 | 23 | public function testLoadParameters() 24 | { 25 | $container = new ContainerBuilder(); 26 | $extension = new IncenteevDynamicParametersExtension(); 27 | 28 | $config = array( 29 | 'parameters' => array( 30 | 'foo' => 'FOO', 31 | 'bar' => 'SYMFONY__BAR', 32 | ), 33 | ); 34 | 35 | $extension->load(array($config), $container); 36 | 37 | $expected = array( 38 | 'foo' => array('variable' => 'FOO', 'yaml' => false), 39 | 'bar' => array('variable' => 'SYMFONY__BAR', 'yaml' => false), 40 | ); 41 | 42 | $this->assertTrue($container->hasParameter('incenteev_dynamic_parameters.parameters')); 43 | $this->assertSame($expected, $container->getParameter('incenteev_dynamic_parameters.parameters')); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /tests/ParameterRetrieverTest.php: -------------------------------------------------------------------------------- 1 | getMock('Symfony\Component\DependencyInjection\ContainerInterface'); 12 | $container->expects($this->once()) 13 | ->method('getParameter') 14 | ->with('foo') 15 | ->willReturn('bar'); 16 | 17 | putenv('MISSING'); 18 | 19 | $retriever = new ParameterRetriever($container, array('foo' => $this->buildParam('MISSING'))); 20 | 21 | $this->assertSame('bar', $retriever->getParameter('foo')); 22 | } 23 | 24 | public function testUnknownParameter() 25 | { 26 | $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); 27 | $container->expects($this->once()) 28 | ->method('getParameter') 29 | ->with('foo') 30 | ->willReturn('bar'); 31 | 32 | $retriever = new ParameterRetriever($container, array('baz' => $this->buildParam('BAZ'))); 33 | 34 | $this->assertSame('bar', $retriever->getParameter('foo')); 35 | } 36 | 37 | public function testEnvParameter() 38 | { 39 | $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); 40 | $container->expects($this->never()) 41 | ->method('getParameter'); 42 | 43 | $retriever = new ParameterRetriever($container, array('foo' => $this->buildParam('INCENTEEV_TEST_FOO'))); 44 | 45 | putenv('INCENTEEV_TEST_FOO=bar'); 46 | 47 | $this->assertSame('bar', $retriever->getParameter('foo')); 48 | } 49 | 50 | public function testYamlParameter() 51 | { 52 | $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); 53 | $container->expects($this->never()) 54 | ->method('getParameter'); 55 | 56 | $retriever = new ParameterRetriever($container, array('foo' => $this->buildParam('INCENTEEV_TEST_BAR', true))); 57 | 58 | putenv('INCENTEEV_TEST_BAR=true'); 59 | 60 | $this->assertSame(true, $retriever->getParameter('foo')); 61 | } 62 | 63 | private function buildParam($envVar, $isYaml = false) 64 | { 65 | return array('variable' => $envVar, 'yaml' => $isYaml); 66 | } 67 | } 68 | --------------------------------------------------------------------------------