xref: /PHP-8.2/ext/spl/tests/iterator_047.phpt (revision c6357b80)
1--TEST--
2SPL: RecursiveCachingIterator and exception in has/getChildren
3--FILE--
4<?php
5
6class MyRecursiveArrayIterator extends RecursiveArrayIterator
7{
8    static public $fail = 0;
9
10    static function fail($state, $method): void
11    {
12        if (self::$fail == $state)
13        {
14            throw new Exception("State $state: $method()");
15        }
16    }
17
18    function hasChildren(): bool
19    {
20        echo __METHOD__ . "()\n";
21        self::fail(1, __METHOD__);
22        return parent::hasChildren();
23    }
24
25    function getChildren(): ?RecursiveArrayIterator
26    {
27        echo __METHOD__ . "()\n";
28        self::fail(2, __METHOD__);
29        return parent::getChildren();
30    }
31}
32
33class MyRecursiveCachingIterator extends RecursiveCachingIterator
34{
35    function show()
36    {
37        MyRecursiveArrayIterator::$fail = 0;
38        while(MyRecursiveArrayIterator::$fail < 4)
39        {
40            echo "===" . MyRecursiveArrayIterator::$fail . "===\n";
41            try
42            {
43                foreach(new RecursiveIteratorIterator($this) as $k => $v)
44                {
45                    var_dump($k);
46                    var_dump($v);
47                }
48            }
49            catch (Exception $e)
50            {
51                echo "Exception: " . $e->getMessage() . " in " . $e->getFile() . " on line " . $e->getLine() . "\n";
52            }
53            MyRecursiveArrayIterator::$fail++;
54        }
55    }
56}
57
58$it = new MyRecursiveArrayIterator(array(0, array(10), 2, array(30), 4));
59$it = new MyRecursiveCachingIterator($it);
60
61$it->show();
62
63?>
64--EXPECTF--
65===0===
66MyRecursiveArrayIterator::hasChildren()
67int(0)
68int(0)
69MyRecursiveArrayIterator::hasChildren()
70MyRecursiveArrayIterator::getChildren()
71
72Warning: Array to string conversion in %s on line %d
73MyRecursiveArrayIterator::hasChildren()
74int(0)
75int(10)
76MyRecursiveArrayIterator::hasChildren()
77int(2)
78int(2)
79MyRecursiveArrayIterator::hasChildren()
80MyRecursiveArrayIterator::getChildren()
81
82Warning: Array to string conversion in %s on line %d
83MyRecursiveArrayIterator::hasChildren()
84int(0)
85int(30)
86MyRecursiveArrayIterator::hasChildren()
87int(4)
88int(4)
89===1===
90MyRecursiveArrayIterator::hasChildren()
91Exception: State 1: MyRecursiveArrayIterator::hasChildren() in %s on line %d
92===2===
93MyRecursiveArrayIterator::hasChildren()
94int(0)
95int(0)
96MyRecursiveArrayIterator::hasChildren()
97MyRecursiveArrayIterator::getChildren()
98Exception: State 2: MyRecursiveArrayIterator::getChildren() in %s on line %d
99===3===
100MyRecursiveArrayIterator::hasChildren()
101int(0)
102int(0)
103MyRecursiveArrayIterator::hasChildren()
104MyRecursiveArrayIterator::getChildren()
105
106Warning: Array to string conversion in %s on line %d
107MyRecursiveArrayIterator::hasChildren()
108int(0)
109int(10)
110MyRecursiveArrayIterator::hasChildren()
111int(2)
112int(2)
113MyRecursiveArrayIterator::hasChildren()
114MyRecursiveArrayIterator::getChildren()
115
116Warning: Array to string conversion in %s on line %d
117MyRecursiveArrayIterator::hasChildren()
118int(0)
119int(30)
120MyRecursiveArrayIterator::hasChildren()
121int(4)
122int(4)
123