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