|
| 1 | +--TEST-- |
| 2 | +MongoDB\Driver\Cursor iterator handlers |
| 3 | +--SKIPIF-- |
| 4 | +<?php require __DIR__ . "/../utils/basic-skipif.inc"; CLEANUP(STANDALONE) ?> |
| 5 | +--FILE-- |
| 6 | +<?php |
| 7 | +require_once __DIR__ . "/../utils/basic.inc"; |
| 8 | + |
| 9 | +class MyIteratorIterator extends IteratorIterator |
| 10 | +{ |
| 11 | + private $name; |
| 12 | + |
| 13 | + public function __construct(Traversable $iterator, $name) |
| 14 | + { |
| 15 | + parent::__construct($iterator); |
| 16 | + $this->name = (string) $name; |
| 17 | + } |
| 18 | + |
| 19 | + public function dump() |
| 20 | + { |
| 21 | + $key = parent::key(); |
| 22 | + $current = parent::current(); |
| 23 | + $position = is_int($key) ? (string) $key : 'null'; |
| 24 | + $document = is_array($current) ? sprintf("{_id: %d}", $current['_id']) : 'null'; |
| 25 | + printf("%s: %s => %s\n", $this->name, $position, $document); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +$manager = new MongoDB\Driver\Manager(STANDALONE); |
| 30 | + |
| 31 | +$bulkWrite = new MongoDB\Driver\BulkWrite; |
| 32 | + |
| 33 | +for ($i = 0; $i < 5; $i++) { |
| 34 | + $bulkWrite->insert(array('_id' => $i)); |
| 35 | +} |
| 36 | + |
| 37 | +$writeResult = $manager->executeBulkWrite(NS, $bulkWrite); |
| 38 | +printf("Inserted: %d\n", $writeResult->getInsertedCount()); |
| 39 | + |
| 40 | +$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query(array())); |
| 41 | +$a = new MyIteratorIterator($cursor, 'A'); |
| 42 | +$b = new MyIteratorIterator($cursor, 'B'); |
| 43 | + |
| 44 | +echo "\nBefore rewinding, position and current element are not populated:\n"; |
| 45 | +$a->dump(); |
| 46 | +$b->dump(); |
| 47 | + |
| 48 | +echo "\nAfter rewinding, current element is populated:\n"; |
| 49 | +$a->rewind(); |
| 50 | +$b->rewind(); |
| 51 | +$a->dump(); |
| 52 | +$b->dump(); |
| 53 | + |
| 54 | +echo "\nMultiple iterators have their own position, but share the same Cursor buffer:\n"; |
| 55 | +$a->next(); |
| 56 | +$a->dump(); |
| 57 | +$b->next(); |
| 58 | +$b->dump(); |
| 59 | +$a->next(); |
| 60 | +$a->dump(); |
| 61 | + |
| 62 | +echo "\nRewinding only populates current element and does not alter position:\n"; |
| 63 | +$a->rewind(); |
| 64 | +$a->dump(); |
| 65 | +$b->rewind(); |
| 66 | +$b->dump(); |
| 67 | + |
| 68 | +echo "\nAdvancing first iterator until end of shared Cursor buffer is reached:\n"; |
| 69 | +$a->next(); |
| 70 | +$a->dump(); |
| 71 | +$a->next(); |
| 72 | +$a->dump(); |
| 73 | + |
| 74 | +echo "\nRewinding second iterator to position it at end of shared Cursor buffer:\n"; |
| 75 | +$b->rewind(); |
| 76 | +$b->dump(); |
| 77 | + |
| 78 | +?> |
| 79 | +===DONE=== |
| 80 | +<?php exit(0); ?> |
| 81 | +--EXPECT-- |
| 82 | +Inserted: 5 |
| 83 | + |
| 84 | +Before rewinding, position and current element are not populated: |
| 85 | +A: null => null |
| 86 | +B: null => null |
| 87 | + |
| 88 | +After rewinding, current element is populated: |
| 89 | +A: 0 => {_id: 0} |
| 90 | +B: 0 => {_id: 0} |
| 91 | + |
| 92 | +Multiple iterators have their own position, but share the same Cursor buffer: |
| 93 | +A: 1 => {_id: 1} |
| 94 | +B: 1 => {_id: 2} |
| 95 | +A: 2 => {_id: 3} |
| 96 | + |
| 97 | +Rewinding only populates current element and does not alter position: |
| 98 | +A: 2 => {_id: 3} |
| 99 | +B: 1 => {_id: 3} |
| 100 | + |
| 101 | +Advancing first iterator until end of shared Cursor buffer is reached: |
| 102 | +A: 3 => {_id: 4} |
| 103 | +A: null => null |
| 104 | + |
| 105 | +Rewinding second iterator to position it at end of shared Cursor buffer: |
| 106 | +B: null => null |
| 107 | +===DONE=== |
0 commit comments