Passed
Push — LessIssues ( a0ddef...af1f20 )
by Fabien
03:25
created

DefaultControllerTest::testCVDoesNotExistIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
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
            $cvXml = array(
66
                'identity'          => $this->curriculumVitae->getIdentity(),
67
                'followMe'          => $this->curriculumVitae->getFollowMe(),
68
                'lookingFor'        => $this->curriculumVitae->getLookingFor(),
69
                'experiences'       => $this->curriculumVitae->getExperiences(),
70
                'skills'            => $this->curriculumVitae->getSkills(),
71
                'educations'        => $this->curriculumVitae->getEducations(),
72
                'languageSkills'    => $this->curriculumVitae->getLanguageSkills(),
73
                'miscellaneous'     => $this->curriculumVitae->getMiscellaneous()
74
            );
75
76
            $this->assertSame($cvXml, $data);
77
        }
78
    }
79
80
    public function testOutputXmlXmlComparaison()
81
    {
82
        $this->client = static::createClient();
83
84
        $langs = array('en', 'fr');
85
        foreach ($langs as $value) {
86
            $this->client->request('GET', '/example/'.$value.'.xml');
87
            $response = $this->client->getResponse();
88
            $response->headers->set('Content-Type', 'application/xml');
89
            $data = $response->getContent();
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
            //initialisation du serializer
106
            $encoders    = array(new XmlEncoder('CurriculumVitae'));
107
            $normalizers = array(new GetSetMethodNormalizer());
108
            $serializer  = new Serializer($normalizers, $encoders);
109
110
            $this->assertSame($serializer->serialize($cvXml, 'xml'), $data);
111
        }
112
    }
113
114
    public function testOutputFollowMeLink()
115
    {
116
        $result         = array();
117
        $arrayFunctions = new ArrayFunctions();
118
119
        $this->client = static::createClient();
120
121
        $langs = array('en', 'fr');
122
        foreach ($langs as $lang) {
123
            $crawler = $this->client->request('GET', '/example/'.$lang);
124
125
            // Read the Curriculum Vitae
126
            $pathToFile            = __DIR__.'/../../Resources/data/example.xml';
127
            $this->curriculumVitae = new CurriculumVitae($pathToFile, $lang);
128
129
            $cvXml = array('followMe' => $this->curriculumVitae->getFollowMe());
130
131
            $testValue = $arrayFunctions->arrayValuesRecursive($cvXml);
132
            foreach ($testValue as $value) {
133
                $alt  = 0;
134
                $alt += $crawler->filter('img[alt="'.$value.'"]')->count();
135
                $alt += $crawler->filter('img[title="'.$value.'"]')->count();
136
                $alt += $crawler->filter('img[src="/'.$value.'"]')->count();
137
                $alt += $crawler->filter('a[href="'.$value.'"]')->count();
138
139
                if ($alt == 0) {
140
                    $result[] = 'The value '.$value.' is not diplay for language '.$lang;
141
                }
142
            }
143
        }
144
        $this->assertEquals(0, count($result),
145
            implode("\n", $result)
146
        );
147
    }
148
149
    private function outputHtmlXmlComparaison($lang = 'en')
150
    {
151
        $arrayFunctions = new ArrayFunctions();
152
        $crawler        = $this->client->request('GET', '/example/'.$lang);
153
154
        // Read the Curriculum Vitae
155
        $pathToFile            = __DIR__.'/../../Resources/data/example.xml';
156
        $this->curriculumVitae = new CurriculumVitae($pathToFile, $lang);
157
158
        $cvXml = array(
159
                'identity'          => $this->curriculumVitae->getIdentity(),
160
                'lookingFor'        => $this->curriculumVitae->getLookingFor(),
161
                'experiences'       => $this->curriculumVitae->getExperiences(),
162
                'skills'            => $this->curriculumVitae->getSkills(),
163
                'educations'        => $this->curriculumVitae->getEducations(),
164
                'languageSkills'    => $this->curriculumVitae->getLanguageSkills(),
165
                'miscellaneous'     => $this->curriculumVitae->getMiscellaneous()
166
        );
167
        // Remove all no visible elements
168
        $cvXml = $this->removeNoVisibleElementDependingOnLanguages($lang, $cvXml);
169
        $cvXml = $this->removeNoVisibleElementForAllLanguages($cvXml);
170
171
        $testValue = $arrayFunctions->arrayValuesRecursive($cvXml);
172
        $result    = array();
173
        foreach ($testValue as $value) {
174
            if ($crawler->filter('html:contains("'.$value.'")')->count() == 0) {
175
                $result[] = 'The value '.$value.' is not diplay for language '.$lang;
176
            }
177
        }
178
        $this->assertEquals(0, count($result),
179
            implode("\n", $result)
180
        );
181
    }
182
183
    /**
184
     * @param string $lang
185
     * @param array $cvXml
186
     *
187
     * @return array
188
     */
189
    private function removeNoVisibleElementDependingOnLanguages($lang, $cvXml)
190
    {
191
        switch ($lang) {
192
            case 'en':
193
                unset($cvXml['identity']['myself']['birthday']);
194
                break;
195
            default:
196
                // code...
197
                break;
198
        }
199
        return $cvXml;
200
    }
201
202
    /**
203
     * @param array $cvXml
204
     *
205
     * @return array
206
     */
207
    private function removeNoVisibleElementForAllLanguages($cvXml)
208
    {
209
        unset($cvXml['identity']['myself']['picture']);
210
        unset($cvXml['identity']['address']['street']);
211
        unset($cvXml['identity']['address']['postalcode']);
212
        unset($cvXml['identity']['address']['googlemap']);
213
        unset($cvXml['identity']['contact']['mobile']);
214
        unset($cvXml['identity']['contact']['email']);
215
        unset($cvXml['experiences']['FirstExperience']['society']['society']['ref']);
216
        unset($cvXml['experiences']['FirstExperience']['society']['siteurl']);
217
        unset($cvXml['experiences']['SecondExperience']['collapse']);
218
        unset($cvXml['experiences']['SecondExperience']['society']['society']['ref']);
219
        unset($cvXml['experiences']['SecondExperience']['society']['siteurl']);
220
        unset($cvXml['experiences']['ThirdExperience']['society']['society']['ref']);
221
        unset($cvXml['experiences']['FourthExperience']['collapse']);
222
        unset($cvXml['skills']['Functional']['lines']['success']['percentage']);
223
        unset($cvXml['skills']['Functional']['lines']['success']['class']);
224
        unset($cvXml['skills']['Functional']['lines']['success']['striped']);
225
        unset($cvXml['skills']['Functional']['lines']['otherSucess']['percentage']);
226
        unset($cvXml['skills']['Functional']['lines']['otherSucess']['class']);
227
        unset($cvXml['skills']['Functional']['lines']['info']['percentage']);
228
        unset($cvXml['skills']['Functional']['lines']['info']['class']);
229
        unset($cvXml['skills']['Functional']['lines']['info']['striped']);
230
        unset($cvXml['skills']['Functional']['lines']['warning']['percentage']);
231
        unset($cvXml['skills']['Functional']['lines']['warning']['class']);
232
        unset($cvXml['skills']['Functional']['lines']['danger']['percentage']);
233
        unset($cvXml['skills']['Functional']['lines']['danger']['class']);
234
        unset($cvXml['skills']['Functional']['lines']['noClass']['percentage']);
235
        unset($cvXml['skills']['OtherSkill']['lines']['success']['percentage']);
236
        unset($cvXml['skills']['OtherSkill']['lines']['success']['class']);
237
        unset($cvXml['skills']['OtherSkill']['lines']['success']['striped']);
238
        unset($cvXml['skills']['OtherSkill']['lines']['info']['percentage']);
239
        unset($cvXml['skills']['OtherSkill']['lines']['info']['class']);
240
        unset($cvXml['skills']['OtherSkill']['lines']['info']['striped']);
241
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['percentage']);
242
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['class']);
243
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['striped']);
244
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['percentage']);
245
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['class']);
246
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['striped']);
247
        unset($cvXml['educations']['HighSchool']['collapse']);
248
        unset($cvXml['educations']['FirstSchool']['collapse']);
249
        unset($cvXml['languageSkills']['French']['icon']);
250
        unset($cvXml['languageSkills']['English']['icon']);
251
252
        return $cvXml;
253
    }
254
}
255