Completed
Pull Request — master (#14)
by Fabien
15:46 queued 12:42
created

testOutputXmlXmlComparaison()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
rs 8.8571
cc 2
eloc 23
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 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
    }
45
46
    /**
47
     * @expectedException(Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
48
     */
49
    public function testCVDoesNotExistIndex()
50
    {
51
        $client = static::createClient();
52
        $client->request('GET', '/nofile');
53
    }
54
55
    /**
56
     * @expectedException(Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
57
     */
58
    public function testBadLanguage()
59
    {
60
        $client = static::createClient();
61
        $client->request('GET', '/example/XX');
62
    }
63
64
    private $curriculumVitae;
65
    private $client;
66
67
    public function testOutputHtmlXmlComparaison()
68
    {
69
        $this->client = static::createClient();
70
71
        $langs = array('en', 'fr');
72
        foreach ($langs as $value) {
73
            $this->outputHtmlXmlComparaison($value);
74
        }
75
    }
76
77
    public function testOutputJSONXmlComparaison()
78
    {
79
        $this->client = static::createClient();
80
81
        $langs = array('en', 'fr');
82
        foreach ($langs as $value) {
83
            $this->client->request('GET', '/example/'.$value.'.json');
84
85
            $response = $this->client->getResponse();
86
            $data     = json_decode($response->getContent(), TRUE);
87
88
            // Read the Curriculum Vitae
89
            $pathToFile            = __DIR__.'/../../Resources/data/example.xml';
90
            $this->curriculumVitae = new CurriculumVitae($pathToFile, $value);
91
92
            $cvXml = array(
93
                'identity'          => $this->curriculumVitae->getIdentity(),
94
                'followMe'          => $this->curriculumVitae->getFollowMe(),
95
                'lookingFor'        => $this->curriculumVitae->getLookingFor(),
96
                'experiences'       => $this->curriculumVitae->getExperiences(),
97
                'skills'            => $this->curriculumVitae->getSkills(),
98
                'educations'        => $this->curriculumVitae->getEducations(),
99
                'languageSkills'    => $this->curriculumVitae->getLanguageSkills(),
100
                'miscellaneous'     => $this->curriculumVitae->getMiscellaneous()
101
            );
102
103
            $this->assertSame($cvXml, $data);
104
        }
105
    }
106
107
    public function testOutputXmlXmlComparaison()
108
    {
109
        $this->client = static::createClient();
110
111
        $langs = array('en', 'fr');
112
        foreach ($langs as $value) {
113
            $this->client->request('GET', '/example/'.$value.'.xml');
114
            $response = $this->client->getResponse();
115
            $response->headers->set('Content-Type', 'application/xml');
116
            $data = $response->getContent();
117
118
            // Read the Curriculum Vitae
119
            $pathToFile            = __DIR__.'/../../Resources/data/example.xml';
120
            $this->curriculumVitae = new CurriculumVitae($pathToFile, $value);
121
122
            $cvXml = array(
123
                'identity'          => $this->curriculumVitae->getIdentity(),
124
                'followMe'          => $this->curriculumVitae->getFollowMe(),
125
                'lookingFor'        => $this->curriculumVitae->getLookingFor(),
126
                'experiences'       => $this->curriculumVitae->getExperiences(),
127
                'skills'            => $this->curriculumVitae->getSkills(),
128
                'educations'        => $this->curriculumVitae->getEducations(),
129
                'languageSkills'    => $this->curriculumVitae->getLanguageSkills(),
130
                'miscellaneous'     => $this->curriculumVitae->getMiscellaneous()
131
            );
132
            //initialisation du serializer
133
            $encoders    = array(new XmlEncoder('CurriculumVitae'));
134
            $normalizers = array(new GetSetMethodNormalizer());
135
            $serializer  = new Serializer($normalizers, $encoders);
136
137
            $this->assertSame($serializer->serialize($cvXml, 'xml'), $data);
138
        }
139
    }
140
141
    public function testOutputFollowMeLink()
142
    {
143
        $result = array();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
144
        $arrayFunctions = new ArrayFunctions();
145
146
        $this->client = static::createClient();
147
148
        $langs = array('en', 'fr');
149
        foreach ($langs as $lang) {
150
            $crawler = $this->client->request('GET', '/example/'.$lang);
151
152
            // Read the Curriculum Vitae
153
            $pathToFile            = __DIR__.'/../../Resources/data/example.xml';
154
            $this->curriculumVitae = new CurriculumVitae($pathToFile, $lang);
155
156
            $cvXml = array('followMe' => $this->curriculumVitae->getFollowMe());
157
158
            $testValue = $arrayFunctions->arrayValuesRecursive($cvXml);
159
            foreach ($testValue as $value) {
160
                $alt =  0;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
161
                $alt += $crawler->filter('img[alt="'.$value.'"]')->count();
162
                $alt += $crawler->filter('img[title="'.$value.'"]')->count();
163
                $alt += $crawler->filter('img[src="/'.$value.'"]')->count();
164
                $alt += $crawler->filter('a[href="'.$value.'"]')->count();
165
166
                if ($alt == 0) {
167
                    $result[] = 'The value '.$value.' is not diplay for language '.$lang;
168
                }
169
            }
170
        }
171
        $this->assertEquals(0, count($result),
172
            implode("\n", $result)
173
        );
174
    }
175
176
    private function outputHtmlXmlComparaison($lang = 'en')
177
    {
178
        $arrayFunctions = new ArrayFunctions();
179
        $crawler = $this->client->request('GET', '/example/'.$lang);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
180
181
        // Read the Curriculum Vitae
182
        $pathToFile            = __DIR__.'/../../Resources/data/example.xml';
183
        $this->curriculumVitae = new CurriculumVitae($pathToFile, $lang);
184
185
        $cvXml = array(
186
                'identity'          => $this->curriculumVitae->getIdentity(),
187
                'lookingFor'        => $this->curriculumVitae->getLookingFor(),
188
                'experiences'       => $this->curriculumVitae->getExperiences(),
189
                'skills'            => $this->curriculumVitae->getSkills(),
190
                'educations'        => $this->curriculumVitae->getEducations(),
191
                'languageSkills'    => $this->curriculumVitae->getLanguageSkills(),
192
                'miscellaneous'     => $this->curriculumVitae->getMiscellaneous()
193
        );
194
        // Remove all no visible elements
195
        $cvXml = $this->removeNoVisibleElementDependingOnLanguages($lang, $cvXml);
196
        $cvXml = $this->removeNoVisibleElementForAllLanguages($cvXml);
197
198
        $testValue = $arrayFunctions->arrayValuesRecursive($cvXml);
199
        $result = array();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
200
        foreach ($testValue as $value) {
201
            if ($crawler->filter('html:contains("'.$value.'")')->count() == 0) {
202
                $result[] = 'The value '.$value.' is not diplay for language '.$lang;
203
            }
204
        }
205
        $this->assertEquals(0, count($result),
206
            implode("\n", $result)
207
        );
208
    }
209
210
    /**
211
     * @param string $lang
212
     * @param array $cvXml
213
     *
214
     * @return array
215
     */
216
    private function removeNoVisibleElementDependingOnLanguages($lang, $cvXml)
217
    {
218
        switch ($lang) {
219
            case 'en':
220
                unset($cvXml['identity']['myself']['birthday']);
221
                break;
222
            default:
223
                // code...
224
                break;
225
        }
226
        return $cvXml;
227
    }
228
229
    /**
230
     * @param array $cvXml
231
     *
232
     * @return array
233
     */
234
    private function removeNoVisibleElementForAllLanguages($cvXml)
235
    {
236
        unset($cvXml['identity']['myself']['picture']);
237
        unset($cvXml['identity']['address']['street']);
238
        unset($cvXml['identity']['address']['postalcode']);
239
        unset($cvXml['identity']['address']['googlemap']);
240
        unset($cvXml['identity']['contact']['mobile']);
241
        unset($cvXml['identity']['contact']['email']);
242
        unset($cvXml['experiences']['FirstExperience']['society']['society']['ref']);
243
        unset($cvXml['experiences']['FirstExperience']['society']['siteurl']);
244
        unset($cvXml['experiences']['SecondExperience']['collapse']);
245
        unset($cvXml['experiences']['SecondExperience']['society']['society']['ref']);
246
        unset($cvXml['experiences']['SecondExperience']['society']['siteurl']);
247
        unset($cvXml['experiences']['ThirdExperience']['society']['society']['ref']);
248
        unset($cvXml['experiences']['FourthExperience']['collapse']);
249
        unset($cvXml['skills']['Functional']['lines']['success']['percentage']);
250
        unset($cvXml['skills']['Functional']['lines']['success']['class']);
251
        unset($cvXml['skills']['Functional']['lines']['success']['striped']);
252
        unset($cvXml['skills']['Functional']['lines']['otherSucess']['percentage']);
253
        unset($cvXml['skills']['Functional']['lines']['otherSucess']['class']);
254
        unset($cvXml['skills']['Functional']['lines']['info']['percentage']);
255
        unset($cvXml['skills']['Functional']['lines']['info']['class']);
256
        unset($cvXml['skills']['Functional']['lines']['info']['striped']);
257
        unset($cvXml['skills']['Functional']['lines']['warning']['percentage']);
258
        unset($cvXml['skills']['Functional']['lines']['warning']['class']);
259
        unset($cvXml['skills']['Functional']['lines']['danger']['percentage']);
260
        unset($cvXml['skills']['Functional']['lines']['danger']['class']);
261
        unset($cvXml['skills']['Functional']['lines']['noClass']['percentage']);
262
        unset($cvXml['skills']['OtherSkill']['lines']['success']['percentage']);
263
        unset($cvXml['skills']['OtherSkill']['lines']['success']['class']);
264
        unset($cvXml['skills']['OtherSkill']['lines']['success']['striped']);
265
        unset($cvXml['skills']['OtherSkill']['lines']['info']['percentage']);
266
        unset($cvXml['skills']['OtherSkill']['lines']['info']['class']);
267
        unset($cvXml['skills']['OtherSkill']['lines']['info']['striped']);
268
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['percentage']);
269
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['class']);
270
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['striped']);
271
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['percentage']);
272
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['class']);
273
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['striped']);
274
        unset($cvXml['educations']['HighSchool']['collapse']);
275
        unset($cvXml['educations']['FirstSchool']['collapse']);
276
        unset($cvXml['languageSkills']['French']['icon']);
277
        unset($cvXml['languageSkills']['English']['icon']);
278
279
        return $cvXml;
280
    }
281
}
282