Test Failed
Push — Codacy ( aac1d5...c04715 )
by Fabien
02:10
created

testOutputHtmlXmlComparaison()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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