xref: /PHP-7.3/ext/spl/tests/iterator_047.phpt (revision 610c7fbe)
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)
11	{
12		if (self::$fail == $state)
13		{
14			throw new Exception("State $state: $method()");
15		}
16	}
17
18	function hasChildren()
19	{
20		echo __METHOD__ . "()\n";
21		self::fail(1, __METHOD__);
22		return parent::hasChildren();
23	}
24
25	function getChildren()
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===DONE===
65<?php exit(0); ?>
66--EXPECTF--
67===0===
68MyRecursiveArrayIterator::hasChildren()
69int(0)
70int(0)
71MyRecursiveArrayIterator::hasChildren()
72MyRecursiveArrayIterator::getChildren()
73
74Notice: Array to string conversion in %siterator_047.php on line %d
75MyRecursiveArrayIterator::hasChildren()
76int(0)
77int(10)
78MyRecursiveArrayIterator::hasChildren()
79int(2)
80int(2)
81MyRecursiveArrayIterator::hasChildren()
82MyRecursiveArrayIterator::getChildren()
83
84Notice: Array to string conversion in %siterator_047.php on line %d
85MyRecursiveArrayIterator::hasChildren()
86int(0)
87int(30)
88MyRecursiveArrayIterator::hasChildren()
89int(4)
90int(4)
91===1===
92MyRecursiveArrayIterator::hasChildren()
93Exception: State 1: MyRecursiveArrayIterator::hasChildren() in %s on line %d
94===2===
95MyRecursiveArrayIterator::hasChildren()
96int(0)
97int(0)
98MyRecursiveArrayIterator::hasChildren()
99MyRecursiveArrayIterator::getChildren()
100Exception: State 2: MyRecursiveArrayIterator::getChildren() in %s on line %d
101===3===
102MyRecursiveArrayIterator::hasChildren()
103int(0)
104int(0)
105MyRecursiveArrayIterator::hasChildren()
106MyRecursiveArrayIterator::getChildren()
107
108Notice: Array to string conversion in %siterator_047.php on line %d
109MyRecursiveArrayIterator::hasChildren()
110int(0)
111int(10)
112MyRecursiveArrayIterator::hasChildren()
113int(2)
114int(2)
115MyRecursiveArrayIterator::hasChildren()
116MyRecursiveArrayIterator::getChildren()
117
118Notice: Array to string conversion in %siterator_047.php on line %d
119MyRecursiveArrayIterator::hasChildren()
120int(0)
121int(30)
122MyRecursiveArrayIterator::hasChildren()
123int(4)
124int(4)
125===DONE===
126