xref: /php-src/ext/dom/tests/bug67949.phpt (revision b8a1041f)
1--TEST--
2Bug #67949: DOMNodeList elements should be accessible through array notation
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8// Note: non-numeric string accesses fail on NodeLists (as per spec).
9
10$html = <<<HTML
11<div>data</div>
12<a href="test">hello world</a>
13HTML;
14$doc = new DOMDocument;
15$doc->loadHTML($html);
16
17$nodes = $doc->getElementsByTagName('div');
18
19echo "--- testing has_dimension ---\n";
20var_dump(isset($nodes[0]));
21var_dump(isset($nodes[1]));
22var_dump(isset($nodes[-1]));
23
24echo "--- testing property access ---\n";
25var_dump($nodes[0]->textContent);
26var_dump($nodes[1]->textContent);
27
28echo "--- testing offset not a long: array ---\n";
29$offset = ['test'];
30var_dump($offset);
31var_dump(isset($nodes[$offset]), $nodes[$offset]->textContent);
32
33echo "--- testing offset not a long: Reference to string ---\n";
34$something = 'href';
35$offset = &$something;
36
37var_dump($offset);
38var_dump(isset($nodes[$offset]), $nodes[$offset]->textContent);
39
40echo "--- testing offset not a long: string ---\n";
41$offset = 'test';
42var_dump($offset);
43var_dump(isset($nodes[$offset]), $nodes[$offset]->textContent);
44
45echo "--- testing read_dimension with null offset ---\n";
46try {
47    var_dump($nodes[][] = 1);
48} catch (Error $e) {
49    echo $e->getMessage(), "\n";
50}
51
52echo "--- testing attribute access ---\n";
53$anchor = $doc->getElementsByTagName('a')[0];
54var_dump($anchor->attributes[0]->name);
55
56echo "==DONE==\n";
57?>
58--EXPECTF--
59--- testing has_dimension ---
60bool(true)
61bool(false)
62bool(false)
63--- testing property access ---
64string(4) "data"
65
66Warning: Attempt to read property "textContent" on null in %s on line %d
67NULL
68--- testing offset not a long: array ---
69array(1) {
70  [0]=>
71  string(4) "test"
72}
73
74Warning: Attempt to read property "textContent" on null in %s on line %d
75bool(false)
76NULL
77--- testing offset not a long: Reference to string ---
78string(4) "href"
79
80Warning: Attempt to read property "textContent" on null in %s on line %d
81bool(false)
82NULL
83--- testing offset not a long: string ---
84string(4) "test"
85
86Warning: Attempt to read property "textContent" on null in %s on line %d
87bool(false)
88NULL
89--- testing read_dimension with null offset ---
90Cannot access DOMNodeList without offset
91--- testing attribute access ---
92string(4) "href"
93==DONE==
94