iterator_037.phpt 2.8 KB

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