003-move-errors.phpt 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. --TEST--
  2. XMLReader: libxml2 XML Reader, Move cursor to an attribute, with invalid arguments
  3. --CREDITS--
  4. Mark Baker mark@lange.demon.co.uk at the PHPNW2017 Conference for PHP Testfest 2017
  5. --EXTENSIONS--
  6. xmlreader
  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';
  13. file_put_contents($filename, $xmlstring);
  14. // Load test data into a new XML Reader
  15. $reader = new XMLReader();
  16. if (!$reader->open($filename)) {
  17. exit('XML could not be read');
  18. }
  19. // Parse the data
  20. while ($reader->read()) {
  21. if ($reader->nodeType != XMLREADER::END_ELEMENT) {
  22. // Find the book node
  23. if ($reader->nodeType == XMLREADER::ELEMENT && $reader->name == 'book') {
  24. echo $reader->name . "\n";
  25. $attr = $reader->moveToNextAttribute();
  26. var_dump($attr);
  27. echo $reader->name . ": ";
  28. echo $reader->value . "\n";
  29. // Test for call with an empty string argument
  30. try {
  31. $reader->moveToAttribute('');
  32. } catch (ValueError $exception) {
  33. echo $exception->getMessage() . "\n";
  34. }
  35. // Ensure that node pointer has not changed position
  36. echo $reader->name . ": ";
  37. echo $reader->value . "\n";
  38. // Test for call by name for an attribute that doesn't exist
  39. $attr = $reader->moveToAttribute('isbn');
  40. var_dump($attr);
  41. // Ensure that node pointer has not changed position
  42. echo $reader->name . ": ";
  43. echo $reader->value . "\n";
  44. // Test for call by number for an attribute that doesn't exist
  45. $attr = $reader->moveToAttributeNo(911);
  46. var_dump($attr);
  47. // Oddly, node pointer moves back to the element in this case
  48. echo $reader->name . "\n";
  49. }
  50. }
  51. }
  52. // clean up
  53. $reader->close();
  54. ?>
  55. --CLEAN--
  56. <?php
  57. unlink(__DIR__.'/003-move-errors.xml');
  58. ?>
  59. --EXPECT--
  60. book
  61. bool(true)
  62. num: 1
  63. XMLReader::moveToAttribute(): Argument #1 ($name) cannot be empty
  64. num: 1
  65. bool(false)
  66. num: 1
  67. bool(false)
  68. book