iterator_031.phpt 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. --TEST--
  2. SPL: AppendIterator::append() rewinds when necessary
  3. --FILE--
  4. <?php
  5. class MyArrayIterator extends ArrayIterator
  6. {
  7. function rewind(): void
  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(): void
  25. {
  26. echo __METHOD__ . "\n";
  27. parent::rewind();
  28. }
  29. function valid(): bool
  30. {
  31. echo __METHOD__ . "\n";
  32. return parent::valid();
  33. }
  34. function append(Iterator $what): void
  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(\Error $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. --EXPECT--
  71. MyArrayIterator::rewind
  72. 0=>1
  73. 1=>2
  74. MyAppendIterator::__construct
  75. MyAppendIterator::append
  76. The object is in an invalid state as the parent constructor was not called
  77. AppendIterator::getIterator() must be called exactly once per instance
  78. MyAppendIterator::append
  79. MyArrayIterator::rewind
  80. MyAppendIterator::append
  81. MyAppendIterator::append
  82. MyAppendIterator::rewind
  83. MyArrayIterator::rewind
  84. MyAppendIterator::valid
  85. 0=>1
  86. MyAppendIterator::valid
  87. 1=>2
  88. MyArrayIterator::rewind
  89. MyAppendIterator::valid
  90. 0=>1
  91. MyAppendIterator::valid
  92. 1=>2
  93. MyArrayIterator::rewind
  94. MyAppendIterator::valid
  95. 0=>1
  96. MyAppendIterator::valid
  97. 1=>2
  98. MyAppendIterator::valid