Completed
Pull Request — master (#14)
by Fabien
08:10 queued 03:47
created

removeNoVisibleElementForAllLanguages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 47
rs 9.0303
c 1
b 0
f 0
cc 1
eloc 44
nc 1
nop 1
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
    /**
38
     * @expectedException(Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
39
     */
40
    public function testIfAnExportPDFServiceIsNotPresent()
41
    {
42
        $client = static::createClient();
43
        $client->request('GET', '/example/en/pdf');
44
        $this->assertEquals(404, $client->getResponse()->getStatusCode());
45
    }
46
47
    /**
48
     * @expectedException(Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
49
     */
50
    public function testCVDoesNotExistIndex()
51
    {
52
        $client = static::createClient();
53
        $client->request('GET', '/nofile');
54
        $this->assertEquals(404, $client->getResponse()->getStatusCode());
55
    }
56
57
    /**
58
     * @expectedException(Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
59
     */
60
    public function testBadLanguage()
61
    {
62
        $client = static::createClient();
63
        $client->request('GET', '/example/XX');
64
        $this->assertEquals(404, $client->getResponse()->getStatusCode());
65
    }
66
67
    private $curriculumVitae;
68
    private $client;
69
70
    public function testOutputHtmlXmlComparaison()
71
    {
72
        $this->client = static::createClient();
73
74
        $langs = array('en', 'fr');
75
        foreach ($langs as $value) {
76
            $this->outputHtmlXmlComparaison($value);
77
        }
78
    }
79
80
    public function testOutputJSONXmlComparaison()
81
    {
82
        $this->client = static::createClient();
83
84
        $langs = array('en', 'fr');
85
        foreach ($langs as $value) {
86
            $this->client->request('GET', '/example/'.$value.'.json');
87
88
            $response = $this->client->getResponse();
89
            $data     = json_decode($response->getContent(), TRUE);
90
91
            // Read the Curriculum Vitae
92
            $pathToFile            = __DIR__.'/../../Resources/data/example.xml';
93
            $this->curriculumVitae = new CurriculumVitae($pathToFile, $value);
94
95
            $cvXml = 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
            $this->assertSame($cvXml, $data);
107
        }
108
    }
109
110
    public function testOutputXmlXmlComparaison()
111
    {
112
        $this->client = static::createClient();
113
114
        $langs = array('en', 'fr');
115
        foreach ($langs as $value) {
116
            $this->client->request('GET', '/example/'.$value.'.xml');
117
            $response = $this->client->getResponse();
118
            $response->headers->set('Content-Type', 'application/xml');
119
            $data = $response->getContent();
120
121
            // Read the Curriculum Vitae
122
            $pathToFile            = __DIR__.'/../../Resources/data/example.xml';
123
            $this->curriculumVitae = new CurriculumVitae($pathToFile, $value);
124
125
            $cvXml = array(
126
                'identity'          => $this->curriculumVitae->getIdentity(),
127
                'followMe'          => $this->curriculumVitae->getFollowMe(),
128
                'lookingFor'        => $this->curriculumVitae->getLookingFor(),
129
                'experiences'       => $this->curriculumVitae->getExperiences(),
130
                'skills'            => $this->curriculumVitae->getSkills(),
131
                'educations'        => $this->curriculumVitae->getEducations(),
132
                'languageSkills'    => $this->curriculumVitae->getLanguageSkills(),
133
                'miscellaneous'     => $this->curriculumVitae->getMiscellaneous()
134
            );
135
            //initialisation du serializer
136
            $encoders    = array(new XmlEncoder('CurriculumVitae'));
137
            $normalizers = array(new GetSetMethodNormalizer());
138
            $serializer  = new Serializer($normalizers, $encoders);
139
140
            $this->assertSame($serializer->serialize($cvXml, 'xml'), $data);
141
        }
142
    }
143
144
    public function testOutputFollowMeLink()
145
    {
146
        $result         = array();
147
        $arrayFunctions = new ArrayFunctions();
148
149
        $this->client = static::createClient();
150
151
        $langs = array('en', 'fr');
152
        foreach ($langs as $lang) {
153
            $crawler = $this->client->request('GET', '/example/'.$lang);
154
155
            // Read the Curriculum Vitae
156
            $pathToFile            = __DIR__.'/../../Resources/data/example.xml';
157
            $this->curriculumVitae = new CurriculumVitae($pathToFile, $lang);
158
159
            $cvXml = array('followMe' => $this->curriculumVitae->getFollowMe());
160
161
            $testValue = $arrayFunctions->arrayValuesRecursive($cvXml);
162
            foreach ($testValue as $value) {
163
                $alt  = 0;
164
                $alt += $crawler->filter('img[alt="'.$value.'"]')->count();
165
                $alt += $crawler->filter('img[title="'.$value.'"]')->count();
166
                $alt += $crawler->filter('img[src="/'.$value.'"]')->count();
167
                $alt += $crawler->filter('a[href="'.$value.'"]')->count();
168
169
                if ($alt == 0) {
170
                    $result[] = 'The value '.$value.' is not diplay for language '.$lang;
171
                }
172
            }
173
        }
174
        $this->assertEquals(0, count($result),
175
            implode("\n", $result)
176
        );
177
    }
178
179
    private function outputHtmlXmlComparaison($lang = 'en')
180
    {
181
        $arrayFunctions = new ArrayFunctions();
182
        $crawler        = $this->client->request('GET', '/example/'.$lang);
183
184
        // Read the Curriculum Vitae
185
        $pathToFile            = __DIR__.'/../../Resources/data/example.xml';
186
        $this->curriculumVitae = new CurriculumVitae($pathToFile, $lang);
187
188
        $cvXml = array(
189
                'identity'          => $this->curriculumVitae->getIdentity(),
190
                'lookingFor'        => $this->curriculumVitae->getLookingFor(),
191
                'experiences'       => $this->curriculumVitae->getExperiences(),
192
                'skills'            => $this->curriculumVitae->getSkills(),
193
                'educations'        => $this->curriculumVitae->getEducations(),
194
                'languageSkills'    => $this->curriculumVitae->getLanguageSkills(),
195
                'miscellaneous'     => $this->curriculumVitae->getMiscellaneous()
196
        );
197
        // Remove all no visible elements
198
        $cvXml = $this->removeNoVisibleElementDependingOnLanguages($lang, $cvXml);
199
        $cvXml = $this->removeNoVisibleElementForAllLanguages($cvXml);
200
201
        $testValue = $arrayFunctions->arrayValuesRecursive($cvXml);
202
        $result    = array();
203
        foreach ($testValue as $value) {
204
            if ($crawler->filter('html:contains("'.$value.'")')->count() == 0) {
205
                $result[] = 'The value '.$value.' is not diplay for language '.$lang;
206
            }
207
        }
208
        $this->assertEquals(0, count($result),
209
            implode("\n", $result)
210
        );
211
    }
212
213
    /**
214
     * @param string $lang
215
     * @param array $cvXml
216
     *
217
     * @return array
218
     */
219
    private function removeNoVisibleElementDependingOnLanguages($lang, $cvXml)
220
    {
221
        switch ($lang) {
222
            case 'en':
223
                unset($cvXml['identity']['myself']['birthday']);
224
                break;
225
            default:
226
                // code...
227
                break;
228
        }
229
        return $cvXml;
230
    }
231
232
    /**
233
     * @param array $cvXml
234
     *
235
     * @return array
236
     */
237
    private function removeNoVisibleElementForAllLanguages($cvXml)
238
    {
239
        unset($cvXml['identity']['myself']['picture']);
240
        unset($cvXml['identity']['address']['street']);
241
        unset($cvXml['identity']['address']['postalcode']);
242
        unset($cvXml['identity']['address']['googlemap']);
243
        unset($cvXml['identity']['contact']['mobile']);
244
        unset($cvXml['identity']['contact']['email']);
245
        unset($cvXml['experiences']['FirstExperience']['society']['society']['ref']);
246
        unset($cvXml['experiences']['FirstExperience']['society']['siteurl']);
247
        unset($cvXml['experiences']['SecondExperience']['collapse']);
248
        unset($cvXml['experiences']['SecondExperience']['society']['society']['ref']);
249
        unset($cvXml['experiences']['SecondExperience']['society']['siteurl']);
250
        unset($cvXml['experiences']['ThirdExperience']['society']['society']['ref']);
251
        unset($cvXml['experiences']['FourthExperience']['collapse']);
252
        unset($cvXml['skills']['Functional']['lines']['success']['percentage']);
253
        unset($cvXml['skills']['Functional']['lines']['success']['class']);
254
        unset($cvXml['skills']['Functional']['lines']['success']['striped']);
255
        unset($cvXml['skills']['Functional']['lines']['otherSucess']['percentage']);
256
        unset($cvXml['skills']['Functional']['lines']['otherSucess']['class']);
257
        unset($cvXml['skills']['Functional']['lines']['info']['percentage']);
258
        unset($cvXml['skills']['Functional']['lines']['info']['class']);
259
        unset($cvXml['skills']['Functional']['lines']['info']['striped']);
260
        unset($cvXml['skills']['Functional']['lines']['warning']['percentage']);
261
        unset($cvXml['skills']['Functional']['lines']['warning']['class']);
262
        unset($cvXml['skills']['Functional']['lines']['danger']['percentage']);
263
        unset($cvXml['skills']['Functional']['lines']['danger']['class']);
264
        unset($cvXml['skills']['Functional']['lines']['noClass']['percentage']);
265
        unset($cvXml['skills']['OtherSkill']['lines']['success']['percentage']);
266
        unset($cvXml['skills']['OtherSkill']['lines']['success']['class']);
267
        unset($cvXml['skills']['OtherSkill']['lines']['success']['striped']);
268
        unset($cvXml['skills']['OtherSkill']['lines']['info']['percentage']);
269
        unset($cvXml['skills']['OtherSkill']['lines']['info']['class']);
270
        unset($cvXml['skills']['OtherSkill']['lines']['info']['striped']);
271
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['percentage']);
272
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['class']);
273
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['striped']);
274
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['percentage']);
275
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['class']);
276
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['striped']);
277
        unset($cvXml['educations']['HighSchool']['collapse']);
278
        unset($cvXml['educations']['FirstSchool']['collapse']);
279
        unset($cvXml['languageSkills']['French']['icon']);
280
        unset($cvXml['languageSkills']['English']['icon']);
281
282
        return $cvXml;
283
    }
284
}
285