1--TEST--
2compareDocumentPosition: entity ordering
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8$dom = new DOMDocument();
9$dom->loadXML(<<<XML
10<?xml version="1.0"?>
11<!DOCTYPE root [
12<!ENTITY e1 "e1">
13<!ENTITY e2 "e2">
14]>
15<container>
16    <child>&e1;</child>
17</container>
18XML, LIBXML_NOENT);
19
20$entities = iterator_to_array($dom->doctype->entities);
21usort($entities, fn ($a, $b) => strcmp($a->nodeName, $b->nodeName));
22
23echo "--- Compare entities ---\n";
24
25var_dump($entities[0]->compareDocumentPosition($entities[1]) === DOMNode::DOCUMENT_POSITION_FOLLOWING);
26var_dump($entities[1]->compareDocumentPosition($entities[0]) === DOMNode::DOCUMENT_POSITION_PRECEDING);
27
28$xpath = new DOMXPath($dom);
29$child = $xpath->query('//child')->item(0);
30
31echo "--- Compare entities against first child ---\n";
32
33var_dump($entities[0]->compareDocumentPosition($child->firstChild) === DOMNode::DOCUMENT_POSITION_FOLLOWING);
34var_dump($entities[1]->compareDocumentPosition($child->firstChild) === DOMNode::DOCUMENT_POSITION_FOLLOWING);
35
36echo "--- Compare first child against entities ---\n";
37
38var_dump($child->firstChild->compareDocumentPosition($entities[0]) === DOMNode::DOCUMENT_POSITION_PRECEDING);
39var_dump($child->firstChild->compareDocumentPosition($entities[1]) === DOMNode::DOCUMENT_POSITION_PRECEDING);
40
41?>
42--EXPECT--
43--- Compare entities ---
44bool(true)
45bool(true)
46--- Compare entities against first child ---
47bool(true)
48bool(true)
49--- Compare first child against entities ---
50bool(true)
51bool(true)
52