014.phpt 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. --TEST--
  2. XMLReader: libxml2 XML Reader, read-only element values can not be modified
  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__ . '/_014.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 a node to try modifying
  23. if ($reader->nodeType == XMLREADER::ELEMENT && $reader->name == 'book') {
  24. // Try to set the value of the element from book1 to movie1
  25. try {
  26. $reader->value = 'movie1';
  27. } catch (Error $exception) {
  28. echo $exception->getMessage() . "\n";
  29. }
  30. // Try to set the value of the first "num" attribute from "1" to "num attribute 1"
  31. $attr = $reader->moveToFirstAttribute();
  32. try {
  33. $reader->value = 'num attribute 1';
  34. } catch (Error $exception) {
  35. echo $exception->getMessage() . "\n";
  36. }
  37. // Try to set the name of the first attribute from "num" to "number"
  38. try {
  39. $reader->name = 'number';
  40. } catch (Error $exception) {
  41. echo $exception->getMessage() . "\n";
  42. }
  43. }
  44. }
  45. }
  46. // clean up
  47. $reader->close();
  48. ?>
  49. --CLEAN--
  50. <?php
  51. unlink(__DIR__.'/_014.xml');
  52. ?>
  53. --EXPECT--
  54. Cannot write to read-only property
  55. Cannot write to read-only property
  56. Cannot write to read-only property