1--TEST--
2XMLReader: libxml2 XML Reader, Move cursor to an attribute, with invalid arguments
3--CREDITS--
4Mark Baker mark@lange.demon.co.uk at the PHPNW2017 Conference for PHP Testfest 2017
5--SKIPIF--
6<?php if (!extension_loaded("xmlreader")) print "skip"; ?>
7--FILE--
8<?php
9// Set up test data in a new file
10$xmlstring = '<?xml version="1.0" encoding="UTF-8"?>
11<books><book num="1" idx="2">book1</book></books>';
12$filename = __DIR__ . '/003-move-errors.xml';
13file_put_contents($filename, $xmlstring);
14
15// Load test data into a new XML Reader
16$reader = new XMLReader();
17if (!$reader->open($filename)) {
18    exit('XML could not be read');
19}
20
21// Parse the data
22while ($reader->read()) {
23    if ($reader->nodeType != XMLREADER::END_ELEMENT) {
24        // Find the book node
25        if ($reader->nodeType == XMLREADER::ELEMENT && $reader->name == 'book') {
26            echo $reader->name . "\n";
27
28            $attr = $reader->moveToNextAttribute();
29            var_dump($attr);
30            echo $reader->name . ": ";
31            echo $reader->value . "\n";
32
33            // Test for call with an empty string argument
34            $attr = $reader->moveToAttribute('');
35            var_dump($attr);
36            // Ensure that node pointer has not changed position
37            echo $reader->name . ": ";
38            echo $reader->value . "\n";
39
40            // Test for call by name for an attribute that doesn't exist
41            $attr = $reader->moveToAttribute('isbn');
42            var_dump($attr);
43            // Ensure that node pointer has not changed position
44            echo $reader->name . ": ";
45            echo $reader->value . "\n";
46
47            // Test for call by number for an attribute that doesn't exist
48            $attr = $reader->moveToAttributeNo(911);
49            var_dump($attr);
50            // Oddly, node pointer moves back to the element in this case
51            echo $reader->name . "\n";
52        }
53    }
54}
55
56// clean up
57$reader->close();
58?>
59===DONE===
60--CLEAN--
61<?php
62unlink(__DIR__.'/003-move-errors.xml');
63?>
64--EXPECTF--
65book
66bool(true)
67num: 1
68
69Warning: XMLReader::moveToAttribute(): Attribute Name is required in %s on line %d
70bool(false)
71num: 1
72bool(false)
73num: 1
74bool(false)
75book
76===DONE===
77