Completed
Push — feature/env-params-via-compile... ( f9751d )
by Narcotic
08:27
created

parameterSettingDataProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 36
rs 8.8571
cc 1
eloc 28
nc 1
nop 0
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
        // put $_SERVER on the side
31
        $origServer = $_SERVER;
32
33
        $_SERVER[$envName] = $envValue;
34
35
        $containerDouble = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
36
                                ->disableOriginalConstructor()
37
                                ->getMock();
38
39
        if (is_null($exception)) {
40
            $containerDouble
41
                ->expects($this->exactly(1))
42
                ->method('setParameter')
43
                ->with(
44
                    $paramName,
45
                    $paramValue
46
                );
47
        } else {
48
            $this->setExpectedException($exception);
49
        }
50
51
        $compilerPass = new EnvParametersCompilerPass();
52
        $compilerPass->process($containerDouble);
53
54
        // put $_SERVER back in place side
55
        $_SERVER = $origServer;
56
    }
57
58
    /**
59
     * data provider for param settings
60
     *
61
     * @return array data
62
     */
63
    public function parameterSettingDataProvider()
64
    {
65
        return [
66
            'simple' => [
67
                'SYMFONY__test__parameter',
68
                'test',
69
                'test.parameter',
70
                'test'
71
            ],
72
            'simple-underscore' => [
73
                'SYMFONY__test__parameter_underscore',
74
                'test_underscore',
75
                'test.parameter_underscore',
76
                'test_underscore'
77
            ],
78
            'json-arr' => [
79
                'SYMFONY__test__parameter_json1',
80
                '[{"test": "test"}, {"test": "test"}]',
81
                'test.parameter_json1',
82
                [['test' => 'test'], ['test' => 'test']]
83
            ],
84
            'json-object' => [
85
                'SYMFONY__test__parameter_json2',
86
                '{"test": "test"}',
87
                'test.parameter_json2',
88
                ['test' => 'test']
89
            ],
90
            'json-invalid' => [
91
                'SYMFONY__test__parameter_json3',
92
                '{"test": "test",,}',
93
                null,
94
                null,
95
                '\RuntimeException'
96
            ]
97
        ];
98
    }
99
}
100