xref: /PHP-7.0/ext/spl/tests/iterator_037.phpt (revision 610c7fbe)
1--TEST--
2SPL: CachingIterator and __toString
3--FILE--
4<?php
5
6function test($ar, $flags)
7{
8	echo "===$flags===\n";
9	$it = new CachingIterator($ar, 0);
10	try
11	{
12		$it->setFlags($flags);
13	}
14	catch (Exception $e)
15	{
16		echo 'Exception: ' . $e->getMessage() . "\n";
17		var_dump($it->getFlags());
18		return;
19	}
20	var_dump($it->getFlags());
21	try
22	{
23		foreach($it as $v)
24		{
25			var_dump((string)$it);
26		}
27	}
28	catch (Exception $e)
29	{
30		echo 'Exception: ' . $e->getMessage() . "\n";
31	}
32}
33
34class MyItem
35{
36	function __construct($value)
37	{
38		$this->value = $value;
39	}
40
41	function __toString()
42	{
43		return (string)$this->value;
44	}
45}
46
47class MyArrayIterator extends ArrayIterator
48{
49	function __toString()
50	{
51		return $this->key() . ':' . $this->current();
52	}
53}
54
55$ar = new MyArrayIterator(array(1, 2, 3));
56
57test($ar, CachingIterator::CALL_TOSTRING);
58test($ar, CachingIterator::TOSTRING_USE_KEY);
59test($ar, CachingIterator::TOSTRING_USE_CURRENT);
60
61$ar = new MyArrayIterator(array(new MyItem(1), new MyItem(2), new MyItem(3)));
62
63test($ar, CachingIterator::TOSTRING_USE_INNER);
64test($ar, CachingIterator::CALL_TOSTRING | CachingIterator::TOSTRING_USE_KEY);
65test($ar, CachingIterator::CALL_TOSTRING | CachingIterator::TOSTRING_USE_CURRENT);
66test($ar, CachingIterator::CALL_TOSTRING | CachingIterator::TOSTRING_USE_INNER);
67test($ar, CachingIterator::TOSTRING_USE_KEY | CachingIterator::TOSTRING_USE_CURRENT);
68test($ar, CachingIterator::TOSTRING_USE_KEY | CachingIterator::TOSTRING_USE_INNER);
69
70echo "===X===\n";
71try
72{
73	$it = new CachingIterator($ar, CachingIterator::CALL_TOSTRING);
74	$it->setFlags(0);
75}
76catch (Exception $e)
77{
78	echo 'Exception: ' . $e->getMessage() . "\n";
79}
80try
81{
82	$it = new CachingIterator($ar, CachingIterator::TOSTRING_USE_INNER);
83	$it->setFlags(0);
84}
85catch (Exception $e)
86{
87	echo 'Exception: ' . $e->getMessage() . "\n";
88}
89
90?>
91===DONE===
92--EXPECTF--
93===1===
94int(1)
95string(1) "1"
96string(1) "2"
97string(1) "3"
98===2===
99int(2)
100string(1) "0"
101string(1) "1"
102string(1) "2"
103===4===
104int(4)
105string(1) "1"
106string(1) "2"
107string(1) "3"
108===8===
109int(8)
110string(3) "0:1"
111string(3) "1:2"
112string(3) "2:3"
113===3===
114Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
115int(0)
116===5===
117Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
118int(0)
119===9===
120Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
121int(0)
122===6===
123Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
124int(0)
125===10===
126Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
127int(0)
128===X===
129Exception: Unsetting flag CALL_TO_STRING is not possible
130Exception: Unsetting flag TOSTRING_USE_INNER is not possible
131===DONE===
132