testArgumentOfUnknownType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Danmichaelo\QuiteSimpleXMLElement;
4
5
use PHPUnit\Framework\TestCase;
6
7
class QuiteSimpleXMLElementTest extends TestCase
8
{
9
    public function testExampleXmlWithNamespace()
10
    {
11
        $xml = '
12
	 	  <ns1:NCIPMessage xmlns:ns1="http://www.niso.org/2008/ncip">
13
	 	     <ns1:CheckOutItemResponse>
14
	 	        <ns1:ItemId>
15
	 	           <ns1:AgencyId>x</ns1:AgencyId>
16
	 	           <ns1:ItemIdentifierValue>zzzzzzz</ns1:ItemIdentifierValue>
17
	 	        </ns1:ItemId>
18
	 	        <ns1:UserId>
19
	 	           <ns1:AgencyId>x</ns1:AgencyId>
20
	 	           <ns1:UserIdentifierValue>xxxxxxxxxx</ns1:UserIdentifierValue>
21
	 	        </ns1:UserId>
22
	 	        <ns1:DateDue>2013-09-21T18:54:39.718+02:00</ns1:DateDue>
23
	 	        <ns1:ItemOptionalFields>
24
	 	           <ns1:BibliographicDescription>
25
	 	              <ns1:Author>DuCharme, Bob</ns1:Author>
26
	 	              <ns1:BibliographicRecordId>
27
	 	                 <ns1:BibliographicRecordIdentifier>11447981x</ns1:BibliographicRecordIdentifier>
28
	 	                 <ns1:BibliographicRecordIdentifierCode>Accession Number</ns1:BibliographicRecordIdentifierCode>
29
	 	              </ns1:BibliographicRecordId>
30
	 	              <ns1:Edition/>
31
	 	              <ns1:Pagination>XIII, 235 s., ill.</ns1:Pagination>
32
	 	              <ns1:PublicationDate>2011</ns1:PublicationDate>
33
	 	              <ns1:Publisher>O\'Reilly</ns1:Publisher>
34
	 	              <ns1:Title>Learning SPARQL : querying and updating with SPARQL 1.1</ns1:Title>
35
	 	              <ns1:Language>eng</ns1:Language>
36
	 	              <ns1:MediumType>Book</ns1:MediumType>
37
	 	           </ns1:BibliographicDescription>
38
	 	        </ns1:ItemOptionalFields>
39
	 	        <ns1:Ext>
40
	 	           <ns1:UserOptionalFields>
41
	 	              <ns1:UserLanguage>eng</ns1:UserLanguage>
42
	 	           </ns1:UserOptionalFields>
43
	 	        </ns1:Ext>
44
	 	     </ns1:CheckOutItemResponse>
45
	 	  </ns1:NCIPMessage>';
46
        $dom = new QuiteSimpleXMLElement($xml);
47
        $ns = ['n' => 'http://www.niso.org/2008/ncip'];
48
        $dom->registerXPathNamespaces($ns);
49
50
        $this->assertInstanceOf('Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement', $dom);
51
        $this->assertEquals('2013-09-21T18:54:39.718+02:00', $dom->text('/n:NCIPMessage/n:CheckOutItemResponse/n:DateDue'));
52
        $this->assertEquals('2013-09-21T18:54:39.718+02:00', $dom->first('/n:NCIPMessage/n:CheckOutItemResponse')->text('n:DateDue'));
53
        $this->assertEquals('2013-09-21T18:54:39.718+02:00', $dom->text('//n:DateDue'));
54
55
        // xpath should return a QuiteSimpleXMLElement element
56
        $this->assertInstanceOf('Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement', $dom->first('/n:NCIPMessage'));
57
58
        // and we should get the SimpleXMLElement from el()
59
        $this->assertInstanceOf('SimpleXMLElement', $dom->first('/n:NCIPMessage')->el());
60
    }
61
62
    public function testExampleXmlWithDefaultNamespacePrefix()
63
    {
64
        $xml = '
65
		  <ns1:NCIPMessage xmlns:ns1="http://www.niso.org/2008/ncip">
66
		     <ns1:CheckOutItemResponse>
67
		        <ns1:DateDue>2013-09-21T18:54:39.718+02:00</ns1:DateDue>
68
		     </ns1:CheckOutItemResponse>
69
		  </ns1:NCIPMessage>';
70
        $dom = new QuiteSimpleXMLElement($xml);
71
72
        $this->assertInstanceOf('Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement', $dom);
73
        $this->assertEquals('2013-09-21T18:54:39.718+02:00', $dom->text('/ns1:NCIPMessage/ns1:CheckOutItemResponse/ns1:DateDue'));
74
    }
75
76
    public function testAsXML()
77
    {
78
        $xml = '
79
		  <ns1:NCIPMessage xmlns:ns1="http://www.niso.org/2008/ncip">
80
		     <ns1:CheckOutItemResponse>
81
		        <ns1:DateDue>2013-09-21T18:54:39.718+02:00</ns1:DateDue>
82
		     </ns1:CheckOutItemResponse>
83
		  </ns1:NCIPMessage>';
84
        $dom = new QuiteSimpleXMLElement($xml);
85
86
        $this->assertXmlStringEqualsXmlString($xml, $dom->asXML());
87
    }
88
89
    public function testGetName()
90
    {
91
        $xml = '
92
		  <ns1:NCIPMessage xmlns:ns1="http://www.niso.org/2008/ncip">
93
		     <ns1:CheckOutItemResponse>
94
		        <ns1:DateDue>2013-09-21T18:54:39.718+02:00</ns1:DateDue>
95
		     </ns1:CheckOutItemResponse>
96
		  </ns1:NCIPMessage>';
97
        $root = new QuiteSimpleXMLElement($xml);
98
        $node = $root->first('/ns1:NCIPMessage/ns1:CheckOutItemResponse');
99
100
        $this->assertEquals('NCIPMessage', $root->getName());
101
        $this->assertEquals('CheckOutItemResponse', $node->getName());
102
    }
103
104
    public function testAttr()
105
    {
106
        $xml = '
107
			<sear:SEGMENTS xmlns:sear="http://www.exlibrisgroup.com/xsd/jaguar/search">
108
				<sear:FACETLIST ACCURATE_COUNTERS="true">
109
					<sear:FACET NAME="creator" COUNT="200">
110
						Test
111
					</sear:FACET>
112
				</sear:FACETLIST>
113
			</sear:SEGMENTS>';
114
        $root = new QuiteSimpleXMLElement($xml);
115
        $node = $root->first('/sear:SEGMENTS/sear:FACETLIST/sear:FACET');
116
117
        $this->assertEquals('creator', $node->attr('NAME'));
118
        $this->assertEquals('200', $node->attr('COUNT'));
119
        $this->assertEquals('', $node->attr('SOMEETHING_ELSE'));
120
    }
121
122
    public function testNamespacedAttr()
123
    {
124
        $xml = '
125
            <sear:SEGMENTS xmlns:sear="http://www.exlibrisgroup.com/xsd/jaguar/search">
126
                <sear:FACETLIST ACCURATE_COUNTERS="true">
127
                    <sear:FACET sear:NAME="creator" COUNT="200">
128
                        Test
129
                    </sear:FACET>
130
                </sear:FACETLIST>
131
            </sear:SEGMENTS>';
132
        $root = new QuiteSimpleXMLElement($xml);
133
        $node = $root->first('/sear:SEGMENTS/sear:FACETLIST/sear:FACET');
134
135
        $this->assertEquals('creator', $node->attr('sear:NAME'));
136
        $this->assertEquals('', $node->attr('sear:SOMEETHING_ELSE'));
137
    }
138
139
    public function testText()
140
    {
141
        $xml = '
142
			<sear:SEGMENTS xmlns:sear="http://www.exlibrisgroup.com/xsd/jaguar/search">
143
				<sear:FACETLIST ACCURATE_COUNTERS="true">
144
					<sear:FACET NAME="creator" COUNT="200">
145
						Test
146
					</sear:FACET>
147
				</sear:FACETLIST>
148
			</sear:SEGMENTS>';
149
        $root = new QuiteSimpleXMLElement($xml);
150
        $node = $root->first('/sear:SEGMENTS/sear:FACETLIST/sear:FACET');
151
152
        $this->assertEquals('Test', $node->text());
153
        $this->assertEquals('Test', $root->text('/sear:SEGMENTS/sear:FACETLIST/sear:FACET'));
154
    }
155
156
    public function testUntrimmedText()
157
    {
158
        $xml = '
159
            <sear:SEGMENTS xmlns:sear="http://www.exlibrisgroup.com/xsd/jaguar/search">
160
                <sear:FACETLIST ACCURATE_COUNTERS="true">
161
                    <sear:FACET NAME="creator" COUNT="200">
162
                        Test
163
                    </sear:FACET>
164
                </sear:FACETLIST>
165
            </sear:SEGMENTS>';
166
        $root = new QuiteSimpleXMLElement($xml);
167
        $node = $root->first('/sear:SEGMENTS/sear:FACETLIST/sear:FACET');
168
169
        $this->assertEquals('
170
                        Test
171
                    ', $node->text('.', false));
172
    }
173
174
    public function testTextOfNonExistingNode()
175
    {
176
        $xml = '
177
            <sear:SEGMENTS xmlns:sear="http://www.exlibrisgroup.com/xsd/jaguar/search">
178
                <sear:FACETLIST ACCURATE_COUNTERS="true">
179
                    <sear:FACET NAME="creator" COUNT="200">
180
                        Test
181
                    </sear:FACET>
182
                </sear:FACETLIST>
183
            </sear:SEGMENTS>';
184
        $root = new QuiteSimpleXMLElement($xml);
185
186
        $this->assertEquals('', $root->text('/sear:NONEXISTING/sear:NONEXISTING'));
187
    }
188
189
    public function testSetValue()
190
    {
191
        $xml = '
192
            <sear:SEGMENTS xmlns:sear="http://www.exlibrisgroup.com/xsd/jaguar/search">
193
                <sear:FACETLIST ACCURATE_COUNTERS="true">
194
                    <sear:FACET NAME="creator" COUNT="200">
195
                        Test
196
                    </sear:FACET>
197
                </sear:FACETLIST>
198
            </sear:SEGMENTS>';
199
        $root = new QuiteSimpleXMLElement($xml);
200
        $node = $root->first('/sear:SEGMENTS/sear:FACETLIST/sear:FACET');
201
        $node->setValue('Hello');
202
203
        $this->assertEquals('Hello', $root->text('/sear:SEGMENTS/sear:FACETLIST/sear:FACET'));
204
        $this->assertEquals('creator', $root->first('/sear:SEGMENTS/sear:FACETLIST/sear:FACET')->attr('NAME'));
205
    }
206
207
    public function testHas()
208
    {
209
        $xml = '
210
			<sear:SEGMENTS xmlns:sear="http://www.exlibrisgroup.com/xsd/jaguar/search">
211
				<sear:FACETLIST ACCURATE_COUNTERS="true">
212
					<sear:FACET NAME="creator" COUNT="200">
213
						Test
214
					</sear:FACET>
215
				</sear:FACETLIST>
216
			</sear:SEGMENTS>';
217
        $root = new QuiteSimpleXMLElement($xml);
218
        $node1 = $root->first('/sear:SEGMENTS/sear:FACETLIST');
219
        $node2 = $root->first('/sear:SEGMENTS');
220
221
        $this->assertTrue($node1->has('sear:FACET'));
222
        $this->assertFalse($node1->has('sear:OCTET'));
223
        $this->assertTrue($node2->has('sear:FACETLIST'));
224
        $this->assertTrue($node2->has('sear:FACETLIST/sear:FACET'));
225
        $this->assertFalse($node2->has('sear:NA/sear:NA'));
226
    }
227
228
    public function testGetNamespaces()
229
    {
230
        $xml = '
231
		  <ns1:NCIPMessage xmlns:ns1="http://www.niso.org/2008/ncip">
232
		     <ns1:CheckOutItemResponse>
233
		        <ns1:DateDue>2013-09-21T18:54:39.718+02:00</ns1:DateDue>
234
		     </ns1:CheckOutItemResponse>
235
		  </ns1:NCIPMessage>';
236
        $root = new QuiteSimpleXMLElement($xml);
237
238
        $this->assertEquals(['ns1' => 'http://www.niso.org/2008/ncip'], $root->getNamespaces());
239
    }
240
241
    public function testChildCount()
242
    {
243
        $xml = '
244
		  <ns1:NCIPMessage xmlns:ns1="http://www.niso.org/2008/ncip">
245
		     <ns1:CheckOutItemResponse>
246
		        <ns1:DateDue>2013-09-21T18:54:39.718+02:00</ns1:DateDue>
247
		     </ns1:CheckOutItemResponse>
248
		  </ns1:NCIPMessage>';
249
        $dom = new QuiteSimpleXMLElement($xml);
250
        $node1 = $dom->first('/ns1:NCIPMessage');
251
        $node2 = $dom->first('/ns1:NCIPMessage/ns1:CheckOutItemResponse/ns1:DateDue');
252
        $this->assertEquals(0, $node1->count());
253
        $this->assertEquals(1, $node1->count('ns1'));
254
        $this->assertEquals(0, $node2->count('ns1'));
255
    }
256
257
    public function testChildCountWithRegisteredNamespaces()
258
    {
259
        $xml = '
260
	 	  <ns1:NCIPMessage xmlns:ns1="http://www.niso.org/2008/ncip">
261
	 	     <ns1:CheckOutItemResponse>
262
	 	        <ns1:DateDue>2013-09-21T18:54:39.718+02:00</ns1:DateDue>
263
	 	     </ns1:CheckOutItemResponse>
264
	 	  </ns1:NCIPMessage>';
265
        $dom = new QuiteSimpleXMLElement($xml);
266
        $dom->registerXPathNamespaces([
267
            'n' => 'http://www.niso.org/2008/ncip'
268
        ]);
269
270
        $node1 = $dom->first('/n:NCIPMessage');
271
        $node2 = $dom->first('/n:NCIPMessage/n:CheckOutItemResponse/n:DateDue');
272
        $this->assertEquals(1, $node1->count('n'));
273
        $this->assertEquals(0, $node2->count('n'));
274
    }
275
276
    public function testChildren()
277
    {
278
        $xml = '
279
		  <ns1:NCIPMessage xmlns:ns1="http://www.niso.org/2008/ncip">
280
		     <ns1:CheckOutItemResponse>
281
		        <ns1:DateDue>2013-09-21T18:54:39.718+02:00</ns1:DateDue>
282
		     </ns1:CheckOutItemResponse>
283
		  </ns1:NCIPMessage>';
284
        $root = new QuiteSimpleXMLElement($xml);
285
        $node1 = $root->first('/ns1:NCIPMessage');
286
        $kids = $node1->children('ns1');
287
288
        $this->assertCount(1, $kids);
289
        $kiddo = $kids[0];
290
        $this->assertInstanceOf('Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement', $kiddo);
291
        $this->assertEquals('CheckOutItemResponse', $kiddo->getName());
292
        $this->assertEquals('2013-09-21T18:54:39.718+02:00', $kiddo->text('ns1:DateDue'));
293
    }
294
295
    public function testAll()
296
    {
297
        $xml = '
298
          <ns1:NCIPMessage xmlns:ns1="http://www.niso.org/2008/ncip">
299
             <ns1:Test>
300
                Hello
301
             </ns1:Test>
302
          </ns1:NCIPMessage>';
303
        $dom = new QuiteSimpleXMLElement($xml);
304
305
        $this->assertCount(0, $dom->all('/Test[prop="val"]'));
306
        $this->assertCount(1, $dom->all('/ns1:NCIPMessage/ns1:Test'));
307
    }
308
309
    /**
310
     * @expectedException \Danmichaelo\QuiteSimpleXMLElement\InvalidXMLException
311
     */
312
    public function testParseErroneousXMLShouldThrowException()
313
    {
314
        $xml = '<ns1:NCI';
315
        new QuiteSimpleXMLElement($xml);
316
    }
317
318
    /**
319
     * @expectedException \Danmichaelo\QuiteSimpleXMLElement\InvalidXMLException
320
     */
321
    public function testParseEmptyXMLShouldThrowException()
322
    {
323
        $xml = '';
324
        new QuiteSimpleXMLElement($xml);
325
    }
326
327
    /**
328
     * @expectedException \InvalidArgumentException
329
     */
330
    public function testArgumentIsNull()
331
    {
332
        new QuiteSimpleXMLElement(null);
333
    }
334
335
    /**
336
     * @expectedException \InvalidArgumentException
337
     */
338
    public function testArgumentOfUnknownType()
339
    {
340
        new QuiteSimpleXMLElement(2.0);
341
    }
342
343
    /**
344
     * @expectedException \InvalidArgumentException
345
     */
346
    public function testArgumentOfUnknownClass()
347
    {
348
        new QuiteSimpleXMLElement(/** @scrutinizer ignore-type */ new \DateTime());
349
    }
350
351
    public function testReplace()
352
    {
353
        $xml = '
354
          <ns1:NCIPMessage xmlns:ns1="http://www.niso.org/2008/ncip">
355
             <ns1:Test>
356
                Hello
357
             </ns1:Test>
358
             <ns1:Test>
359
                Hi
360
             </ns1:Test>
361
          </ns1:NCIPMessage>';
362
        $expectedResult = '
363
          <ns1:NCIPMessage xmlns:ns1="http://www.niso.org/2008/ncip">
364
             <ns1:Test2>Replaced</ns1:Test2>
365
             <ns1:Test>
366
                Hi
367
             </ns1:Test>
368
          </ns1:NCIPMessage>';
369
        $el = new QuiteSimpleXMLElement($xml);
370
        $node = $el->first('ns1:Test');
371
372
        $new = new QuiteSimpleXMLElement('<ns1:Test2 xmlns:ns1="http://www.niso.org/2008/ncip">Replaced</ns1:Test2>');
373
        $node->replace($new);
374
375
        $this->assertXmlStringEqualsXmlString($expectedResult, $el->asXML());
376
    }
377
378
    public function testMake()
379
    {
380
        $el = QuiteSimpleXMLElement::make('<doc xmlns="http://www.loc.gov/zing/srw/"><hello>world</hello></doc>', [
381
            's' => 'http://www.loc.gov/zing/srw/',
382
        ]);
383
384
        $this->assertEquals('world', $el->text('s:hello'));
385
    }
386
387
    public function testToString()
388
    {
389
        $el = QuiteSimpleXMLElement::make('<doc xmlns="http://www.loc.gov/zing/srw/"><hello>world</hello></doc>', [
390
            's' => 'http://www.loc.gov/zing/srw/',
391
        ]);
392
393
        $this->assertEquals('world', strval($el->first('s:hello')));
394
    }
395
396
    public function testAttributes()
397
    {
398
        $el = QuiteSimpleXMLElement::make('<doc xmlns="http://www.loc.gov/zing/srw/"><hello here="there">world</hello></doc>', [
399
            's' => 'http://www.loc.gov/zing/srw/',
400
        ]);
401
402
        $this->assertEquals(['here' => 'there'], iterator_to_array($el->first('s:hello')->attributes()));
403
    }
404
405
    public function testFirstReturnsNullForNonexistingNode()
406
    {
407
        $el = QuiteSimpleXMLElement::make('<doc xmlns="http://www.loc.gov/zing/srw/"><hello>first</hello><hello>second</hello></doc>', [
408
            's' => 'http://www.loc.gov/zing/srw/',
409
        ]);
410
411
        $this->assertSame(null, $el->first('world'));
412
    }
413
414
    public function testXpathShouldReturnInstancesOfQuiteSimpleXMLElement()
415
    {
416
        $el = QuiteSimpleXMLElement::make('<doc xmlns="http://www.loc.gov/zing/srw/"><hello>first</hello><hello>second</hello></doc>', [
417
            's' => 'http://www.loc.gov/zing/srw/',
418
        ]);
419
420
        $this->assertContainsOnlyInstancesOf('Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement', $el->xpath('hello'));
421
    }
422
}
423