EnvPlaceholderParameterBagTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Tests\ParameterBag;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
  13. class EnvPlaceholderParameterBagTest extends TestCase
  14. {
  15. /**
  16. * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  17. */
  18. public function testGetThrowsInvalidArgumentExceptionIfEnvNameContainsNonWordCharacters()
  19. {
  20. $bag = new EnvPlaceholderParameterBag();
  21. $bag->get('env(%foo%)');
  22. }
  23. public function testMergeWillNotDuplicateIdenticalParameters()
  24. {
  25. $envVariableName = 'DB_HOST';
  26. $parameter = sprintf('env(%s)', $envVariableName);
  27. $firstBag = new EnvPlaceholderParameterBag();
  28. // initialize placeholders
  29. $firstBag->get($parameter);
  30. $secondBag = clone $firstBag;
  31. $firstBag->mergeEnvPlaceholders($secondBag);
  32. $mergedPlaceholders = $firstBag->getEnvPlaceholders();
  33. $placeholderForVariable = $mergedPlaceholders[$envVariableName];
  34. $placeholder = array_values($placeholderForVariable)[0];
  35. $this->assertCount(1, $placeholderForVariable);
  36. $this->assertInternalType('string', $placeholder);
  37. $this->assertContains($envVariableName, $placeholder);
  38. }
  39. public function testMergeWhereFirstBagIsEmptyWillWork()
  40. {
  41. $envVariableName = 'DB_HOST';
  42. $parameter = sprintf('env(%s)', $envVariableName);
  43. $firstBag = new EnvPlaceholderParameterBag();
  44. $secondBag = new EnvPlaceholderParameterBag();
  45. // initialize placeholder only in second bag
  46. $secondBag->get($parameter);
  47. $this->assertEmpty($firstBag->getEnvPlaceholders());
  48. $firstBag->mergeEnvPlaceholders($secondBag);
  49. $mergedPlaceholders = $firstBag->getEnvPlaceholders();
  50. $placeholderForVariable = $mergedPlaceholders[$envVariableName];
  51. $placeholder = array_values($placeholderForVariable)[0];
  52. $this->assertCount(1, $placeholderForVariable);
  53. $this->assertInternalType('string', $placeholder);
  54. $this->assertContains($envVariableName, $placeholder);
  55. }
  56. public function testMergeWherePlaceholderOnlyExistsInSecond()
  57. {
  58. $uniqueEnvName = 'DB_HOST';
  59. $commonEnvName = 'DB_USER';
  60. $uniqueParamName = sprintf('env(%s)', $uniqueEnvName);
  61. $commonParamName = sprintf('env(%s)', $commonEnvName);
  62. $firstBag = new EnvPlaceholderParameterBag();
  63. // initialize common placeholder
  64. $firstBag->get($commonParamName);
  65. $secondBag = clone $firstBag;
  66. // initialize unique placeholder
  67. $secondBag->get($uniqueParamName);
  68. $firstBag->mergeEnvPlaceholders($secondBag);
  69. $merged = $firstBag->getEnvPlaceholders();
  70. $this->assertCount(1, $merged[$uniqueEnvName]);
  71. // second bag has same placeholder for commonEnvName
  72. $this->assertCount(1, $merged[$commonEnvName]);
  73. }
  74. public function testMergeWithDifferentIdentifiersForPlaceholders()
  75. {
  76. $envName = 'DB_USER';
  77. $paramName = sprintf('env(%s)', $envName);
  78. $firstBag = new EnvPlaceholderParameterBag();
  79. $secondBag = new EnvPlaceholderParameterBag();
  80. // initialize placeholders
  81. $firstPlaceholder = $firstBag->get($paramName);
  82. $secondPlaceholder = $secondBag->get($paramName);
  83. $firstBag->mergeEnvPlaceholders($secondBag);
  84. $merged = $firstBag->getEnvPlaceholders();
  85. $this->assertNotEquals($firstPlaceholder, $secondPlaceholder);
  86. $this->assertCount(2, $merged[$envName]);
  87. }
  88. public function testResolveEnvCastsIntToString()
  89. {
  90. $bag = new EnvPlaceholderParameterBag();
  91. $bag->get('env(INT_VAR)');
  92. $bag->set('env(Int_Var)', 2);
  93. $bag->resolve();
  94. $this->assertSame('2', $bag->all()['env(int_var)']);
  95. }
  96. public function testResolveEnvAllowsNull()
  97. {
  98. $bag = new EnvPlaceholderParameterBag();
  99. $bag->get('env(NULL_VAR)');
  100. $bag->set('env(Null_Var)', null);
  101. $bag->resolve();
  102. $this->assertNull($bag->all()['env(null_var)']);
  103. }
  104. /**
  105. * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
  106. * @expectedExceptionMessage The default value of env parameter "ARRAY_VAR" must be scalar or null, array given.
  107. */
  108. public function testResolveThrowsOnBadDefaultValue()
  109. {
  110. $bag = new EnvPlaceholderParameterBag();
  111. $bag->get('env(ARRAY_VAR)');
  112. $bag->set('env(Array_Var)', array());
  113. $bag->resolve();
  114. }
  115. public function testGetEnvAllowsNull()
  116. {
  117. $bag = new EnvPlaceholderParameterBag();
  118. $bag->set('env(NULL_VAR)', null);
  119. $bag->get('env(NULL_VAR)');
  120. $bag->resolve();
  121. $this->assertNull($bag->all()['env(null_var)']);
  122. }
  123. /**
  124. * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
  125. * @expectedExceptionMessage The default value of an env() parameter must be scalar or null, but "array" given to "env(ARRAY_VAR)".
  126. */
  127. public function testGetThrowsOnBadDefaultValue()
  128. {
  129. $bag = new EnvPlaceholderParameterBag();
  130. $bag->set('env(ARRAY_VAR)', array());
  131. $bag->get('env(ARRAY_VAR)');
  132. $bag->resolve();
  133. }
  134. }