|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* EnvParametersCompilerPassTest class file |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\CoreBundle\Tests\DependencyInjection\CompilerPass; |
|
7
|
|
|
|
|
8
|
|
|
use Graviton\CoreBundle\Compiler\EnvParametersCompilerPass; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
12
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
13
|
|
|
* @link http://swisscom.ch |
|
14
|
|
|
*/ |
|
15
|
|
|
class EnvParametersCompilerPassTest extends \PHPUnit_Framework_TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @dataProvider parameterSettingDataProvider |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $envName env name |
|
21
|
|
|
* @param string $envValue env value |
|
22
|
|
|
* @param string $paramName param name |
|
23
|
|
|
* @param mixed $paramValue param value |
|
24
|
|
|
* @param string $exception optional expected exception |
|
25
|
|
|
* |
|
26
|
|
|
* @return void |
|
27
|
|
|
*/ |
|
28
|
|
|
public function testParameterSetting($envName, $envValue, $paramName, $paramValue, $exception = null) |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
$_SERVER[$envName] = $envValue; |
|
31
|
|
|
|
|
32
|
|
|
$containerDouble = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder') |
|
33
|
|
|
->disableOriginalConstructor() |
|
34
|
|
|
->getMock(); |
|
35
|
|
|
|
|
36
|
|
|
if (is_null($exception)) { |
|
37
|
|
|
$containerDouble |
|
38
|
|
|
->expects($this->exactly(1)) |
|
39
|
|
|
->method('setParameter') |
|
40
|
|
|
->with( |
|
41
|
|
|
$paramName, |
|
42
|
|
|
$paramValue |
|
43
|
|
|
); |
|
44
|
|
|
} else { |
|
45
|
|
|
$this->setExpectedException($exception); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
try { |
|
49
|
|
|
$compilerPass = new EnvParametersCompilerPass(); |
|
50
|
|
|
$compilerPass->process($containerDouble); |
|
51
|
|
|
} finally { |
|
52
|
|
|
unset($_SERVER[$envName]); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* data provider for param settings |
|
58
|
|
|
* |
|
59
|
|
|
* @return array data |
|
60
|
|
|
*/ |
|
61
|
|
|
public function parameterSettingDataProvider() |
|
62
|
|
|
{ |
|
63
|
|
|
return [ |
|
64
|
|
|
'simple' => [ |
|
65
|
|
|
'SYMFONY__test__parameter', |
|
66
|
|
|
'test', |
|
67
|
|
|
'test.parameter', |
|
68
|
|
|
'test' |
|
69
|
|
|
], |
|
70
|
|
|
'simple-underscore' => [ |
|
71
|
|
|
'SYMFONY__test__parameter_underscore', |
|
72
|
|
|
'test_underscore', |
|
73
|
|
|
'test.parameter_underscore', |
|
74
|
|
|
'test_underscore' |
|
75
|
|
|
], |
|
76
|
|
|
'json-arr' => [ |
|
77
|
|
|
'SYMFONY__test__parameter_json1', |
|
78
|
|
|
'[{"test": "test"}, {"test": "test"}]', |
|
79
|
|
|
'test.parameter_json1', |
|
80
|
|
|
[['test' => 'test'], ['test' => 'test']] |
|
81
|
|
|
], |
|
82
|
|
|
'json-object' => [ |
|
83
|
|
|
'SYMFONY__test__parameter_json2', |
|
84
|
|
|
'{"test": "test"}', |
|
85
|
|
|
'test.parameter_json2', |
|
86
|
|
|
['test' => 'test'] |
|
87
|
|
|
], |
|
88
|
|
|
'json-invalid' => [ |
|
89
|
|
|
'SYMFONY__test__parameter_json3', |
|
90
|
|
|
'{"test": "test",,}', |
|
91
|
|
|
null, |
|
92
|
|
|
null, |
|
93
|
|
|
'\RuntimeException' |
|
94
|
|
|
] |
|
95
|
|
|
]; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: