ConfigUtilTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 5
Bugs 0 Features 4
Metric Value
wmc 3
c 5
b 0
f 4
lcom 1
cbo 5
dl 0
loc 112
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B testConvertToOptions() 0 37 1
A testConvertToOption() 0 10 1
A testConvertToArray() 0 51 1
1
<?php
2
3
/*
4
 * (c) Jim Martens <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace TwoMartens\Bundle\CoreBundle\Tests\Util;
11
12
use Symfony\Component\Yaml\Yaml;
13
use TwoMartens\Bundle\CoreBundle\Model\Option;
14
use TwoMartens\Bundle\CoreBundle\Model\OptionCategory;
15
use TwoMartens\Bundle\CoreBundle\Util\ConfigUtil;
16
17
/**
18
 * Tests the ConfigUtil.
19
 *
20
 * @author    Jim Martens <[email protected]>
21
 * @copyright 2013-2015 Jim Martens
22
 */
23
class ConfigUtilTest extends \PHPUnit_Framework_TestCase
24
{
25
    /**
26
     * Tests the convertToOptions method.
27
     */
28
    public function testConvertToOptions()
29
    {
30
        $path = __DIR__.'/config/config.yml';
31
        $contents = file_get_contents($path);
32
        $yamlProcessed = Yaml::parse($contents);
33
        $output = ConfigUtil::convertToOptions($yamlProcessed);
34
        $expectedOutput = new OptionCategory();
35
        // imports category
36
        $importsCategory = new OptionCategory('imports');
37
        $subOptions = [
38
            new Option(0, 'resource', 'string', 'parameters.yml'),
39
            new Option(0, 'resource', 'string', 'security.yml'),
40
            new Option(0, 'resource', 'string', '@TwoMartensCoreBundle/Resources/config/services.yml')
41
        ];
42
        $importsCategory->setOptions($subOptions);
43
44
        // test category
45
        $testCategory = new OptionCategory('test');
46
        $subOptions = [
47
            new Option(0, 'jacob', 'null', null),
48
            new Option(0, 'hans', 'array', ['achim', 'jacob', 'manfred'])
49
        ];
50
        $testCategory->setOptions($subOptions);
51
        $manfredCategory = new OptionCategory('michael');
52
        $subOptions = [
53
            new Option(0, 'security', 'boolean', true)
54
        ];
55
        $manfredCategory->setOptions($subOptions);
56
        $testCategory->setCategories([$manfredCategory]);
57
58
        // fail category
59
        $failCategory = new OptionCategory('fail');
60
61
        $expectedOutput->setCategories([$importsCategory, $testCategory, $failCategory]);
62
63
        $this->assertEquals($expectedOutput, $output);
64
    }
65
66
    /**
67
     * Tests the convertToOption method.
68
     */
69
    public function testConvertToOption()
70
    {
71
        $optionName = 'testOption';
72
        $optionValue = true;
73
74
        $option = ConfigUtil::convertToOption($optionName, $optionValue);
75
        $this->assertEquals('testOption', $option->getName());
76
        $this->assertTrue($option->getValue());
77
        $this->assertEquals('boolean', $option->getType());
78
    }
79
80
    /**
81
     * Tests the convertToArray method.
82
     */
83
    public function testConvertToArray()
84
    {
85
        // simple case
86
        $optionCategory = new OptionCategory();
87
        $mainCategory = new OptionCategory('twomartens.core');
88
        $options = [
89
            new Option(0, 'one', 'string', 'hello'),
90
            new Option(0, 'two', 'boolean', false),
91
            new Option(0, 'three', 'array', [1, 2, 3]),
92
        ];
93
        $mainCategory->setOptions($options);
94
        $optionCategory->setCategories([$mainCategory]);
95
96
        $yamlData = ConfigUtil::convertToArray($optionCategory);
97
        $expectedYamlData = [
98
            'twomartens.core' => [
99
                'one' => 'hello',
100
                'two' => false,
101
                'three' => [1, 2, 3]
102
            ]
103
        ];
104
105
        $this->assertEquals($expectedYamlData, $yamlData);
106
107
        // complex case
108
        $optionCategory = new OptionCategory();
109
        $acpCategory = new OptionCategory('acp');
110
        $ourCategory = new OptionCategory('twomartens.core');
111
        $acpCategory->setCategories([$ourCategory]);
112
        $optionCategory->setCategories([$acpCategory]);
113
114
        $options = [
115
            new Option(0, 'one', 'boolean', true),
116
            new Option(0, 'two', 'string', 'foo'),
117
            new Option(0, 'three', 'array', [4, 5, 6])
118
        ];
119
        $ourCategory->setOptions($options);
120
121
        $yamlData = ConfigUtil::convertToArray($optionCategory);
122
        $expectedYamlData = [
123
            'acp' => [
124
                'twomartens.core' => [
125
                    'one' => true,
126
                    'two' => 'foo',
127
                    'three' => [4, 5, 6]
128
                ]
129
            ]
130
        ];
131
132
        $this->assertEquals($expectedYamlData, $yamlData);
133
    }
134
}
135