|
| 1 | +<?php |
| 2 | + |
| 3 | +namespaceSymfony\Component\DependencyInjection\Tests; |
| 4 | + |
| 5 | +usePHPUnit\Framework\TestCase; |
| 6 | +useSymfony\Component\DependencyInjection\Container; |
| 7 | +useSymfony\Component\DependencyInjection\ContainerBuilder; |
| 8 | +useSymfony\Component\DependencyInjection\EnvVarProcessor; |
| 9 | + |
| 10 | +class EnvVarProcessorTestextends TestCase |
| 11 | +{ |
| 12 | +constTEST_CONST ='test'; |
| 13 | + |
| 14 | +/** |
| 15 | + * @dataProvider validStrings |
| 16 | + */ |
| 17 | +publicfunctiontestGetEnvString($value,$processed) |
| 18 | + { |
| 19 | +$container =newContainerBuilder(); |
| 20 | +$container->setParameter('env(foo)',$value); |
| 21 | +$container->compile(); |
| 22 | + |
| 23 | +$processor =newEnvVarProcessor($container); |
| 24 | + |
| 25 | +$result =$processor->getEnv('string','foo',function () { |
| 26 | +$this->fail('Should not be called'); |
| 27 | + }); |
| 28 | + |
| 29 | +$this->assertSame($processed,$result); |
| 30 | + } |
| 31 | + |
| 32 | +publicfunctionvalidStrings() |
| 33 | + { |
| 34 | +returnarray( |
| 35 | +array('hello','hello'), |
| 36 | +array('true','true'), |
| 37 | +array('false','false'), |
| 38 | +array('null','null'), |
| 39 | +array('1','1'), |
| 40 | +array('0','0'), |
| 41 | +array('1.1','1.1'), |
| 42 | +array('1e1','1e1'), |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | +/** |
| 47 | + * @dataProvider validBools |
| 48 | + */ |
| 49 | +publicfunctiontestGetEnvBool($value,$processed) |
| 50 | + { |
| 51 | +$processor =newEnvVarProcessor(newContainer()); |
| 52 | + |
| 53 | +$result =$processor->getEnv('bool','foo',function ($name)use ($value) { |
| 54 | +$this->assertSame('foo',$name); |
| 55 | + |
| 56 | +return$value; |
| 57 | + }); |
| 58 | + |
| 59 | +$this->assertSame($processed,$result); |
| 60 | + } |
| 61 | + |
| 62 | +publicfunctionvalidBools() |
| 63 | + { |
| 64 | +returnarray( |
| 65 | +array('true',true), |
| 66 | +array('false',false), |
| 67 | +array('null',false), |
| 68 | +array('1',true), |
| 69 | +array('0',false), |
| 70 | +array('1.1',true), |
| 71 | +array('1e1',true), |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | +/** |
| 76 | + * @dataProvider validInts |
| 77 | + */ |
| 78 | +publicfunctiontestGetEnvInt($value,$processed) |
| 79 | + { |
| 80 | +$processor =newEnvVarProcessor(newContainer()); |
| 81 | + |
| 82 | +$result =$processor->getEnv('int','foo',function ($name)use ($value) { |
| 83 | +$this->assertSame('foo',$name); |
| 84 | + |
| 85 | +return$value; |
| 86 | + }); |
| 87 | + |
| 88 | +$this->assertSame($processed,$result); |
| 89 | + } |
| 90 | + |
| 91 | +publicfunctionvalidInts() |
| 92 | + { |
| 93 | +returnarray( |
| 94 | +array('1',1), |
| 95 | +array('1.1',1), |
| 96 | +array('1e1',10), |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | +/** |
| 101 | + * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException |
| 102 | + * @expectedExceptionMessage Non-numeric env var |
| 103 | + * @dataProvider invalidInts |
| 104 | + */ |
| 105 | +publicfunctiontestGetEnvIntInvalid($value) |
| 106 | + { |
| 107 | +$processor =newEnvVarProcessor(newContainer()); |
| 108 | + |
| 109 | +$processor->getEnv('int','foo',function ($name)use ($value) { |
| 110 | +$this->assertSame('foo',$name); |
| 111 | + |
| 112 | +return$value; |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | +publicfunctioninvalidInts() |
| 117 | + { |
| 118 | +returnarray( |
| 119 | +array('foo'), |
| 120 | +array('true'), |
| 121 | +array('null'), |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | +/** |
| 126 | + * @dataProvider validFloats |
| 127 | + */ |
| 128 | +publicfunctiontestGetEnvFloat($value,$processed) |
| 129 | + { |
| 130 | +$processor =newEnvVarProcessor(newContainer()); |
| 131 | + |
| 132 | +$result =$processor->getEnv('float','foo',function ($name)use ($value) { |
| 133 | +$this->assertSame('foo',$name); |
| 134 | + |
| 135 | +return$value; |
| 136 | + }); |
| 137 | + |
| 138 | +$this->assertSame($processed,$result); |
| 139 | + } |
| 140 | + |
| 141 | +publicfunctionvalidFloats() |
| 142 | + { |
| 143 | +returnarray( |
| 144 | +array('1',1.0), |
| 145 | +array('1.1',1.1), |
| 146 | +array('1e1',10.0), |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | +/** |
| 151 | + * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException |
| 152 | + * @expectedExceptionMessage Non-numeric env var |
| 153 | + * @dataProvider invalidFloats |
| 154 | + */ |
| 155 | +publicfunctiontestGetEnvFloatInvalid($value) |
| 156 | + { |
| 157 | +$processor =newEnvVarProcessor(newContainer()); |
| 158 | + |
| 159 | +$processor->getEnv('float','foo',function ($name)use ($value) { |
| 160 | +$this->assertSame('foo',$name); |
| 161 | + |
| 162 | +return$value; |
| 163 | + }); |
| 164 | + } |
| 165 | + |
| 166 | +publicfunctioninvalidFloats() |
| 167 | + { |
| 168 | +returnarray( |
| 169 | +array('foo'), |
| 170 | +array('true'), |
| 171 | +array('null'), |
| 172 | + ); |
| 173 | + } |
| 174 | + |
| 175 | +/** |
| 176 | + * @dataProvider validConsts |
| 177 | + */ |
| 178 | +publicfunctiontestGetEnvConst($value,$processed) |
| 179 | + { |
| 180 | +$processor =newEnvVarProcessor(newContainer()); |
| 181 | + |
| 182 | +$result =$processor->getEnv('const','foo',function ($name)use ($value) { |
| 183 | +$this->assertSame('foo',$name); |
| 184 | + |
| 185 | +return$value; |
| 186 | + }); |
| 187 | + |
| 188 | +$this->assertSame($processed,$result); |
| 189 | + } |
| 190 | + |
| 191 | +publicfunctionvalidConsts() |
| 192 | + { |
| 193 | +returnarray( |
| 194 | +array('Symfony\Component\DependencyInjection\Tests\EnvVarProcessorTest::TEST_CONST',self::TEST_CONST), |
| 195 | +array('E_ERROR',E_ERROR), |
| 196 | + ); |
| 197 | + } |
| 198 | + |
| 199 | +/** |
| 200 | + * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException |
| 201 | + * @expectedExceptionMessage undefined constant |
| 202 | + * @dataProvider invalidConsts |
| 203 | + */ |
| 204 | +publicfunctiontestGetEnvConstInvalid($value) |
| 205 | + { |
| 206 | +$processor =newEnvVarProcessor(newContainer()); |
| 207 | + |
| 208 | +$processor->getEnv('const','foo',function ($name)use ($value) { |
| 209 | +$this->assertSame('foo',$name); |
| 210 | + |
| 211 | +return$value; |
| 212 | + }); |
| 213 | + } |
| 214 | + |
| 215 | +publicfunctioninvalidConsts() |
| 216 | + { |
| 217 | +returnarray( |
| 218 | +array('Symfony\Component\DependencyInjection\Tests\EnvVarProcessorTest::UNDEFINED_CONST'), |
| 219 | +array('UNDEFINED_CONST'), |
| 220 | + ); |
| 221 | + } |
| 222 | + |
| 223 | +publicfunctiontestGetEnvBase64() |
| 224 | + { |
| 225 | +$processor =newEnvVarProcessor(newContainer()); |
| 226 | + |
| 227 | +$result =$processor->getEnv('base64','foo',function ($name) { |
| 228 | +$this->assertSame('foo',$name); |
| 229 | + |
| 230 | +returnbase64_encode('hello'); |
| 231 | + }); |
| 232 | + |
| 233 | +$this->assertSame('hello',$result); |
| 234 | + } |
| 235 | + |
| 236 | +publicfunctiontestGetEnvJson() |
| 237 | + { |
| 238 | +$processor =newEnvVarProcessor(newContainer()); |
| 239 | + |
| 240 | +$result =$processor->getEnv('json','foo',function ($name) { |
| 241 | +$this->assertSame('foo',$name); |
| 242 | + |
| 243 | +returnjson_encode(array(1)); |
| 244 | + }); |
| 245 | + |
| 246 | +$this->assertSame(array(1),$result); |
| 247 | + } |
| 248 | + |
| 249 | +/** |
| 250 | + * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException |
| 251 | + * @expectedExceptionMessage Syntax error |
| 252 | + */ |
| 253 | +publicfunctiontestGetEnvInvalidJson() |
| 254 | + { |
| 255 | +$processor =newEnvVarProcessor(newContainer()); |
| 256 | + |
| 257 | +$processor->getEnv('json','foo',function ($name) { |
| 258 | +$this->assertSame('foo',$name); |
| 259 | + |
| 260 | +return'invalid_json'; |
| 261 | + }); |
| 262 | + } |
| 263 | + |
| 264 | +/** |
| 265 | + * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException |
| 266 | + * @expectedExceptionMessage Invalid JSON env var |
| 267 | + * @dataProvider otherJsonValues |
| 268 | + */ |
| 269 | +publicfunctiontestGetEnvJsonOther($value) |
| 270 | + { |
| 271 | +$processor =newEnvVarProcessor(newContainer()); |
| 272 | + |
| 273 | +$processor->getEnv('json','foo',function ($name)use ($value) { |
| 274 | +$this->assertSame('foo',$name); |
| 275 | + |
| 276 | +returnjson_encode($value); |
| 277 | + }); |
| 278 | + } |
| 279 | + |
| 280 | +publicfunctionotherJsonValues() |
| 281 | + { |
| 282 | +returnarray( |
| 283 | +array(1), |
| 284 | +array(1.1), |
| 285 | +array(true), |
| 286 | +array(false), |
| 287 | + ); |
| 288 | + } |
| 289 | + |
| 290 | +/** |
| 291 | + * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException |
| 292 | + * @expectedExceptionMessage Unsupported env var prefix |
| 293 | + */ |
| 294 | +publicfunctiontestGetEnvUnknown() |
| 295 | + { |
| 296 | +$processor =newEnvVarProcessor(newContainer()); |
| 297 | + |
| 298 | +$processor->getEnv('unknown','foo',function ($name) { |
| 299 | +$this->assertSame('foo',$name); |
| 300 | + |
| 301 | +return'foo'; |
| 302 | + }); |
| 303 | + } |
| 304 | +} |