1--TEST--
2ParentNode/ChildNode pre-insertion validation
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7$dom = Dom\XMLDocument::createFromString("<!DOCTYPE root><root/>");
8$doctype = $dom->doctype;
9$dom->removeChild($doctype);
10
11echo "--- Trying to insert text node into the document ---\n";
12
13try {
14    $dom->append("foo", "bar", "baz");
15} catch (DOMException $e) {
16    echo "Exception: " . $e->getMessage() . "\n";
17}
18try {
19    $dom->append($dom->createTextNode("text node"));
20} catch (DOMException $e) {
21    echo "Exception: " . $e->getMessage() . "\n";
22}
23try {
24    $dom->append($dom->createCDATASection("text node"));
25} catch (DOMException $e) {
26    echo "Exception: " . $e->getMessage() . "\n";
27}
28
29echo "--- Trying to insert doctype into not a document ---\n";
30
31$element = $dom->createElement("foo");
32try {
33    $element->append($doctype);
34} catch (DOMException $e) {
35    echo "Exception: " . $e->getMessage() . "\n";
36}
37
38echo "--- Trying to insert doctype at the wrong place in a document ---\n";
39
40try {
41    $dom->append($doctype);
42} catch (DOMException $e) {
43    echo "Exception: " . $e->getMessage() . "\n";
44}
45
46echo "--- Prepend doctype in a document should work ---\n";
47
48$dom->prepend($doctype);
49
50echo "--- Trying to create multiple document roots ---\n";
51
52try {
53    $dom->append($dom->createElement("foo"));
54} catch (DOMException $e) {
55    echo "Exception: " . $e->getMessage() . "\n";
56}
57
58echo "--- Trying to insert an element before a document type ---\n";
59
60$dom->documentElement->remove();
61try {
62    $dom->prepend($element);
63} catch (DOMException $e) {
64    echo "Exception: " . $e->getMessage() . "\n";
65}
66
67echo "--- Document output ---\n";
68
69echo $dom->saveXml(), "\n";
70
71echo "--- Document fragment edge cases with multiple elements ---\n";
72
73$dom = Dom\XMLDocument::createEmpty();
74$fragment = $dom->createDocumentFragment();
75$fragment->append($dom->createElement("foo"));
76$fragment->append($dom->createElement("bar"));
77try {
78    $dom->append($fragment);
79} catch (DOMException $e) {
80    echo "Exception: " . $e->getMessage() . "\n";
81}
82
83echo "--- Document fragment edge cases with text ---\n";
84
85$dom = Dom\XMLDocument::createEmpty();
86$fragment = $dom->createDocumentFragment();
87$fragment->append("foo");
88$fragment->append($dom->createCDATASection("bar"));
89try {
90    $dom->append($fragment);
91} catch (DOMException $e) {
92    echo "Exception: " . $e->getMessage() . "\n";
93}
94
95?>
96--EXPECT--
97--- Trying to insert text node into the document ---
98Exception: Cannot insert text as a child of a document
99Exception: Cannot insert text as a child of a document
100Exception: Cannot insert text as a child of a document
101--- Trying to insert doctype into not a document ---
102Exception: Cannot insert a document type into anything other than a document
103--- Trying to insert doctype at the wrong place in a document ---
104Exception: Document types must be the first child in a document
105--- Prepend doctype in a document should work ---
106--- Trying to create multiple document roots ---
107Exception: Cannot have more than one element child in a document
108--- Trying to insert an element before a document type ---
109Exception: Document types must be the first child in a document
110--- Document output ---
111<?xml version="1.0" encoding="UTF-8"?>
112<!DOCTYPE root>
113
114--- Document fragment edge cases with multiple elements ---
115Exception: Cannot have more than one element child in a document
116--- Document fragment edge cases with text ---
117Exception: Cannot insert text as a child of a document
118