Passed
Push — Codacy ( 230dad...27b2c4 )
by Fabien
02:35
created

DefaultControllerTest::outputHtmlXmlComparaison()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 76
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 76
rs 8.9667
cc 3
eloc 64
nc 4
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 $curriculumVitae;
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
84
            $response = $this->client->getResponse();
85
            $data     = json_decode($response->getContent(), TRUE);
86
87
            // Read the Curriculum Vitae
88
            $pathToFile            = __DIR__.'/../../Resources/data/example.xml';
89
            $this->curriculumVitae = new CurriculumVitae($pathToFile, $value);
90
91
            $cvXml = array(
92
                'identity'          => $this->curriculumVitae->getIdentity(),
93
                'followMe'          => $this->curriculumVitae->getFollowMe(),
94
                'lookingFor'        => $this->curriculumVitae->getLookingFor(),
95
                'experiences'       => $this->curriculumVitae->getExperiences(),
96
                'skills'            => $this->curriculumVitae->getSkills(),
97
                'educations'        => $this->curriculumVitae->getEducations(),
98
                'languageSkills'    => $this->curriculumVitae->getLanguageSkills(),
99
                'miscellaneous'     => $this->curriculumVitae->getMiscellaneous()
100
            );
101
102
            $this->assertSame($cvXml, $data);
103
        }
104
    }
105
106
    public function testOutputXmlXmlComparaison()
107
    {
108
        $this->client = static::createClient();
109
110
        $langs = array('en', 'fr');
111
        foreach ($langs as $value) {
112
            $this->client->request('GET', '/example/'.$value.'.xml');
113
            $response = $this->client->getResponse();
114
            $response->headers->set('Content-Type', 'application/xml');
115
            $data = $response->getContent();
116
117
            // Read the Curriculum Vitae
118
            $pathToFile            = __DIR__.'/../../Resources/data/example.xml';
119
            $this->curriculumVitae = new CurriculumVitae($pathToFile, $value);
120
121
            $cvXml = array(
122
                'identity'          => $this->curriculumVitae->getIdentity(),
123
                'followMe'          => $this->curriculumVitae->getFollowMe(),
124
                'lookingFor'        => $this->curriculumVitae->getLookingFor(),
125
                'experiences'       => $this->curriculumVitae->getExperiences(),
126
                'skills'            => $this->curriculumVitae->getSkills(),
127
                'educations'        => $this->curriculumVitae->getEducations(),
128
                'languageSkills'    => $this->curriculumVitae->getLanguageSkills(),
129
                'miscellaneous'     => $this->curriculumVitae->getMiscellaneous()
130
            );
131
            //initialisation du serializer
132
            $encoders    = array(new XmlEncoder('CurriculumVitae'));
133
            $normalizers = array(new GetSetMethodNormalizer());
134
            $serializer  = new Serializer($normalizers, $encoders);
135
136
            $this->assertSame($serializer->serialize($cvXml, 'xml'), $data);
137
        }
138
    }
139
140
    public function testOutputFollowMeLink()
141
    {
142
        $this->client = static::createClient();
143
144
        $langs = array('en', 'fr');
145
        foreach ($langs as $lang) {
146
            $crawler = $this->client->request('GET', '/example/'.$lang);
147
148
            // Read the Curriculum Vitae
149
            $pathToFile            = __DIR__.'/../../Resources/data/example.xml';
150
            $this->curriculumVitae = new CurriculumVitae($pathToFile, $lang);
151
152
            $cvXml = array('followMe' => $this->curriculumVitae->getFollowMe());
153
154
            $testValue = $this->arrayValuesRecursive($cvXml);
155
            foreach ($testValue as $value) {
156
                $alt  = $crawler->filter('img[alt="'.$value.'"]')->count();
157
                $alt += $crawler->filter('img[title="'.$value.'"]')->count();
158
                $alt += $crawler->filter('img[src="/'.$value.'"]')->count();
159
                $alt += $crawler->filter('a[href="'.$value.'"]')->count();
160
161
                $this->assertGreaterThan(0, $alt,
162
                    'The value '.$value.' is not diplay for language '.$lang
163
                );
164
            }
165
        }
166
    }
167
168
    private function outputHtmlXmlComparaison($lang = 'en')
169
    {
170
        $crawler = $this->client->request('GET', '/example/'.$lang);
171
172
        // Read the Curriculum Vitae
173
        $pathToFile            = __DIR__.'/../../Resources/data/example.xml';
174
        $this->curriculumVitae = new CurriculumVitae($pathToFile, $lang);
175
176
        $cvXml = array(
177
                'identity'          => $this->curriculumVitae->getIdentity(),
178
                'lookingFor'        => $this->curriculumVitae->getLookingFor(),
179
                'experiences'       => $this->curriculumVitae->getExperiences(),
180
                'skills'            => $this->curriculumVitae->getSkills(),
181
                'educations'        => $this->curriculumVitae->getEducations(),
182
                'languageSkills'    => $this->curriculumVitae->getLanguageSkills(),
183
                'miscellaneous'     => $this->curriculumVitae->getMiscellaneous()
184
        );
185
        // Remove all no visible elements
186
        switch ($lang) {
187
            case 'en':
188
                unset($cvXml['identity']['myself']['birthday']);
189
                break;
190
            default:
191
                // code...
192
                break;
193
        }
194
        unset($cvXml['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['experiences']['FirstExperience']['society']['society']['ref']);
200
        unset($cvXml['experiences']['FirstExperience']['society']['siteurl']);
201
        unset($cvXml['experiences']['SecondExperience']['collapse']);
202
        unset($cvXml['experiences']['SecondExperience']['society']['society']['ref']);
203
        unset($cvXml['experiences']['SecondExperience']['society']['siteurl']);
204
        unset($cvXml['experiences']['ThirdExperience']['society']['society']['ref']);
205
        unset($cvXml['experiences']['FourthExperience']['collapse']);
206
        unset($cvXml['skills']['Functional']['lines']['success']['percentage']);
207
        unset($cvXml['skills']['Functional']['lines']['success']['class']);
208
        unset($cvXml['skills']['Functional']['lines']['success']['striped']);
209
        unset($cvXml['skills']['Functional']['lines']['otherSucess']['percentage']);
210
        unset($cvXml['skills']['Functional']['lines']['otherSucess']['class']);
211
        unset($cvXml['skills']['Functional']['lines']['info']['percentage']);
212
        unset($cvXml['skills']['Functional']['lines']['info']['class']);
213
        unset($cvXml['skills']['Functional']['lines']['info']['striped']);
214
        unset($cvXml['skills']['Functional']['lines']['warning']['percentage']);
215
        unset($cvXml['skills']['Functional']['lines']['warning']['class']);
216
        unset($cvXml['skills']['Functional']['lines']['danger']['percentage']);
217
        unset($cvXml['skills']['Functional']['lines']['danger']['class']);
218
        unset($cvXml['skills']['Functional']['lines']['noClass']['percentage']);
219
        unset($cvXml['skills']['OtherSkill']['lines']['success']['percentage']);
220
        unset($cvXml['skills']['OtherSkill']['lines']['success']['class']);
221
        unset($cvXml['skills']['OtherSkill']['lines']['success']['striped']);
222
        unset($cvXml['skills']['OtherSkill']['lines']['info']['percentage']);
223
        unset($cvXml['skills']['OtherSkill']['lines']['info']['class']);
224
        unset($cvXml['skills']['OtherSkill']['lines']['info']['striped']);
225
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['percentage']);
226
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['class']);
227
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['striped']);
228
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['percentage']);
229
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['class']);
230
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['striped']);
231
        unset($cvXml['educations']['HighSchool']['collapse']);
232
        unset($cvXml['educations']['FirstSchool']['collapse']);
233
        unset($cvXml['languageSkills']['French']['icon']);
234
        unset($cvXml['languageSkills']['English']['icon']);
235
236
        $testValue = $this->arrayValuesRecursive($cvXml);
237
        foreach ($testValue as $value) {
238
            $this->assertGreaterThan(0,
239
                $crawler->filter('html:contains("'.$value.'")')->count(),
240
                'The value '.$value.' is not diplay for language '.$lang
241
            );
242
        }
243
    }
244
245
    private function arrayValuesRecursive($array)
246
    {
247
        $return = array();
248
        foreach($array as $value) {
249
            $return = $this->arrayValuesMerge($return, $value);
250
        }
251
        return $return;
252
    }
253
254
    private function arrayValuesMerge($return, $value)
255
    {
256
        if(is_array($value)) {
257
            return array_merge($return, $this->arrayValuesRecursive($value));
258
        }
259
260
        return array_merge($return, array($value));
261
    }
262
}
263