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