|
| 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 | +use Symfony\Component\DependencyInjection\Exception\LogicException; |
| 10 | + |
| 11 | +class EnvVarProcessorTest extends TestCase |
| 12 | +{ |
| 13 | + const TEST_CONST = 'test'; |
| 14 | + |
| 15 | + public function testGetEnvString() |
| 16 | + { |
| 17 | + $container = new ContainerBuilder(); |
| 18 | + $container->setParameter('env(foo)', 'hello'); |
| 19 | + $container->compile(); |
| 20 | + |
| 21 | + $processor = new EnvVarProcessor($container); |
| 22 | + |
| 23 | + $result = $processor->getEnv('string', 'foo', function () { |
| 24 | + throw new LogicException('Shouldnt be called'); |
| 25 | + }); |
| 26 | + |
| 27 | + $this->assertSame('hello', $result); |
| 28 | + } |
| 29 | + |
| 30 | + public function testGetEnvBool() |
| 31 | + { |
| 32 | + $processor = new EnvVarProcessor(new Container()); |
| 33 | + |
| 34 | + $result = $processor->getEnv('bool', 'foo', function ($name) { |
| 35 | + $this->assertSame('foo', $name); |
| 36 | + |
| 37 | + return '1'; |
| 38 | + }); |
| 39 | + |
| 40 | + $this->assertTrue($result); |
| 41 | + } |
| 42 | + |
| 43 | + public function testGetEnvInt() |
| 44 | + { |
| 45 | + $processor = new EnvVarProcessor(new Container()); |
| 46 | + |
| 47 | + $result = $processor->getEnv('int', 'foo', function ($name) { |
| 48 | + $this->assertSame('foo', $name); |
| 49 | + |
| 50 | + return '1'; |
| 51 | + }); |
| 52 | + |
| 53 | + $this->assertSame(1, $result); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException |
| 58 | + * @expectedExceptionMessage Non-numeric env var |
| 59 | + */ |
| 60 | + public function testGetEnvIntInvalid() |
| 61 | + { |
| 62 | + $processor = new EnvVarProcessor(new Container()); |
| 63 | + |
| 64 | + $processor->getEnv('int', 'foo', function ($name) { |
| 65 | + $this->assertSame('foo', $name); |
| 66 | + |
| 67 | + return 'bar'; |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + public function testGetEnvFloat() |
| 72 | + { |
| 73 | + $processor = new EnvVarProcessor(new Container()); |
| 74 | + |
| 75 | + $result = $processor->getEnv('float', 'foo', function ($name) { |
| 76 | + $this->assertSame('foo', $name); |
| 77 | + |
| 78 | + return '1.1'; |
| 79 | + }); |
| 80 | + |
| 81 | + $this->assertSame(1.1, $result); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException |
| 86 | + * @expectedExceptionMessage Non-numeric env var |
| 87 | + */ |
| 88 | + public function testGetEnvFloatInvalid() |
| 89 | + { |
| 90 | + $processor = new EnvVarProcessor(new Container()); |
| 91 | + |
| 92 | + $processor->getEnv('float', 'foo', function ($name) { |
| 93 | + $this->assertSame('foo', $name); |
| 94 | + |
| 95 | + return 'bar'; |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + public function testGetEnvConst() |
| 100 | + { |
| 101 | + $processor = new EnvVarProcessor(new Container()); |
| 102 | + |
| 103 | + $result = $processor->getEnv('const', 'foo', function ($name) { |
| 104 | + $this->assertSame('foo', $name); |
| 105 | + |
| 106 | + return 'Symfony\Component\DependencyInjection\Tests\EnvVarProcessorTest::TEST_CONST'; |
| 107 | + }); |
| 108 | + |
| 109 | + $this->assertSame(self::TEST_CONST, $result); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException |
| 114 | + * @expectedExceptionMessage undefined constant |
| 115 | + */ |
| 116 | + public function testGetEnvConstInvalid() |
| 117 | + { |
| 118 | + $processor = new EnvVarProcessor(new Container()); |
| 119 | + |
| 120 | + $processor->getEnv('const', 'foo', function ($name) { |
| 121 | + $this->assertSame('foo', $name); |
| 122 | + |
| 123 | + return 'Symfony\Component\DependencyInjection\Tests\EnvVarProcessorTest::TEST_CONST_OTHER'; |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + public function testGetEnvBase64() |
| 128 | + { |
| 129 | + $processor = new EnvVarProcessor(new Container()); |
| 130 | + |
| 131 | + $result = $processor->getEnv('base64', 'foo', function ($name) { |
| 132 | + $this->assertSame('foo', $name); |
| 133 | + |
| 134 | + return base64_encode('hello'); |
| 135 | + }); |
| 136 | + |
| 137 | + $this->assertSame('hello', $result); |
| 138 | + } |
| 139 | + |
| 140 | + public function testGetEnvJson() |
| 141 | + { |
| 142 | + $processor = new EnvVarProcessor(new Container()); |
| 143 | + |
| 144 | + $result = $processor->getEnv('json', 'foo', function ($name) { |
| 145 | + $this->assertSame('foo', $name); |
| 146 | + |
| 147 | + return json_encode(array(1)); |
| 148 | + }); |
| 149 | + |
| 150 | + $this->assertSame(array(1), $result); |
| 151 | + } |
| 152 | + |
| 153 | + public function testGetEnvJsonNull() |
| 154 | + { |
| 155 | + $processor = new EnvVarProcessor(new Container()); |
| 156 | + |
| 157 | + $result = $processor->getEnv('json', 'foo', function ($name) { |
| 158 | + $this->assertSame('foo', $name); |
| 159 | + |
| 160 | + return json_encode(null); |
| 161 | + }); |
| 162 | + |
| 163 | + $this->assertNull($result); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException |
| 168 | + * @expectedExceptionMessage Invalid JSON env var |
| 169 | + */ |
| 170 | + public function testGetEnvJsonOther() |
| 171 | + { |
| 172 | + $processor = new EnvVarProcessor(new Container()); |
| 173 | + |
| 174 | + $processor->getEnv('json', 'foo', function ($name) { |
| 175 | + $this->assertSame('foo', $name); |
| 176 | + |
| 177 | + return json_encode(1); |
| 178 | + }); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException |
| 183 | + * @expectedExceptionMessage Unsupported env var prefix |
| 184 | + */ |
| 185 | + public function testGetEnvUnknown() |
| 186 | + { |
| 187 | + $processor = new EnvVarProcessor(new Container()); |
| 188 | + |
| 189 | + $processor->getEnv('unknown', 'foo', function ($name) { |
| 190 | + $this->assertSame('foo', $name); |
| 191 | + |
| 192 | + return 'foo'; |
| 193 | + }); |
| 194 | + } |
| 195 | +} |
0 commit comments