xref: /PHP-8.2/ext/dom/tests/bug80332_1.phpt (revision 9f7d8880)
1--TEST--
2Bug #80332 (Completely broken array access functionality with DOMNamedNodeMap) - DOMNamedNodeMap variation
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8$doc = new DOMDocument;
9
10$doc->loadHTML('<span attr1="value1" attr2="value2"></span>');
11
12$x = new DOMXPath($doc);
13$span = $x->query('//span')[0];
14
15print "Node name: {$span->nodeName}\n";
16
17function test($span, $key) {
18    $key_formatted = match ($key) {
19        false => 'false',
20        true => 'true',
21        null => 'null',
22        default => is_string($key) ? "'$key'" : $key,
23    };
24    echo "Attribute [{$key_formatted}] name: ", $span->attributes[$key]->nodeName ?? '/', "\n";
25    echo "Attribute [{$key_formatted}] value: ", $span->attributes[$key]->nodeValue ?? '/', "\n";
26    echo "Attribute [{$key_formatted}] isset: ", isset($span->attributes[$key]) ? "yes" : "no", "\n";
27    echo "\n";
28}
29
30test($span, 0);
31test($span, false);
32test($span, true);
33test($span, null);
34test($span, 'attr2');
35// This one should fail because there is no 'hi' attribute
36test($span, 'hi');
37test($span, '0');
38test($span, '0.5');
39test($span, '1');
40// This one should fail because it's out of bounds
41test($span, '2147483647');
42
43?>
44--EXPECT--
45Node name: span
46Attribute [0] name: attr1
47Attribute [0] value: value1
48Attribute [0] isset: yes
49
50Attribute [false] name: attr1
51Attribute [false] value: value1
52Attribute [false] isset: yes
53
54Attribute [true] name: attr2
55Attribute [true] value: value2
56Attribute [true] isset: yes
57
58Attribute [null] name: attr1
59Attribute [null] value: value1
60Attribute [null] isset: yes
61
62Attribute ['attr2'] name: attr2
63Attribute ['attr2'] value: value2
64Attribute ['attr2'] isset: yes
65
66Attribute ['hi'] name: /
67Attribute ['hi'] value: /
68Attribute ['hi'] isset: no
69
70Attribute ['0'] name: attr1
71Attribute ['0'] value: value1
72Attribute ['0'] isset: yes
73
74Attribute ['0.5'] name: attr1
75Attribute ['0.5'] value: value1
76Attribute ['0.5'] isset: yes
77
78Attribute ['1'] name: attr2
79Attribute ['1'] value: value2
80Attribute ['1'] isset: yes
81
82Attribute ['2147483647'] name: /
83Attribute ['2147483647'] value: /
84Attribute ['2147483647'] isset: no
85