Completed
Push — master ( c59679...9e7f7b )
by Fabien
03:51
created

testXml2arrayWithBadLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 1
Metric Value
c 2
b 2
f 1
dl 0
loc 11
rs 9.4285
cc 1
eloc 6
nc 1
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\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 = array();
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 = array();
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 = array(
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 = array(
79
            'attr' => "The value win, not the attribute!!!",
80
            'onlyattribute'   => array(
81
                'attributekey' => "attributevalue"),
82
            'attr2'   => array(
83
                'attributekey' => "attributevalue",
84
                'product' => "product"),
85
            'value'  => "value"
86
        );
87
88
        $this->assertXml2Array($expected, $string, $string);
89
    }
90
91
    public function testXml2arrayWithCrossRefDepth1() {
92
        $CV = <<<XML
93
<?xml version='1.0'?>
94
<document>
95
 <society crossref="societies/society[@ref='OneSociety']/*"></society>
96
 <societies>
97
  <society ref="OneSociety">
98
    <name>OneSociety</name>
99
    <address>An address</address>
100
    <siteurl>http://www.google.com</siteurl>
101
  </society>
102
 </societies>
103
</document>
104
XML;
105
        $string = <<<XML
106
<?xml version='1.0'?>
107
<document>
108
 <society crossref="societies/society[@ref='OneSociety']/*"></society>
109
</document>
110
XML;
111
        $expected = array('society' => array(
112
            'name'      => "OneSociety",
113
            'address'   => "An address",
114
            'siteurl'   => "http://www.google.com"
115
        ));
116
117
        $this->assertXml2Array($expected, $CV, $string);
118
    }
119
120
    public function testXml2arrayWithCrossRefDepth2() {
121
        $CV = <<<XML
122
<?xml version='1.0'?>
123
<document>
124
 <job crossref="experience[@id='OneExperience']/*"></job>
125
 <experience id="OneExperience">
126
  <society crossref="societies/society[@ref='OneSociety']/*"></society>
127
  <job>My first job</job>
128
 </experience>
129
 <societies>
130
  <society ref="OneSociety">
131
    <name>OneSociety</name>
132
    <address>An address</address>
133
    <siteurl>http://www.google.com</siteurl>
134
  </society>
135
 </societies>
136
</document>
137
XML;
138
        $string = <<<XML
139
<?xml version='1.0'?>
140
<document>
141
 <job crossref="experience[@id='OneExperience']/*"></job>
142
</document>
143
XML;
144
        $expected = array('job' => array(
145
            'society' => array(
146
                'name'      => "OneSociety",
147
                'address'   => "An address",
148
                'siteurl'   => "http://www.google.com"),
149
            'job'   => "My first job"
150
        ));
151
152
        $this->assertXml2Array($expected, $CV, $string);
153
154
        $string = <<<XML
155
<?xml version='1.0'?>
156
<document>
157
 <experience id="OneExperience">
158
  <society crossref="societies/society[@ref='OneSociety']/*"></society>
159
  <job>My first job</job>
160
 </experience>
161
</document>
162
XML;
163
        $expected = array('OneExperience' => array(
164
            'society' => array(
165
                'name'      => "OneSociety",
166
                'address'   => "An address",
167
                'siteurl'   => "http://www.google.com"),
168
            'job'   => "My first job"
169
        ));
170
171
        $this->assertXml2Array($expected, $CV, $string);
172
173
        $string = <<<XML
174
<?xml version='1.0'?>
175
<document>
176
  <society crossref="societies/society[@ref='OneSociety']/*"></society>
177
</document>
178
XML;
179
        $expected = array('society' => array(
180
            'name'      => "OneSociety",
181
            'address'   => "An address",
182
            'siteurl'   => "http://www.google.com")
183
        );
184
185
        $this->assertXml2Array($expected, $CV, $string);
186
    }
187
188
    public function testXml2arrayWithCrossRef() {
189
        $CV = <<<XML
190
<?xml version='1.0'?>
191
<document>
192
 <job crossref="experience[@id='OneExperience']/*"></job>
193
 <experience id="OneExperience">
194
  <society crossref="societies/society[@ref='OneSociety']/*"></society>
195
  <job>My first job</job>
196
 </experience>
197
 <societies>
198
  <society ref="OneSociety">
199
    <name>OneSociety</name>
200
    <address>An address</address>
201
    <siteurl>http://www.google.com</siteurl>
202
  </society>
203
 </societies>
204
</document>
205
XML;
206
        $string = <<<XML
207
<?xml version='1.0'?>
208
<document>
209
 <job crossref="experience[@id='OneExperience']/*"></job>
210
</document>
211
XML;
212
        $expected = array('job' => array(
213
            'society' => array(
214
                'name'      => "OneSociety",
215
                'address'   => "An address",
216
                'siteurl'   => "http://www.google.com"),
217
            'job'   => "My first job"
218
        ));
219
220
        $this->assertXml2Array($expected, $CV, $string);
221
    }
222
223
    public function testXml2arrayWithCVCrossRef() {
224
        $CV = <<<XML
225
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
226
<root>
227
    <langs>
228
        <lang id="en">English</lang>
229
        <lang id="fr">Français</lang>
230
    </langs>
231
    <curriculumVitae>
232
        <lookingFor>
233
            <experience crossref="curriculumVitae/experiences/items/experience[@id='SecondJob']/*"></experience>
234
            <presentation lang="en">A good presentation.</presentation>
235
            <presentation lang="fr">Une bonne présentation.</presentation>
236
        </lookingFor>
237
        <experiences anchor="experiences">
238
            <anchorTitle lang="en">Experiences</anchorTitle>
239
            <anchorTitle lang="fr">Expériences Professionnelles</anchorTitle>
240
            <items>
241
                <experience id="SecondJob">
242
                    <date lang="en">Apr 2011 - Present</date>
243
                    <date lang="fr">Avr. 2011 - Aujourd'hui</date>
244
                    <job lang="en">Second Job</job>
245
                    <job lang="fr">Deuxième Job</job>
246
                    <society crossref="societies/society[@ref='OneSociety']/*"></society>
247
                    <missions lang="en">
248
                        <item>A item.</item>
249
                    </missions>
250
                    <missions lang="fr">
251
                        <item>Un item.</item>
252
                    </missions>
253
                </experience>
254
                <experience id="FirstJob">
255
                    <date lang="en">Nov 2009 - Apr 2011</date>
256
                    <date lang="fr">Nov. 2009 - Avr. 2011</date>
257
                    <job lang="en">First Job</job>
258
                    <job lang="fr">Premier Job</job>
259
                    <society crossref="societies/society[@ref='OneSociety']/*"></society>
260
                    <missions lang="en">
261
                        <item>A item.</item>
262
                    </missions>
263
                    <missions lang="fr">
264
                        <item>Un item.</item>
265
                    </missions>
266
                </experience>
267
            </items>
268
        </experiences>
269
    </curriculumVitae>
270
    <societies>
271
        <society ref="OneSociety">
272
            <name>OneSociety</name>
273
            <address>address</address>
274
            <siteurl>http://www.google.com</siteurl>
275
        </society>
276
    </societies>
277
</root>
278
XML;
279
        $expected = array(
280
            'langs'  => array(
281
                'en' => "English",
282
                'fr' => "Français"),
283
            'curriculumVitae' => array(
284
                'lookingFor'  => array(
285
                    'experience'     => array(
286
                        'job'      => "Second Job",
287
                        'date'     => "Apr 2011 - Present",
288
                        'society'  => array(
289
                            'name'    => "OneSociety",
290
                            'address' => "address",
291
                            'siteurl' => "http://www.google.com"),
292
                        'missions' => array(
293
                            'item' => array("A item."))),
294
                    'presentation' => "A good presentation."),
295
                'experiences' => array(
296
                    'anchorTitle' => "Experiences",
297
                    'items' => array(
298
                        'SecondJob' => array(
299
                            'job'      => "Second Job",
300
                            'date'     => "Apr 2011 - Present",
301
                            'society'  => array(
302
                                'name'    => "OneSociety",
303
                                'address' => "address",
304
                                'siteurl' => "http://www.google.com"),
305
                            'missions' => array(
306
                                'item' => array("A item."))),
307
                        'FirstJob'  => array(
308
                            'job'      => "First Job",
309
                            'date'     => "Nov 2009 - Apr 2011",
310
                            'society'  => array(
311
                                'name'    => "OneSociety",
312
                                'address' => "address",
313
                                'siteurl' => "http://www.google.com"),
314
                            'missions' => array(
315
                                'item' => array("A item.")))),
316
                    'anchor' => "experiences")),
317
            'societies' => array(
318
                'society' => array(
319
                    'name'    => "OneSociety",
320
                    'address' => "address",
321
                    'siteurl' => "http://www.google.com"))
322
        );
323
324
        $this->assertXml2Array($expected, $CV, $CV);
325
    }
326
327
    private function assertXml2Array($expected, $CV, $XML) {
328
        $this->Xml2arrayFunctions = new Xml2arrayFunctions(simplexml_load_string($CV));
329
        $result = $this->Xml2arrayFunctions->xml2array(simplexml_load_string($XML));
330
331
        $this->assertEquals($expected, $result);
332
    }
333
}
334