xref: /PHP-5.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()
73MyRecursiveArrayIterator::hasChildren()
74int(0)
75int(10)
76MyRecursiveArrayIterator::hasChildren()
77int(2)
78int(2)
79MyRecursiveArrayIterator::hasChildren()
80MyRecursiveArrayIterator::getChildren()
81MyRecursiveArrayIterator::hasChildren()
82int(0)
83int(30)
84MyRecursiveArrayIterator::hasChildren()
85int(4)
86int(4)
87===1===
88MyRecursiveArrayIterator::hasChildren()
89Exception: State 1: MyRecursiveArrayIterator::hasChildren() in %s on line %d
90===2===
91MyRecursiveArrayIterator::hasChildren()
92int(0)
93int(0)
94MyRecursiveArrayIterator::hasChildren()
95MyRecursiveArrayIterator::getChildren()
96Exception: State 2: MyRecursiveArrayIterator::getChildren() in %s on line %d
97===3===
98MyRecursiveArrayIterator::hasChildren()
99int(0)
100int(0)
101MyRecursiveArrayIterator::hasChildren()
102MyRecursiveArrayIterator::getChildren()
103MyRecursiveArrayIterator::hasChildren()
104int(0)
105int(10)
106MyRecursiveArrayIterator::hasChildren()
107int(2)
108int(2)
109MyRecursiveArrayIterator::hasChildren()
110MyRecursiveArrayIterator::getChildren()
111MyRecursiveArrayIterator::hasChildren()
112int(0)
113int(30)
114MyRecursiveArrayIterator::hasChildren()
115int(4)
116int(4)
117===DONE===
118