1--TEST--
2Test DOM\Document::$body setter errors
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8function testNormalReplace($cb)
9{
10    $dom = DOM\HTMLDocument::createFromString('<p>foo</p>', LIBXML_NOERROR);
11    var_dump($dom->body?->nodeName);
12    try {
13        $dom->body = $cb($dom);
14    } catch (Throwable $e) {
15        echo $e->getMessage(), "\n";
16    }
17    var_dump($dom->body?->nodeName);
18}
19
20echo "--- Set body to NULL ---\n";
21testNormalReplace(fn ($dom) => NULL);
22
23echo "--- Wrong element tag in right namespace ---\n";
24testNormalReplace(fn ($dom) => $dom->createElementNS("http://www.w3.org/1999/xhtml", "foo"));
25
26echo "--- Right element tag in wrong namespace ---\n";
27testNormalReplace(fn ($dom) => $dom->createElementNS("urn:a", "body"));
28
29echo "--- Right element tag in no namespace ---\n";
30testNormalReplace(fn ($dom) => $dom->createElementNS("", "frameset"));
31
32echo "--- Set body without document element ---\n";
33$dom = DOM\XMLDocument::createEmpty();
34try {
35    $dom->body = $dom->createElementNS("http://www.w3.org/1999/xhtml", "body");
36} catch (DOMException $e) {
37    echo $e->getMessage(), "\n";
38}
39var_dump($dom->body?->nodeName);
40
41?>
42--EXPECT--
43--- Set body to NULL ---
44string(4) "BODY"
45The new body must either be a body or a frameset tag
46string(4) "BODY"
47--- Wrong element tag in right namespace ---
48string(4) "BODY"
49The new body must either be a body or a frameset tag
50string(4) "BODY"
51--- Right element tag in wrong namespace ---
52string(4) "BODY"
53Cannot assign Dom\Element to property Dom\Document::$body of type ?Dom\HTMLElement
54string(4) "BODY"
55--- Right element tag in no namespace ---
56string(4) "BODY"
57Cannot assign Dom\Element to property Dom\Document::$body of type ?Dom\HTMLElement
58string(4) "BODY"
59--- Set body without document element ---
60A body can only be set if there is a document element
61NULL
62