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--EXTENSIONS-- 6xmlreader 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 try { 35 $reader->moveToAttribute(''); 36 } catch (ValueError $exception) { 37 echo $exception->getMessage() . "\n"; 38 } 39 40 // Ensure that node pointer has not changed position 41 echo $reader->name . ": "; 42 echo $reader->value . "\n"; 43 44 // Test for call by name for an attribute that doesn't exist 45 $attr = $reader->moveToAttribute('isbn'); 46 var_dump($attr); 47 // Ensure that node pointer has not changed position 48 echo $reader->name . ": "; 49 echo $reader->value . "\n"; 50 51 // Test for call by number for an attribute that doesn't exist 52 $attr = $reader->moveToAttributeNo(911); 53 var_dump($attr); 54 // Oddly, node pointer moves back to the element in this case 55 echo $reader->name . "\n"; 56 } 57 } 58} 59 60// clean up 61$reader->close(); 62?> 63--CLEAN-- 64<?php 65unlink(__DIR__.'/003-move-errors.xml'); 66?> 67--EXPECT-- 68book 69bool(true) 70num: 1 71XMLReader::moveToAttribute(): Argument #1 ($name) cannot be empty 72num: 1 73bool(false) 74num: 1 75bool(false) 76book 77