Passed
Push — master ( f06e36...9f14e6 )
by Fabien
03:39
created

DefaultControllerTest::getCvXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 13
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 13
loc 13
rs 9.4285
cc 1
eloc 10
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\Controller;
13
14
use FabienCrassat\CurriculumVitaeBundle\Entity\CurriculumVitae;
15
use FabienCrassat\CurriculumVitaeBundle\Utility\ArrayFunctions;
16
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
17
use Symfony\Component\Serializer\Serializer;
18
use Symfony\Component\Serializer\Encoder\XmlEncoder;
19
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
20
21
class DefaultControllerTest extends WebTestCase
22
{
23
    public function testIndex()
24
    {
25
        $client = static::createClient();
26
        $client->request('GET', '/');
27
        $this->assertEquals(301, $client->getResponse()->getStatusCode());
28
    }
29
30
    public function testDisplay()
31
    {
32
        $client  = static::createClient();
33
        $crawler = $client->request('GET', '/example');
34
        $this->assertGreaterThan(0, $crawler->filter('html:contains("First Name Last Name")')->count());
35
    }
36
37
    private $curriculumVitae;
38
    private $client;
39
40
    public function testOutputHtmlXmlComparaison()
41
    {
42
        $this->client = static::createClient();
43
44
        $langs = array('en', 'fr');
45
        foreach ($langs as $value) {
46
            $this->outputHtmlXmlComparaison($value);
47
        }
48
    }
49
50
    public function testOutputJSONXmlComparaison()
51
    {
52
        $this->client = static::createClient();
53
54
        $langs = array('en', 'fr');
55
        foreach ($langs as $value) {
56
            $this->client->request('GET', '/example/'.$value.'.json');
57
58
            $response = $this->client->getResponse();
59
            $data     = json_decode($response->getContent(), TRUE);
60
61
            // Read the Curriculum Vitae
62
            $pathToFile            = __DIR__.'/../../Resources/data/example.xml';
63
            $this->curriculumVitae = new CurriculumVitae($pathToFile, $value);
64
65
            $this->assertSame($this->getCvXML(), $data);
66
        }
67
    }
68
69
    public function testOutputXmlXmlComparaison()
70
    {
71
        $this->client = static::createClient();
72
73
        $langs = array('en', 'fr');
74
        foreach ($langs as $value) {
75
            $this->client->request('GET', '/example/'.$value.'.xml');
76
            $response = $this->client->getResponse();
77
            $response->headers->set('Content-Type', 'application/xml');
78
            $data = $response->getContent();
79
80
            // Read the Curriculum Vitae
81
            $pathToFile            = __DIR__.'/../../Resources/data/example.xml';
82
            $this->curriculumVitae = new CurriculumVitae($pathToFile, $value);
83
84
            //initialisation du serializer
85
            $encoders    = array(new XmlEncoder('CurriculumVitae'));
86
            $normalizers = array(new GetSetMethodNormalizer());
87
            $serializer  = new Serializer($normalizers, $encoders);
88
89
            $this->assertSame($serializer->serialize($this->getCvXML(), 'xml'), $data);
90
        }
91
    }
92
93 View Code Duplication
    private function getCvXML()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95
        return array(
96
            'identity'          => $this->curriculumVitae->getIdentity(),
97
            'followMe'          => $this->curriculumVitae->getFollowMe(),
98
            'lookingFor'        => $this->curriculumVitae->getLookingFor(),
99
            'experiences'       => $this->curriculumVitae->getExperiences(),
100
            'skills'            => $this->curriculumVitae->getSkills(),
101
            'educations'        => $this->curriculumVitae->getEducations(),
102
            'languageSkills'    => $this->curriculumVitae->getLanguageSkills(),
103
            'miscellaneous'     => $this->curriculumVitae->getMiscellaneous()
104
        );
105
    }
106
107
    public function testOutputFollowMeLink()
108
    {
109
        $result         = array();
110
        $arrayFunctions = new ArrayFunctions();
111
112
        $this->client = static::createClient();
113
114
        $langs = array('en', 'fr');
115
        foreach ($langs as $lang) {
116
            $crawler = $this->client->request('GET', '/example/'.$lang);
117
118
            // Read the Curriculum Vitae
119
            $pathToFile            = __DIR__.'/../../Resources/data/example.xml';
120
            $this->curriculumVitae = new CurriculumVitae($pathToFile, $lang);
121
122
            $cvXml = array('followMe' => $this->curriculumVitae->getFollowMe());
123
124
            $testValue = $arrayFunctions->arrayValuesRecursive($cvXml);
125
            foreach ($testValue as $value) {
126
                $alt  = 0;
127
                $alt += $crawler->filter('img[alt="'.$value.'"]')->count();
128
                $alt += $crawler->filter('img[title="'.$value.'"]')->count();
129
                $alt += $crawler->filter('img[src="/'.$value.'"]')->count();
130
                $alt += $crawler->filter('a[href="'.$value.'"]')->count();
131
132
                if ($alt == 0) {
133
                    $result[] = 'The value '.$value.' is not diplay for language '.$lang;
134
                }
135
            }
136
        }
137
        $this->assertEquals(0, count($result),
138
            implode("\n", $result)
139
        );
140
    }
141
142
    private function outputHtmlXmlComparaison($lang = 'en')
143
    {
144
        $arrayFunctions = new ArrayFunctions();
145
        $crawler        = $this->client->request('GET', '/example/'.$lang);
146
147
        // Read the Curriculum Vitae
148
        $pathToFile            = __DIR__.'/../../Resources/data/example.xml';
149
        $this->curriculumVitae = new CurriculumVitae($pathToFile, $lang);
150
151
        $cvXml = array(
152
                'identity'          => $this->curriculumVitae->getIdentity(),
153
                'lookingFor'        => $this->curriculumVitae->getLookingFor(),
154
                'experiences'       => $this->curriculumVitae->getExperiences(),
155
                'skills'            => $this->curriculumVitae->getSkills(),
156
                'educations'        => $this->curriculumVitae->getEducations(),
157
                'languageSkills'    => $this->curriculumVitae->getLanguageSkills(),
158
                'miscellaneous'     => $this->curriculumVitae->getMiscellaneous()
159
        );
160
        // Remove all no visible elements
161
        $cvXml = $this->removeNoVisibleElementDependingOnLanguages($lang, $cvXml);
162
        $cvXml = $this->removeNoVisibleElementForAllLanguages($cvXml);
163
164
        $testValue = $arrayFunctions->arrayValuesRecursive($cvXml);
165
        $result    = array();
166
        foreach ($testValue as $value) {
167
            if ($crawler->filter('html:contains("'.$value.'")')->count() == 0) {
168
                $result[] = 'The value '.$value.' is not diplay for language '.$lang;
169
            }
170
        }
171
        $this->assertEquals(0, count($result),
172
            implode("\n", $result)
173
        );
174
    }
175
176
    /**
177
     * @param string $lang
178
     * @param array $cvXml
179
     *
180
     * @return array
181
     */
182
    private function removeNoVisibleElementDependingOnLanguages($lang, $cvXml)
183
    {
184
        switch ($lang) {
185
            case 'en':
186
                unset($cvXml['identity']['myself']['birthday']);
187
                break;
188
            default:
189
                // code...
190
                break;
191
        }
192
        return $cvXml;
193
    }
194
195
    /**
196
     * @param array $cvXml
197
     *
198
     * @return array
199
     */
200
    private function removeNoVisibleElementForAllLanguages($cvXml)
201
    {
202
        unset($cvXml['identity']['myself']['picture']);
203
        unset($cvXml['identity']['address']['street']);
204
        unset($cvXml['identity']['address']['postalcode']);
205
        unset($cvXml['identity']['address']['googlemap']);
206
        unset($cvXml['identity']['contact']['mobile']);
207
        unset($cvXml['identity']['contact']['email']);
208
        unset($cvXml['experiences']['FirstExperience']['society']['society']['ref']);
209
        unset($cvXml['experiences']['FirstExperience']['society']['siteurl']);
210
        unset($cvXml['experiences']['SecondExperience']['collapse']);
211
        unset($cvXml['experiences']['SecondExperience']['society']['society']['ref']);
212
        unset($cvXml['experiences']['SecondExperience']['society']['siteurl']);
213
        unset($cvXml['experiences']['ThirdExperience']['society']['society']['ref']);
214
        unset($cvXml['experiences']['FourthExperience']['collapse']);
215
        unset($cvXml['skills']['Functional']['lines']['success']['percentage']);
216
        unset($cvXml['skills']['Functional']['lines']['success']['class']);
217
        unset($cvXml['skills']['Functional']['lines']['success']['striped']);
218
        unset($cvXml['skills']['Functional']['lines']['otherSucess']['percentage']);
219
        unset($cvXml['skills']['Functional']['lines']['otherSucess']['class']);
220
        unset($cvXml['skills']['Functional']['lines']['info']['percentage']);
221
        unset($cvXml['skills']['Functional']['lines']['info']['class']);
222
        unset($cvXml['skills']['Functional']['lines']['info']['striped']);
223
        unset($cvXml['skills']['Functional']['lines']['warning']['percentage']);
224
        unset($cvXml['skills']['Functional']['lines']['warning']['class']);
225
        unset($cvXml['skills']['Functional']['lines']['danger']['percentage']);
226
        unset($cvXml['skills']['Functional']['lines']['danger']['class']);
227
        unset($cvXml['skills']['Functional']['lines']['noClass']['percentage']);
228
        unset($cvXml['skills']['OtherSkill']['lines']['success']['percentage']);
229
        unset($cvXml['skills']['OtherSkill']['lines']['success']['class']);
230
        unset($cvXml['skills']['OtherSkill']['lines']['success']['striped']);
231
        unset($cvXml['skills']['OtherSkill']['lines']['info']['percentage']);
232
        unset($cvXml['skills']['OtherSkill']['lines']['info']['class']);
233
        unset($cvXml['skills']['OtherSkill']['lines']['info']['striped']);
234
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['percentage']);
235
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['class']);
236
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['striped']);
237
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['percentage']);
238
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['class']);
239
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['striped']);
240
        unset($cvXml['educations']['HighSchool']['collapse']);
241
        unset($cvXml['educations']['FirstSchool']['collapse']);
242
        unset($cvXml['languageSkills']['French']['icon']);
243
        unset($cvXml['languageSkills']['English']['icon']);
244
245
        return $cvXml;
246
    }
247
}
248