Passed
Push — master ( 3efdf7...5c444f )
by Fabien
06:03
created

FabienCrassatCurriculumVitaeExtensionTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the FabienCrassat\CurriculumVitaeBundle Symfony bundle.
5
 *
6
 * (c) Fabien Crassat <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FabienCrassat\CurriculumVitaeBundle\Tests\DependencyInjection;
13
14
use FabienCrassat\CurriculumVitaeBundle\DependencyInjection\FabienCrassatCurriculumVitaeExtension;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\Yaml\Parser;
17
18
class FabienCrassatCurriculumVitaeExtensionTest extends \PHPUnit\Framework\TestCase
19
{
20
    /**
21
    * @var ContainerBuilder
22
    */
23
    private $configuration;
24
    private $loader;
25
26
    public function setUp()
27
    {
28
        $this->configuration = new ContainerBuilder;
29
        $this->loader        = new FabienCrassatCurriculumVitaeExtension;
30
    }
31
32
    /**
33
    * Tests services existence
34
    */
35
    public function testLoadDefaultParameters()
36
    {
37
        $this->createEmptyConfiguration();
38
39
        $this->assertHasParameters();
40
41
        $this->assertStringEndsWith(
42
            'Resources/data',
43
            $this->configuration->getParameter('fabiencrassat_curriculumvitae.path_to_cv')
44
        );
45
46
        $parameters = array(
47
            'fabiencrassat_curriculumvitae.default_cv'
48
                => 'example',
49
            'fabiencrassat_curriculumvitae.default_lang'
50
                => 'en',
51
            'fabiencrassat_curriculumvitae.template'
52
                => 'FabienCrassatCurriculumVitaeBundle:CurriculumVitae:index.html.twig',
53
        );
54
        $this->compareParameters($parameters);
55
    }
56
57
    /**
58
    * Tests custom mailer
59
    */
60
    public function testLoadCustomParameters()
61
    {
62
        $this->createFullConfiguration();
63
64
        $this->assertHasParameters();
65
66
        $parameters = array(
67
            'fabiencrassat_curriculumvitae.path_to_cv' => './Tests/Resources/data',
68
            'fabiencrassat_curriculumvitae.default_cv' => 'mycv',
69
            'fabiencrassat_curriculumvitae.default_lang' => 'fr',
70
            'fabiencrassat_curriculumvitae.template' => 'AcmeHelloBundle:CV:index.html.twig',
71
        );
72
        $this->compareParameters($parameters);
73
    }
74
75
    /**
76
    * Creates an empty configuration
77
    */
78
    private function createEmptyConfiguration()
79
    {
80
        $this->createConfiguration($this->getEmptyConfig());
81
    }
82
83
    /**
84
    * Creates a full configuration
85
    */
86
    private function createFullConfiguration()
87
    {
88
        $this->createConfiguration($this->getFullConfig());
89
    }
90
91
    /**
92
    * Creates a configuration
93
    */
94
    private function createConfiguration($config)
95
    {
96
        $this->loader->load(array($config), $this->configuration);
97
        $this->assertTrue($this->configuration instanceof ContainerBuilder);
98
    }
99
100
    /**
101
    * Gets an empty config
102
    *
103
    * @return String[]
104
    */
105
    private function getEmptyConfig()
106
    {
107
        $yaml   = <<<EOF
108
EOF;
109
        $parser = new Parser;
110
111
        return (array) $parser->parse($yaml);
112
    }
113
114
    /**
115
    * Gets a full config
116
    *
117
    * @return String[]
118
    */
119
    private function getFullConfig()
120
    {
121
        $yaml   = <<<EOF
122
path_to_cv:
123
    "./Tests/Resources/data"
124
custo_default_cv:
125
    "mycv"
126
default_lang:
127
    "fr"
128
template:
129
    "AcmeHelloBundle:CV:index.html.twig"
130
EOF;
131
        $parser = new Parser;
132
133
        return (array) $parser->parse($yaml);
134
    }
135
136
    /**
137
    * Asserts the identifiers matched parameters
138
    */
139
    private function assertHasParameters()
140
    {
141
        $this->assertHasParameter('fabiencrassat_curriculumvitae.path_to_cv');
142
        $this->assertHasParameter('fabiencrassat_curriculumvitae.default_cv');
143
        $this->assertHasParameter('fabiencrassat_curriculumvitae.default_lang');
144
        $this->assertHasParameter('fabiencrassat_curriculumvitae.template');
145
    }
146
147
    /**
148
    * Compare the identifiers matched parameters
149
    */
150
    private function compareParameters($parameters)
151
    {
152
        foreach ($parameters as $parameter => $valueToCompare) {
153
            $this->assertEquals(
154
                $this->configuration->getParameter($parameter),
155
                $valueToCompare
156
            );
157
        }
158
    }
159
160
    /**
161
    * Asserts the given identifier matched a parameter
162
    *
163
    * @param string $identifier
164
    */
165
    private function assertHasParameter($identifier)
166
    {
167
        $this->assertTrue($this->configuration->hasParameter($identifier));
168
    }
169
}
170