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

DefaultControllerTest::outputHtmlXmlComparaison()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 32
rs 8.8571
cc 3
eloc 21
nc 3
nop 1
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 $dir        = __DIR__;
27
    private $pathToFile = '/../../Resources/data/example.xml';
28
    private $langs      = array('en', 'fr');
29
30
    public function testIndex()
31
    {
32
        $client = static::createClient();
33
        $client->request('GET', '/');
34
        $this->assertEquals(301, $client->getResponse()->getStatusCode());
35
    }
36
37
    public function testDisplay()
38
    {
39
        $client  = static::createClient();
40
        $crawler = $client->request('GET', '/example');
41
        $this->assertGreaterThan(0, $crawler->filter('html:contains("First Name Last Name")')->count());
42
    }
43
44
    public function testOutputHtmlXmlComparaison()
45
    {
46
        $this->client = static::createClient();
47
48
        foreach ($this->langs as $lang) {
49
            $this->outputHtmlXmlComparaison($lang);
50
        }
51
    }
52
53
    public function testOutputJSONXmlComparaison()
54
    {
55
        $this->client = static::createClient();
56
57
        foreach ($this->langs as $lang) {
58
            $this->client->request('GET', '/example/'.$lang.'.json');
59
60
            $response = $this->client->getResponse();
61
            $data     = json_decode($response->getContent(), TRUE);
62
63
            // Read the Curriculum Vitae
64
            $this->curriculumVitae = new CurriculumVitae($this->dir.$this->pathToFile, $lang);
65
66
            $this->assertSame(
67
                $this->curriculumVitae->getCurriculumVitaeArray(),
68
                $data);
69
        }
70
    }
71
72
    public function testOutputXmlXmlComparaison()
73
    {
74
        $this->client = static::createClient();
75
76
        foreach ($this->langs as $lang) {
77
            $this->client->request('GET', '/example/'.$lang.'.xml');
78
            $response = $this->client->getResponse();
79
            $response->headers->set('Content-Type', 'application/xml');
80
            $data = $response->getContent();
81
82
            // Read the Curriculum Vitae
83
            $this->curriculumVitae = new CurriculumVitae($this->dir.$this->pathToFile, $lang);
84
85
            $this->assertSame(
86
                $this->initSerializer()->serialize(
87
                    $this->curriculumVitae->getCurriculumVitaeArray(),
88
                    'xml'),
89
                $data);
90
        }
91
    }
92
93
    private function initSerializer()
94
    {
95
        //initialisation du serializer
96
        $encoders    = array(new XmlEncoder('CurriculumVitae'));
97
        $normalizers = array(new GetSetMethodNormalizer());
98
        return new Serializer($normalizers, $encoders);
99
    }
100
101
    public function testOutputFollowMeLink()
102
    {
103
        $result         = array();
104
        $arrayFunctions = new ArrayFunctions();
105
106
        $this->client = static::createClient();
107
108
        foreach ($this->langs as $lang) {
109
            $crawler = $this->client->request('GET', '/example/'.$lang);
110
111
            // Read the Curriculum Vitae
112
            $this->curriculumVitae = new CurriculumVitae($this->dir.$this->pathToFile, $lang);
113
114
            $cvXml = array('followMe' => $this->curriculumVitae->getFollowMe());
115
116
            $testValue = $arrayFunctions->arrayValuesRecursive($cvXml);
117
            foreach ($testValue as $value) {
118
                $alt  = 0;
119
                $alt += $crawler->filter('img[alt="'.$value.'"]')->count();
120
                $alt += $crawler->filter('img[title="'.$value.'"]')->count();
121
                $alt += $crawler->filter('img[src="/'.$value.'"]')->count();
122
                $alt += $crawler->filter('a[href="'.$value.'"]')->count();
123
124
                if ($alt == 0) {
125
                    $result[] = 'The value '.$value.' is not diplay for language '.$lang;
126
                }
127
            }
128
        }
129
        $this->assertEquals(0, count($result),
130
            implode("\n", $result)
131
        );
132
    }
133
134
    private function outputHtmlXmlComparaison($lang = 'en')
135
    {
136
        $arrayFunctions = new ArrayFunctions();
137
        $crawler        = $this->client->request('GET', '/example/'.$lang);
138
139
        // Read the Curriculum Vitae
140
        $this->curriculumVitae = new CurriculumVitae($this->dir.$this->pathToFile, $lang);
141
142
        $cvXml = array(
143
                'identity'          => $this->curriculumVitae->getIdentity(),
144
                'lookingFor'        => $this->curriculumVitae->getLookingFor(),
145
                'experiences'       => $this->curriculumVitae->getExperiences(),
146
                'skills'            => $this->curriculumVitae->getSkills(),
147
                'educations'        => $this->curriculumVitae->getEducations(),
148
                'languageSkills'    => $this->curriculumVitae->getLanguageSkills(),
149
                'miscellaneous'     => $this->curriculumVitae->getMiscellaneous()
150
        );
151
        // Remove all no visible elements
152
        $cvXml = $this->removeNoVisibleElementDependingOnLanguages($lang, $cvXml);
153
        $cvXml = $this->removeNoVisibleElementForAllLanguages($cvXml);
154
155
        $testValue = $arrayFunctions->arrayValuesRecursive($cvXml);
156
        $result    = array();
157
        foreach ($testValue as $value) {
158
            if ($crawler->filter('html:contains("'.$value.'")')->count() == 0) {
159
                $result[] = 'The value '.$value.' is not diplay for language '.$lang;
160
            }
161
        }
162
        $this->assertEquals(0, count($result),
163
            implode("\n", $result)
164
        );
165
    }
166
167
    /**
168
     * @param string $lang
169
     * @param array $cvXml
170
     *
171
     * @return array
172
     */
173
    private function removeNoVisibleElementDependingOnLanguages($lang, $cvXml)
174
    {
175
        switch ($lang) {
176
            case 'en':
177
                unset($cvXml['identity']['myself']['birthday']);
178
                break;
179
            default:
180
                // code...
181
                break;
182
        }
183
        return $cvXml;
184
    }
185
186
    /**
187
     * @param array $cvXml
188
     *
189
     * @return array
190
     */
191
    private function removeNoVisibleElementForAllLanguages($cvXml)
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['identity']['contact']['email']);
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
        return $cvXml;
237
    }
238
}
239