iterator_037.phpt 3.5 KB

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