1--TEST--
2Passing a too long string to ChildNode or ParentNode methods causes an exception
3--EXTENSIONS--
4dom
5--INI--
6memory_limit=-1
7--SKIPIF--
8<?php
9if (PHP_INT_SIZE !== 8) die('skip Only for 64-bit');
10if (getenv('SKIP_SLOW_TESTS')) die('skip slow test');
11// Copied from file_get_contents_file_put_contents_5gb.phpt
12function get_system_memory(): int|float|false
13{
14    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
15        // Windows-based memory check
16        @exec('wmic OS get FreePhysicalMemory', $output);
17        if (isset($output[1])) {
18            return ((int)trim($output[1])) * 1024;
19        }
20    } else {
21        // Unix/Linux-based memory check
22        $memInfo = @file_get_contents("/proc/meminfo");
23        if ($memInfo) {
24            preg_match('/MemFree:\s+(\d+) kB/', $memInfo, $matches);
25            return $matches[1] * 1024; // Convert to bytes
26        }
27    }
28    return false;
29}
30if (get_system_memory() < 4 * 1024 * 1024 * 1024) {
31    die('skip Reason: Insufficient RAM (less than 4GB)');
32}
33?>
34--FILE--
35<?php
36$dom = new DOMDocument;
37$element = $dom->appendChild($dom->createElement('root'));
38$str = str_repeat('X', 2**31 + 10);
39try {
40    $element->append('x', $str);
41} catch (ValueError $e) {
42    echo $e->getMessage(), "\n";
43}
44try {
45    $element->prepend('x', $str);
46} catch (ValueError $e) {
47    echo $e->getMessage(), "\n";
48}
49try {
50    $element->after('x', $str);
51} catch (ValueError $e) {
52    echo $e->getMessage(), "\n";
53}
54try {
55    $element->before('x', $str);
56} catch (ValueError $e) {
57    echo $e->getMessage(), "\n";
58}
59try {
60    $element->replaceWith('x', $str);
61} catch (ValueError $e) {
62    echo $e->getMessage(), "\n";
63}
64try {
65    $element->replaceChildren('x', $str);
66} catch (ValueError $e) {
67    echo $e->getMessage(), "\n";
68}
69var_dump($dom->childNodes->count());
70var_dump($element->childNodes->count());
71?>
72--EXPECT--
73DOMElement::append(): Argument #2 must be less than or equal to 2147483647 bytes long
74DOMElement::prepend(): Argument #2 must be less than or equal to 2147483647 bytes long
75DOMElement::after(): Argument #2 must be less than or equal to 2147483647 bytes long
76DOMElement::before(): Argument #2 must be less than or equal to 2147483647 bytes long
77DOMElement::replaceWith(): Argument #2 must be less than or equal to 2147483647 bytes long
78DOMElement::replaceChildren(): Argument #2 must be less than or equal to 2147483647 bytes long
79int(1)
80int(0)
81