xref: /PHP-7.4/ext/libxml/tests/002.phpt (revision 38e47dd0)
1--TEST--
2libxml_get_errors()
3--SKIPIF--
4<?php if (!extension_loaded('simplexml')) die('skip'); ?>
5--FILE--
6<?php
7
8var_dump(libxml_use_internal_errors(true));
9
10$xmlstr = <<< XML
11<?xml version='1.0' standalone='yes'?>
12    <movies>
13        <movie>
14            <titles>PHP: Behind the Parser</title>
15        </movie>
16    </movies>
17XML;
18
19$doc = simplexml_load_string($xmlstr);
20$xml = explode("\n", $xmlstr);
21
22if (!$doc) {
23	$errors = libxml_get_errors();
24
25	foreach ($errors as $error) {
26		echo display_xml_error($error, $xml);
27	}
28
29	var_dump(libxml_get_last_error());
30}
31
32
33function display_xml_error($error, $xml)
34{
35	$return  = $xml[$error->line - 1] . "\n";
36	$return .= str_repeat('-', $error->column) . "^\n";
37
38	switch ($error->level) {
39		case LIBXML_ERR_WARNING:
40			$return .= "Warning $error->code: ";
41			break;
42		case LIBXML_ERR_ERROR:
43			$return .= "Error $error->code: ";
44			break;
45		case LIBXML_ERR_FATAL:
46			$return .= "Fatal Error $error->code: ";
47			break;
48	}
49
50	$return .= trim($error->message) . "\n  Line: $error->line" . "\n  Column: $error->column";
51
52	if ($error->file) {
53		$return .= "\n  File: $error->file";
54	}
55
56	return "$return\n\n--------------------------------------------\n\n";
57}
58
59
60echo "Done\n";
61?>
62--EXPECTF--
63bool(false)
64            <titles>PHP: Behind the Parser</title>
65%s
66Fatal Error 76: Opening and ending tag mismatch: titles line 4 and title
67  Line: 4
68  Column: %d
69
70--------------------------------------------
71
72object(LibXMLError)#%d (6) {
73  ["level"]=>
74  int(3)
75  ["code"]=>
76  int(76)
77  ["column"]=>
78  int(%d)
79  ["message"]=>
80  string(57) "Opening and ending tag mismatch: titles line 4 and title
81"
82  ["file"]=>
83  string(0) ""
84  ["line"]=>
85  int(4)
86}
87Done
88