iterator_031.phpt 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. --TEST--
  2. SPL: AppendIterator::append() rewinds when necessary
  3. --FILE--
  4. <?php
  5. class MyArrayIterator extends ArrayIterator
  6. {
  7. function rewind()
  8. {
  9. echo __METHOD__ . "\n";
  10. parent::rewind();
  11. }
  12. }
  13. $it = new MyArrayIterator(array(1,2));
  14. foreach($it as $k=>$v)
  15. {
  16. echo "$k=>$v\n";
  17. }
  18. class MyAppendIterator extends AppendIterator
  19. {
  20. function __construct()
  21. {
  22. echo __METHOD__ . "\n";
  23. }
  24. function rewind()
  25. {
  26. echo __METHOD__ . "\n";
  27. parent::rewind();
  28. }
  29. function valid()
  30. {
  31. echo __METHOD__ . "\n";
  32. return parent::valid();
  33. }
  34. function append(Iterator $what)
  35. {
  36. echo __METHOD__ . "\n";
  37. parent::append($what);
  38. }
  39. function parent__construct()
  40. {
  41. parent::__construct();
  42. }
  43. }
  44. $ap = new MyAppendIterator;
  45. try
  46. {
  47. $ap->append($it);
  48. }
  49. catch(LogicException $e)
  50. {
  51. echo $e->getMessage() . "\n";
  52. }
  53. $ap->parent__construct();
  54. try
  55. {
  56. $ap->parent__construct($it);
  57. }
  58. catch(BadMethodCallException $e)
  59. {
  60. echo $e->getMessage() . "\n";
  61. }
  62. $ap->append($it);
  63. $ap->append($it);
  64. $ap->append($it);
  65. foreach($ap as $k=>$v)
  66. {
  67. echo "$k=>$v\n";
  68. }
  69. ?>
  70. ===DONE===
  71. <?php exit(0); ?>
  72. --EXPECT--
  73. MyArrayIterator::rewind
  74. 0=>1
  75. 1=>2
  76. MyAppendIterator::__construct
  77. MyAppendIterator::append
  78. The object is in an invalid state as the parent constructor was not called
  79. AppendIterator::getIterator() must be called exactly once per instance
  80. MyAppendIterator::append
  81. MyArrayIterator::rewind
  82. MyAppendIterator::append
  83. MyAppendIterator::append
  84. MyAppendIterator::rewind
  85. MyArrayIterator::rewind
  86. MyAppendIterator::valid
  87. 0=>1
  88. MyAppendIterator::valid
  89. 1=>2
  90. MyArrayIterator::rewind
  91. MyAppendIterator::valid
  92. 0=>1
  93. MyAppendIterator::valid
  94. 1=>2
  95. MyArrayIterator::rewind
  96. MyAppendIterator::valid
  97. 0=>1
  98. MyAppendIterator::valid
  99. 1=>2
  100. MyAppendIterator::valid
  101. ===DONE===