1--TEST-- 2Tests for DOMProcessingInstruction class 3--EXTENSIONS-- 4dom 5--FILE-- 6<?php 7 8class FailingStringable { 9 public function __toString(): string { 10 throw new Exception("failed in __toString"); 11 } 12} 13 14echo "--- Test construction ---\n"; 15 16try { 17 $pi = new DOMProcessingInstruction("\0"); 18} catch (DOMException $e) { 19 echo $e->getMessage(), "\n"; 20} 21 22$pi = new DOMProcessingInstruction("test"); 23 24echo "--- Test fields ---\n"; 25 26var_dump($pi->target); 27var_dump($pi->data); 28$pi->data = "ok"; 29var_dump($pi->data); 30try { 31 $pi->data = new FailingStringable; 32} catch (Throwable $e) { 33 echo $e->getMessage(), "\n"; 34} 35var_dump($pi->data); 36$pi->data = 12345; 37var_dump($pi->data); 38$pi->data = "my data <>"; 39var_dump($pi->data); 40 41echo "--- Test appending ---\n"; 42 43$doc = new DOMDocument; 44$doc->appendChild($doc->createElement('root')); 45$doc->documentElement->appendChild($doc->adoptNode($pi)); 46echo $doc->saveXML(); 47 48echo "--- Test construction with __construct by reflection and fields ---\n"; 49 50$class = new ReflectionClass('DOMProcessingInstruction'); 51$instance = $class->newInstanceWithoutConstructor(); 52 53try { 54 var_dump($instance->target); 55} catch (Throwable $e) { 56 echo $e->getMessage(), "\n"; 57} 58try { 59 var_dump($instance->data); 60} catch (Throwable $e) { 61 echo $e->getMessage(), "\n"; 62} 63try { 64 $instance->data = "hello"; 65} catch (Throwable $e) { 66 echo $e->getMessage(), "\n"; 67} 68 69?> 70--EXPECT-- 71--- Test construction --- 72Invalid Character Error 73--- Test fields --- 74string(4) "test" 75string(0) "" 76string(2) "ok" 77failed in __toString 78string(2) "ok" 79string(5) "12345" 80string(10) "my data <>" 81--- Test appending --- 82<?xml version="1.0"?> 83<root><?test my data <>?></root> 84--- Test construction with __construct by reflection and fields --- 85Invalid State Error 86Invalid State Error 87Invalid State Error 88