iterator_045.phpt 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. --TEST--
  2. SPL: CachingIterator and offsetSet/Unset, getCache using flag FULL_CACHE
  3. --FILE--
  4. <?php
  5. class MyFoo
  6. {
  7. function __toString()
  8. {
  9. return 'foo';
  10. }
  11. }
  12. class MyCachingIterator extends CachingIterator
  13. {
  14. function __construct(Iterator $it, $flags = 0)
  15. {
  16. parent::__construct($it, $flags);
  17. }
  18. function testSet($ar)
  19. {
  20. echo __METHOD__ . "()\n";
  21. foreach($ar as $k => $v)
  22. {
  23. echo "set($k,$v)\n";
  24. $this->offsetSet($k, $v);
  25. }
  26. }
  27. function testUnset($ar)
  28. {
  29. echo __METHOD__ . "()\n";
  30. foreach($ar as $k => $v)
  31. {
  32. echo "unset($v)\n";
  33. $this->offsetUnset($v);
  34. }
  35. }
  36. function fill()
  37. {
  38. echo __METHOD__ . "()\n";
  39. foreach($this as $v) ;
  40. }
  41. function show()
  42. {
  43. echo __METHOD__ . "()\n";
  44. var_dump($this->getCache());
  45. }
  46. }
  47. $it = new MyCachingIterator(new ArrayIterator(array(0, 'foo'=>1, 2, 'bar'=>3, 4)));
  48. try
  49. {
  50. var_dump($it->offsetSet(0, 0));
  51. }
  52. catch(Exception $e)
  53. {
  54. echo "Exception: " . $e->getMessage() . "\n";
  55. }
  56. try
  57. {
  58. var_dump($it->offsetUnset(0));
  59. }
  60. catch(Exception $e)
  61. {
  62. echo "Exception: " . $e->getMessage() . "\n";
  63. }
  64. $it = new MyCachingIterator(new ArrayIterator(array(0, 1, 2, 3)), CachingIterator::FULL_CACHE);
  65. var_dump($it->offsetSet());
  66. var_dump($it->offsetSet(0));
  67. var_dump($it->offsetUnset());
  68. $checks = array(0 => 25, 1 => 42, 3 => 'FooBar');
  69. $unsets = array(0, 2);
  70. $it->testSet($checks);
  71. $it->show();
  72. $it->testUnset($unsets);
  73. $it->show();
  74. $it->fill();
  75. $it->show();
  76. $it->testSet($checks);
  77. $it->show();
  78. $it->testUnset($unsets);
  79. $it->show();
  80. ?>
  81. ===DONE===
  82. <?php exit(0); ?>
  83. --EXPECTF--
  84. Exception: MyCachingIterator does not use a full cache (see CachingIterator::__construct)
  85. Exception: MyCachingIterator does not use a full cache (see CachingIterator::__construct)
  86. Warning: CachingIterator::offsetSet() expects exactly 2 parameters, 0 given in %siterator_045.php on line %d
  87. NULL
  88. Warning: CachingIterator::offsetSet() expects exactly 2 parameters, 1 given in %siterator_045.php on line %d
  89. NULL
  90. Warning: CachingIterator::offsetUnset() expects exactly 1 parameter, 0 given in %siterator_045.php on line %d
  91. NULL
  92. MyCachingIterator::testSet()
  93. set(0,25)
  94. set(1,42)
  95. set(3,FooBar)
  96. MyCachingIterator::show()
  97. array(3) {
  98. [0]=>
  99. int(25)
  100. [1]=>
  101. int(42)
  102. [3]=>
  103. string(6) "FooBar"
  104. }
  105. MyCachingIterator::testUnset()
  106. unset(0)
  107. unset(2)
  108. MyCachingIterator::show()
  109. array(2) {
  110. [1]=>
  111. int(42)
  112. [3]=>
  113. string(6) "FooBar"
  114. }
  115. MyCachingIterator::fill()
  116. MyCachingIterator::show()
  117. array(4) {
  118. [0]=>
  119. int(0)
  120. [1]=>
  121. int(1)
  122. [2]=>
  123. int(2)
  124. [3]=>
  125. int(3)
  126. }
  127. MyCachingIterator::testSet()
  128. set(0,25)
  129. set(1,42)
  130. set(3,FooBar)
  131. MyCachingIterator::show()
  132. array(4) {
  133. [0]=>
  134. int(25)
  135. [1]=>
  136. int(42)
  137. [2]=>
  138. int(2)
  139. [3]=>
  140. string(6) "FooBar"
  141. }
  142. MyCachingIterator::testUnset()
  143. unset(0)
  144. unset(2)
  145. MyCachingIterator::show()
  146. array(2) {
  147. [1]=>
  148. int(42)
  149. [3]=>
  150. string(6) "FooBar"
  151. }
  152. ===DONE===