iterator_045.phpt 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. $checks = array(0 => 25, 1 => 42, 3 => 'FooBar');
  66. $unsets = array(0, 2);
  67. $it->testSet($checks);
  68. $it->show();
  69. $it->testUnset($unsets);
  70. $it->show();
  71. $it->fill();
  72. $it->show();
  73. $it->testSet($checks);
  74. $it->show();
  75. $it->testUnset($unsets);
  76. $it->show();
  77. ?>
  78. --EXPECT--
  79. Exception: MyCachingIterator does not use a full cache (see CachingIterator::__construct)
  80. Exception: MyCachingIterator does not use a full cache (see CachingIterator::__construct)
  81. MyCachingIterator::testSet()
  82. set(0,25)
  83. set(1,42)
  84. set(3,FooBar)
  85. MyCachingIterator::show()
  86. array(3) {
  87. [0]=>
  88. int(25)
  89. [1]=>
  90. int(42)
  91. [3]=>
  92. string(6) "FooBar"
  93. }
  94. MyCachingIterator::testUnset()
  95. unset(0)
  96. unset(2)
  97. MyCachingIterator::show()
  98. array(2) {
  99. [1]=>
  100. int(42)
  101. [3]=>
  102. string(6) "FooBar"
  103. }
  104. MyCachingIterator::fill()
  105. MyCachingIterator::show()
  106. array(4) {
  107. [0]=>
  108. int(0)
  109. [1]=>
  110. int(1)
  111. [2]=>
  112. int(2)
  113. [3]=>
  114. int(3)
  115. }
  116. MyCachingIterator::testSet()
  117. set(0,25)
  118. set(1,42)
  119. set(3,FooBar)
  120. MyCachingIterator::show()
  121. array(4) {
  122. [0]=>
  123. int(25)
  124. [1]=>
  125. int(42)
  126. [2]=>
  127. int(2)
  128. [3]=>
  129. string(6) "FooBar"
  130. }
  131. MyCachingIterator::testUnset()
  132. unset(0)
  133. unset(2)
  134. MyCachingIterator::show()
  135. array(2) {
  136. [1]=>
  137. int(42)
  138. [3]=>
  139. string(6) "FooBar"
  140. }