Completed
Pull Request — master (#16)
by Fabien
06:12 queued 03:13
created

testOutputXmlXmlComparaison()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 2
eloc 13
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
24
    private $curriculumVitae;
25
    private $client;
26
    private $pathToFile;
27
    private $langs;
28
29
    function setUp()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
30
    {
31
        $this->pathToFile = __DIR__.'/../../Resources/data/example.xml';
32
        $this->langs      = array('en', 'fr');
33
    }
34
35
    public function testIndex()
36
    {
37
        $client = static::createClient();
38
        $client->request('GET', '/');
39
        $this->assertEquals(301, $client->getResponse()->getStatusCode());
40
    }
41
42
    public function testDisplay()
43
    {
44
        $client  = static::createClient();
45
        $crawler = $client->request('GET', '/example');
46
        $this->assertGreaterThan(0, $crawler->filter('html:contains("First Name Last Name")')->count());
47
    }
48
49
    public function testOutputHtmlXmlComparaison()
50
    {
51
        $this->client = static::createClient();
52
53
        foreach ($this->langs as $lang) {
54
            $this->outputHtmlXmlComparaison($lang);
55
        }
56
    }
57
58
    public function testOutputJSONXmlComparaison()
59
    {
60
        $this->client = static::createClient();
61
62
        foreach ($this->langs as $lang) {
63
            $this->client->request('GET', '/example/'.$lang.'.json');
64
65
            $response = $this->client->getResponse();
66
            $data     = json_decode($response->getContent(), TRUE);
67
68
            // Read the Curriculum Vitae
69
            $this->curriculumVitae = new CurriculumVitae($this->pathToFile, $lang);
70
71
            $this->assertSame(
72
                $this->curriculumVitae->getCurriculumVitaeArray(),
73
                $data);
74
        }
75
    }
76
77
    public function testOutputXmlXmlComparaison()
78
    {
79
        $this->client = static::createClient();
80
81
        foreach ($this->langs as $lang) {
82
            $this->client->request('GET', '/example/'.$lang.'.xml');
83
            $response = $this->client->getResponse();
84
            $response->headers->set('Content-Type', 'application/xml');
85
            $data = $response->getContent();
86
87
            // Read the Curriculum Vitae
88
            $this->curriculumVitae = new CurriculumVitae($this->pathToFile, $lang);
89
90
            $this->assertSame(
91
                $this->initSerializer()->serialize(
92
                    $this->curriculumVitae->getCurriculumVitaeArray(),
93
                    'xml'),
94
                $data);
95
        }
96
    }
97
98
    private function initSerializer()
99
    {
100
        //initialisation du serializer
101
        $encoders    = array(new XmlEncoder('CurriculumVitae'));
102
        $normalizers = array(new GetSetMethodNormalizer());
103
        return new Serializer($normalizers, $encoders);
104
    }
105
106
    public function testOutputFollowMeLink()
107
    {
108
        $result         = array();
109
        $arrayFunctions = new ArrayFunctions();
110
111
        $this->client = static::createClient();
112
113
        foreach ($this->langs as $lang) {
114
            $crawler = $this->client->request('GET', '/example/'.$lang);
115
116
            // Read the Curriculum Vitae
117
            $this->curriculumVitae = new CurriculumVitae($this->pathToFile, $lang);
118
119
            $cvXml = array('followMe' => $this->curriculumVitae->getFollowMe());
120
121
            $testValue = $arrayFunctions->arrayValuesRecursive($cvXml);
122
            foreach ($testValue as $value) {
123
                $alt  = 0;
124
                $alt += $crawler->filter('img[alt="'.$value.'"]')->count();
125
                $alt += $crawler->filter('img[title="'.$value.'"]')->count();
126
                $alt += $crawler->filter('img[src="/'.$value.'"]')->count();
127
                $alt += $crawler->filter('a[href="'.$value.'"]')->count();
128
129
                if ($alt == 0) {
130
                    $result[] = 'The value '.$value.' is not diplay for language '.$lang;
131
                }
132
            }
133
        }
134
        $this->assertEquals(0, count($result),
135
            implode("\n", $result)
136
        );
137
    }
138
139
    private function outputHtmlXmlComparaison($lang = 'en')
140
    {
141
        $arrayFunctions = new ArrayFunctions();
142
        $crawler        = $this->client->request('GET', '/example/'.$lang);
143
144
        // Read the Curriculum Vitae
145
        $this->curriculumVitae = new CurriculumVitae($this->pathToFile, $lang);
146
147
        $cvXml = array(
148
                'identity'          => $this->curriculumVitae->getIdentity(),
149
                'lookingFor'        => $this->curriculumVitae->getLookingFor(),
150
                'experiences'       => $this->curriculumVitae->getExperiences(),
151
                'skills'            => $this->curriculumVitae->getSkills(),
152
                'educations'        => $this->curriculumVitae->getEducations(),
153
                'languageSkills'    => $this->curriculumVitae->getLanguageSkills(),
154
                'miscellaneous'     => $this->curriculumVitae->getMiscellaneous()
155
        );
156
        // Remove all no visible elements
157
        $cvXml = $this->removeNoVisibleElementDependingOnLanguages($lang, $cvXml);
158
        $cvXml = $this->removeNoVisibleElementForAllLanguages($cvXml);
159
160
        $testValue = $arrayFunctions->arrayValuesRecursive($cvXml);
161
        $result    = array();
162
        foreach ($testValue as $value) {
163
            if ($crawler->filter('html:contains("'.$value.'")')->count() == 0) {
164
                $result[] = 'The value '.$value.' is not diplay for language '.$lang;
165
            }
166
        }
167
        $this->assertEquals(0, count($result),
168
            implode("\n", $result)
169
        );
170
    }
171
172
    /**
173
     * @param string $lang
174
     * @param array $cvXml
175
     *
176
     * @return array
177
     */
178
    private function removeNoVisibleElementDependingOnLanguages($lang, $cvXml)
179
    {
180
        switch ($lang) {
181
            case 'en':
182
                unset($cvXml['identity']['myself']['birthday']);
183
                break;
184
            default:
185
                // code...
186
                break;
187
        }
188
        return $cvXml;
189
    }
190
191
    /**
192
     * @param array $cvXml
193
     *
194
     * @return array
195
     */
196
    private function removeNoVisibleElementForAllLanguages($cvXml)
197
    {
198
        unset($cvXml['identity']['myself']['picture']);
199
        unset($cvXml['identity']['address']['street']);
200
        unset($cvXml['identity']['address']['postalcode']);
201
        unset($cvXml['identity']['address']['googlemap']);
202
        unset($cvXml['identity']['contact']['mobile']);
203
        unset($cvXml['identity']['contact']['email']);
204
        unset($cvXml['experiences']['FirstExperience']['society']['society']['ref']);
205
        unset($cvXml['experiences']['FirstExperience']['society']['siteurl']);
206
        unset($cvXml['experiences']['SecondExperience']['collapse']);
207
        unset($cvXml['experiences']['SecondExperience']['society']['society']['ref']);
208
        unset($cvXml['experiences']['SecondExperience']['society']['siteurl']);
209
        unset($cvXml['experiences']['ThirdExperience']['society']['society']['ref']);
210
        unset($cvXml['experiences']['FourthExperience']['collapse']);
211
        unset($cvXml['skills']['Functional']['lines']['success']['percentage']);
212
        unset($cvXml['skills']['Functional']['lines']['success']['class']);
213
        unset($cvXml['skills']['Functional']['lines']['success']['striped']);
214
        unset($cvXml['skills']['Functional']['lines']['otherSucess']['percentage']);
215
        unset($cvXml['skills']['Functional']['lines']['otherSucess']['class']);
216
        unset($cvXml['skills']['Functional']['lines']['info']['percentage']);
217
        unset($cvXml['skills']['Functional']['lines']['info']['class']);
218
        unset($cvXml['skills']['Functional']['lines']['info']['striped']);
219
        unset($cvXml['skills']['Functional']['lines']['warning']['percentage']);
220
        unset($cvXml['skills']['Functional']['lines']['warning']['class']);
221
        unset($cvXml['skills']['Functional']['lines']['danger']['percentage']);
222
        unset($cvXml['skills']['Functional']['lines']['danger']['class']);
223
        unset($cvXml['skills']['Functional']['lines']['noClass']['percentage']);
224
        unset($cvXml['skills']['OtherSkill']['lines']['success']['percentage']);
225
        unset($cvXml['skills']['OtherSkill']['lines']['success']['class']);
226
        unset($cvXml['skills']['OtherSkill']['lines']['success']['striped']);
227
        unset($cvXml['skills']['OtherSkill']['lines']['info']['percentage']);
228
        unset($cvXml['skills']['OtherSkill']['lines']['info']['class']);
229
        unset($cvXml['skills']['OtherSkill']['lines']['info']['striped']);
230
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['percentage']);
231
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['class']);
232
        unset($cvXml['skills']['OtherSkill']['lines']['warning']['striped']);
233
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['percentage']);
234
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['class']);
235
        unset($cvXml['skills']['OtherSkill']['lines']['danger']['striped']);
236
        unset($cvXml['educations']['HighSchool']['collapse']);
237
        unset($cvXml['educations']['FirstSchool']['collapse']);
238
        unset($cvXml['languageSkills']['French']['icon']);
239
        unset($cvXml['languageSkills']['English']['icon']);
240
241
        return $cvXml;
242
    }
243
}
244