Completed
Push — master ( 2329f6...21d34e )
by Lucas
13:38 queued 03:35
created

testParameterSetting()   B

Complexity

Conditions 2
Paths 4

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
dl 0
loc 27
rs 8.8571
c 3
b 1
f 1
cc 2
eloc 19
nc 4
nop 5
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)
0 ignored issues
show
Coding Style introduced by
testParameterSetting uses the super-global variable $_SERVER which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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