1--TEST-- 2XMLReader: libxml2 XML Reader, Get 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-get-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->getAttribute(''); 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->getAttribute('isbn'); 46 var_dump($attr); // NULL expected 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->getAttributeNo(911); 53 var_dump($attr); // NULL expected 54 // Ensure that node pointer has not changed position 55 echo $reader->name . ": "; 56 echo $reader->value . "\n"; 57 } 58 } 59} 60 61// clean up 62$reader->close(); 63?> 64--CLEAN-- 65<?php 66unlink(__DIR__.'/003-get-errors.xml'); 67?> 68--EXPECT-- 69book 70bool(true) 71num: 1 72XMLReader::getAttribute(): Argument #1 ($name) must not be empty 73num: 1 74NULL 75num: 1 76NULL 77num: 1 78