xref: /PHP-7.4/ext/dom/tests/dom003.phpt (revision 26dfce7f)
1--TEST--
2Test 3: Exception Test
3--SKIPIF--
4<?php require_once('skipif.inc'); ?>
5--FILE--
6<?php
7
8$dom = new domdocument;
9$dom->load(__DIR__."/book.xml");
10$rootNode = $dom->documentElement;
11print "--- Catch exception with try/catch\n";
12try {
13    $rootNode->appendChild($rootNode);
14} catch (domexception $e) {
15	ob_start();
16    var_dump($e);
17	$contents = ob_get_contents();
18	ob_end_clean();
19	echo preg_replace('/object\(DOMElement\).+\{.*?\}/s', 'DOMElement', $contents);
20}
21print "--- Don't catch exception with try/catch\n";
22$rootNode->appendChild($rootNode);
23
24
25?>
26--EXPECTF--
27--- Catch exception with try/catch
28object(DOMException)#%d (%d) {
29  ["message":protected]=>
30  string(23) "Hierarchy Request Error"
31  ["string":"Exception":private]=>
32  string(0) ""
33  ["file":protected]=>
34  string(%d) "%sdom003.php"
35  ["line":protected]=>
36  int(8)
37  ["trace":"Exception":private]=>
38  array(1) {
39    [0]=>
40    array(6) {
41      ["file"]=>
42      string(%d) "%sdom003.php"
43      ["line"]=>
44      int(8)
45      ["function"]=>
46      string(11) "appendChild"
47      ["class"]=>
48      string(7) "DOMNode"
49      ["type"]=>
50      string(2) "->"
51      ["args"]=>
52      array(1) {
53        [0]=>
54        DOMElement
55      }
56    }
57  }
58  ["previous":"Exception":private]=>
59  NULL
60  ["code"]=>
61  int(3)
62}
63--- Don't catch exception with try/catch
64
65Fatal error: Uncaught DOMException: Hierarchy Request Error in %sdom003.php:%d
66Stack trace:
67#0 %sdom003.php(%d): DOMNode->appendChild(Object(DOMElement))
68#1 {main}
69  thrown in %sdom003.php on line %d
70