Passed
Push — master ( 494588...ccadcf )
by Fabien
05:22
created

testXml2arrayWithCrossRefDepth1()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
nc 1
nop 0
dl 0
loc 29
rs 8.8571
c 0
b 0
f 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\Entity;
13
14
use FabienCrassat\CurriculumVitaeBundle\Entity\Xml2arrayFunctions;
15
16
class Xml2arrayFunctionsTest extends \PHPUnit\Framework\TestCase
17
{
18
    private $xml2arrayFunctions;
19
20
    public function testXml2arrayEmpty() {
21
        $string   = <<<XML
22
<?xml version='1.0'?>
23
<document>
24
</document>
25
XML;
26
        $expected = [];
27
28
        $this->assertXml2Array($expected, $string, $string);
29
    }
30
31
    public function testXml2arrayWithBadLanguage() {
32
        $string   = <<<XML
33
<?xml version='1.0'?>
34
<document>
35
    <node lang='unknown'>something we don't want</node>
36
</document>
37
XML;
38
        $expected = [];
39
40
        $this->assertXml2Array($expected, $string, $string);
41
    }
42
43
    public function testXml2arraySimple() {
44
        $string   = <<<XML
45
<?xml version='1.0'?>
46
<document>
47
 <title>Forty What?</title>
48
 <from>Joe</from>
49
 <to>Jane</to>
50
 <body>
51
  I know that's the answer -- but what's the question?
52
 </body>
53
</document>
54
XML;
55
        $expected = [
56
            'title' => 'Forty What?',
57
            'from'  => 'Joe',
58
            'to'    => 'Jane',
59
            'body'  => "I know that's the answer -- but what's the question?"
60
        ];
61
62
        $this->assertXml2Array($expected, $string, $string);
63
    }
64
65
    public function testXml2arrayWithAttribute() {
66
        $string   = <<<XML
67
<?xml version='1.0'?>
68
<document>
69
 <attr attributekey="we don't care">The value win, not the attribute!!!</attr>
70
 <attr2 attributekey="attributevalue"><product>product</product></attr2>
71
 <onlyattribute attributekey="attributevalue"></onlyattribute>
72
 <value>value</value>
73
 <id id="id"></id>
74
 <ref ref="ref"></ref>
75
 <lang lang="lang"></lang>
76
</document>
77
XML;
78
        $expected = [
79
            'attr'            => 'The value win, not the attribute!!!',
80
            'onlyattribute'   => [
81
                'attributekey' => 'attributevalue'],
82
            'attr2' => [
83
                'attributekey' => 'attributevalue',
84
                'product'      => 'product'],
85
            'value' => 'value'
86
        ];
87
88
        $this->assertXml2Array($expected, $string, $string);
89
    }
90
91
    public function testXml2arrayWithCrossRefDepth1() {
92
        $curriculumVitae = <<<XML
93
<?xml version='1.0'?>
94
<document>
95
 <mysociety crossref="societies/society[@ref='SocietyFoo']/*"></mysociety>
96
 <societies>
97
  <society ref="SocietyFoo">
98
    <name>SocietyFoo</name>
99
    <anaddress>An address</anaddress>
100
    <url>http://www.google.com</url>
101
  </society>
102
 </societies>
103
</document>
104
XML;
105
106
        $string = <<<XML
107
<?xml version='1.0'?>
108
<document>
109
 <mysociety crossref="societies/society[@ref='SocietyFoo']/*"></mysociety>
110
</document>
111
XML;
112
113
        $expected = ['mysociety' => [
114
            'name'      => 'SocietyFoo',
115
            'anaddress' => 'An address',
116
            'url'       => 'http://www.google.com'
117
        ]];
118
119
        $this->assertXml2Array($expected, $curriculumVitae, $string);
120
    }
121
122
    public function testXml2arrayWithCrossRefDepth2() {
123
        $curriculumVitae = <<<XML
124
<?xml version='1.0'?>
125
<document>
126
 <job crossref="experience[@id='OneExperience']/*"></job>
127
 <experience id="OneExperience">
128
  <asociety crossref="societies/society[@ref='SocietyBar']/*"></asociety>
129
  <job>My first job</job>
130
 </experience>
131
 <societies>
132
  <society ref="SocietyBar">
133
    <name>OneSociety</name>
134
    <address>An address</address>
135
    <linktositeurl>http://www.anurl.com</linktositeurl>
136
  </society>
137
 </societies>
138
</document>
139
XML;
140
141
        $string = <<<XML
142
<?xml version='1.0'?>
143
<document>
144
 <job crossref="experience[@id='OneExperience']/*"></job>
145
</document>
146
XML;
147
148
        $society = [
149
            'name'          => 'OneSociety',
150
            'address'       => 'An address',
151
            'linktositeurl' => 'http://www.anurl.com'
152
        ];
153
154
        $expected = ['job' => [
155
            'asociety' => $society,
156
            'job'      => 'My first job'
157
        ]];
158
159
        $this->assertXml2Array($expected, $curriculumVitae, $string);
160
161
        $string = <<<XML
162
<?xml version='1.0'?>
163
<document>
164
 <experience id="OneExperience">
165
  <asociety crossref="societies/society[@ref='SocietyBar']/*"></asociety>
166
  <job>My first job</job>
167
 </experience>
168
</document>
169
XML;
170
171
        $expected = ['OneExperience' => [
172
            'asociety' => $society,
173
            'job'      => 'My first job'
174
        ]];
175
176
        $this->assertXml2Array($expected, $curriculumVitae, $string);
177
178
        $string = <<<XML
179
<?xml version='1.0'?>
180
<document>
181
  <asociety crossref="societies/society[@ref='SocietyBar']/*"></asociety>
182
</document>
183
XML;
184
185
        $expected = ['asociety' => $society];
186
187
        $this->assertXml2Array($expected, $curriculumVitae, $string);
188
    }
189
190
    public function testXml2arrayWithCrossRef() {
191
        $curriculumVitae = <<<XML
192
<?xml version='1.0'?>
193
<document>
194
 <job crossref="experience[@id='OneExperience']/*"></job>
195
 <experience id="OneExperience">
196
  <society crossref="societies/society[@ref='MySociety']/*"></society>
197
  <job>A experience</job>
198
 </experience>
199
 <societies>
200
  <society ref="MySociety">
201
    <name>MySociety</name>
202
    <oneaddress>My address</oneaddress>
203
    <siteurl>http://www.crassat.com</siteurl>
204
  </society>
205
 </societies>
206
</document>
207
XML;
208
209
        $string = <<<XML
210
<?xml version='1.0'?>
211
<document>
212
 <job crossref="experience[@id='OneExperience']/*"></job>
213
</document>
214
XML;
215
216
        $expected = ['job' => [
217
            'society' => [
218
                'name'       => 'MySociety',
219
                'oneaddress' => 'My address',
220
                'siteurl'    => 'http://www.crassat.com'],
221
            'job'   => 'A experience'
222
        ]];
223
224
        $this->assertXml2Array($expected, $curriculumVitae, $string);
225
    }
226
227
    public function testXml2arrayWithCVCrossRef() {
228
        $curriculumVitae = <<<XML
229
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
230
<root>
231
    <langs>
232
        <lang id="en">English</lang><lang id="fr">Français</lang>
233
    </langs>
234
    <curriculumVitae>
235
        <lookingFor>
236
            <experience crossref="curriculumVitae/experiences/items/experience[@id='SecondJob']/*"></experience>
237
            <presentation lang="en">A good presentation.</presentation>
238
            <presentation lang="fr">Une bonne présentation.</presentation>
239
        </lookingFor>
240
        <experiences anchor="experiences">
241
            <anchorTitle lang="en">Experiences</anchorTitle>
242
            <anchorTitle lang="fr">Expériences Professionnelles</anchorTitle>
243
            <items>
244
                <experience id="SecondJob">
245
                    <date lang="en">Apr 2011 - Present</date>
246
                    <date lang="fr">Avr. 2011 - Aujourd'hui</date>
247
                    <job lang="en">Second Job</job>
248
                    <job lang="fr">Deuxième Job</job>
249
                    <onesociety crossref="societies/society[@ref='ASociety']/*"></onesociety>
250
                    <missions lang="en">
251
                        <item>A mission of my second job.</item>
252
                    </missions>
253
                    <missions lang="fr">
254
                        <item>Une mission de mon deuxième job.</item>
255
                    </missions>
256
                </experience>
257
                <experience id="FirstJob">
258
                    <date lang="en">Nov 2009 - Apr 2011</date>
259
                    <date lang="fr">Nov. 2009 - Avr. 2011</date>
260
                    <job lang="en">First Job</job>
261
                    <job lang="fr">Premier Job</job>
262
                    <onesociety crossref="societies/society[@ref='ASociety']/*"></onesociety>
263
                    <missions lang="en">
264
                        <item>A mission of my first job.</item>
265
                    </missions>
266
                    <missions lang="fr">
267
                        <item>Une mission de mon premier job.</item>
268
                    </missions>
269
                </experience>
270
            </items>
271
        </experiences>
272
    </curriculumVitae>
273
    <societies>
274
        <society ref="ASociety">
275
            <name>ASociety</name>
276
            <myaddress>myaddress</myaddress>
277
            <siteurl>http://cv.crassat.com</siteurl>
278
        </society>
279
    </societies>
280
</root>
281
XML;
282
283
        $society           = [
284
            'name'      => 'ASociety',
285
            'myaddress' => 'myaddress',
286
            'siteurl'   => 'http://cv.crassat.com'];
287
        $currentExperience = [
288
            'job'        => 'Second Job',
289
            'date'       => 'Apr 2011 - Present',
290
            'onesociety' => $society,
291
            'missions'   => [
292
                'item'   => ['A mission of my second job.']]];
293
294
        $expected = [
295
            'langs'  => [
296
                'en' => 'English',
297
                'fr' => 'Français'],
298
            'curriculumVitae' => [
299
                'lookingFor'  => [
300
                    'experience'   => $currentExperience,
301
                    'presentation' => 'A good presentation.'],
302
                'experiences' => [
303
                    'anchorTitle' => 'Experiences',
304
                    'items'       => [
305
                        'SecondJob' => $currentExperience,
306
                        'FirstJob'  => [
307
                            'job'        => 'First Job',
308
                            'date'       => 'Nov 2009 - Apr 2011',
309
                            'onesociety' => $society,
310
                            'missions'   => [
311
                                'item'   => ['A mission of my first job.']]]],
312
                    'anchor' => 'experiences']],
313
            'societies' => ['society' => $society]
314
        ];
315
316
        $this->assertXml2Array($expected, $curriculumVitae, $curriculumVitae);
317
    }
318
319
    /**
320
     * @param string $curriculumVitae
321
     * @param string $xml
322
     */
323
    private function assertXml2Array($expected, $curriculumVitae, $xml) {
324
        $this->xml2arrayFunctions = new Xml2arrayFunctions(simplexml_load_string($curriculumVitae));
325
326
        $result = $this->xml2arrayFunctions->xml2array(simplexml_load_string($xml));
327
328
        $this->assertEquals($expected, $result);
329
    }
330
}
331