removeNoVisibleElementForAllLanguages()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 43
nc 1
nop 1
dl 0
loc 46
rs 9.232
c 0
b 0
f 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
24
    private $curriculumVitae;
25
    private $client;
26
    private $pathToFile;
27
    private $langs;
28
29
    public function setUp()
30
    {
31
        $this->pathToFile = __DIR__.'/../../Resources/data/example.xml';
32
        $this->langs      = ['en', 'fr'];
33
    }
34
35
    public function testIndex()
36
    {
37
        $clientForTestIndex = static::createClient();
38
        $clientForTestIndex->request('GET', '/');
39
        $this->assertEquals(301, $clientForTestIndex->getResponse()->getStatusCode());
40
    }
41
42
    public function testDisplay()
43
    {
44
        $clientForTestDisplay = static::createClient();
45
        $crawler = $clientForTestDisplay->request('GET', '/example');
46
        $this->assertEquals(200, $clientForTestDisplay->getResponse()->getStatusCode());
47
        $this->assertGreaterThan(0, $crawler->filter('html:contains("First Name Last Name")')->count());
48
    }
49
50
    public function testOutputHtmlXmlComparaison()
51
    {
52
        $this->client = static::createClient();
53
54
        foreach ($this->langs as $lang) {
55
            $this->outputHtmlXmlComparaison($lang);
56
        }
57
    }
58
59
    public function testOutputJSONXmlComparaison()
60
    {
61
        $this->client = static::createClient();
62
63
        foreach ($this->langs as $lang) {
64
            $this->client->request('GET', '/example/'.$lang.'.json');
65
66
            $response = $this->client->getResponse();
67
            $data     = json_decode($response->getContent(), TRUE);
68
69
            // Read the Curriculum Vitae
70
            $this->curriculumVitae = new CurriculumVitae($this->pathToFile, $lang);
71
72
            $this->assertSame(
73
                $this->curriculumVitae->getCurriculumVitaeArray(),
74
                $data);
75
        }
76
    }
77
78
    public function testOutputXmlXmlComparaison()
79
    {
80
        $this->client = static::createClient();
81
82
        foreach ($this->langs as $lang) {
83
            $this->client->request('GET', '/example/'.$lang.'.xml');
84
            $response = $this->client->getResponse();
85
            $response->headers->set('Content-Type', 'application/xml');
86
            $data = $response->getContent();
87
88
            // Read the Curriculum Vitae
89
            $this->curriculumVitae = new CurriculumVitae($this->pathToFile, $lang);
90
91
            $this->assertSame(
92
                $this->initSerializer()->serialize(
93
                    $this->curriculumVitae->getCurriculumVitaeArray(),
94
                    'xml'),
95
                $data);
96
        }
97
    }
98
99
    private function initSerializer()
100
    {
101
        //initialisation du serializer
102
        $encoders    = [new XmlEncoder('CurriculumVitae')];
103
        $normalizers = [new GetSetMethodNormalizer()];
104
        return new Serializer($normalizers, $encoders);
105
    }
106
107
    public function testOutputFollowMeLink()
108
    {
109
        $result         = [];
110
        $arrayFunctions = new ArrayFunctions();
111
112
        $this->client = static::createClient();
113
114
        foreach ($this->langs as $lang) {
115
            $crawler = $this->client->request('GET', '/example/'.$lang);
116
117
            // Read the Curriculum Vitae
118
            $this->curriculumVitae = new CurriculumVitae($this->pathToFile, $lang);
119
120
            $cvXml = ['followMe' => $this->curriculumVitae->getFollowMe()];
121
122
            $testValue = $arrayFunctions->arrayValuesRecursive($cvXml);
123
            foreach ($testValue as $value) {
124
                $alt  = 0;
125
                $alt += $crawler->filter('img[alt="'.$value.'"]')->count();
126
                $alt += $crawler->filter('img[title="'.$value.'"]')->count();
127
                $alt += $crawler->filter('img[src="/'.$value.'"]')->count();
128
                $alt += $crawler->filter('a[href="'.$value.'"]')->count();
129
130
                if ($alt == 0) {
131
                    $result[] = 'The value '.$value.' is not diplay for language '.$lang;
132
                }
133
            }
134
        }
135
        $this->assertEquals(0, count($result),
136
            implode("\n", $result)
137
        );
138
    }
139
140
    private function outputHtmlXmlComparaison($lang = 'en')
141
    {
142
        $arrayFunctions = new ArrayFunctions();
143
        $crawler        = $this->client->request('GET', '/example/'.$lang);
144
145
        // Read the Curriculum Vitae
146
        $this->curriculumVitae = new CurriculumVitae($this->pathToFile, $lang);
147
148
        $cvXml = [
149
                'identity'                   => $this->curriculumVitae->getIdentity(),
150
                'lookingFor'                 => $this->curriculumVitae->getLookingFor(),
151
                CurriculumVitae::EXPERIENCES => $this->curriculumVitae->getExperiences(),
152
                'skills'                     => $this->curriculumVitae->getSkills(),
153
                'educations'                 => $this->curriculumVitae->getEducations(),
154
                'languageSkills'             => $this->curriculumVitae->getLanguageSkills(),
155
                'miscellaneous'              => $this->curriculumVitae->getMiscellaneous()
156
        ];
157
        // Remove all no visible elements
158
        $cvXml = $this->removeNoVisibleElementDependingOnLanguages($lang, $cvXml);
159
        $cvXml = $this->removeNoVisibleElementForAllLanguages($cvXml);
160
161
        $testValue = $arrayFunctions->arrayValuesRecursive($cvXml);
162
        $result    = [];
163
        foreach ($testValue as $value) {
164
            if ($crawler->filter('html:contains("'.$value.'")')->count() == 0) {
165
                $result[] = 'The value '.$value.' is not diplay for language '.$lang;
166
            }
167
        }
168
        $this->assertEquals(0, count($result),
169
            implode("\n", $result)
170
        );
171
    }
172
173
    /**
174
     * @param string $lang
175
     * @param array $cvXml
176
     *
177
     * @return array
178
     */
179
    private function removeNoVisibleElementDependingOnLanguages($lang, $cvXml)
180
    {
181
        if ($lang === 'en') {
182
            unset($cvXml['identity'][CurriculumVitae::IDENTITY_MYSELF]['birthday']);
183
        }
184
        return $cvXml;
185
    }
186
187
    /**
188
     * @param array $cvXml
189
     *
190
     * @return array
191
     */
192
    private function removeNoVisibleElementForAllLanguages($cvXml)
193
    {
194
        unset($cvXml['identity'][CurriculumVitae::IDENTITY_MYSELF]['picture']);
195
        unset($cvXml['identity']['address']['street']);
196
        unset($cvXml['identity']['address']['postalcode']);
197
        unset($cvXml['identity']['address']['googlemap']);
198
        unset($cvXml['identity']['contact']['mobile']);
199
        unset($cvXml['identity']['contact']['email']);
200
        unset($cvXml[CurriculumVitae::EXPERIENCES]['FirstExperience']['society']['society']['ref']);
201
        unset($cvXml[CurriculumVitae::EXPERIENCES]['FirstExperience']['society']['siteurl']);
202
        unset($cvXml[CurriculumVitae::EXPERIENCES]['SecondExperience']['collapse']);
203
        unset($cvXml[CurriculumVitae::EXPERIENCES]['SecondExperience']['society']['society']['ref']);
204
        unset($cvXml[CurriculumVitae::EXPERIENCES]['SecondExperience']['society']['siteurl']);
205
        unset($cvXml[CurriculumVitae::EXPERIENCES]['ThirdExperience']['society']['society']['ref']);
206
        unset($cvXml[CurriculumVitae::EXPERIENCES]['FourthExperience']['collapse']);
207
        unset($cvXml['skills']['Functional']['lines']['success']['percentage']);
208
        unset($cvXml['skills']['Functional']['lines']['success']['class']);
209
        unset($cvXml['skills']['Functional']['lines']['success']['striped']);
210
        unset($cvXml['skills']['Functional']['lines']['otherSucess']['percentage']);
211
        unset($cvXml['skills']['Functional']['lines']['otherSucess']['class']);
212
        unset($cvXml['skills']['Functional']['lines']['info']['percentage']);
213
        unset($cvXml['skills']['Functional']['lines']['info']['class']);
214
        unset($cvXml['skills']['Functional']['lines']['info']['striped']);
215
        unset($cvXml['skills']['Functional']['lines']['warning']['percentage']);
216
        unset($cvXml['skills']['Functional']['lines']['warning']['class']);
217
        unset($cvXml['skills']['Functional']['lines']['danger']['percentage']);
218
        unset($cvXml['skills']['Functional']['lines']['danger']['class']);
219
        unset($cvXml['skills']['Functional']['lines']['noClass']['percentage']);
220
        unset($cvXml['skills']['OtherSkill']['lines']['success']['percentage']);
221
        unset($cvXml['skills']['OtherSkill']['lines']['success']['class']);
222
        unset($cvXml['skills']['OtherSkill']['lines']['success']['striped']);
223
        unset($cvXml['skills']['OtherSkill']['lines']['info']['percentage']);
224
        unset($cvXml['skills']['OtherSkill']['lines']['info']['class']);
225
        unset($cvXml['skills']['OtherSkill']['lines']['info']['striped']);
226
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['percentage']);
227
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['class']);
228
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['striped']);
229
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['percentage']);
230
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['class']);
231
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['striped']);
232
        unset($cvXml['educations']['HighSchool']['collapse']);
233
        unset($cvXml['educations']['FirstSchool']['collapse']);
234
        unset($cvXml['languageSkills']['French']['icon']);
235
        unset($cvXml['languageSkills']['English']['icon']);
236
237
        return $cvXml;
238
    }
239
}
240