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